Initial commit

This commit is contained in:
AxolotlMaid 2023-11-19 14:39:48 +00:00
commit f41871f359
40 changed files with 2073 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package com.axolotlmaid.optionsprofiles;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class OptionsProfilesMod {
public static final String MOD_ID = "optionsprofiles";
public static void init() {
Path profilesDirectory = Paths.get("options-profiles");
if (Files.notExists(profilesDirectory)) {
try {
Files.createDirectory(profilesDirectory);
} catch (IOException e) {
System.out.println("An error occurred when creating the 'options-profiles' directory.");
e.printStackTrace();
}
}
}
}

View file

@ -0,0 +1,55 @@
package com.axolotlmaid.optionsprofiles.gui;
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
public class EditProfileScreen extends Screen {
private final Screen lastScreen;
private final Component profileName;
private EditBox profileNameEdit;
public EditProfileScreen(Screen screen, Component profileName) {
super(Component.literal(Component.translatable("gui.optionsprofiles.editing-profile-title").getString() + profileName.getString()));
this.lastScreen = screen;
this.profileName = profileName;
}
protected void init() {
this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, this.height - 130, 204, 20, Component.empty());
this.profileNameEdit.setValue(profileName.getString());
this.addWidget(this.profileNameEdit);
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.overwrite-options"), (button) -> {
new Profiles().writeOptionsFilesIntoProfile(profileName.getString());
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(this.width / 2 - 50, this.height - 85).build());
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.rename-profile"), (button) -> {
new Profiles().renameProfile(profileName.getString(), this.profileNameEdit.getValue());
this.minecraft.setScreen(new EditProfileScreen(lastScreen, Component.literal(this.profileNameEdit.getValue())));
}).size(100, 20).pos(this.width / 2 - 50, this.height - 65).build());
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.delete-profile").withStyle(ChatFormatting.RED), (button) -> {
new Profiles().deleteProfile(profileName.getString());
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(5, this.height - 25).build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> {
this.minecraft.setScreen(this.lastScreen);
}).size(100, 20).pos(this.width / 2 - 50, this.height - 40).build());
}
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
this.profileNameEdit.render(guiGraphics, mouseX, mouseY, delta);
guiGraphics.drawCenteredString(this.font, this.title, this.width / 2, 8, 16777215);
guiGraphics.drawCenteredString(this.font, Component.translatable("gui.optionsprofiles.profile-name-text"), this.width / 2, this.height - 145, 16777215);
}
}

View file

@ -0,0 +1,111 @@
package com.axolotlmaid.optionsprofiles.gui;
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ContainerObjectSelectionList;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.network.chat.Component;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
public class ProfilesList extends ContainerObjectSelectionList<ProfilesList.Entry> {
final ProfilesScreen profilesScreen;
public ProfilesList(ProfilesScreen profilesScreen, Minecraft minecraft) {
super(minecraft, profilesScreen.width + 45, profilesScreen.height, 20, profilesScreen.height - 32, 20);
this.profilesScreen = profilesScreen;
refreshEntries();
}
public void refreshEntries() {
this.clearEntries();
Path profilesDirectory = Paths.get("options-profiles/");
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(profilesDirectory)) {
for (Path profile : directoryStream) {
this.addEntry(new ProfilesList.ProfileEntry(Component.literal(profile.getFileName().toString())));
}
} catch (Exception e) {
System.out.println("An error occurred when listing profiles.");
e.printStackTrace();
}
}
protected int getScrollbarPosition() {
return super.getScrollbarPosition() + 15;
}
public int getRowWidth() {
return super.getRowWidth() + 32;
}
public class ProfileEntry extends Entry {
private final Component profileName;
private final Button editButton;
private final Button loadButton;
ProfileEntry(Component profileName) {
this.profileName = profileName;
this.editButton = Button.builder(Component.translatable("gui.optionsprofiles.edit-profile"), (button) -> {
minecraft.setScreen(new EditProfileScreen(profilesScreen, profileName));
}).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.edit-profile")).build();
this.loadButton = Button.builder(Component.translatable("gui.optionsprofiles.load-profile"), (button) -> {
new Profiles().loadProfile(profileName.getString());
minecraft.options.load();
minecraft.options.loadSelectedResourcePacks(minecraft.getResourcePackRepository());
minecraft.reloadResourcePacks();
minecraft.options.save();
button.active = false;
}).size(75, 20).createNarration((supplier) -> Component.translatable("gui.optionsprofiles.load-profile")).build();
this.loadButton.active = !new Profiles().isProfileLoaded(profileName.getString());
}
public void render(GuiGraphics guiGraphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
Font fontRenderer = ProfilesList.this.minecraft.font;
int textY = y + entryHeight / 2;
Objects.requireNonNull(ProfilesList.this.minecraft.font);
guiGraphics.drawString(fontRenderer, this.profileName, x - 50, textY - 9 / 2, 16777215, false);
this.editButton.setX(x + 115);
this.editButton.setY(y);
this.editButton.render(guiGraphics, mouseX, mouseY, tickDelta);
this.loadButton.setX(x + 190);
this.loadButton.setY(y);
this.loadButton.render(guiGraphics, mouseX, mouseY, tickDelta);
}
public List<? extends GuiEventListener> children() {
return ImmutableList.of(this.editButton, this.loadButton);
}
public List<? extends NarratableEntry> narratables() {
return ImmutableList.of(this.editButton, this.loadButton);
}
}
public abstract static class Entry extends ContainerObjectSelectionList.Entry<ProfilesList.Entry> {
public Entry() {
}
}
}

