Add version 1.2 features

This commit is contained in:
axolotlmaid 2024-05-21 21:31:13 +01:00
parent 34e6cc3020
commit a9eaaae9e9
2 changed files with 83 additions and 78 deletions

View file

@ -22,19 +22,19 @@ public class EditProfileScreen extends Screen {
}
protected void init() {
this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, this.height - 130, 204, 20, Component.empty());
this.profileNameEdit = new EditBox(this.font, this.width / 2 - 102, 116, 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());
}).size(100, 20).pos(this.width / 2 - 50, 145).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());
}).size(100, 20).pos(this.width / 2 - 50, 166).build());
this.addRenderableWidget(Button.builder(Component.translatable("gui.optionsprofiles.delete-profile").withStyle(ChatFormatting.RED), (button) -> {
new Profiles().deleteProfile(profileName.getString());
@ -50,6 +50,6 @@ public class EditProfileScreen extends Screen {
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);
guiGraphics.drawCenteredString(this.font, Component.translatable("gui.optionsprofiles.profile-name-text"), this.width / 2, 100, 16777215);
}
}

View file

@ -1,7 +1,5 @@
package com.axolotlmaid.optionsprofiles.profiles;
import dev.architectury.platform.Platform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@ -13,7 +11,7 @@ import java.util.List;
import java.util.stream.Stream;
public class Profiles {
public void createProfile() {
public static void createProfile() {
String profileName = "Profile 1";
Path profile = Paths.get("options-profiles/" + profileName);
@ -26,24 +24,29 @@ public class Profiles {
Files.createDirectory(profile);
if (Files.exists(profile)) {
System.out.println("Profile created.");
System.out.println("[Options Profiles] Profile created.");
writeOptionsFilesIntoProfile(profileName);
} else {
System.out.println("Profile was not created successfully.");
System.out.println("[Options Profiles] Profile was not created successfully.");
}
} catch (IOException e) {
System.out.println("An error occurred when creating a profile.");
System.out.println("[Options Profiles] An error occurred when creating a profile.");
e.printStackTrace();
}
}
public void writeOptionsFilesIntoProfile(String profileName) {
private static void writeOptionFile(String profileName, String optionsFile, boolean isSodium) {
Path profile = Paths.get("options-profiles/" + profileName);
// options.txt
Path options = Paths.get("options.txt");
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt");
Path options;
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/" + optionsFile);
if (isSodium) {
options = Paths.get("config/" + optionsFile);
} else {
options = Paths.get(optionsFile);
}
try (Stream<String> paths = Files.lines(options)) {
if (Files.exists(profileOptions))
@ -54,43 +57,32 @@ public class Profiles {
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.");
System.out.println("[Options Profiles] An error occurred when writing a profile.");
e.printStackTrace();
}
});
} catch (IOException e) {
System.out.println("An error occurred when reading options.txt.");
System.out.println("[Options Profiles] An error occurred when reading an options file.");
e.printStackTrace();
}
}
public static void writeOptionsFilesIntoProfile(String profileName) {
// options.txt
writeOptionFile(profileName, "options.txt", false);
// 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");
if (Files.exists(Paths.get("config/sodium-options.json"))) {
writeOptionFile(profileName, "sodium-options.json", true);
}
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();
}
}
// optionsof.txt
if (Files.exists(Paths.get("optionsof.txt"))) {
writeOptionFile(profileName, "optionsof.txt", false);
}
}
public boolean isProfileLoaded(String profileName) {
public static boolean isProfileLoaded(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
Path options = Paths.get("options.txt");
@ -100,8 +92,7 @@ public class Profiles {
List<String> linesOptions = Files.readAllLines(options);
List<String> linesProfileOptions = Files.readAllLines(profileOptions);
if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) {
// sodium-options.json
Path sodiumConfiguration = Paths.get("config/sodium-options.json");
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
@ -111,7 +102,16 @@ public class Profiles {
return linesOptions.equals(linesProfileOptions) && linesSodiumConfig.equals(linesSodiumConfigProfile);
}
}
// optionsof.txt
Path optifineConfiguration = Paths.get("optionsof.txt");
Path optifineConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/optionsof.txt");
if (Files.exists(optifineConfigurationProfile)) {
List<String> linesOptifineConfig = Files.readAllLines(optifineConfiguration);
List<String> linesOptifineConfigProfile = Files.readAllLines(optifineConfigurationProfile);
return linesOptions.equals(linesProfileOptions) && linesOptifineConfig.equals(linesOptifineConfigProfile);
}
return linesOptions.equals(linesProfileOptions);
@ -122,12 +122,11 @@ public class Profiles {
return false;
}
public void loadProfile(String profileName) {
private static void loadOptionFile(String profileName, String optionsFile) {
Path profile = Paths.get("options-profiles/" + profileName);
// options.txt
Path options = Paths.get("options.txt");
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/options.txt");
Path options = Paths.get(optionsFile);
Path profileOptions = Paths.get(profile.toAbsolutePath() + "/" + optionsFile);
try (Stream<String> paths = Files.lines(profileOptions)) {
Files.newBufferedWriter(options, StandardOpenOption.TRUNCATE_EXISTING);
@ -137,49 +136,55 @@ public class Profiles {
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.");
System.out.println("[Options Profiles] An error occurred when loading a profile.");
e.printStackTrace();
}
});
} catch (IOException e) {
System.out.println("An error occurred when loading a profile.");
System.out.println("[Options Profiles] An error occurred when loading a profile.");
e.printStackTrace();
}
}
public static void loadProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
// options.txt
loadOptionFile(profileName, "options.txt");
// sodium-options.json
if (Platform.isFabric()) {
if (Platform.isModLoaded("sodium")) {
Path sodiumConfigurationProfile = Paths.get(profile.toAbsolutePath() + "/sodium-options.json");
if (Files.exists(sodiumConfigurationProfile)) {
if (Files.exists(Paths.get(profile.toAbsolutePath() + "/sodium-options.json")))
SodiumConfigLoader.load(sodiumConfigurationProfile);
}
}
}
// optionsof.txt
if (Files.exists(Paths.get(profile.toAbsolutePath() + "/optionsof.txt")))
loadOptionFile(profileName, "optionsof.txt");
}
public void renameProfile(String profileName, String newProfileName) {
public static 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!");
System.out.println("[Options Profiles] New profile already exists!");
try {
Files.move(profile, newProfile);
if (Files.exists(newProfile)) {
System.out.println("Profile renamed.");
System.out.println("[Options Profiles] Profile renamed.");
} else {
System.out.println("Profile was not renamed successfully.");
System.out.println("[Options Profiles] Profile was not renamed successfully.");
}
} catch (IOException e) {
System.out.println("Profile was not renamed successfully.");
System.out.println("[Options Profiles] Profile was not renamed successfully.");
e.printStackTrace();
}
}
public void deleteProfile(String profileName) {
public static void deleteProfile(String profileName) {
Path profile = Paths.get("options-profiles/" + profileName);
try (Stream<Path> files = Files.walk(profile)) {
@ -188,10 +193,10 @@ public class Profiles {
.map(Path::toFile)
.forEach(File::delete);
} catch (IOException e) {
System.out.println("Profile was not deleted.");
System.out.println("[Options Profiles] Profile was not deleted.");
e.printStackTrace();
}
System.out.println("Profile deleted.");
System.out.println("[Options Profiles] Profile deleted.");
}
}