Skip to content

Commit

Permalink
Dev. progress for v3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCSDev committed Jan 31, 2024
1 parent 7edcecb commit 626f8d5
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
});
}
// ==================================================
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ServerPlayerEntity, PlayerPreferences> PlayerPrefs;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
});
}
// ==================================================
Expand All @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServerPlayerEntity, PlayerPreferences> PlayerPrefs;
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down
Loading

0 comments on commit 626f8d5

Please sign in to comment.