diff --git a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java index 479d6dce..295d75ee 100644 --- a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java +++ b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java @@ -1,17 +1,24 @@ package io.github.thecsdev.betterstats.client.gui.screen.hud; import static io.github.thecsdev.betterstats.BetterStats.getModID; +import static io.github.thecsdev.betterstats.api.client.gui.panel.BSComponentPanel.BS_WIDGETS_TEXTURE; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getLiveStatsEnabled; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; +import java.awt.Rectangle; + import org.jetbrains.annotations.Nullable; import io.github.thecsdev.betterstats.BetterStats; +import io.github.thecsdev.betterstats.BetterStatsConfig; import io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenWrapper; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen; import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext; +import io.github.thecsdev.tcdcommons.api.client.gui.util.UITexture; import io.github.thecsdev.tcdcommons.api.client.gui.widget.TButtonWidget; import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider; import net.minecraft.client.gui.screen.Screen; @@ -27,10 +34,12 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IParentScreenProvider { // ================================================== - public static final Text TEXT_TITLE = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen"); - public static final Text TEXT_TUTORIAL_1 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1"); - public static final Text TEXT_TUTORIAL_2 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2"); - public static final Text TEXT_TUTORIAL_3 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3"); + private static final String P = "betterstats.client.gui.screen.hud.betterstatshudscreen"; + public static final Text TEXT_TITLE = translatable(P); + public static final Text TEXT_TUTORIAL_1 = translatable(P + ".tutorial_1"); + public static final Text TEXT_TUTORIAL_2 = translatable(P + ".tutorial_2"); + public static final Text TEXT_TUTORIAL_3 = translatable(P + ".tutorial_3"); + public static final Text TEXT_LIVE_TOGGLE = translatable(P + ".live_stats_toggle"); // public static final Identifier HUD_SCREEN_ID = new Identifier(getModID(), "stats_hud"); // -------------------------------------------------- @@ -44,17 +53,13 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar private BetterStatsHudScreen() { super(TEXT_TITLE, HUD_SCREEN_ID); } // -------------------------------------------------- protected final @Override TScreenWrapper createScreenWrapper() { return new BetterStatsHudScreenWrapper(this); } - protected final @Override void onClosed() - { - super.onClosed(); //super must be called here - BetterStatsClientNetworkHandler.c2s_liveStats(); - } // ================================================== protected final @Override void init() { - //if is open, add the done button + //if the hud screen is opened, add some extra widgets to it if(isOpen()) { + //add the done button, that closes the screen, and shows the tutorial final var btn_done = new TButtonWidget( (getWidth() / 2) - 50, (getHeight() / 2) - 10, 100, 20, @@ -66,7 +71,27 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar )); btn_done.setOnClick(__ -> close()); addChild(btn_done, false); + + //add a "realtime stats" toggle button + if(BetterStatsConfig.CLIENT_NET_CONSENT && getServerHasBSS()) + { + final var btn_toggleRealtime = new TButtonWidget( + btn_done.getEndX() + 5, btn_done.getY(), + 20, 20); + btn_toggleRealtime.setTooltip(Tooltip.of(TEXT_LIVE_TOGGLE)); + btn_toggleRealtime.setIcon(getLiveStatsEnabled() ? + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); + btn_toggleRealtime.setOnClick(__ -> + { + BetterStatsClientNetworkHandler.c2s_liveStats(!getLiveStatsEnabled()); + refresh(); + }); + addChild(btn_toggleRealtime, false); + } } + + //initialize the 'super' gui afterwards super.init(); } // -------------------------------------------------- diff --git a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java index 3f8a17ef..a69fe6b9 100644 --- a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java +++ b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java @@ -4,7 +4,7 @@ import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_iHaveBSS; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_liveStats; -import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.serverHasBSS; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_TOGGLE_TOOLTIP; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; @@ -90,7 +90,7 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); + btn_bssNet.setEnabled(getServerHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); addChild(btn_bssNet, false); //credits button diff --git a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java index 083bc8b2..71eb42d9 100644 --- a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java +++ b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java @@ -26,6 +26,7 @@ private BetterStatsClientNetworkHandler() {} // -------------------------------------------------- private static boolean serverHasBSS = false; + private static boolean liveStats = false; // ================================================== public static void init() {} static @@ -36,6 +37,7 @@ public static void init() {} //when the client disconnects, clear all flags, including user consent LOGGER.info("Clearing '" + BetterStatsClientNetworkHandler.class.getSimpleName() + "' flags."); serverHasBSS = false; + liveStats = false; CLIENT_NET_CONSENT = false; }); @@ -64,7 +66,8 @@ public static void init() {} }); } // ================================================== - public static boolean serverHasBSS() { return serverHasBSS; } + public static boolean getServerHasBSS() { return serverHasBSS; } + public static boolean getLiveStatsEnabled() { return liveStats; } // -------------------------------------------------- /** * Returns {@code true} if {@link BetterStatsClientNetworkHandler} @@ -93,6 +96,7 @@ public static final void c2s_liveStats(boolean receiveLiveUpdates) final var data = new PacketByteBuf(Unpooled.buffer()); data.writeBoolean(receiveLiveUpdates); MC_CLIENT.getNetworkHandler().sendPacket(new CustomPayloadC2SPacket(C2S_LIVE_STATS, data)); + liveStats = receiveLiveUpdates; } // ================================================== } \ No newline at end of file diff --git a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java index 389a975b..fbaa7b3a 100644 --- a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java +++ b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java @@ -78,10 +78,9 @@ public static void init() {} { //obtain prefs final var prefs = PlayerPrefs.get(ctx.getPlayer()); - if(prefs == null) return; //shouldn't happen at all, but just in case + if(prefs == null || !prefs.hasBss) return; //null shouldn't happen at all, but just in case //update prefs - prefs.hasBss = true; prefs.liveStats = ctx.getPacketBuffer().readBoolean(); }); } diff --git a/betterstats-3-fabric-1.20.1/src/main/resources/assets/betterstats/lang/en_us.json b/betterstats-3-fabric-1.20.1/src/main/resources/assets/betterstats/lang/en_us.json index 52910138..c5d4c5bf 100644 --- a/betterstats-3-fabric-1.20.1/src/main/resources/assets/betterstats/lang/en_us.json +++ b/betterstats-3-fabric-1.20.1/src/main/resources/assets/betterstats/lang/en_us.json @@ -62,6 +62,7 @@ "betterstats.client.gui.screen.hud.betterstatshudscreen": "Statistics hud", "betterstats.client.gui.screen.hud.betterstatshudscreen.pin_stat": "Pin to HUD", + "betterstats.client.gui.screen.hud.betterstatshudscreen.live_stats_toggle": "Toggle live HUD stat updates", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1": "Right click a statistic ([Shift]+RMB) to add it here.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2": "Right click a HUD entry to modify or remove it.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3": "Press Escape (ESC) to close this screen.", diff --git a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java index 479d6dce..295d75ee 100644 --- a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java +++ b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java @@ -1,17 +1,24 @@ package io.github.thecsdev.betterstats.client.gui.screen.hud; import static io.github.thecsdev.betterstats.BetterStats.getModID; +import static io.github.thecsdev.betterstats.api.client.gui.panel.BSComponentPanel.BS_WIDGETS_TEXTURE; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getLiveStatsEnabled; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; +import java.awt.Rectangle; + import org.jetbrains.annotations.Nullable; import io.github.thecsdev.betterstats.BetterStats; +import io.github.thecsdev.betterstats.BetterStatsConfig; import io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenWrapper; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen; import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext; +import io.github.thecsdev.tcdcommons.api.client.gui.util.UITexture; import io.github.thecsdev.tcdcommons.api.client.gui.widget.TButtonWidget; import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider; import net.minecraft.client.gui.screen.Screen; @@ -27,10 +34,12 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IParentScreenProvider { // ================================================== - public static final Text TEXT_TITLE = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen"); - public static final Text TEXT_TUTORIAL_1 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1"); - public static final Text TEXT_TUTORIAL_2 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2"); - public static final Text TEXT_TUTORIAL_3 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3"); + private static final String P = "betterstats.client.gui.screen.hud.betterstatshudscreen"; + public static final Text TEXT_TITLE = translatable(P); + public static final Text TEXT_TUTORIAL_1 = translatable(P + ".tutorial_1"); + public static final Text TEXT_TUTORIAL_2 = translatable(P + ".tutorial_2"); + public static final Text TEXT_TUTORIAL_3 = translatable(P + ".tutorial_3"); + public static final Text TEXT_LIVE_TOGGLE = translatable(P + ".live_stats_toggle"); // public static final Identifier HUD_SCREEN_ID = new Identifier(getModID(), "stats_hud"); // -------------------------------------------------- @@ -44,17 +53,13 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar private BetterStatsHudScreen() { super(TEXT_TITLE, HUD_SCREEN_ID); } // -------------------------------------------------- protected final @Override TScreenWrapper createScreenWrapper() { return new BetterStatsHudScreenWrapper(this); } - protected final @Override void onClosed() - { - super.onClosed(); //super must be called here - BetterStatsClientNetworkHandler.c2s_liveStats(); - } // ================================================== protected final @Override void init() { - //if is open, add the done button + //if the hud screen is opened, add some extra widgets to it if(isOpen()) { + //add the done button, that closes the screen, and shows the tutorial final var btn_done = new TButtonWidget( (getWidth() / 2) - 50, (getHeight() / 2) - 10, 100, 20, @@ -66,7 +71,27 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar )); btn_done.setOnClick(__ -> close()); addChild(btn_done, false); + + //add a "realtime stats" toggle button + if(BetterStatsConfig.CLIENT_NET_CONSENT && getServerHasBSS()) + { + final var btn_toggleRealtime = new TButtonWidget( + btn_done.getEndX() + 5, btn_done.getY(), + 20, 20); + btn_toggleRealtime.setTooltip(Tooltip.of(TEXT_LIVE_TOGGLE)); + btn_toggleRealtime.setIcon(getLiveStatsEnabled() ? + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); + btn_toggleRealtime.setOnClick(__ -> + { + BetterStatsClientNetworkHandler.c2s_liveStats(!getLiveStatsEnabled()); + refresh(); + }); + addChild(btn_toggleRealtime, false); + } } + + //initialize the 'super' gui afterwards super.init(); } // -------------------------------------------------- diff --git a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java index 3f8a17ef..a69fe6b9 100644 --- a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java +++ b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java @@ -4,7 +4,7 @@ import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_iHaveBSS; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_liveStats; -import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.serverHasBSS; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_TOGGLE_TOOLTIP; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; @@ -90,7 +90,7 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); + btn_bssNet.setEnabled(getServerHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); addChild(btn_bssNet, false); //credits button diff --git a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java index d2ea9354..8980cf64 100644 --- a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java +++ b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java @@ -26,6 +26,7 @@ private BetterStatsClientNetworkHandler() {} // -------------------------------------------------- private static boolean serverHasBSS = false; + private static boolean liveStats = false; // ================================================== public static void init() {} static @@ -35,6 +36,7 @@ public static void init() {} //when the client disconnects, clear all flags, including user consent LOGGER.info("Clearing '" + BetterStatsClientNetworkHandler.class.getSimpleName() + "' flags."); serverHasBSS = false; + liveStats = false; CLIENT_NET_CONSENT = false; }); @@ -63,7 +65,8 @@ public static void init() {} }); } // ================================================== - public static boolean serverHasBSS() { return serverHasBSS; } + public static boolean getServerHasBSS() { return serverHasBSS; } + public static boolean getLiveStatsEnabled() { return liveStats; } // -------------------------------------------------- /** * Returns {@code true} if {@link BetterStatsClientNetworkHandler} @@ -92,6 +95,7 @@ public static final void c2s_liveStats(boolean receiveLiveUpdates) final var data = new PacketByteBuf(Unpooled.buffer()); data.writeBoolean(receiveLiveUpdates); new TCustomPayload(C2S_LIVE_STATS, data).sendC2S(); + liveStats = receiveLiveUpdates; } // ================================================== } \ No newline at end of file diff --git a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java index f3409ea0..e8e961d8 100644 --- a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java +++ b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java @@ -78,10 +78,9 @@ public static void init() {} { //obtain prefs final var prefs = PlayerPrefs.get(ctx.getPlayer()); - if(prefs == null) return; //shouldn't happen at all, but just in case + if(prefs == null || !prefs.hasBss) return; //null shouldn't happen at all, but just in case - //update prefs - prefs.hasBss = true; + //update prefs prefs.liveStats = ctx.getPacketBuffer().readBoolean(); }); } diff --git a/betterstats-3-fabric-1.20.2/src/main/resources/assets/betterstats/lang/en_us.json b/betterstats-3-fabric-1.20.2/src/main/resources/assets/betterstats/lang/en_us.json index 52910138..c5d4c5bf 100644 --- a/betterstats-3-fabric-1.20.2/src/main/resources/assets/betterstats/lang/en_us.json +++ b/betterstats-3-fabric-1.20.2/src/main/resources/assets/betterstats/lang/en_us.json @@ -62,6 +62,7 @@ "betterstats.client.gui.screen.hud.betterstatshudscreen": "Statistics hud", "betterstats.client.gui.screen.hud.betterstatshudscreen.pin_stat": "Pin to HUD", + "betterstats.client.gui.screen.hud.betterstatshudscreen.live_stats_toggle": "Toggle live HUD stat updates", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1": "Right click a statistic ([Shift]+RMB) to add it here.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2": "Right click a HUD entry to modify or remove it.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3": "Press Escape (ESC) to close this screen.", diff --git a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java index 479d6dce..295d75ee 100644 --- a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java +++ b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/screen/hud/BetterStatsHudScreen.java @@ -1,17 +1,24 @@ package io.github.thecsdev.betterstats.client.gui.screen.hud; import static io.github.thecsdev.betterstats.BetterStats.getModID; +import static io.github.thecsdev.betterstats.api.client.gui.panel.BSComponentPanel.BS_WIDGETS_TEXTURE; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getLiveStatsEnabled; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.literal; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; +import java.awt.Rectangle; + import org.jetbrains.annotations.Nullable; import io.github.thecsdev.betterstats.BetterStats; +import io.github.thecsdev.betterstats.BetterStatsConfig; import io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TScreenWrapper; import io.github.thecsdev.tcdcommons.api.client.gui.screen.TWidgetHudScreen; import io.github.thecsdev.tcdcommons.api.client.gui.util.TDrawContext; +import io.github.thecsdev.tcdcommons.api.client.gui.util.UITexture; import io.github.thecsdev.tcdcommons.api.client.gui.widget.TButtonWidget; import io.github.thecsdev.tcdcommons.api.client.util.interfaces.IParentScreenProvider; import net.minecraft.client.gui.screen.Screen; @@ -27,10 +34,12 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IParentScreenProvider { // ================================================== - public static final Text TEXT_TITLE = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen"); - public static final Text TEXT_TUTORIAL_1 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1"); - public static final Text TEXT_TUTORIAL_2 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2"); - public static final Text TEXT_TUTORIAL_3 = translatable("betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3"); + private static final String P = "betterstats.client.gui.screen.hud.betterstatshudscreen"; + public static final Text TEXT_TITLE = translatable(P); + public static final Text TEXT_TUTORIAL_1 = translatable(P + ".tutorial_1"); + public static final Text TEXT_TUTORIAL_2 = translatable(P + ".tutorial_2"); + public static final Text TEXT_TUTORIAL_3 = translatable(P + ".tutorial_3"); + public static final Text TEXT_LIVE_TOGGLE = translatable(P + ".live_stats_toggle"); // public static final Identifier HUD_SCREEN_ID = new Identifier(getModID(), "stats_hud"); // -------------------------------------------------- @@ -44,17 +53,13 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar private BetterStatsHudScreen() { super(TEXT_TITLE, HUD_SCREEN_ID); } // -------------------------------------------------- protected final @Override TScreenWrapper createScreenWrapper() { return new BetterStatsHudScreenWrapper(this); } - protected final @Override void onClosed() - { - super.onClosed(); //super must be called here - BetterStatsClientNetworkHandler.c2s_liveStats(); - } // ================================================== protected final @Override void init() { - //if is open, add the done button + //if the hud screen is opened, add some extra widgets to it if(isOpen()) { + //add the done button, that closes the screen, and shows the tutorial final var btn_done = new TButtonWidget( (getWidth() / 2) - 50, (getHeight() / 2) - 10, 100, 20, @@ -66,7 +71,27 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar )); btn_done.setOnClick(__ -> close()); addChild(btn_done, false); + + //add a "realtime stats" toggle button + if(BetterStatsConfig.CLIENT_NET_CONSENT && getServerHasBSS()) + { + final var btn_toggleRealtime = new TButtonWidget( + btn_done.getEndX() + 5, btn_done.getY(), + 20, 20); + btn_toggleRealtime.setTooltip(Tooltip.of(TEXT_LIVE_TOGGLE)); + btn_toggleRealtime.setIcon(getLiveStatsEnabled() ? + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : + new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); + btn_toggleRealtime.setOnClick(__ -> + { + BetterStatsClientNetworkHandler.c2s_liveStats(!getLiveStatsEnabled()); + refresh(); + }); + addChild(btn_toggleRealtime, false); + } } + + //initialize the 'super' gui afterwards super.init(); } // -------------------------------------------------- diff --git a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java index 3f8a17ef..a69fe6b9 100644 --- a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java +++ b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/gui/stats/panel/ActionBarPanel.java @@ -4,7 +4,7 @@ import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_iHaveBSS; import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.c2s_liveStats; -import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.serverHasBSS; +import static io.github.thecsdev.betterstats.client.network.BetterStatsClientNetworkHandler.getServerHasBSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.TXT_TOGGLE_TOOLTIP; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; @@ -90,7 +90,7 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); + btn_bssNet.setEnabled(getServerHasBSS() && !MC_CLIENT.isInSingleplayer() && !CLIENT_NET_CONSENT); addChild(btn_bssNet, false); //credits button diff --git a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java index d2ea9354..8980cf64 100644 --- a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java +++ b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java @@ -26,6 +26,7 @@ private BetterStatsClientNetworkHandler() {} // -------------------------------------------------- private static boolean serverHasBSS = false; + private static boolean liveStats = false; // ================================================== public static void init() {} static @@ -35,6 +36,7 @@ public static void init() {} //when the client disconnects, clear all flags, including user consent LOGGER.info("Clearing '" + BetterStatsClientNetworkHandler.class.getSimpleName() + "' flags."); serverHasBSS = false; + liveStats = false; CLIENT_NET_CONSENT = false; }); @@ -63,7 +65,8 @@ public static void init() {} }); } // ================================================== - public static boolean serverHasBSS() { return serverHasBSS; } + public static boolean getServerHasBSS() { return serverHasBSS; } + public static boolean getLiveStatsEnabled() { return liveStats; } // -------------------------------------------------- /** * Returns {@code true} if {@link BetterStatsClientNetworkHandler} @@ -92,6 +95,7 @@ public static final void c2s_liveStats(boolean receiveLiveUpdates) final var data = new PacketByteBuf(Unpooled.buffer()); data.writeBoolean(receiveLiveUpdates); new TCustomPayload(C2S_LIVE_STATS, data).sendC2S(); + liveStats = receiveLiveUpdates; } // ================================================== } \ No newline at end of file diff --git a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java index f3409ea0..1296c0c5 100644 --- a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java +++ b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java @@ -78,10 +78,9 @@ public static void init() {} { //obtain prefs final var prefs = PlayerPrefs.get(ctx.getPlayer()); - if(prefs == null) return; //shouldn't happen at all, but just in case + if(prefs == null || !prefs.hasBss) return; //null shouldn't happen at all, but just in case //update prefs - prefs.hasBss = true; prefs.liveStats = ctx.getPacketBuffer().readBoolean(); }); } diff --git a/betterstats-3-fabric-1.20.4/src/main/resources/assets/betterstats/lang/en_us.json b/betterstats-3-fabric-1.20.4/src/main/resources/assets/betterstats/lang/en_us.json index 52910138..c5d4c5bf 100644 --- a/betterstats-3-fabric-1.20.4/src/main/resources/assets/betterstats/lang/en_us.json +++ b/betterstats-3-fabric-1.20.4/src/main/resources/assets/betterstats/lang/en_us.json @@ -62,6 +62,7 @@ "betterstats.client.gui.screen.hud.betterstatshudscreen": "Statistics hud", "betterstats.client.gui.screen.hud.betterstatshudscreen.pin_stat": "Pin to HUD", + "betterstats.client.gui.screen.hud.betterstatshudscreen.live_stats_toggle": "Toggle live HUD stat updates", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_1": "Right click a statistic ([Shift]+RMB) to add it here.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_2": "Right click a HUD entry to modify or remove it.", "betterstats.client.gui.screen.hud.betterstatshudscreen.tutorial_3": "Press Escape (ESC) to close this screen.",