move handshake to data

This commit is contained in:
gnembon committed 2023-08-08 12:28:55 +02:00
1 parent 7a19bbe997
commit 36c09e3c88
3 files changed
+16 -28

No files matched your search

@@ -32,8 +32,8 @@ public class CarpetClient
}
}
public static final int HI = 69;
public static final int HELLO = 420;
public static final String HI = "69";
public static final String HELLO = "420";
public static final int DATA = 1;
public static ShapesRenderer shapes = null;
@@ -8,7 +8,6 @@ import carpet.api.settings.InvalidRuleValueException;
import carpet.fakes.LevelInterface;
import carpet.helpers.TickRateManager;
import carpet.api.settings.SettingsManager;
import io.netty.buffer.Unpooled;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
@@ -18,16 +17,14 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NumericTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
public class ClientNetworkHandler
{
private static final Map<String, BiConsumer<LocalPlayer, Tag>> dataHandlers = new HashMap<String, BiConsumer<LocalPlayer, Tag>>();
static
{
dataHandlers.put(CarpetClient.HI, (p, t) -> onHi(t.getAsString()));
dataHandlers.put("Rules", (p, t) -> {
CompoundTag ruleset = (CompoundTag)t;
for (String ruleKey: ruleset.getAllKeys())
@@ -96,21 +93,19 @@ public class ClientNetworkHandler
dataHandlers.put("clientCommand", (p, t) -> {
CarpetClient.onClientCommand(t);
});
};
}
// Ran on the Main Minecraft Thread
public static void handleData(CarpetClient.CarpetPayload data, LocalPlayer player)
{
if (data.command() == CarpetClient.HI)
onHi(data.data());
if (data.command() == CarpetClient.DATA)
onSyncData(data.data(), player);
}
private static void onHi(CompoundTag data)
private static void onHi(String version)
{
CarpetClient.setCarpet();
CarpetClient.serverCarpetVersion = data.getString("version");
CarpetClient.serverCarpetVersion = version;
if (CarpetSettings.carpetVersion.equals(CarpetClient.serverCarpetVersion))
{
CarpetSettings.LOG.info("Joined carpet server with matching carpet version");
@@ -127,9 +122,9 @@ public class ClientNetworkHandler
public static void respondHello()
{
CompoundTag data = new CompoundTag();
data.putString("version", CarpetSettings.carpetVersion);
data.putString(CarpetClient.HELLO, CarpetSettings.carpetVersion);
CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket(
new CarpetClient.CarpetPayload(CarpetClient.HELLO, data)
new CarpetClient.CarpetPayload(CarpetClient.DATA, data)
));
}
@@ -21,11 +21,8 @@ import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.StringTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
@@ -36,16 +33,13 @@ public class ServerNetworkHandler
private static final Set<ServerPlayer> validCarpetPlayers = new HashSet<>();
private static final Map<String, BiConsumer<ServerPlayer, Tag>> dataHandlers = Map.of(
"clientCommand", (p, t) -> {
handleClientCommand(p, (CompoundTag)t);
}
CarpetClient.HELLO, (p, t) -> onHello(p, t.getAsString()),
"clientCommand", (p, t) -> handleClientCommand(p, (CompoundTag)t)
);
public static void handleData(CarpetClient.CarpetPayload data, ServerPlayer player)
{
int id = data.command();
if (id == CarpetClient.HELLO)
onHello(player, data.data());
if (id == CarpetClient.DATA)
onClientData(player, data.data());
}
@@ -64,8 +58,8 @@ public class ServerNetworkHandler
if (!((ServerGamePacketListenerImplInterface)playerEntity.connection).getConnection().isMemoryConnection())
{
CompoundTag data = new CompoundTag();
data.putString("version", CarpetSettings.carpetVersion);
playerEntity.connection.send( make( CarpetClient.HI, data));
data.putString(CarpetClient.HI, CarpetSettings.carpetVersion);
playerEntity.connection.send( make( CarpetClient.DATA, data));
}
else
{
@@ -73,15 +67,14 @@ public class ServerNetworkHandler
}
}
public static void onHello(ServerPlayer playerEntity, CompoundTag packetData)
public static void onHello(ServerPlayer playerEntity, String version)
{
validCarpetPlayers.add(playerEntity);
String clientVersion = packetData.getString("version");
remoteCarpetPlayers.put(playerEntity, clientVersion);
if (clientVersion.equals(CarpetSettings.carpetVersion))
remoteCarpetPlayers.put(playerEntity, version);
if (version.equals(CarpetSettings.carpetVersion))
CarpetSettings.LOG.info("Player "+playerEntity.getName().getString()+" joined with a matching carpet client");
else
CarpetSettings.LOG.warn("Player "+playerEntity.getName().getString()+" joined with another carpet version: "+clientVersion);
CarpetSettings.LOG.warn("Player "+playerEntity.getName().getString()+" joined with another carpet version: "+ version);
DataBuilder data = DataBuilder.create(playerEntity.server); // tickrate related settings are sent on world change
CarpetServer.forEachManager(sm -> sm.getCarpetRules().forEach(data::withRule));
playerEntity.connection.send(make(data));