From 626f8d5015208ca5ddae2c5253cd5290cf7be1d7 Mon Sep 17 00:00:00 2001 From: Dave Date: Wed, 31 Jan 2024 11:05:09 +0100 Subject: [PATCH] Dev. progress for v3.8 --- .../betterstats/BetterStatsConfig.java | 3 ++- .../gui/stats/panel/ActionBarPanel.java | 15 +++++------ .../BetterStatsClientNetworkHandler.java | 25 +++++++++++++------ .../network/BetterStatsNetworkHandler.java | 23 ++++++++++++++++- .../betterstats/BetterStatsConfig.java | 3 ++- .../gui/stats/panel/ActionBarPanel.java | 15 +++++------ .../BetterStatsClientNetworkHandler.java | 25 +++++++++++++------ .../network/BetterStatsNetworkHandler.java | 22 +++++++++++++++- .../betterstats/BetterStatsConfig.java | 3 ++- .../gui/stats/panel/ActionBarPanel.java | 15 +++++------ .../BetterStatsClientNetworkHandler.java | 25 +++++++++++++------ .../network/BetterStatsNetworkHandler.java | 22 +++++++++++++++- 12 files changed, 145 insertions(+), 51 deletions(-) diff --git a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java index f0f4453b..34b65a12 100644 --- a/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java +++ b/betterstats-3-fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java @@ -16,9 +16,10 @@ public class BetterStatsConfig extends AutoConfig * @apiNote CHANGING THE VALUE OF THIS VARIABLE TO {@code true} * MUST NOT AND SHALL NOT BE DONE WITHOUT THE USER'S CONSENT! */ - public static @NonSerialized boolean LEGAL_NET_CONSENT = false; + public static @NonSerialized boolean CLIENT_NET_CONSENT = false; // -------------------------------------------------- public @SerializedAs("client-guiMobsFollowCursor") boolean guiMobsFollowCursor = true; + public @SerializedAs("client-trustAllServersBssNet") boolean trustAllServersBssNet = true; public @SerializedAs("server-registerCommands") boolean registerCommands = true; public @SerializedAs("server-enableSAS") boolean enableServerSAS = true; public @SerializedAs("server-sasConfig") SASConfig sasConfig = new SASConfig(); 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 ed28bab4..3f8a17ef 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 @@ -1,7 +1,8 @@ package io.github.thecsdev.betterstats.client.gui.stats.panel; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; 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.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; @@ -65,16 +66,16 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws //bss network button final var btn_bssNet = new TButtonWidget(btn_options.getX() - 20, btn_options.getY(), 20, 20); btn_bssNet.setTooltip(Tooltip.of(TXT_TOGGLE_TOOLTIP)); - btn_bssNet.setIcon(LEGAL_NET_CONSENT ? + btn_bssNet.setIcon(CLIENT_NET_CONSENT ? new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); btn_bssNet.setOnClick(__ -> { //if turning off, send a packet indicating no more live updates - if(LEGAL_NET_CONSENT) + if(CLIENT_NET_CONSENT) { c2s_liveStats(false); - LEGAL_NET_CONSENT = false; + CLIENT_NET_CONSENT = false; refresh(); return; } @@ -83,13 +84,13 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws final var currentScreen = MC_CLIENT.currentScreen; final BooleanConsumer confirmScreenCallback = (accepted) -> { - LEGAL_NET_CONSENT = accepted; + CLIENT_NET_CONSENT = accepted; MC_CLIENT.setScreen(currentScreen); - if(LEGAL_NET_CONSENT) c2s_liveStats(); + if(CLIENT_NET_CONSENT) c2s_iHaveBSS(true); }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer()); + btn_bssNet.setEnabled(serverHasBSS() && !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 0077b948..083bc8b2 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 @@ -1,8 +1,9 @@ package io.github.thecsdev.betterstats.client.network; import static io.github.thecsdev.betterstats.BetterStats.LOGGER; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_I_HAVE_BSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_LIVE_STATS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.NETWORK_VERSION; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.S2C_I_HAVE_BSS; @@ -35,10 +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; - LEGAL_NET_CONSENT = false; - - //also clear the HUD, as it now references an outdated stats-provider - //BetterStatsHudScreen.getInstance().clearEntries(); -- no longer an issue + CLIENT_NET_CONSENT = false; }); //initialize network handlers @@ -61,8 +59,8 @@ public static void init() {} //if the client is in single player, handle live stats updates //in case the statistics hud had entries in it - if(MC_CLIENT.isInSingleplayer()) - TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_liveStats()); + if(MC_CLIENT.isInSingleplayer() || BetterStats.getInstance().getConfig().trustAllServersBssNet) + TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_iHaveBSS(true)); }); } // ================================================== @@ -72,8 +70,19 @@ public static void init() {} * Returns {@code true} if {@link BetterStatsClientNetworkHandler} * is allowed to communicate with the server. */ - public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && LEGAL_NET_CONSENT); } + public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && CLIENT_NET_CONSENT); } // -------------------------------------------------- + public static final void c2s_iHaveBSS(boolean forceSend) + { + if(!forceSend && !comms()) return; + CLIENT_NET_CONSENT = true; + + //construct and send + final var data = new PacketByteBuf(Unpooled.buffer()); + data.writeIntLE(NETWORK_VERSION); + MC_CLIENT.getNetworkHandler().sendPacket(new CustomPayloadC2SPacket(C2S_I_HAVE_BSS, data)); + } + public static final void c2s_liveStats() { c2s_liveStats(BetterStatsHudScreen.getInstance().entryCount() > 0); } public static final void c2s_liveStats(boolean receiveLiveUpdates) { 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 832548a3..389a975b 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 @@ -1,6 +1,7 @@ package io.github.thecsdev.betterstats.network; import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable; + import java.util.WeakHashMap; import org.jetbrains.annotations.ApiStatus.Internal; @@ -27,9 +28,10 @@ private BetterStatsNetworkHandler() {} public static final Text TXT_TOGGLE_TOOLTIP = translatable("betterstats.network.betterstatsnetworkhandler.toggle_tooltip"); public static final Text TXT_CONSENT_WARNING = translatable("betterstats.network.betterstatsnetworkhandler.consent_warning"); // - public static final int NETWORK_VERSION = 1; + public static final int NETWORK_VERSION = 2; // public static final Identifier S2C_I_HAVE_BSS; + public static final Identifier C2S_I_HAVE_BSS; public static final Identifier C2S_LIVE_STATS; // -------------------------------------------------- public static final WeakHashMap PlayerPrefs; @@ -40,6 +42,7 @@ public static void init() {} //init packet IDs final var modId = BetterStats.getModID(); S2C_I_HAVE_BSS = new Identifier(modId, "s2c_bss"); + C2S_I_HAVE_BSS = new Identifier(modId, "c2s_bss"); C2S_LIVE_STATS = new Identifier(modId, "c2s_live_stats"); //init variables @@ -53,6 +56,24 @@ public static void init() {} }); //init network handlers + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_I_HAVE_BSS, ctx -> + { + //obtain prefs + final var prefs = PlayerPrefs.get(ctx.getPlayer()); + if(prefs == null) return; //shouldn't happen at all, but just in case + + //obtain data buffer and make sure data is present + final var buffer = ctx.getPacketBuffer(); + if(buffer.readableBytes() == 0) return; + + //obtain network version and compare it + final int netVer = buffer.readIntLE(); + if(netVer != NETWORK_VERSION) return; + + //update prefs + prefs.hasBss = true; + }); + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_LIVE_STATS, ctx -> { //obtain prefs diff --git a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java index f0f4453b..34b65a12 100644 --- a/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java +++ b/betterstats-3-fabric-1.20.2/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java @@ -16,9 +16,10 @@ public class BetterStatsConfig extends AutoConfig * @apiNote CHANGING THE VALUE OF THIS VARIABLE TO {@code true} * MUST NOT AND SHALL NOT BE DONE WITHOUT THE USER'S CONSENT! */ - public static @NonSerialized boolean LEGAL_NET_CONSENT = false; + public static @NonSerialized boolean CLIENT_NET_CONSENT = false; // -------------------------------------------------- public @SerializedAs("client-guiMobsFollowCursor") boolean guiMobsFollowCursor = true; + public @SerializedAs("client-trustAllServersBssNet") boolean trustAllServersBssNet = true; public @SerializedAs("server-registerCommands") boolean registerCommands = true; public @SerializedAs("server-enableSAS") boolean enableServerSAS = true; public @SerializedAs("server-sasConfig") SASConfig sasConfig = new SASConfig(); 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 ed28bab4..3f8a17ef 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 @@ -1,7 +1,8 @@ package io.github.thecsdev.betterstats.client.gui.stats.panel; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; 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.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; @@ -65,16 +66,16 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws //bss network button final var btn_bssNet = new TButtonWidget(btn_options.getX() - 20, btn_options.getY(), 20, 20); btn_bssNet.setTooltip(Tooltip.of(TXT_TOGGLE_TOOLTIP)); - btn_bssNet.setIcon(LEGAL_NET_CONSENT ? + btn_bssNet.setIcon(CLIENT_NET_CONSENT ? new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); btn_bssNet.setOnClick(__ -> { //if turning off, send a packet indicating no more live updates - if(LEGAL_NET_CONSENT) + if(CLIENT_NET_CONSENT) { c2s_liveStats(false); - LEGAL_NET_CONSENT = false; + CLIENT_NET_CONSENT = false; refresh(); return; } @@ -83,13 +84,13 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws final var currentScreen = MC_CLIENT.currentScreen; final BooleanConsumer confirmScreenCallback = (accepted) -> { - LEGAL_NET_CONSENT = accepted; + CLIENT_NET_CONSENT = accepted; MC_CLIENT.setScreen(currentScreen); - if(LEGAL_NET_CONSENT) c2s_liveStats(); + if(CLIENT_NET_CONSENT) c2s_iHaveBSS(true); }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer()); + btn_bssNet.setEnabled(serverHasBSS() && !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 9078fd91..d2ea9354 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 @@ -1,8 +1,9 @@ package io.github.thecsdev.betterstats.client.network; import static io.github.thecsdev.betterstats.BetterStats.LOGGER; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_I_HAVE_BSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_LIVE_STATS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.NETWORK_VERSION; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.S2C_I_HAVE_BSS; @@ -34,10 +35,7 @@ public static void init() {} //when the client disconnects, clear all flags, including user consent LOGGER.info("Clearing '" + BetterStatsClientNetworkHandler.class.getSimpleName() + "' flags."); serverHasBSS = false; - LEGAL_NET_CONSENT = false; - - //also clear the HUD, as it now references an outdated stats-provider - //BetterStatsHudScreen.getInstance().clearEntries(); -- no longer an issue + CLIENT_NET_CONSENT = false; }); //initialize network handlers @@ -60,8 +58,8 @@ public static void init() {} //if the client is in single player, handle live stats updates //in case the statistics hud had entries in it - if(MC_CLIENT.isInSingleplayer()) - TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_liveStats()); + if(MC_CLIENT.isInSingleplayer() || BetterStats.getInstance().getConfig().trustAllServersBssNet) + TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_iHaveBSS(true)); }); } // ================================================== @@ -71,8 +69,19 @@ public static void init() {} * Returns {@code true} if {@link BetterStatsClientNetworkHandler} * is allowed to communicate with the server. */ - public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && LEGAL_NET_CONSENT); } + public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && CLIENT_NET_CONSENT); } // -------------------------------------------------- + public static final void c2s_iHaveBSS(boolean forceSend) + { + if(!forceSend && !comms()) return; + CLIENT_NET_CONSENT = true; + + //construct and send + final var data = new PacketByteBuf(Unpooled.buffer()); + data.writeIntLE(NETWORK_VERSION); + new TCustomPayload(C2S_I_HAVE_BSS, data).sendC2S(); + } + public static final void c2s_liveStats() { c2s_liveStats(BetterStatsHudScreen.getInstance().entryCount() > 0); } public static final void c2s_liveStats(boolean receiveLiveUpdates) { 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 7217a148..f3409ea0 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 @@ -28,9 +28,10 @@ private BetterStatsNetworkHandler() {} public static final Text TXT_TOGGLE_TOOLTIP = translatable("betterstats.network.betterstatsnetworkhandler.toggle_tooltip"); public static final Text TXT_CONSENT_WARNING = translatable("betterstats.network.betterstatsnetworkhandler.consent_warning"); // - public static final int NETWORK_VERSION = 1; + public static final int NETWORK_VERSION = 2; // public static final Identifier S2C_I_HAVE_BSS; + public static final Identifier C2S_I_HAVE_BSS; public static final Identifier C2S_LIVE_STATS; // -------------------------------------------------- public static final WeakHashMap PlayerPrefs; @@ -41,6 +42,7 @@ public static void init() {} //init packet IDs final var modId = BetterStats.getModID(); S2C_I_HAVE_BSS = new Identifier(modId, "s2c_bss"); + C2S_I_HAVE_BSS = new Identifier(modId, "c2s_bss"); C2S_LIVE_STATS = new Identifier(modId, "c2s_live_stats"); //init variables @@ -54,6 +56,24 @@ public static void init() {} }); //init network handlers + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_I_HAVE_BSS, ctx -> + { + //obtain prefs + final var prefs = PlayerPrefs.get(ctx.getPlayer()); + if(prefs == null) return; //shouldn't happen at all, but just in case + + //obtain data buffer and make sure data is present + final var buffer = ctx.getPacketBuffer(); + if(buffer.readableBytes() == 0) return; + + //obtain network version and compare it + final int netVer = buffer.readIntLE(); + if(netVer != NETWORK_VERSION) return; + + //update prefs + prefs.hasBss = true; + }); + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_LIVE_STATS, ctx -> { //obtain prefs diff --git a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java index f0f4453b..34b65a12 100644 --- a/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java +++ b/betterstats-3-fabric-1.20.4/src/main/java/io/github/thecsdev/betterstats/BetterStatsConfig.java @@ -16,9 +16,10 @@ public class BetterStatsConfig extends AutoConfig * @apiNote CHANGING THE VALUE OF THIS VARIABLE TO {@code true} * MUST NOT AND SHALL NOT BE DONE WITHOUT THE USER'S CONSENT! */ - public static @NonSerialized boolean LEGAL_NET_CONSENT = false; + public static @NonSerialized boolean CLIENT_NET_CONSENT = false; // -------------------------------------------------- public @SerializedAs("client-guiMobsFollowCursor") boolean guiMobsFollowCursor = true; + public @SerializedAs("client-trustAllServersBssNet") boolean trustAllServersBssNet = true; public @SerializedAs("server-registerCommands") boolean registerCommands = true; public @SerializedAs("server-enableSAS") boolean enableServerSAS = true; public @SerializedAs("server-sasConfig") SASConfig sasConfig = new SASConfig(); 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 ed28bab4..3f8a17ef 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 @@ -1,7 +1,8 @@ package io.github.thecsdev.betterstats.client.gui.stats.panel; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; 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.network.BetterStatsNetworkHandler.TXT_CONSENT_WARNING; @@ -65,16 +66,16 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws //bss network button final var btn_bssNet = new TButtonWidget(btn_options.getX() - 20, btn_options.getY(), 20, 20); btn_bssNet.setTooltip(Tooltip.of(TXT_TOGGLE_TOOLTIP)); - btn_bssNet.setIcon(LEGAL_NET_CONSENT ? + btn_bssNet.setIcon(CLIENT_NET_CONSENT ? new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(20, 80, 20, 20)) : new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 80, 20, 20))); btn_bssNet.setOnClick(__ -> { //if turning off, send a packet indicating no more live updates - if(LEGAL_NET_CONSENT) + if(CLIENT_NET_CONSENT) { c2s_liveStats(false); - LEGAL_NET_CONSENT = false; + CLIENT_NET_CONSENT = false; refresh(); return; } @@ -83,13 +84,13 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws final var currentScreen = MC_CLIENT.currentScreen; final BooleanConsumer confirmScreenCallback = (accepted) -> { - LEGAL_NET_CONSENT = accepted; + CLIENT_NET_CONSENT = accepted; MC_CLIENT.setScreen(currentScreen); - if(LEGAL_NET_CONSENT) c2s_liveStats(); + if(CLIENT_NET_CONSENT) c2s_iHaveBSS(true); }; MC_CLIENT.setScreen(new ConfirmScreen(confirmScreenCallback, TXT_TOGGLE_TOOLTIP, TXT_CONSENT_WARNING)); }); - btn_bssNet.setEnabled(serverHasBSS() && !MC_CLIENT.isInSingleplayer()); + btn_bssNet.setEnabled(serverHasBSS() && !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 9078fd91..d2ea9354 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 @@ -1,8 +1,9 @@ package io.github.thecsdev.betterstats.client.network; import static io.github.thecsdev.betterstats.BetterStats.LOGGER; -import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT; +import static io.github.thecsdev.betterstats.BetterStatsConfig.CLIENT_NET_CONSENT; import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT; +import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_I_HAVE_BSS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.C2S_LIVE_STATS; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.NETWORK_VERSION; import static io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler.S2C_I_HAVE_BSS; @@ -34,10 +35,7 @@ public static void init() {} //when the client disconnects, clear all flags, including user consent LOGGER.info("Clearing '" + BetterStatsClientNetworkHandler.class.getSimpleName() + "' flags."); serverHasBSS = false; - LEGAL_NET_CONSENT = false; - - //also clear the HUD, as it now references an outdated stats-provider - //BetterStatsHudScreen.getInstance().clearEntries(); -- no longer an issue + CLIENT_NET_CONSENT = false; }); //initialize network handlers @@ -60,8 +58,8 @@ public static void init() {} //if the client is in single player, handle live stats updates //in case the statistics hud had entries in it - if(MC_CLIENT.isInSingleplayer()) - TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_liveStats()); + if(MC_CLIENT.isInSingleplayer() || BetterStats.getInstance().getConfig().trustAllServersBssNet) + TaskScheduler.executeOnce(MC_CLIENT, () -> MC_CLIENT.getNetworkHandler() != null, () -> c2s_iHaveBSS(true)); }); } // ================================================== @@ -71,8 +69,19 @@ public static void init() {} * Returns {@code true} if {@link BetterStatsClientNetworkHandler} * is allowed to communicate with the server. */ - public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && LEGAL_NET_CONSENT); } + public static boolean comms() { return MC_CLIENT.isInSingleplayer() || (serverHasBSS && CLIENT_NET_CONSENT); } // -------------------------------------------------- + public static final void c2s_iHaveBSS(boolean forceSend) + { + if(!forceSend && !comms()) return; + CLIENT_NET_CONSENT = true; + + //construct and send + final var data = new PacketByteBuf(Unpooled.buffer()); + data.writeIntLE(NETWORK_VERSION); + new TCustomPayload(C2S_I_HAVE_BSS, data).sendC2S(); + } + public static final void c2s_liveStats() { c2s_liveStats(BetterStatsHudScreen.getInstance().entryCount() > 0); } public static final void c2s_liveStats(boolean receiveLiveUpdates) { 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 7217a148..f3409ea0 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 @@ -28,9 +28,10 @@ private BetterStatsNetworkHandler() {} public static final Text TXT_TOGGLE_TOOLTIP = translatable("betterstats.network.betterstatsnetworkhandler.toggle_tooltip"); public static final Text TXT_CONSENT_WARNING = translatable("betterstats.network.betterstatsnetworkhandler.consent_warning"); // - public static final int NETWORK_VERSION = 1; + public static final int NETWORK_VERSION = 2; // public static final Identifier S2C_I_HAVE_BSS; + public static final Identifier C2S_I_HAVE_BSS; public static final Identifier C2S_LIVE_STATS; // -------------------------------------------------- public static final WeakHashMap PlayerPrefs; @@ -41,6 +42,7 @@ public static void init() {} //init packet IDs final var modId = BetterStats.getModID(); S2C_I_HAVE_BSS = new Identifier(modId, "s2c_bss"); + C2S_I_HAVE_BSS = new Identifier(modId, "c2s_bss"); C2S_LIVE_STATS = new Identifier(modId, "c2s_live_stats"); //init variables @@ -54,6 +56,24 @@ public static void init() {} }); //init network handlers + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_I_HAVE_BSS, ctx -> + { + //obtain prefs + final var prefs = PlayerPrefs.get(ctx.getPlayer()); + if(prefs == null) return; //shouldn't happen at all, but just in case + + //obtain data buffer and make sure data is present + final var buffer = ctx.getPacketBuffer(); + if(buffer.readableBytes() == 0) return; + + //obtain network version and compare it + final int netVer = buffer.readIntLE(); + if(netVer != NETWORK_VERSION) return; + + //update prefs + prefs.hasBss = true; + }); + CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_LIVE_STATS, ctx -> { //obtain prefs