From fb7cab28bf7246729e01b4e9aed98cf09fd44ea1 Mon Sep 17 00:00:00 2001 From: tr7zw Date: Thu, 1 Aug 2024 19:30:34 +0200 Subject: [PATCH] Start separating logic --- .../exordium/versionless/config/Config.java | 1 - .../dev/tr7zw/exordium/BufferManager.java | 115 -------------- .../dev/tr7zw/exordium/ExordiumModBase.java | 13 +- .../dev/tr7zw/exordium/access/ChatAccess.java | 16 +- .../exordium/access/VanillaBufferAccess.java | 6 - .../exordium/components/BufferComponent.java | 9 ++ .../exordium/components/BufferInstance.java | 44 ++++++ .../exordium/components/BufferManager.java | 133 ++++++++++++++++ .../components/vanilla/ChatComponent.java | 51 +++++++ .../exordium/config/ExordiumConfigScreen.java | 4 +- .../exordium/mixin/ChatComponentMixin.java | 143 ++++++++++-------- .../dev/tr7zw/exordium/mixin/GuiMixin.java | 9 +- .../exordium/util/BufferedComponent.java | 59 ++++---- .../tr7zw/exordium/util/ScoreboardHelper.java | 3 +- 14 files changed, 383 insertions(+), 223 deletions(-) delete mode 100644 src/main/java/dev/tr7zw/exordium/BufferManager.java create mode 100644 src/main/java/dev/tr7zw/exordium/components/BufferComponent.java create mode 100644 src/main/java/dev/tr7zw/exordium/components/BufferInstance.java create mode 100644 src/main/java/dev/tr7zw/exordium/components/BufferManager.java create mode 100644 src/main/java/dev/tr7zw/exordium/components/vanilla/ChatComponent.java diff --git a/Exordium-Versionless/src/main/java/dev/tr7zw/exordium/versionless/config/Config.java b/Exordium-Versionless/src/main/java/dev/tr7zw/exordium/versionless/config/Config.java index 7c87454..2cdf452 100644 --- a/Exordium-Versionless/src/main/java/dev/tr7zw/exordium/versionless/config/Config.java +++ b/Exordium-Versionless/src/main/java/dev/tr7zw/exordium/versionless/config/Config.java @@ -18,7 +18,6 @@ public class Config { public ComponentSettings crosshairSettings = new ComponentSettings(false, 20); public ComponentSettings bossbarSettings = new ComponentSettings(true, 5); - @Getter @Setter public static class ComponentSettings { diff --git a/src/main/java/dev/tr7zw/exordium/BufferManager.java b/src/main/java/dev/tr7zw/exordium/BufferManager.java deleted file mode 100644 index 18342b5..0000000 --- a/src/main/java/dev/tr7zw/exordium/BufferManager.java +++ /dev/null @@ -1,115 +0,0 @@ -package dev.tr7zw.exordium; - -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Consumer; -import java.util.function.Function; - -import dev.tr7zw.exordium.access.ChatAccess; -import dev.tr7zw.exordium.access.GuiAccess; -import dev.tr7zw.exordium.access.TablistAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.CrosshairOverlayAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.DebugOverlayAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.ExperienceBarOverlayAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.HotbarOverlayAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.ScoreBoardOverlayAccess; -import dev.tr7zw.exordium.access.VanillaBufferAccess.VignetteOverlayAccess; -import dev.tr7zw.exordium.util.BufferedComponent; -import lombok.NoArgsConstructor; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Gui; -import net.minecraft.client.gui.GuiGraphics; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.scores.DisplaySlot; -import net.minecraft.world.scores.Scoreboard; - -@NoArgsConstructor -public class BufferManager { - - private final Map> vanillaBuffers = new HashMap<>(); - private final Map> customHandlers = new HashMap<>(); - private final Map customEndHandlers = new HashMap<>(); - - public void initialize() { - Minecraft minecraft = Minecraft.getInstance(); - vanillaBuffers.put(new ResourceLocation("debug_text"), - gui -> ((DebugOverlayAccess) gui).getDebugOverlayBuffer()); - vanillaBuffers.put(new ResourceLocation("crosshair"), - gui -> ((CrosshairOverlayAccess) gui).exordium_getCrosshairOverlayBuffer()); - vanillaBuffers.put(new ResourceLocation("experience_bar"), - gui -> ((ExperienceBarOverlayAccess) gui).getExperienceBarOverlayBuffer()); - vanillaBuffers.put(new ResourceLocation("scoreboard"), - gui -> ((ScoreBoardOverlayAccess) gui).getScoreBoardOverlayBuffer()); - vanillaBuffers.put(new ResourceLocation("hotbar"), gui -> ((HotbarOverlayAccess) gui).getHotbarOverlayBuffer()); - registerCustomHandler(new ResourceLocation("vignette"), data -> { - VignetteOverlayAccess vignette = (VignetteOverlayAccess) minecraft.gui; - if (ExordiumModBase.instance.config.vignetteSettings.isEnabled()) { - if (!vignette.getVignetteOverlayBuffer().render()) { - vignette.renderCustomVignette(data.gui()); - } - data.cancel().set(true); - } - vignette.getVignetteOverlayBuffer().renderEnd(); - }); - registerCustomHandler(new ResourceLocation("chat_panel"), data -> { - GuiAccess guiAccess = (GuiAccess) minecraft.gui; - ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent(); - chatAccess.updateState(guiAccess.getTickCount()); - BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer(); - if (bufferedComponent.render()) { - data.cancel().set(true); - } - }); - registerCustomEndHandler(new ResourceLocation("chat_panel"), () -> { - GuiAccess guiAccess = (GuiAccess) minecraft.gui; - ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent(); - BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer(); - bufferedComponent.renderEnd(); - }); - registerCustomHandler(new ResourceLocation("player_list"), data -> { - GuiAccess guiAccess = (GuiAccess) minecraft.gui; - TablistAccess tablistAccess = (TablistAccess) guiAccess.getPlayerTabOverlay(); - Scoreboard scoreboard = minecraft.level.getScoreboard(); - tablistAccess.updateState(scoreboard, scoreboard.getDisplayObjective(DisplaySlot.LIST)); - BufferedComponent bufferedComponent = tablistAccess.getPlayerListOverlayBuffer(); - if (bufferedComponent.render()) { - data.cancel().set(true); - } - }); - registerCustomEndHandler(new ResourceLocation("player_list"), () -> { - GuiAccess guiAccess = (GuiAccess) minecraft.gui; - TablistAccess tabAccess = (TablistAccess) guiAccess.getPlayerTabOverlay(); - BufferedComponent bufferedComponent = tabAccess.getPlayerListOverlayBuffer(); - bufferedComponent.renderEnd(); - }); - } - - public BufferedComponent getBufferedComponent(ResourceLocation resourceLocation, Gui gui) { - Function vanFun = vanillaBuffers.get(resourceLocation); - if (vanFun != null) { - return vanFun.apply(gui); - } - return null; - } - - public Consumer getCustomHandler(ResourceLocation resourceLocation) { - return customHandlers.get(resourceLocation); - } - - public Runnable getCustomEndHandler(ResourceLocation resourceLocation) { - return customEndHandlers.get(resourceLocation); - } - - public void registerCustomHandler(ResourceLocation resourceLocation, Consumer handler) { - customHandlers.put(resourceLocation, handler); - } - - public void registerCustomEndHandler(ResourceLocation resourceLocation, Runnable handler) { - customEndHandlers.put(resourceLocation, handler); - } - - public record HandlerData(GuiGraphics gui, AtomicBoolean cancel) { - } - -} diff --git a/src/main/java/dev/tr7zw/exordium/ExordiumModBase.java b/src/main/java/dev/tr7zw/exordium/ExordiumModBase.java index 2364b00..fcabeec 100644 --- a/src/main/java/dev/tr7zw/exordium/ExordiumModBase.java +++ b/src/main/java/dev/tr7zw/exordium/ExordiumModBase.java @@ -14,6 +14,7 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; +import dev.tr7zw.exordium.components.BufferManager; import dev.tr7zw.exordium.config.ExordiumConfigScreen; import dev.tr7zw.exordium.util.CustomShaderManager; import dev.tr7zw.exordium.util.DelayedRenderCallManager; @@ -40,8 +41,8 @@ public abstract class ExordiumModBase { private final DelayedRenderCallManager delayedRenderCallManager = new DelayedRenderCallManager(); @Getter private final CustomShaderManager customShaderManager = new CustomShaderManager(); - @Getter private final BufferManager bufferManager = new BufferManager(); + private boolean lateInit = true; void onInitialize() { instance = this; @@ -61,7 +62,6 @@ void onInitialize() { writeConfig(); // Config got modified } } - bufferManager.initialize(); initModloader(); } @@ -92,4 +92,13 @@ public static void correctBlendMode() { GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); } + public BufferManager getBufferManager() { + // FIXME + if (lateInit) { + bufferManager.initialize(); + lateInit = false; + } + return bufferManager; + } + } diff --git a/src/main/java/dev/tr7zw/exordium/access/ChatAccess.java b/src/main/java/dev/tr7zw/exordium/access/ChatAccess.java index ce0deb1..bf469c6 100644 --- a/src/main/java/dev/tr7zw/exordium/access/ChatAccess.java +++ b/src/main/java/dev/tr7zw/exordium/access/ChatAccess.java @@ -1,9 +1,19 @@ package dev.tr7zw.exordium.access; -import dev.tr7zw.exordium.access.VanillaBufferAccess.ChatOverlayAccess; +import java.util.List; -public interface ChatAccess extends ChatOverlayAccess { +import net.minecraft.client.GuiMessage; - void updateState(int tickCount); +public interface ChatAccess { + + List getTrimmedMessages(); + + int getChatScollbarPos(); + + boolean isChatFocused(); + + boolean isChatHidden(); + + int getLinesPerPage(); } diff --git a/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java b/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java index 6fe681d..ef7dc6e 100644 --- a/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java +++ b/src/main/java/dev/tr7zw/exordium/access/VanillaBufferAccess.java @@ -11,12 +11,6 @@ interface DebugOverlayAccess extends VanillaBufferAccess { } - interface ChatOverlayAccess extends VanillaBufferAccess { - - BufferedComponent getChatOverlayBuffer(); - - } - interface CrosshairOverlayAccess extends VanillaBufferAccess { BufferedComponent exordium_getCrosshairOverlayBuffer(); diff --git a/src/main/java/dev/tr7zw/exordium/components/BufferComponent.java b/src/main/java/dev/tr7zw/exordium/components/BufferComponent.java new file mode 100644 index 0000000..e67a3dc --- /dev/null +++ b/src/main/java/dev/tr7zw/exordium/components/BufferComponent.java @@ -0,0 +1,9 @@ +package dev.tr7zw.exordium.components; + +public interface BufferComponent { + + public void captureState(T context); + + public boolean hasChanged(int tickCount, T context); + +} diff --git a/src/main/java/dev/tr7zw/exordium/components/BufferInstance.java b/src/main/java/dev/tr7zw/exordium/components/BufferInstance.java new file mode 100644 index 0000000..f5fd683 --- /dev/null +++ b/src/main/java/dev/tr7zw/exordium/components/BufferInstance.java @@ -0,0 +1,44 @@ +package dev.tr7zw.exordium.components; + +import java.util.function.Supplier; + +import dev.tr7zw.exordium.util.BufferedComponent; +import dev.tr7zw.exordium.versionless.config.Config; +import lombok.Getter; +import net.minecraft.resources.ResourceLocation; + +public final class BufferInstance { + + @Getter + private final ResourceLocation id; + private final BufferComponent component; + private final BufferedComponent buffer; + + BufferInstance(ResourceLocation id, BufferComponent component, Supplier settings) { + this.id = id; + this.component = component; + this.buffer = new BufferedComponent(settings); + } + + /** + * Tries to render the buffer. Returns false if the buffer was not rendered and + * the normal render logic should be used. + * + * @param context + * @return + */ + public boolean renderBuffer(int ticks, T context) { + return buffer.render(() -> component.hasChanged(ticks, context)); + } + + /** + * This method should be called after the render logic, no matter if the buffer + * or normal render logic was used. + * + * @param context + */ + public void postRender(T context) { + buffer.renderEnd(() -> component.captureState(context)); + } + +} diff --git a/src/main/java/dev/tr7zw/exordium/components/BufferManager.java b/src/main/java/dev/tr7zw/exordium/components/BufferManager.java new file mode 100644 index 0000000..4b30401 --- /dev/null +++ b/src/main/java/dev/tr7zw/exordium/components/BufferManager.java @@ -0,0 +1,133 @@ +package dev.tr7zw.exordium.components; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Supplier; + +import dev.tr7zw.exordium.ExordiumModBase; +import dev.tr7zw.exordium.access.ChatAccess; +import dev.tr7zw.exordium.access.GuiAccess; +import dev.tr7zw.exordium.access.TablistAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.CrosshairOverlayAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.DebugOverlayAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.ExperienceBarOverlayAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.HotbarOverlayAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.ScoreBoardOverlayAccess; +import dev.tr7zw.exordium.access.VanillaBufferAccess.VignetteOverlayAccess; +import dev.tr7zw.exordium.components.vanilla.ChatComponent; +import dev.tr7zw.exordium.util.BufferedComponent; +import dev.tr7zw.exordium.versionless.config.Config; +import lombok.NoArgsConstructor; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Gui; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.scores.DisplaySlot; +import net.minecraft.world.scores.Scoreboard; + +@NoArgsConstructor +public class BufferManager { + +// private final Map> vanillaBuffers = new HashMap<>(); +// private final Map> customHandlers = new HashMap<>(); +// private final Map customEndHandlers = new HashMap<>(); + private final Map> buffers = new HashMap<>(); + + public void initialize() { + ExordiumModBase inst = ExordiumModBase.instance; + Minecraft minecraft = Minecraft.getInstance(); +// vanillaBuffers.put(new ResourceLocation("debug_text"), +// gui -> ((DebugOverlayAccess) gui).getDebugOverlayBuffer()); +// vanillaBuffers.put(new ResourceLocation("crosshair"), +// gui -> ((CrosshairOverlayAccess) gui).exordium_getCrosshairOverlayBuffer()); +// vanillaBuffers.put(new ResourceLocation("experience_bar"), +// gui -> ((ExperienceBarOverlayAccess) gui).getExperienceBarOverlayBuffer()); +// vanillaBuffers.put(new ResourceLocation("scoreboard"), +// gui -> ((ScoreBoardOverlayAccess) gui).getScoreBoardOverlayBuffer()); +// vanillaBuffers.put(new ResourceLocation("hotbar"), gui -> ((HotbarOverlayAccess) gui).getHotbarOverlayBuffer()); +// registerCustomHandler(new ResourceLocation("vignette"), data -> { +// VignetteOverlayAccess vignette = (VignetteOverlayAccess) minecraft.gui; +// if (ExordiumModBase.instance.config.vignetteSettings.isEnabled()) { +// if (!vignette.getVignetteOverlayBuffer().render()) { +// vignette.renderCustomVignette(data.gui()); +// } +// data.cancel().set(true); +// } +// vignette.getVignetteOverlayBuffer().renderEnd(); +// }); +// registerCustomHandler(new ResourceLocation("chat_panel"), data -> { +// GuiAccess guiAccess = (GuiAccess) minecraft.gui; +// ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent(); +// chatAccess.updateState(guiAccess.getTickCount()); +// BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer(); +// if (bufferedComponent.render()) { +// data.cancel().set(true); +// } +// }); + registerBuffer(ChatComponent.getId(), new ChatComponent(), () -> inst.config.chatSettings); + +// registerCustomEndHandler(new ResourceLocation("chat_panel"), () -> { +// GuiAccess guiAccess = (GuiAccess) minecraft.gui; +// ChatAccess chatAccess = (ChatAccess) guiAccess.getChatComponent(); +// BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer(); +// bufferedComponent.renderEnd(); +// }); +// registerCustomHandler(new ResourceLocation("player_list"), data -> { +// GuiAccess guiAccess = (GuiAccess) minecraft.gui; +// TablistAccess tablistAccess = (TablistAccess) guiAccess.getPlayerTabOverlay(); +// Scoreboard scoreboard = minecraft.level.getScoreboard(); +// tablistAccess.updateState(scoreboard, scoreboard.getDisplayObjective(DisplaySlot.LIST)); +// BufferedComponent bufferedComponent = tablistAccess.getPlayerListOverlayBuffer(); +// if (bufferedComponent.render()) { +// data.cancel().set(true); +// } +// }); +// registerCustomEndHandler(new ResourceLocation("player_list"), () -> { +// GuiAccess guiAccess = (GuiAccess) minecraft.gui; +// TablistAccess tabAccess = (TablistAccess) guiAccess.getPlayerTabOverlay(); +// BufferedComponent bufferedComponent = tabAccess.getPlayerListOverlayBuffer(); +// bufferedComponent.renderEnd(); +// }); + } + + @SuppressWarnings("unchecked") + public BufferInstance getBufferInstance(ResourceLocation id, Class context) { + return (BufferInstance) buffers.get(id); + } + + public void registerBuffer(ResourceLocation id, BufferComponent component, + Supplier settings) { + this.buffers.put(id, new BufferInstance<>(id, component, settings)); + } +// +// public BufferedComponent getBufferedComponent(ResourceLocation resourceLocation, Gui gui) { +// Function vanFun = vanillaBuffers.get(resourceLocation); +// if (vanFun != null) { +// return vanFun.apply(gui); +// } +// return null; +// } +// +// public Consumer getCustomHandler(ResourceLocation resourceLocation) { +// return customHandlers.get(resourceLocation); +// } +// +// public Runnable getCustomEndHandler(ResourceLocation resourceLocation) { +// return customEndHandlers.get(resourceLocation); +// } +// +// public void registerCustomHandler(ResourceLocation resourceLocation, Consumer handler) { +// customHandlers.put(resourceLocation, handler); +// } +// +// public void registerCustomEndHandler(ResourceLocation resourceLocation, Runnable handler) { +// customEndHandlers.put(resourceLocation, handler); +// } +// +// public record HandlerData(GuiGraphics gui, AtomicBoolean cancel) { +// } + +} diff --git a/src/main/java/dev/tr7zw/exordium/components/vanilla/ChatComponent.java b/src/main/java/dev/tr7zw/exordium/components/vanilla/ChatComponent.java new file mode 100644 index 0000000..6d71a9d --- /dev/null +++ b/src/main/java/dev/tr7zw/exordium/components/vanilla/ChatComponent.java @@ -0,0 +1,51 @@ +package dev.tr7zw.exordium.components.vanilla; + +import dev.tr7zw.exordium.access.ChatAccess; +import dev.tr7zw.exordium.components.BufferComponent; +import dev.tr7zw.util.NMSHelper; +import lombok.Getter; +import net.minecraft.client.GuiMessage; +import net.minecraft.resources.ResourceLocation; + +public class ChatComponent implements BufferComponent { + + @Getter + private static final ResourceLocation id = NMSHelper.getResourceLocation("minecraft", "chat_panel"); + + private int lastScrollbarPos = 0; + private int messageCount = 0; + private boolean wasFocused = false; + private GuiMessage.Line lastMessage = null; + + @Override + public void captureState(ChatAccess context) { + lastMessage = context.getTrimmedMessages().isEmpty() ? null : context.getTrimmedMessages().get(0); + lastScrollbarPos = context.getChatScollbarPos(); + messageCount = context.getTrimmedMessages().size(); + wasFocused = context.isChatFocused(); + } + + @Override + public boolean hasChanged(int tickCount, ChatAccess context) { + GuiMessage.Line msg = context.getTrimmedMessages().isEmpty() ? null : context.getTrimmedMessages().get(0); + boolean changed = context.getChatScollbarPos() != lastScrollbarPos + || messageCount != context.getTrimmedMessages().size() || context.isChatFocused() != wasFocused + || msg != lastMessage; + if (changed) { + System.out.println("Changed"); + return true; + } + int j = context.getLinesPerPage(); + for (int o = 0; o + context.getChatScollbarPos() < context.getTrimmedMessages().size() && o < j; o++) { + GuiMessage.Line guiMessage = context.getTrimmedMessages().get(o + context.getChatScollbarPos()); + if (guiMessage != null) { + int p = tickCount - guiMessage.addedTime(); + if (p > 170 && p < 200) { // 180 is correct, add a tiny buffer for the frame to catch up + return true; + } + } + } + return false; + } + +} diff --git a/src/main/java/dev/tr7zw/exordium/config/ExordiumConfigScreen.java b/src/main/java/dev/tr7zw/exordium/config/ExordiumConfigScreen.java index 2a91d69..9a34fcb 100644 --- a/src/main/java/dev/tr7zw/exordium/config/ExordiumConfigScreen.java +++ b/src/main/java/dev/tr7zw/exordium/config/ExordiumConfigScreen.java @@ -48,8 +48,8 @@ private void addSettings(List> options, Config.ComponentSettin (b) -> settings.setForceBlend(b))); } if (!name.equals("debug")) { // debug has that already built in and on - options.add(getOnOffOption("text.exordium.setting." + name + ".forceupdates", () -> settings.isForceUpdates(), - (b) -> settings.setForceUpdates(b))); + options.add(getOnOffOption("text.exordium.setting." + name + ".forceupdates", + () -> settings.isForceUpdates(), (b) -> settings.setForceUpdates(b))); } } diff --git a/src/main/java/dev/tr7zw/exordium/mixin/ChatComponentMixin.java b/src/main/java/dev/tr7zw/exordium/mixin/ChatComponentMixin.java index bd60c91..1f20c86 100644 --- a/src/main/java/dev/tr7zw/exordium/mixin/ChatComponentMixin.java +++ b/src/main/java/dev/tr7zw/exordium/mixin/ChatComponentMixin.java @@ -5,89 +5,106 @@ import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; -import dev.tr7zw.exordium.ExordiumModBase; import dev.tr7zw.exordium.access.ChatAccess; -import dev.tr7zw.exordium.util.BufferedComponent; +import lombok.Getter; import net.minecraft.client.GuiMessage; import net.minecraft.client.gui.components.ChatComponent; @Mixin(ChatComponent.class) -public abstract class ChatComponentMixin implements ChatAccess { +public class ChatComponentMixin implements ChatAccess { + + @Getter @Final @Shadow private List trimmedMessages; @Shadow private int chatScrollbarPos; - @Unique - private int lastScrollbarPos = 0; - @Unique - private int messageCount = 0; - @Unique - private boolean wasFocused = false; - @Unique - private GuiMessage.Line lastMessage = null; - - @Unique - boolean outdated = false; - - @Unique - private final BufferedComponent chatBufferedComponent = new BufferedComponent( - () -> ExordiumModBase.instance.config.chatSettings) { - - @Override - public boolean shouldRenderNextCappedFrame() { - return outdated; - } - - @Override - public void captureState() { - lastMessage = trimmedMessages.isEmpty() ? null : trimmedMessages.get(0); - lastScrollbarPos = chatScrollbarPos; - messageCount = trimmedMessages.size(); - wasFocused = isChatFocused(); - } - }; +// @Unique +// private int lastScrollbarPos = 0; +// @Unique +// private int messageCount = 0; +// @Unique +// private boolean wasFocused = false; +// @Unique +// private GuiMessage.Line lastMessage = null; +// +// @Unique +// boolean outdated = false; +// +// @Unique +// private final BufferedComponent chatBufferedComponent = new BufferedComponent( +// () -> ExordiumModBase.instance.config.chatSettings) { +// +// @Override +// public boolean shouldRenderNextCappedFrame() { +// return outdated; +// } +// +// @Override +// public void captureState() { +// lastMessage = trimmedMessages.isEmpty() ? null : trimmedMessages.get(0); +// lastScrollbarPos = chatScrollbarPos; +// messageCount = trimmedMessages.size(); +// wasFocused = isChatFocused(); +// } +// }; +// +// @Unique +// public boolean hasChanged(int i) { +// GuiMessage.Line msg = trimmedMessages.isEmpty() ? null : trimmedMessages.get(0); +// boolean changed = chatScrollbarPos != lastScrollbarPos || messageCount != trimmedMessages.size() +// || isChatFocused() != wasFocused || msg != lastMessage; +// if (changed) { +// return true; +// } +// int j = getLinesPerPage(); +// for (int o = 0; o + this.chatScrollbarPos < this.trimmedMessages.size() && o < j; o++) { +// GuiMessage.Line guiMessage = this.trimmedMessages.get(o + this.chatScrollbarPos); +// if (guiMessage != null) { +// int p = i - guiMessage.addedTime(); +// if (p > 170 && p < 200) { // 180 is correct, add a tiny buffer for the frame to catch up +// return true; +// } +// } +// } +// return false; +// } +// +// public void updateState(int tickCount) { +// outdated = hasChanged(tickCount); +// } - @Unique - public boolean hasChanged(int i) { - GuiMessage.Line msg = trimmedMessages.isEmpty() ? null : trimmedMessages.get(0); - boolean changed = chatScrollbarPos != lastScrollbarPos || messageCount != trimmedMessages.size() - || isChatFocused() != wasFocused || msg != lastMessage; - if (changed) { - return true; - } - int j = getLinesPerPage(); - for (int o = 0; o + this.chatScrollbarPos < this.trimmedMessages.size() && o < j; o++) { - GuiMessage.Line guiMessage = this.trimmedMessages.get(o + this.chatScrollbarPos); - if (guiMessage != null) { - int p = i - guiMessage.addedTime(); - if (p > 170 && p < 200) { // 180 is correct, add a tiny buffer for the frame to catch up - return true; - } - } - } - return false; - } - - public void updateState(int tickCount) { - outdated = hasChanged(tickCount); + @Override + public int getChatScollbarPos() { + return chatScrollbarPos; } + @Override @Shadow - public abstract boolean isChatFocused(); - - @Shadow - public abstract boolean isChatHidden(); + public boolean isChatFocused() { + // TODO Auto-generated method stub + return false; + } + @Override @Shadow - public abstract int getLinesPerPage(); + public boolean isChatHidden() { + // TODO Auto-generated method stub + return false; + } @Override - public BufferedComponent getChatOverlayBuffer() { - return chatBufferedComponent; + @Shadow + public int getLinesPerPage() { + // TODO Auto-generated method stub + return 0; } +// @Override +// public BufferedComponent getChatOverlayBuffer() { +// return chatBufferedComponent; +// } + } diff --git a/src/main/java/dev/tr7zw/exordium/mixin/GuiMixin.java b/src/main/java/dev/tr7zw/exordium/mixin/GuiMixin.java index 187d15a..cc7f687 100644 --- a/src/main/java/dev/tr7zw/exordium/mixin/GuiMixin.java +++ b/src/main/java/dev/tr7zw/exordium/mixin/GuiMixin.java @@ -14,6 +14,7 @@ import dev.tr7zw.exordium.access.GuiAccess; import dev.tr7zw.exordium.access.TablistAccess; import dev.tr7zw.exordium.access.VanillaBufferAccess; +import dev.tr7zw.exordium.components.BufferInstance; import dev.tr7zw.exordium.util.BufferedComponent; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiGraphics; @@ -41,12 +42,12 @@ public abstract class GuiMixin implements GuiAccess { private void renderChatWrapper(ChatComponent instance, GuiGraphics guiGraphics, int tickCount, int j, int k, boolean b, final Operation operation) { ChatAccess chatAccess = (ChatAccess) chat; - chatAccess.updateState(tickCount); - BufferedComponent bufferedComponent = chatAccess.getChatOverlayBuffer(); - if (!bufferedComponent.render()) { + BufferInstance buffer = ExordiumModBase.instance.getBufferManager() + .getBufferInstance(dev.tr7zw.exordium.components.vanilla.ChatComponent.getId(), ChatAccess.class); + if (!buffer.renderBuffer(tickCount, chatAccess)) { operation.call(instance, guiGraphics, tickCount, j, k, b); } - bufferedComponent.renderEnd(); + buffer.postRender(chatAccess); } @WrapOperation(method = "renderTabList", at = { diff --git a/src/main/java/dev/tr7zw/exordium/util/BufferedComponent.java b/src/main/java/dev/tr7zw/exordium/util/BufferedComponent.java index 2ad91af..a805017 100644 --- a/src/main/java/dev/tr7zw/exordium/util/BufferedComponent.java +++ b/src/main/java/dev/tr7zw/exordium/util/BufferedComponent.java @@ -11,7 +11,7 @@ import lombok.Getter; import net.minecraft.client.Minecraft; -public abstract class BufferedComponent { +public class BufferedComponent { private static final Minecraft MINECRAFT = Minecraft.getInstance(); @Getter @@ -47,10 +47,15 @@ private static void refreshModel(int screenWidth, int screenHeight) { model = new Model(modelData, uvData); } + @Deprecated + public boolean render() { + return render(this::shouldRenderNextCappedFrame); + } + /** * @return true if the buffer was used. False = render as usual */ - public boolean render() { + public boolean render(Supplier hasChanged) { if (!settings.get().isEnabled()) { return false; } @@ -72,10 +77,10 @@ public boolean render() { if (model == null) { refreshModel(screenWidth, screenHeight); } - boolean updateFrame = this.shouldForceRender() || forceRender - || (System.currentTimeMillis() > cooldown && (settings.get().isForceUpdates() || needsRenderPaced())); + boolean updateFrame = this.shouldForceRender() || forceRender || (System.currentTimeMillis() > cooldown + && (settings.get().isForceUpdates() || needsRenderPaced(hasChanged))); if (!updateFrame) { -// renderTextureOverlay(guiTarget.getColorTextureId()); + // renderTextureOverlay(guiTarget.getColorTextureId()); ExordiumModBase.instance.getDelayedRenderCallManager().addBufferedComponent(this); blendStateHolder.apply(); return true; @@ -98,7 +103,12 @@ protected boolean shouldForceRender() { return false; } + @Deprecated public void renderEnd() { + renderEnd(this::captureState); + } + + public void renderEnd(Runnable capture) { if (!blendStateHolder.isBlendStateFetched()) { // capture the expected blend state blendStateHolder.fetch(); @@ -106,7 +116,7 @@ public void renderEnd() { if (!isRendering) { return; } - captureState(); // take the current state of the component + capture.run(); // take the current state of the component ExordiumModBase.instance.setTemporaryScreenOverwrite(null); guiTarget.unbindWrite(); Minecraft.getInstance().getMainRenderTarget().bindWrite(true); @@ -115,25 +125,11 @@ public void renderEnd() { if (forceBlending || settings.get().isForceBlend()) { ExordiumModBase.setForceBlend(false); } -// renderTextureOverlay(guiTarget.getColorTextureId()); + // renderTextureOverlay(guiTarget.getColorTextureId()); ExordiumModBase.instance.getDelayedRenderCallManager().addBufferedComponent(this); blendStateHolder.apply(); } -// private void renderTextureOverlay(int textureid) { -// RenderSystem.disableDepthTest(); -// RenderSystem.depthMask(false); -// RenderSystem.enableBlend(); -// RenderSystem.blendFunc(GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); -// RenderSystem.setShader(GameRenderer::getPositionTexShader); -// RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); -// RenderSystem.setShaderTexture(0, textureid); -// model.draw(RenderSystem.getModelViewMatrix()); -// RenderSystem.depthMask(true); -// RenderSystem.enableDepthTest(); -// RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); -// } - public int getTextureId() { return guiTarget.getColorTextureId(); } @@ -142,25 +138,36 @@ public boolean isRendering() { return isRendering; } - private boolean needsRenderPaced() { + /** + * Checks for changes + * + * @return + */ + private boolean needsRenderPaced(Supplier hasChanged) { boolean reloadOccurred = false; if (reloadCount != ReloadTracker.getReloadCount()) { reloadCount = ReloadTracker.getReloadCount(); reloadOccurred = true; } - if (reloadOccurred || shouldRenderNextCappedFrame()) { + if (reloadOccurred || hasChanged.get()) { return true; } cooldown = System.currentTimeMillis() + (1000 / ExordiumModBase.instance.config.pollRate); return false; } - public abstract boolean shouldRenderNextCappedFrame(); + @Deprecated + public boolean shouldRenderNextCappedFrame() { + throw new IllegalAccessError("Method not implemented"); + } /** - * Take a snapshot of the current stateof the component + * Take a snapshot of the current state of the component */ - public abstract void captureState(); + @Deprecated + public void captureState() { + throw new IllegalAccessError("Method not implemented"); + } } diff --git a/src/main/java/dev/tr7zw/exordium/util/ScoreboardHelper.java b/src/main/java/dev/tr7zw/exordium/util/ScoreboardHelper.java index d3a10a0..b6b6db7 100644 --- a/src/main/java/dev/tr7zw/exordium/util/ScoreboardHelper.java +++ b/src/main/java/dev/tr7zw/exordium/util/ScoreboardHelper.java @@ -55,7 +55,8 @@ public static ScoreboardState getScoreboardData() { Component component2 = PlayerTeam.formatNameForTeam(playerTeam2, score.ownerName()); NumberFormat format = score.numberFormatOverride() == null ? objective.numberFormat() : score.numberFormatOverride(); - list2.add(Pair.of(format == null ? Component.literal(Integer.toString(score.value())) : format.format(score.value()), + list2.add(Pair.of( + format == null ? Component.literal(Integer.toString(score.value())) : format.format(score.value()), component2)); }