Fix more gui stuff

This commit is contained in:
blackilykat 2024-11-02 19:05:08 +01:00
parent deeb89bb45
commit 5416cbf6f9
6 changed files with 12 additions and 275 deletions

View file

@ -22,278 +22,6 @@ import static land.chipmunk.chipmunkmod.ChipmunkMod.LOGGER;
// whyt he fuck is this here lmao i have a working config jsut use that ??1'11'!?
public class ModuleMemory {
private static final File MEMORY_FILE = new File(ChipmunkMod.CONFIG_DIR, "chipmunkmodmemory.data");
public ArrayList<Category> categories = new ArrayList<>();
public void loadButOld() throws IOException {
if(!MEMORY_FILE.exists()) {
saveDefaults();
return;
}
// get the default values for everything and port it all to the local variables
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category category : Gui.categoryList) {
Category localCategory = new Category(category.getMessage().getString(), false);
for (land.chipmunk.chipmunkmod.testclient.gui.components.Module module : category.moduleList) {
Module localModule = new Module(module.getMessage().getString(), module.isEnabled);
for (land.chipmunk.chipmunkmod.testclient.gui.components.Option<?> option : module.optionList) {
localModule.options.add(option.toMemoryOption());
}
localCategory.modules.add(localModule);
}
categories.add(localCategory);
}
FileReader reader = null;
try{
reader = new FileReader(MEMORY_FILE);
} catch(FileNotFoundException ignored) {} // this should never happen
// all the constant keywords for easy switchery
final String startKeyword = "start";
final String endKeyword = "end";
final String categoryKeyword = "category";
final String moduleKeyword = "module";
final String optionKeyword = "option";
final String commentKeyword = "comment";
// declare all variables needed
boolean hasStarted = false;
boolean hasEnded = false;
int charCode;
char character;
boolean isReading = false;
StringBuilder buffer = new StringBuilder();
int reading = 0; // 0=keyword, 1=category, 2=module, 3=option, 4=comment
int argument = 0;
String firstArgument = null;
String secondArgument = null;
String currentCategory = null;
String currentModule = null;
// do the magic reading
assert reader != null;
while(reader.ready() && !hasEnded) {
charCode = reader.read(); character = (char) charCode;
LOGGER.info("i read ac haracter woo "+character);
LOGGER.info("buffer is "+buffer.toString());
switch(character) {
case '\n' -> {} // nothing else will get executed (i think)
case ';' -> {
if(argument==2) {
LOGGER.info("arg is 2");
secondArgument = buffer.toString();
switch (reading) {
case 1 -> { // category
LOGGER.info("doing magic category thing owo AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
// first argument is the name
boolean extended = Boolean.parseBoolean(secondArgument);// second argument is if it's extended
for (Category category : categories) {
LOGGER.info("comparing category "+category.name+" with argument "+firstArgument);
if(!category.name.equals(firstArgument)) continue;
LOGGER.info("found category "+category.name);
category.extended = extended;
break;
}
currentCategory = firstArgument;
}
case 2 -> { // module
// first argument is the name
if(currentCategory == null) {
LOGGER.warn(String.format("Found module %s before a category was declared! ignoring", firstArgument));
continue;
}
boolean enabled = Boolean.parseBoolean(secondArgument); // second argument is if it's enabled
for(Category category : categories) {
if(!category.name.equals(currentCategory)) continue;
for (Module module : category.modules) {
if(!module.name.equals(firstArgument)) continue;
module.enabled = enabled;
break;
}
break;
}
currentModule = firstArgument;
}
case 3 -> { // option
if(currentModule == null) {
LOGGER.warn(String.format("Found option %s before a module was declared! ignoring", firstArgument));
continue;
}
if(currentCategory == null) {
LOGGER.warn(String.format("Found option %s before a category was declared! ignoring", firstArgument));
continue;
}
for(Category category : categories) {
if(!category.name.equals(currentCategory)) continue;
for (Module module : category.modules) {
if(!module.name.equals(firstArgument)) continue;
for(Option<?> option : module.options) {
if(!option.name.equals(firstArgument)) continue;
setOptionValue(option, secondArgument);
break;
}
break;
}
break;
}
}
}
}
if(reading==0) switch (buffer.toString()) {
case startKeyword -> {
if(hasStarted) LOGGER.warn("Found multiple ;start statements, only accepting the first one.");
hasStarted = true;
LOGGER.info("found start astyemtnttnnnt owo");
}
case endKeyword -> hasEnded = true;
}
buffer = new StringBuilder();
isReading = true;
reading = 0;
argument = 0;
}
case ':' -> {
argument++;
switch (reading) {
case 0 -> {
switch (buffer.toString()) {
case categoryKeyword -> reading = 1;
case moduleKeyword -> reading = 2;
case optionKeyword -> reading = 3;
case commentKeyword -> reading = 4;
default -> LOGGER.warn("Unknown argumentful keyword '" + buffer + "', ignoring");
}
}
case 4 -> {} // comment
default -> { // category, module and option all have 2 args so I can treat them equally
if(argument==2) firstArgument = buffer.toString();
argument++;
}
}
buffer = new StringBuilder();
}
default -> {
if(!isReading) continue;
buffer.append(character);
}
}
}
if(!hasStarted) {
LOGGER.warn("Memory file exists but has no ;start statement! Overriding file...");
saveDefaults();
}
if(!hasEnded) {
LOGGER.warn("Memory file exists but has no ;end statement! Overriding file...");
saveDefaults();
}
}
public void apply() {
for (Category category : categories) {
// find matching category and set extended
// then loop through every module and do the same thing
// then loop through every option and do the same thing
// get the matching category
land.chipmunk.chipmunkmod.testclient.gui.components.Category realCategory = null;
for (land.chipmunk.chipmunkmod.testclient.gui.components.Category categoryInGui : Gui.categoryList) {
if(category.name.equals(categoryInGui.getMessage().getString())) {
// it's the right category
realCategory = categoryInGui;
break;
}
}
if(realCategory == null) {
LOGGER.warn(String.format("Category '%s' somehow not found in categoryInGui?? report to dev on discord @blackilykat because this is not supposed to be possible :DD", category.name));
continue;
}
LOGGER.info(String.format("Restoring category '%s' extended: %s", category.name, category.extended));
realCategory.isExtended = category.extended;
for(Module module : category.modules) {
land.chipmunk.chipmunkmod.testclient.gui.components.Module realModule = null;
for(land.chipmunk.chipmunkmod.testclient.gui.components.Module moduleInRealCategory : realCategory.moduleList) {
if(module.name.equals(moduleInRealCategory.getMessage().getString())) {
realModule = moduleInRealCategory;
break;
}
}
if(realModule == null) {
LOGGER.warn(String.format("Module '%s' somehow not found in category '%s'?? report to dev on discord @blackilykat because this is not supposed to be possible :DD", module.name, category.name));
continue;
}
LOGGER.info(String.format("Restoring module '%s' enabled: %s", module.name, module.enabled));
realModule.isEnabled = module.enabled;
for(Option<?> option : module.options) {
land.chipmunk.chipmunkmod.testclient.gui.components.Option<?> realOption = null;
for(land.chipmunk.chipmunkmod.testclient.gui.components.Option<?> optionInRealModule : realModule.optionList) {
if(option.name.equals(optionInRealModule.name)) {
realOption = optionInRealModule;
break;
}
}
if(realOption == null) {
LOGGER.warn(String.format("Option '%s' somehow not found in module '%s' in category '%s'?? report to dev on discord @blackilykat because this is not supposed to be possible :DD", option.name, module.name, category.name));
continue;
}
LOGGER.info(String.format("Restoring option '%s' value: %s", option.name, option.value));
setRealOptionValue(realOption, option);
}
}
}
}
public void saveDefaults() {
}
public <T> void setRealOptionValue(land.chipmunk.chipmunkmod.testclient.gui.components.Option<T> real, Option<?> fake) {
real.optionValue = (T) fake.value; // shut the fuck up intellij this cast is fine
}
public <T> void setOptionValue(Option<T> option, String value) {
if(option.getType() == String.class) {
option.value = (T) value; // ignore warning it's (String) string
} else if (option.getType() == Integer.class) {
Integer newValue = null;
try {
newValue = Integer.valueOf(value);
} catch (NumberFormatException e) {
LOGGER.warn(String.format("Option %s expects an integer, however the value '%s' cannot be parsed as one.", option.name, value));
return;
}
option.value = (T) newValue;
} else if (option.getType() == Double.class) {
Double newValue = null;
try {
newValue = Double.valueOf(value);
} catch (NumberFormatException e) {
LOGGER.warn(String.format("Option %s expects a double, however the value '%s' cannot be parsed as one.", option.name, value));
return;
}
option.value = (T) newValue;
} else {
throw new UnknownOptionTypeException(option.getType());
// no need to catch
// because pro runtime exception
}
}
public static class Category {
public final String name;
public boolean extended;
public ArrayList<Module> modules = new ArrayList<>();
public Category(String name, boolean extended) {
this.name = name;
this.extended = extended;
}
}
public static class Module {
public final String name;
public boolean enabled;

View file

@ -38,6 +38,7 @@ public class BooleanCheckboxOption extends Option<Boolean> {
@Override
public void setValueFromString(String string) {
optionValue = Boolean.valueOf(string);
checkboxWidget.checked = optionValue;
}
@Override

View file

@ -10,13 +10,14 @@ public class DoubleSliderOption extends Option<Double> {
int roundTo = 4;
SliderWidget sliderWidget = new SliderWidget(0, 0, 100, 20, Text.literal("text ig"), 0.0) {
@Override
protected void updateMessage() {
public void updateMessage() {
setMessage(Text.literal(round((value * (maxValue - minValue) + minValue), roundTo)+""));
}
@Override
protected void applyValue() {
optionValue = round((value * (maxValue - minValue) + minValue), roundTo);
System.out.println("Saving value " + optionValue + " from " + value);
}
public double getValue() {
@ -56,6 +57,8 @@ public class DoubleSliderOption extends Option<Double> {
public void setValueFromString(String string) {
optionValue = Double.valueOf(string);
sliderWidget.value = (optionValue - minValue) / (maxValue - minValue);
sliderWidget.updateMessage();
}
public String getValueAsString() {
return Double.toString(optionValue);

View file

@ -9,7 +9,7 @@ public class IntSliderOption extends Option<Integer> {
public int minValue = 0;
SliderWidget sliderWidget = new SliderWidget(0, 0, 100, 20, Text.literal("text ig"), 0.0) {
@Override
protected void updateMessage() {
public void updateMessage() {
setMessage(Text.literal(((int) (value * (maxValue - minValue) + minValue))+""));
}
@ -39,6 +39,8 @@ public class IntSliderOption extends Option<Integer> {
@Override
public void setValueFromString(String string) {
optionValue = Integer.valueOf(string);
sliderWidget.value = optionValue;
sliderWidget.updateMessage();
}
@Override

View file

@ -31,6 +31,7 @@ public class StringOption extends Option<String> {
@Override
public void setValueFromString(String string) {
optionValue = string; // pro conversion
textFieldWidget.setText(optionValue);
}
@Override

View file

@ -1,3 +1,5 @@
accessWidener v2 named
accessible field net/minecraft/client/gui/screen/Screen drawables Ljava/util/List;
accessible field net/minecraft/client/gui/widget/CheckboxWidget checked Z
accessible field net/minecraft/client/gui/widget/CheckboxWidget checked Z
accessible field net/minecraft/client/gui/widget/SliderWidget value D
accessible method net/minecraft/client/gui/widget/SliderWidget updateMessage ()V