View file

@ -0,0 +1,39 @@
package com.axolotlmaid.optionsprofiles.gui;
import com.axolotlmaid.optionsprofiles.profiles.Profiles;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.CommonComponents;
import net.minecraft.network.chat.Component;
public class ProfilesScreen extends Screen {
private final Screen lastScreen;
private ProfilesList profilesList;
public ProfilesScreen(Screen screen) {
super(Component.translatable("gui.optionsprofiles.profiles-menu"));
this.lastScreen = screen;
}
protected void init() {
this.profilesList = new ProfilesList(this, this.minecraft);
this.addWidget(this.profilesList);
// buttons
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.save-current-options"), (button) -> {
new Profiles().createProfile();
this.profilesList.refreshEntries();
}).size(150, 20).pos(this.width / 2 - 155, this.height - 29).build());
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE, (button) -> {
this.minecraft.setScreen(this.lastScreen);
}).size(150, 20).pos(this.width / 2 + 5, this.height - 29).build());
}
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta) {
super.render(guiGraphics, mouseX, mouseY, delta);
this.profilesList.render(guiGraphics, mouseX, mouseY, delta);
guiGraphics.drawCenteredString(this.font, this.title, this.width / 2, 8, 16777215);
}
}

View file

@ -0,0 +1,25 @@
package com.axolotlmaid.optionsprofiles.mixin;
import com.axolotlmaid.optionsprofiles.gui.ProfilesScreen;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.OptionsScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(OptionsScreen.class)
public class MixinOptionsScreen extends Screen {
protected MixinOptionsScreen(Component component) {
super(component);
}
@Inject(at = @At("HEAD"), method = "init")
private void init(CallbackInfo info) {
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.profiles-menu"), (button) -> {
this.minecraft.setScreen(new ProfilesScreen(this));
}).width(100).pos(5, 5).build());
}
}

View file

