aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
diff options
context:
space:
mode:
authorWyvest <45589059+Wyvest@users.noreply.github.com>2022-06-27 03:54:28 +0700
committerWyvest <45589059+Wyvest@users.noreply.github.com>2022-06-27 03:54:28 +0700
commit9afbfbc4c41bf8f90b5de901797e18375da2c01a (patch)
tree125282f723f1e17a8a077c0a3222f777fd74ca06 /src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java
parentf6bebfc3d14812dfac4592c1b35becf83d46cf76 (diff)
downloadOneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.gz
OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.tar.bz2
OneConfig-9afbfbc4c41bf8f90b5de901797e18375da2c01a.zip
cache text
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/hud/SingleTextHud.java70
1 files changed, 62 insertions, 8 deletions
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;
+ }
+ }
+ }
+ }
}