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 626f8d5 commit 5bc13c0
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
// --------------------------------------------------
Expand All @@ -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,
Expand All @@ -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();
}
// --------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
private BetterStatsClientNetworkHandler() {}
// --------------------------------------------------
private static boolean serverHasBSS = false;
private static boolean liveStats = false;
// ==================================================
public static void init() {}
static
Expand All @@ -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;
});

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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;
}
// ==================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
// --------------------------------------------------
Expand All @@ -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,
Expand All @@ -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();
}
// --------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
private BetterStatsClientNetworkHandler() {}
// --------------------------------------------------
private static boolean serverHasBSS = false;
private static boolean liveStats = false;
// ==================================================
public static void init() {}
static
Expand All @@ -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;
});

Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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;
}
// ==================================================
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
Loading

0 comments on commit 5bc13c0

Please sign in to comment.