diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-06-27 03:54:28 +0700 |
---|---|---|
committer | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-06-27 03:54:28 +0700 |
commit | 9afbfbc4c41bf8f90b5de901797e18375da2c01a (patch) | |
tree | 125282f723f1e17a8a077c0a3222f777fd74ca06 /src/main/java/cc/polyfrost | |
parent | f6bebfc3d14812dfac4592c1b35becf83d46cf76 (diff) | |
download | OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.gz OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.bz2 OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.zip |
cache text
Diffstat (limited to 'src/main/java/cc/polyfrost')
11 files changed, 224 insertions, 24 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index ee1d2bd..f864d71 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -1,6 +1,7 @@ package cc.polyfrost.oneconfig.config; import cc.polyfrost.oneconfig.config.annotations.CustomOption; +import cc.polyfrost.oneconfig.config.annotations.Exclude; import cc.polyfrost.oneconfig.config.annotations.HUD; import cc.polyfrost.oneconfig.config.annotations.Page; import cc.polyfrost.oneconfig.config.core.ConfigUtils; @@ -35,7 +36,7 @@ import java.util.function.Supplier; public class Config { public final transient HashMap<String, BasicOption> optionNames = new HashMap<>(); transient protected final String configFile; - transient protected final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); + transient protected final Gson gson = new GsonBuilder().setExclusionStrategies(new ExcludeStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create(); transient public Mod mod; public transient boolean hasBeenInitialized = false; public boolean enabled = true; @@ -43,10 +44,21 @@ public class Config { /** * @param modData information about the mod * @param configFile file where config is stored + * @param initialize whether to initialize the config. */ - public Config(Mod modData, String configFile) { + public Config(Mod modData, String configFile, boolean initialize) { this.configFile = configFile; - init(modData); + if (initialize) { + init(modData); + } + } + + /** + * @param modData information about the mod + * @param configFile file where config is stored + */ + public Config(Mod modData, String configFile) { + this(modData, configFile, true); } public void init(Mod mod) { @@ -230,4 +242,29 @@ public class Config { keyBind.setRunnable(runnable); KeyBindHandler.INSTANCE.addKeyBind(keyBind); } + + private static class ExcludeStrategy implements ExclusionStrategy { + + /** + * @param f the field object that is under test + * @return true if the field should be ignored; otherwise false + */ + @Override + public boolean shouldSkipField(FieldAttributes f) { + Exclude annotation = f.getAnnotation(Exclude.class); + if (annotation != null) { + return annotation.type() != Exclude.ExcludeType.HUD; + } + return false; + } + + /** + * @param clazz the class object that is under test + * @return true if the class should be ignored; otherwise false + */ + @Override + public boolean shouldSkipClass(Class<?> clazz) { + return false; + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java new file mode 100644 index 0000000..6ffd1d9 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java @@ -0,0 +1,24 @@ +package cc.polyfrost.oneconfig.config.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Excludes fields from being serialized or deserialized by OneConfig's Config and HUD + * system. + * + * This can be used interchangeably with the transient modifier built into Java. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Exclude { + ExcludeType type() default ExcludeType.ALL; + + enum ExcludeType { + ALL, + CONFIG, + HUD + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java index fb3ff3a..b8958de 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java @@ -1,5 +1,6 @@ package cc.polyfrost.oneconfig.config.core; +import cc.polyfrost.oneconfig.config.annotations.Exclude; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.config.elements.OptionCategory; import cc.polyfrost.oneconfig.config.elements.OptionPage; @@ -55,6 +56,8 @@ public class ConfigUtils { parentClass = clazz; } for (Field field : fields) { + Exclude exclude = findAnnotation(field, Exclude.class); + if (exclude != null && exclude.type() != Exclude.ExcludeType.CONFIG) continue; Option option = findAnnotation(field, Option.class); if (option == null) continue; options.add(getOption(option, field, object)); diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java b/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java new file mode 100644 index 0000000..c4ff151 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Cacheable.java @@ -0,0 +1,9 @@ +package cc.polyfrost.oneconfig.hud; + +import cc.polyfrost.oneconfig.config.elements.BasicOption; + +import java.util.ArrayList; + +public interface Cacheable { + void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java index e91a7ce..babb810 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/HUDUtils.java @@ -23,6 +23,9 @@ public class HUDUtils { options.add(new ConfigHeader(field, hud, hudAnnotation.name(), category, subcategory, 2)); options.add(new ConfigSwitch(hud.getClass().getField("enabled"), hud, "Enabled", category, subcategory, 2)); options.addAll(ConfigUtils.getClassOptions(hud)); + if (hud instanceof Cacheable) { + ((Cacheable) hud).addCacheOptions(category, subcategory, options); + } options.add(new ConfigCheckbox(hud.getClass().getField("rounded"), hud, "Rounded corners", category, subcategory, 1)); options.get(options.size() - 1).addDependency(() -> hud.enabled); options.add(new ConfigCheckbox(hud.getClass().getField("border"), hud, "Outline/border", category, subcategory, 1)); diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java index 71c6c38..61e960d 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/Hud.java @@ -30,7 +30,7 @@ import cc.polyfrost.oneconfig.config.Config; * * @literal @HUD( * * name = "HUD Element" * * ) - * * public YourHudElement hudElement = new YourHudElement(false); + * * public YourHudElement hudElement = new YourHudElement("Title"); * * } * * }</pre> */ diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java index 5440f10..5dbff4c 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/MultiTextHud.java @@ -1,26 +1,63 @@ package cc.polyfrost.oneconfig.hud; +import cc.polyfrost.oneconfig.config.annotations.Exclude; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.events.event.TickEvent; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigSwitch; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; import cc.polyfrost.oneconfig.libs.universal.UMinecraft; import cc.polyfrost.oneconfig.libs.universal.UScreen; import cc.polyfrost.oneconfig.renderer.RenderManager; import net.minecraft.client.gui.GuiChat; +import java.util.ArrayList; import java.util.List; -public abstract class MultiTextHud extends TextHud { +public abstract class MultiTextHud extends TextHud implements Cacheable { private transient int width = 100; private transient int height; + private transient List<String> cachedStrings = null; + private transient final boolean caching; public MultiTextHud(boolean enabled) { this(enabled, 0, 0); } + public MultiTextHud(boolean enabled, boolean caching) { + this(enabled, 0, 0, caching); + } + public MultiTextHud(boolean enabled, int x, int y) { + this(enabled, x, y, true); + } + + public MultiTextHud(boolean enabled, int x, int y, boolean caching) { super(enabled, x, y); + this.caching = caching; + if (caching) { + EventManager.INSTANCE.register(new TextCacher()); + } } @Override + public void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options) { + if (caching) { + try { + System.out.println("AIJSIOJ!!"); + options.add(new ConfigSwitch(getClass().getField("cacheText"), this, "Cache Text", category, subcategory, 1)); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + } + + @Exclude(type = Exclude.ExcludeType.HUD) + public boolean cacheText = true; + + @Override public int getWidth(float scale) { return (int) (width * scale); } @@ -38,7 +75,7 @@ public abstract class MultiTextHud extends TextHud { int textY = y; width = 0; - for (String line : getLines()) { + for (String line : cachedStrings == null ? getLines() : cachedStrings) { RenderManager.drawScaledString(line, x, textY, color.getRGB(), RenderManager.TextType.toType(textType), scale); width = Math.max(width, UMinecraft.getFontRenderer().getStringWidth(line)); textY += 12 * scale; @@ -63,4 +100,17 @@ public abstract class MultiTextHud extends TextHud { public List<String> getExampleLines() { return getLines(); } + + private class TextCacher { + @Subscribe + private void onTick(TickEvent event) { + if (event.stage == Stage.START) { + if (cacheText) { + cachedStrings = getLines(); + } else { + cachedStrings = null; + } + } + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java index 983047a..f1a4125 100644 --- a/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java +++ b/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java @@ -1,24 +1,67 @@ package cc.polyfrost.oneconfig.hud; import cc.polyfrost.oneconfig.config.annotations.Dropdown; +import cc.polyfrost.oneconfig.config.annotations.Exclude; import cc.polyfrost.oneconfig.config.annotations.Switch; import cc.polyfrost.oneconfig.config.annotations.Text; +import cc.polyfrost.oneconfig.config.elements.BasicOption; +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.events.event.TickEvent; import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.gui.elements.config.ConfigSwitch; +import cc.polyfrost.oneconfig.libs.eventbus.Subscribe; import cc.polyfrost.oneconfig.libs.universal.UMinecraft; import cc.polyfrost.oneconfig.libs.universal.UScreen; import cc.polyfrost.oneconfig.renderer.RenderManager; import net.minecraft.client.gui.GuiChat; -public abstract class SingleTextHud extends TextHud { +import java.util.ArrayList; - public SingleTextHud(boolean enabled) { - this(enabled, 0, 0); +public abstract class SingleTextHud extends TextHud implements Cacheable { + + private transient String cachedString = null; + private transient final boolean caching; + + public SingleTextHud(String title) { + this(title, true); } - public SingleTextHud(boolean enabled, int x, int y) { + public SingleTextHud(String title, boolean enabled) { + this(title, enabled, 0, 0); + } + + public SingleTextHud(String title, boolean enabled, boolean caching) { + this(title, enabled, 0, 0, caching); + } + + public SingleTextHud(String title, boolean enabled, int x, int y) { + this(title, enabled, x, y, true); + } + + public SingleTextHud(String title, boolean enabled, int x, int y, boolean caching) { super(enabled, x, y); + this.title = title; + this.caching = caching; + if (caching) { + EventManager.INSTANCE.register(new TextCacher()); + } } + @Override + public void addCacheOptions(String category, String subcategory, ArrayList<BasicOption> options) { + if (caching) { + try { + options.add(new ConfigSwitch(getClass().getField("cacheText"), this, "Cache Text", category, subcategory, 1)); + } catch (NoSuchFieldException e) { + e.printStackTrace(); + } + } + } + + @Exclude(type = Exclude.ExcludeType.HUD) + public boolean cacheText = true; + @Switch( name = "Brackets" ) @@ -27,7 +70,7 @@ public abstract class SingleTextHud extends TextHud { @Text( name = "Title" ) - public String title = getDefaultTitle(); + public String title; @Dropdown( name = "Title Location", @@ -35,8 +78,6 @@ public abstract class SingleTextHud extends TextHud { ) public int titleLocation = 0; - public abstract String getDefaultTitle(); - @Override public int getWidth(float scale) { return (int) (UMinecraft.getFontRenderer().getStringWidth(getCompleteText(false)) * scale); @@ -77,7 +118,7 @@ public abstract class SingleTextHud extends TextHud { builder.append(title).append(": "); } - builder.append(example ? getExampleText() : getText()); + builder.append(example ? getExampleText() : (cachedString == null ? getText() : cachedString)); if (showTitle && titleLocation == 1) { builder.append(" ").append(title); @@ -94,4 +135,17 @@ public abstract class SingleTextHud extends TextHud { public String getExampleText() { return getText(); } + + private class TextCacher { + @Subscribe + private void onTick(TickEvent event) { + if (event.stage == Stage.START) { + if (cacheText) { + cachedString = getText(); + } else { + cachedString = null; + } + } + } + } } diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java index 2f4960e..4acd533 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java @@ -117,7 +117,13 @@ public class TestConfig_Test extends Config { name = "Test HUD", category = "HUD" ) - public TestHud_Test hud = new TestHud_Test(false, 0, 0); + public TestHud_Test hud = new TestHud_Test(); + + @HUD( + name = "Test Multiline HUD", + category = "HUD" + ) + public TestMultilineHud_Test multilineHud = new TestMultilineHud_Test(); public TestConfig_Test() { super(new Mod("Test Mod", ModType.UTIL_QOL, new VigilanceMigrator("./config/testConfig.toml")), "hacksConfig.json"); diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestHud_Test.java b/src/main/java/cc/polyfrost/oneconfig/test/TestHud_Test.java index 9982e7d..d62f664 100644 --- a/src/main/java/cc/polyfrost/oneconfig/test/TestHud_Test.java +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestHud_Test.java @@ -2,25 +2,21 @@ package cc.polyfrost.oneconfig.test; import cc.polyfrost.oneconfig.config.annotations.Switch; import cc.polyfrost.oneconfig.hud.SingleTextHud; -import net.minecraft.client.Minecraft; public class TestHud_Test extends SingleTextHud { + int times = 0; @Switch( name = "Custom Option" ) public boolean yes; - public TestHud_Test(boolean enabled, int x, int y) { - super(enabled, x, y); - } - - @Override - public String getDefaultTitle() { - return "FPS"; + public TestHud_Test() { + super("Time"); } @Override public String getText() { - return Integer.toString(Minecraft.getDebugFPS()); + times++; + return String.valueOf(times); } } diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestMultilineHud_Test.java b/src/main/java/cc/polyfrost/oneconfig/test/TestMultilineHud_Test.java new file mode 100644 index 0000000..d3bad28 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/test/TestMultilineHud_Test.java @@ -0,0 +1,18 @@ +package cc.polyfrost.oneconfig.test; + +import cc.polyfrost.oneconfig.hud.MultiTextHud; +import com.google.common.collect.Lists; +import net.minecraft.client.Minecraft; + +import java.util.List; + +public class TestMultilineHud_Test extends MultiTextHud { + public TestMultilineHud_Test() { + super(true); + } + + @Override + public List<String> getLines() { + return Lists.newArrayList(String.valueOf(System.currentTimeMillis()), String.valueOf(Minecraft.getSystemTime())); + } +} |