-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
202 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...n/java/io/github/thecsdev/betterstats/client/network/BetterStatsClientNetworkHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
// ================================================== | ||
} |
72 changes: 72 additions & 0 deletions
72
....20.1/src/main/java/io/github/thecsdev/betterstats/network/BetterStatsNetworkHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
// ================================================== | ||
} |
12 changes: 12 additions & 0 deletions
12
...fabric-1.20.1/src/main/java/io/github/thecsdev/betterstats/network/PlayerPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 renamed
BIN
+398 KB
...tcdcommons-3.0.0-beta.2+fabric-1.20.1.jar → ...jarjar/tcdcommons-3.0.0+fabric-1.20.1.jar
Binary file not shown.