Skip to content

Commit

Permalink
Development progress
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCSDev committed Sep 29, 2023
1 parent 1dd5264 commit 28cf8bc
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 8 deletions.
4 changes: 2 additions & 2 deletions betterstats-3-fabric-1.20.1/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ org.gradle.jvmargs=-Xmx1G
mod_name = Better Statistics Screen
mod_description = Improves the statistics screen and makes it more useful.
mod_author = TheCSDev
mod_version = 3.0.0-beta.2+fabric-1.20.1
mod_version = 3.0.0+fabric-1.20.1

mod_contact_homepage = https://github.com/TheCSMods
mod_contact_sources = https://github.com/TheCSMods/mc-better-stats
Expand All @@ -38,7 +38,7 @@ org.gradle.jvmargs=-Xmx1G
mod_depends_minecraft = >=1.20
mod_depends_java = >=17

mod_jar_tcdcommons = META-INF/jarjar/tcdcommons-3.0.0-beta.2+fabric-1.20.1.jar
mod_jar_tcdcommons = META-INF/jarjar/tcdcommons-3.0.0+fabric-1.20.1.jar

pack_mcmeta_format = 15

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler;
import net.fabricmc.loader.api.FabricLoader;

public class BetterStats extends Object
Expand Down Expand Up @@ -55,7 +56,7 @@ else if(!isInstanceValid(this))
this.config.tryLoadFromFile(true);