@ -0,0 +1,197 @@
package com.axolotlmaid.optionsprofiles.profiles;
import dev.architectury.platform.Platform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
public class Profiles {
public void createProfile() {
String profileName = "Profile 1";
Path profile = Paths.get("options-profiles/" + profileName);
for (int i = 1; Files.exists(profile); i++) {
profileName = "Profile " + i;
profile = Paths.get("options-profiles/" + profileName);
}
try {
Files.createDirectory(profile);
if (Files.exists(profile)) {
System.out.println("Profile created.");
writeOptionsFilesIntoProfile(profileName);
} else {
System.out.println("Profile was not created successfully.");
}
} catch (IOException e) {
System.out.println("An error occurred when creating a profile.");
e.printStackTrace();
}
}
public void writeOptionsFilesIntoProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
// options.txt
Path options = Paths.get("options.txt");
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt");
try (Stream<String> paths = Files.lines(options)) {
if (Files.exists(profileOptions))
Files.newBufferedWriter(profileOptions, StandardOpenOption.TRUNCATE_EXISTING);
paths.forEach(line -> {
try {
Files.write(profileOptions, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Files.write(profileOptions, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
System.out.println("An error occurred when writing a profile.");
e.printStackTrace();
}
});
} catch (IOException e) {
System.out.println("An error occurred when reading options.txt.");
e.printStackTrace();
}
// sodium-options.json
if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) {
Path sodiumConfiguration = Paths.get("config/sodium-options.json");
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
try (Stream<String> paths = Files.lines(sodiumConfiguration)) {
if (Files.exists(sodiumConfigurationProfile))
Files.newBufferedWriter(sodiumConfigurationProfile, StandardOpenOption.TRUNCATE_EXISTING);
paths.forEach(line -> {
try {
Files.write(sodiumConfigurationProfile, line.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
Files.write(sodiumConfigurationProfile, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
System.out.println("An error occurred when writing a profile.");
e.printStackTrace();
}
});
} catch (IOException e) {
System.out.println("An error occurred when reading options.txt.");
e.printStackTrace();
}
}
}
}
public boolean isProfileLoaded(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
Path options = Paths.get("options.txt");
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt");
try {
List<String> linesOptions = Files.readAllLines(options);
List<String> linesProfileOptions = Files.readAllLines(profileOptions);
if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) {
Path sodiumConfiguration = Paths.get("config/sodium-options.json");
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
if (Files.exists(sodiumConfigurationProfile)) {
List<String> linesSodiumConfig = Files.readAllLines(sodiumConfiguration);
List<String> linesSodiumConfigProfile = Files.readAllLines(sodiumConfigurationProfile);
return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile);
}
}
}
return linesOptions.equals(linesProfileOptions);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public void loadProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
// options.txt
Path options = Paths.get("options.txt");
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt");
try (Stream<String> paths = Files.lines(profileOptions)) {
Files.newBufferedWriter(options, StandardOpenOption.TRUNCATE_EXISTING);
paths.forEach(line -> {
try {
Files.write(options, line.getBytes(), StandardOpenOption.APPEND);
Files.write(options, "\n".getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
System.out.println("An error occurred when loading a profile.");
e.printStackTrace();
}
});
} catch (IOException e) {
System.out.println("An error occurred when loading a profile.");
e.printStackTrace();
}
// sodium-options.json
if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) {
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
if (Files.exists(sodiumConfigurationProfile)) {
SodiumConfigLoader.load(sodiumConfigurationProfile);
}
}
}
}
public void renameProfile(String profileName, String newProfileName) {
Path profile = Paths.get("options-profiles/" + profileName);
Path newProfile = Paths.get("options-profiles/" + newProfileName);
if (Files.exists(newProfile))
System.out.println("New profile already exists!");
try {
Files.move(profile, newProfile);
if (Files.exists(newProfile)) {
System.out.println("Profile renamed.");
} else {
System.out.println("Profile was not renamed successfully.");
}
} catch (IOException e) {
System.out.println("Profile was not renamed successfully.");
e.printStackTrace();
}
}
public void deleteProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
try (Stream<Path> files = Files.walk(profile)) {
files
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
System.out.println("Profile was not deleted.");
e.printStackTrace();
}
System.out.println("Profile deleted.");
}
}

View file