//init stuff
//TODO - Initialize common stuff here (client/dedicated-server/internal-server)
BetterStatsNetworkHandler.init();
}
// ==================================================
public static BetterStats getInstance() { return Instance; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ public class BetterStatsConfig extends AutoConfig
{
// ==================================================
public static @NonSerialized boolean DEBUG_MODE = false;

/**
* Indicates whether or not the user consents to this mod
* communicating with the server the user is playing on.
* @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 @SerializedAs("guiMobsFollowCursor") boolean guiMobsFollowCursor = true;
// ==================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.github.thecsdev.betterstats.api.client.gui.screen.BetterStatsScreen;
import io.github.thecsdev.betterstats.api.client.registry.BSClientPlayerBadges;
import io.github.thecsdev.betterstats.api.client.registry.BSStatsTabs;
import io.github.thecsdev.betterstats.network.BetterStatsNetworkHandler;
import io.github.thecsdev.tcdcommons.api.client.gui.util.GuiUtils;
import io.github.thecsdev.tcdcommons.api.events.client.gui.screen.GameMenuScreenEvent;
import io.github.thecsdev.tcdcommons.api.hooks.client.gui.widget.ButtonWidgetHooks;
Expand All @@ -22,6 +23,7 @@ public BetterStatsClient()
//initialize and register stuff
BSStatsTabs.register();
BSClientPlayerBadges.register();
BetterStatsNetworkHandler.init();

//an event handler that will handle the game menu screen (the "pause" screen)
GameMenuScreenEvent.INIT_WIDGETS_POST.register(gmScreen ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.thecsdev.betterstats.client.gui.screen.hud;

import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.betterstats.BetterStats.getModID;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

import org.jetbrains.annotations.Nullable;

import io.github.thecsdev.betterstats.BetterStats;
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;
Expand All @@ -30,24 +32,37 @@ public final class BetterStatsHudScreen extends TWidgetHudScreen implements IPar
private @Nullable Screen parent;
//
private float requestTimer = 0;
private final int requestDelay = 100;
private final int requestDelay = 20 * 10;
// ==================================================
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
final boolean live = (entryCount() != 0);
BetterStatsClientNetworkHandler.c2s_liveStats(live);
}
// ==================================================
public final @Override void render(TDrawContext pencil)
{
//render super
super.render(pencil);
super.render(pencil); //super must be called here

// ---------- handle auto-requesting
//don't auto-request during user setup
if(this.client == null || isOpen()) return;

//handle auto-requesting
if(this.client == null || isOpen()) return; //don't auto-request during user setup
this.requestTimer += pencil.deltaTime;
if(this.requestTimer > this.requestDelay)
{
this.requestTimer = 0;
this.client.getNetworkHandler().sendPacket(new ClientStatusC2SPacket(Mode.REQUEST_STATS));

//network optimization;
//- do not send packets when a screen is opened
//- do not send packets when an overlay is present
if(MC_CLIENT.currentScreen == null && MC_CLIENT.getOverlay() == null)
MC_CLIENT.getNetworkHandler().sendPacket(new ClientStatusC2SPacket(Mode.REQUEST_STATS));
}
}
// ==================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.client.BetterStatsClient.MC_CLIENT;
import static io.github.thecsdev.tcdcommons.api.util.TextUtils.translatable;

Expand Down Expand Up @@ -50,6 +51,20 @@ public ActionBarPanel(int x, int y, int width, ActionBarPanelProxy proxy) throws
btn_options.setTooltip(Tooltip.of(translatable("options.title")));
btn_options.setIcon(new UITexture(BS_WIDGETS_TEXTURE, new Rectangle(0, 40, 20, 20)));
addChild(btn_options, false);

//bss network button
final var btn_bssNet = new TButtonWidget(btn_options.getX() - 21, btn_options.getY(), 20, 20);
btn_bssNet.setTooltip(Tooltip.of(translatable("TODO - Tooltip"))); //FIXME - BSS NET tooltip
btn_bssNet.setIcon(LEGAL_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(__ ->
{
//FIXME - Implement BSS NET consent screen!
LEGAL_NET_CONSENT = !LEGAL_NET_CONSENT;
refresh();
});
addChild(btn_bssNet, false);
}
// ==================================================
public static interface ActionBarPanelProxy
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.github.thecsdev.betterstats.client.network;

import static io.github.thecsdev.betterstats.BetterStatsConfig.LEGAL_NET_CONSENT;
import static io.github.thecsdev.betterstats.client.BetterStatsClient.MC_CLIENT;
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;

import org.jetbrains.annotations.ApiStatus.Internal;

import io.github.thecsdev.tcdcommons.api.events.client.MinecraftClientEvent;
import io.github.thecsdev.tcdcommons.api.network.CustomPayloadNetwork;
import io.netty.buffer.Unpooled;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;

public final @Internal class BetterStatsClientNetworkHandler
{
// ==================================================
private BetterStatsClientNetworkHandler() {}
// --------------------------------------------------
private static boolean serverHasBSS = false;
// ==================================================
public static void init() {}
static
{
//initialize event handlers
MinecraftClientEvent.DISCONNECTED.register(client ->
{
//when the client disconnects, clear all flags, including user consent
serverHasBSS = false;
LEGAL_NET_CONSENT = false;
});

//initialize network handlers
CustomPayloadNetwork.registerReceiver(NetworkSide.CLIENTBOUND, S2C_I_HAVE_BSS, ctx ->
{
//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;

//server has BSS
serverHasBSS = true;
});
}
// ==================================================
/**
* 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 final void c2s_liveStats(boolean receiveLiveUpdates)
{
//if communications are off, don't send
if(!comms()) return;

//construct and send
final var data = new PacketByteBuf(Unpooled.buffer());
data.writeBoolean(receiveLiveUpdates);
MC_CLIENT.getNetworkHandler().sendPacket(new CustomPayloadS2CPacket(C2S_LIVE_STATS, data));
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.thecsdev.betterstats.network;

import java.util.WeakHashMap;

import org.jetbrains.annotations.ApiStatus.Internal;

import io.github.thecsdev.betterstats.BetterStats;
import io.github.thecsdev.tcdcommons.api.events.server.PlayerManagerEvent;
import io.github.thecsdev.tcdcommons.api.network.CustomPayloadNetwork;
import io.netty.buffer.Unpooled;
import net.minecraft.network.NetworkSide;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

/**
* Represents the server-side network handler for {@link BetterStats}.
*/
public final @Internal class BetterStatsNetworkHandler
{
// ==================================================
private BetterStatsNetworkHandler() {}
// --------------------------------------------------
public static final int NETWORK_VERSION = 1;
//
public static final Identifier S2C_I_HAVE_BSS;
public static final Identifier C2S_LIVE_STATS;
// --------------------------------------------------
public static final WeakHashMap<ServerPlayerEntity, PlayerPreferences> PlayerPrefs;
// ==================================================
public static void init() {}
static
{
//init packet IDs
final var modId = BetterStats.getModID();
S2C_I_HAVE_BSS = new Identifier(modId, "s2c_bss");
C2S_LIVE_STATS = new Identifier(modId, "c2s_live_stats");

//init variables
PlayerPrefs = new WeakHashMap<>();

//init event handlers
PlayerManagerEvent.PLAYER_CONNECTED.register(player ->
{
PlayerPrefs.put(player, new PlayerPreferences());
s2c_iHaveBSS(player);
});

//init network handlers
CustomPayloadNetwork.registerReceiver(NetworkSide.SERVERBOUND, C2S_LIVE_STATS, ctx ->
{
//obtain prefs
final var prefs = PlayerPrefs.get(ctx.getPlayer());
if(prefs == null) return; //shouldn't happen at all, but just in case

//update prefs
prefs.liveStats = ctx.getPacketBuffer().readBoolean();
});
}
// ==================================================
/**
* Sends the {@link #S2C_I_HAVE_BSS} packet to a given {@link ServerPlayerEntity}.
*/
public static void s2c_iHaveBSS(ServerPlayerEntity player)
{
final var data = new PacketByteBuf(Unpooled.buffer());
data.writeIntLE(NETWORK_VERSION);
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(S2C_I_HAVE_BSS, data));
}
// ==================================================
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.thecsdev.betterstats.network;

import org.jetbrains.annotations.ApiStatus.Internal;

public final @Internal class PlayerPreferences
{
/**
* When set to true, the {@link BetterStatsNetworkHandler} will
* automatically update the client on their stats changes, live.
*/
public boolean liveStats = false;
}
Binary file not shown.

0 comments on commit 28cf8bc

Please sign in to comment.