@ -0,0 +1,82 @@
package com.axolotlmaid.optionsprofiles.profiles;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.jellysquid.mods.sodium.client.SodiumClientMod;
import me.jellysquid.mods.sodium.client.gui.SodiumGameOptions;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
public class SodiumConfigLoader {
public static void load(Path file) {
try (FileReader reader = new FileReader(file.toFile())) {
Gson gson = new GsonBuilder().create();
ConfigData configData = gson.fromJson(reader, ConfigData.class);
apply(configData);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void apply(ConfigData configData) {
SodiumClientMod.options().quality.weatherQuality = SodiumGameOptions.GraphicsQuality.valueOf(configData.quality.weather_quality);
SodiumClientMod.options().quality.leavesQuality = SodiumGameOptions.GraphicsQuality.valueOf(configData.quality.leaves_quality);
SodiumClientMod.options().quality.enableVignette = configData.quality.enable_vignette;
SodiumClientMod.options().advanced.enableMemoryTracing = configData.advanced.enable_memory_tracing;
SodiumClientMod.options().advanced.useAdvancedStagingBuffers = configData.advanced.use_advanced_staging_buffers;
SodiumClientMod.options().advanced.cpuRenderAheadLimit = configData.advanced.cpu_render_ahead_limit;
SodiumClientMod.options().performance.chunkBuilderThreads = configData.performance.chunk_builder_threads;
SodiumClientMod.options().performance.alwaysDeferChunkUpdates = configData.performance.always_defer_chunk_updates_v2;
SodiumClientMod.options().performance.animateOnlyVisibleTextures = configData.performance.animate_only_visible_textures;
SodiumClientMod.options().performance.useEntityCulling = configData.performance.use_entity_culling;
SodiumClientMod.options().performance.useFogOcclusion = configData.performance.use_fog_occlusion;
SodiumClientMod.options().performance.useBlockFaceCulling = configData.performance.use_block_face_culling;
SodiumClientMod.options().performance.useNoErrorGLContext = configData.performance.use_no_error_g_l_context;
SodiumClientMod.options().notifications.hideDonationButton = configData.notifications.hide_donation_button;
try {
SodiumClientMod.options().writeChanges();
} catch (IOException e) {
e.printStackTrace();
}
}
public static class ConfigData {
public Quality quality;
public Advanced advanced;
public Performance performance;
public Notifications notifications;
public static class Quality {
public String weather_quality;
public String leaves_quality;
public boolean enable_vignette;
}
public static class Advanced {
public boolean enable_memory_tracing;
public boolean use_advanced_staging_buffers;
public int cpu_render_ahead_limit;
}
public static class Performance {
public int chunk_builder_threads;
public boolean always_defer_chunk_updates_v2;
public boolean animate_only_visible_textures;
public boolean use_entity_culling;
public boolean use_fog_occlusion;
public boolean use_block_face_culling;
public boolean use_no_error_g_l_context;
}
public static class Notifications {
public boolean hide_donation_button;
}
}
}

View file

@ -0,0 +1,3 @@
{
"accessWidener": "optionsprofiles.accesswidener"
}

View file

@ -0,0 +1,12 @@
{
"gui.optionsprofiles.profiles-menu": "Profiles",
"gui.optionsprofiles.save-current-options": "Save Current Options",
"gui.optionsprofiles.load-profile": "✔ (Load)",
"gui.optionsprofiles.edit-profile": "✎ (Edit)",
"gui.optionsprofiles.editing-profile-title": "Editing Profile: ",
"gui.optionsprofiles.profile-name-text": "Profile Name",
"gui.optionsprofiles.overwrite-options": "Overwrite",
"gui.optionsprofiles.rename-profile": "Rename",
"gui.optionsprofiles.delete-profile": "Delete"
}

View file

@ -0,0 +1,15 @@
{
"gui.optionsprofiles.profiles-menu": "Профили",
"gui.optionsprofiles.save-current-options": "Сохранить настройки",
"gui.optionsprofiles.load-profile": "✔ (Выбрать)",
"gui.optionsprofiles.edit-profile": "✎ (Изменить)",
"gui.optionsprofiles.editing-profile-title": "Изменение профиля: ",
"gui.optionsprofiles.profile-name-text": "Имя профиля",
"gui.optionsprofiles.overwrite-options": "Перезаписать",
"gui.optionsprofiles.rename-profile": "Переименовать",
"gui.optionsprofiles.delete-profile": "удалить",
"modmenu.summaryTranslation.options-profiles": "Cохраняйте и загружайте профили настроек, не выходя из игры.",
"modmenu.descriptionTranslation.options-profiles": "Мод, позволяющий сохранять профили текущих настроек и загружать их, не выходя из игры."
}

View file

@ -0,0 +1,15 @@
{
"gui.optionsprofiles.profiles-menu": "Профиль",
"gui.optionsprofiles.save-current-options": "Хәзерге көйләүләрне саклау",
"gui.optionsprofiles.load-profile": "✔ (Йөкләү)",
"gui.optionsprofiles.edit-profile": "✎ (Үзгәртү)",
"gui.optionsprofiles.editing-profile-title": "Профильне үзгәртү: ",
"gui.optionsprofiles.profile-name-text": "Профиль исеме",
"gui.optionsprofiles.overwrite-options": "перезапись",
"gui.optionsprofiles.rename-profile": "переименовывать",
"gui.optionsprofiles.delete-profile": "бетерү",
"modmenu.summaryTranslation.options-profiles": "Хәзерге көйләүләр профильләрен саклагыз һәм уеннан чыгусыз йөкләгез.",
"modmenu.descriptionTranslation.options-profiles": "Хәзерге көйләүләр профильләрен сакларга һәм уеннан чыгусыз йөкләргә рөхсәт итә торган мод."
}

View file

@ -0,0 +1,14 @@
{
"required": true,
"package": "com.axolotlmaid.optionsprofiles.mixin",
"compatibilityLevel": "JAVA_17",
"minVersion": "0.8",
"client": [
"MixinOptionsScreen"
],
"mixins": [
],
"injectors": {
"defaultRequire": 1
}
}

View file

@ -0,0 +1 @@
accessWidener v2 named