Migrate the Carpet codebase to Mojang Mappings

This commit is contained in:
altrisi committed 2022-01-24 20:33:16 +01:00
1 parent a1a2488bc3
commit 06af9ce8b1
346 files changed
+8353 -8492

No files matched your search

+14 -14
View File
@@ -3,12 +3,12 @@ package carpet.network;
import carpet.CarpetServer;
import carpet.CarpetSettings;
import carpet.script.utils.ShapesRenderer;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
public class CarpetClient
{
@@ -18,12 +18,12 @@ public class CarpetClient
public static final int DATA = 1;
public static ShapesRenderer shapes = null;
private static ClientPlayerEntity clientPlayer = null;
private static LocalPlayer clientPlayer = null;
private static boolean isServerCarpet = false;
public static String serverCarpetVersion;
public static final Identifier CARPET_CHANNEL = new Identifier("carpet:hello");
public static final ResourceLocation CARPET_CHANNEL = new ResourceLocation("carpet:hello");
public static void gameJoined(ClientPlayerEntity player)
public static void gameJoined(LocalPlayer player)
{
synchronized (sync)
{
@@ -55,7 +55,7 @@ public class CarpetClient
isServerCarpet = true;
}
public static ClientPlayerEntity getPlayer()
public static LocalPlayer getPlayer()
{
return clientPlayer;
}
@@ -72,18 +72,18 @@ public class CarpetClient
return true;
}
public static void onClientCommand(NbtElement t)
public static void onClientCommand(Tag t)
{
CarpetSettings.LOG.info("Server Response:");
NbtCompound tag = (NbtCompound)t;
CompoundTag tag = (CompoundTag)t;
CarpetSettings.LOG.info(" - id: "+tag.getString("id"));
CarpetSettings.LOG.info(" - code: "+tag.getInt("code"));
if (tag.contains("error")) CarpetSettings.LOG.warn(" - error: "+tag.getString("error"));
if (tag.contains("output"))
{
NbtList outputTag = (NbtList) tag.get("output");
ListTag outputTag = (ListTag) tag.get("output");
for (int i = 0; i < outputTag.size(); i++)
CarpetSettings.LOG.info(" - response: " + Text.Serializer.fromJson(outputTag.getString(i)).getString());
CarpetSettings.LOG.info(" - response: " + Component.Serializer.fromJson(outputTag.getString(i)).getString());
}
}
}
@@ -7,29 +7,28 @@ import carpet.helpers.TickSpeed;
import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager;
import io.netty.buffer.Unpooled;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.nbt.AbstractNbtNumber;
import net.minecraft.nbt.NbtByte;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.nbt.ByteTag;
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.game.ServerboundCustomPayloadPacket;
public class ClientNetworkHandler
{
private static final Map<String, BiConsumer<ClientPlayerEntity, NbtElement>> dataHandlers = new HashMap<String, BiConsumer<ClientPlayerEntity, NbtElement>>();
private static final Map<String, BiConsumer<LocalPlayer, Tag>> dataHandlers = new HashMap<String, BiConsumer<LocalPlayer, Tag>>();
static
{
dataHandlers.put("Rules", (p, t) -> {
NbtCompound ruleset = (NbtCompound)t;
for (String ruleKey: ruleset.getKeys())
CompoundTag ruleset = (CompoundTag)t;
for (String ruleKey: ruleset.getAllKeys())
{
NbtCompound ruleNBT = (NbtCompound) ruleset.get(ruleKey);
CompoundTag ruleNBT = (CompoundTag) ruleset.get(ruleKey);
SettingsManager manager = null;
String ruleName;
if (ruleNBT.contains("Manager"))
@@ -65,29 +64,29 @@ public class ClientNetworkHandler
}
}
});
dataHandlers.put("TickRate", (p, t) -> TickSpeed.tickrate(((AbstractNbtNumber)t).floatValue(), false));
dataHandlers.put("TickRate", (p, t) -> TickSpeed.tickrate(((NumericTag)t).getAsFloat(), false));
dataHandlers.put("TickingState", (p, t) -> {
NbtCompound tickingState = (NbtCompound)t;
CompoundTag tickingState = (CompoundTag)t;
TickSpeed.setFrozenState(tickingState.getBoolean("is_paused"), tickingState.getBoolean("deepFreeze"));
});
dataHandlers.put("SuperHotState", (p, t) -> {
TickSpeed.is_superHot = ((NbtByte) t).equals(NbtByte.ONE);
TickSpeed.is_superHot = ((ByteTag) t).equals(ByteTag.ONE);
});
dataHandlers.put("TickPlayerActiveTimeout", (p, t) -> TickSpeed.player_active_timeout = ((AbstractNbtNumber)t).intValue());
dataHandlers.put("TickPlayerActiveTimeout", (p, t) -> TickSpeed.player_active_timeout = ((NumericTag)t).getAsInt());
dataHandlers.put("scShape", (p, t) -> { // deprecated // and unused // should remove for 1.17
if (CarpetClient.shapes != null)
CarpetClient.shapes.addShape((NbtCompound)t);
CarpetClient.shapes.addShape((CompoundTag)t);
});
dataHandlers.put("scShapes", (p, t) -> {
if (CarpetClient.shapes != null)
CarpetClient.shapes.addShapes((NbtList) t);
CarpetClient.shapes.addShapes((ListTag) t);
});
dataHandlers.put("clientCommand", (p, t) -> {
CarpetClient.onClientCommand(t);
});
};
public static void handleData(PacketByteBuf data, ClientPlayerEntity player)
public static void handleData(FriendlyByteBuf data, LocalPlayer player)
{
if (data != null)
{
@@ -99,12 +98,12 @@ public class ClientNetworkHandler
}
}
private static void onHi(PacketByteBuf data)
private static void onHi(FriendlyByteBuf data)
{
synchronized (CarpetClient.sync)
{
CarpetClient.setCarpet();
CarpetClient.serverCarpetVersion = data.readString(64);
CarpetClient.serverCarpetVersion = data.readUtf(64);
if (CarpetSettings.carpetVersion.equals(CarpetClient.serverCarpetVersion))
{
CarpetSettings.LOG.info("Joined carpet server with matching carpet version");
@@ -121,17 +120,17 @@ public class ClientNetworkHandler
public static void respondHello()
{
CarpetClient.getPlayer().networkHandler.sendPacket(new CustomPayloadC2SPacket(
CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
(new PacketByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HELLO).writeString(CarpetSettings.carpetVersion)
(new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HELLO).writeUtf(CarpetSettings.carpetVersion)
));
}
private static void onSyncData(PacketByteBuf data, ClientPlayerEntity player)
private static void onSyncData(FriendlyByteBuf data, LocalPlayer player)
{
NbtCompound compound = data.readNbt();
CompoundTag compound = data.readNbt();
if (compound == null) return;
for (String key: compound.getKeys())
for (String key: compound.getAllKeys())
{
if (dataHandlers.containsKey(key)) {
try {
@@ -149,14 +148,14 @@ public class ClientNetworkHandler
public static void clientCommand(String command)
{
NbtCompound tag = new NbtCompound();
CompoundTag tag = new CompoundTag();
tag.putString("id", command);
tag.putString("command", command);
NbtCompound outer = new NbtCompound();
CompoundTag outer = new CompoundTag();
outer.put("clientCommand", tag);
CarpetClient.getPlayer().networkHandler.sendPacket(new CustomPayloadC2SPacket(
CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
(new PacketByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.DATA).writeNbt(outer)
(new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.DATA).writeNbt(outer)
));
}
}
@@ -7,17 +7,6 @@ import carpet.script.utils.SnoopyCommandSource;
import carpet.settings.ParsedRule;
import carpet.settings.SettingsManager;
import io.netty.buffer.Unpooled;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.nbt.NbtString;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -25,19 +14,28 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
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.chat.TextComponent;
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
import net.minecraft.server.level.ServerPlayer;
public class ServerNetworkHandler
{
private static Map<ServerPlayerEntity, String> remoteCarpetPlayers = new HashMap<>();
private static Set<ServerPlayerEntity> validCarpetPlayers = new HashSet<>();
private static Map<ServerPlayer, String> remoteCarpetPlayers = new HashMap<>();
private static Set<ServerPlayer> validCarpetPlayers = new HashSet<>();
private static Map<String, BiConsumer<ServerPlayerEntity, NbtElement>> dataHandlers = new HashMap<String, BiConsumer<ServerPlayerEntity, NbtElement>>(){{
private static Map<String, BiConsumer<ServerPlayer, Tag>> dataHandlers = new HashMap<String, BiConsumer<ServerPlayer, Tag>>(){{
put("clientCommand", (p, t) -> {
handleClientCommand(p, (NbtCompound)t);
handleClientCommand(p, (CompoundTag)t);
});
}};
public static void handleData(PacketByteBuf data, ServerPlayerEntity player)
public static void handleData(FriendlyByteBuf data, ServerPlayer player)
{
if (data != null)
{
@@ -51,13 +49,13 @@ public class ServerNetworkHandler
public static void onPlayerJoin(ServerPlayerEntity playerEntity)
public static void onPlayerJoin(ServerPlayer playerEntity)
{
if (!playerEntity.networkHandler.connection.isLocal())
if (!playerEntity.connection.connection.isMemoryConnection())
{
playerEntity.networkHandler.sendPacket(new CustomPayloadS2CPacket(
playerEntity.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
(new PacketByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HI).writeString(CarpetSettings.carpetVersion)
(new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HI).writeUtf(CarpetSettings.carpetVersion)
));
}
else
@@ -67,11 +65,11 @@ public class ServerNetworkHandler
}
public static void onHello(ServerPlayerEntity playerEntity, PacketByteBuf packetData)
public static void onHello(ServerPlayer playerEntity, FriendlyByteBuf packetData)
{
validCarpetPlayers.add(playerEntity);
String clientVersion = packetData.readString(64);
String clientVersion = packetData.readUtf(64);
remoteCarpetPlayers.put(playerEntity, clientVersion);
if (clientVersion.equals(CarpetSettings.carpetVersion))
CarpetSettings.LOG.info("Player "+playerEntity.getName().getString()+" joined with a matching carpet client");
@@ -85,34 +83,34 @@ public class ServerNetworkHandler
eManager.getRules().forEach(data::withRule);
}
});
playerEntity.networkHandler.sendPacket(new CustomPayloadS2CPacket(CarpetClient.CARPET_CHANNEL, data.build() ));
playerEntity.connection.send(new ClientboundCustomPayloadPacket(CarpetClient.CARPET_CHANNEL, data.build() ));
}
private static void handleClientCommand(ServerPlayerEntity player, NbtCompound commandData)
private static void handleClientCommand(ServerPlayer player, CompoundTag commandData)
{
String command = commandData.getString("command");
String id = commandData.getString("id");
List<Text> output = new ArrayList<>();
Text[] error = {null};
List<Component> output = new ArrayList<>();
Component[] error = {null};
int resultCode = -1;
if (player.getServer() == null)
{
error[0] = new LiteralText("No Server");
error[0] = new TextComponent("No Server");
}
else
{
resultCode = player.getServer().getCommandManager().execute(
resultCode = player.getServer().getCommands().performCommand(
new SnoopyCommandSource(player, error, output), command
);
}
NbtCompound result = new NbtCompound();
CompoundTag result = new CompoundTag();
result.putString("id", id);
result.putInt("code", resultCode);
if (error[0] != null) result.putString("error", error[0].asString());
NbtList outputResult = new NbtList();
for (Text line: output) outputResult.add(NbtString.of(Text.Serializer.toJson(line)));
if (error[0] != null) result.putString("error", error[0].getContents());
ListTag outputResult = new ListTag();
for (Component line: output) outputResult.add(StringTag.valueOf(Component.Serializer.toJson(line)));
if (!output.isEmpty()) result.put("output", outputResult);
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withCustomNbt("clientCommand", result).build()
));
@@ -120,11 +118,11 @@ public class ServerNetworkHandler
}
private static void onClientData(ServerPlayerEntity player, PacketByteBuf data)
private static void onClientData(ServerPlayer player, FriendlyByteBuf data)
{
NbtCompound compound = data.readNbt();
CompoundTag compound = data.readNbt();
if (compound == null) return;
for (String key: compound.getKeys())
for (String key: compound.getAllKeys())
{
if (dataHandlers.containsKey(key))
dataHandlers.get(key).accept(player, compound.get(key));
@@ -136,9 +134,9 @@ public class ServerNetworkHandler
public static void updateRuleWithConnectedClients(ParsedRule<?> rule)
{
if (CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : remoteCarpetPlayers.keySet())
for (ServerPlayer player : remoteCarpetPlayers.keySet())
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withRule(rule).build()
));
@@ -148,9 +146,9 @@ public class ServerNetworkHandler
public static void updateTickSpeedToConnectedPlayers()
{
if (CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : remoteCarpetPlayers.keySet())
for (ServerPlayer player : remoteCarpetPlayers.keySet())
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withTickRate().build()
));
@@ -160,9 +158,9 @@ public class ServerNetworkHandler
public static void updateFrozenStateToConnectedPlayers()
{
if (CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : remoteCarpetPlayers.keySet())
for (ServerPlayer player : remoteCarpetPlayers.keySet())
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withFrozenState().build()
));
@@ -172,9 +170,9 @@ public class ServerNetworkHandler
public static void updateSuperHotStateToConnectedPlayers()
{
if(CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : remoteCarpetPlayers.keySet())
for (ServerPlayer player : remoteCarpetPlayers.keySet())
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withSuperHotState().build()
));
@@ -184,32 +182,32 @@ public class ServerNetworkHandler
public static void updateTickPlayerActiveTimeoutToConnectedPlayers()
{
if (CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : remoteCarpetPlayers.keySet())
for (ServerPlayer player : remoteCarpetPlayers.keySet())
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withTickPlayerActiveTimeout().build()
));
}
}
public static void broadcastCustomCommand(String command, NbtElement data)
public static void broadcastCustomCommand(String command, Tag data)
{
if (CarpetSettings.superSecretSetting) return;
for (ServerPlayerEntity player : validCarpetPlayers)
for (ServerPlayer player : validCarpetPlayers)
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withCustomNbt(command, data).build()
));
}
}
public static void sendCustomCommand(ServerPlayerEntity player, String command, NbtElement data)
public static void sendCustomCommand(ServerPlayer player, String command, Tag data)
{
if (isValidCarpetPlayer(player))
{
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(
player.connection.send(new ClientboundCustomPayloadPacket(
CarpetClient.CARPET_CHANNEL,
DataBuilder.create().withCustomNbt(command, data).build()
));
@@ -217,10 +215,10 @@ public class ServerNetworkHandler
}
public static void onPlayerLoggedOut(ServerPlayerEntity player)
public static void onPlayerLoggedOut(ServerPlayer player)
{
validCarpetPlayers.remove(player);
if (!player.networkHandler.connection.isLocal())
if (!player.connection.connection.isMemoryConnection())
remoteCarpetPlayers.remove(player);
}
@@ -230,14 +228,14 @@ public class ServerNetworkHandler
validCarpetPlayers.clear();
}
public static boolean isValidCarpetPlayer(ServerPlayerEntity player)
public static boolean isValidCarpetPlayer(ServerPlayer player)
{
if (CarpetSettings.superSecretSetting) return false;
return validCarpetPlayers.contains(player);
}
public static String getPlayerStatus(ServerPlayerEntity player)
public static String getPlayerStatus(ServerPlayer player)
{
if (remoteCarpetPlayers.containsKey(player)) return "carpet "+remoteCarpetPlayers.get(player);
if (validCarpetPlayers.contains(player)) return "carpet "+CarpetSettings.carpetVersion;
@@ -246,14 +244,14 @@ public class ServerNetworkHandler
private static class DataBuilder
{
private NbtCompound tag;
private CompoundTag tag;
private static DataBuilder create()
{
return new DataBuilder();
}
private DataBuilder()
{
tag = new NbtCompound();
tag = new CompoundTag();
}
private DataBuilder withTickRate()
{
@@ -262,7 +260,7 @@ public class ServerNetworkHandler
}
private DataBuilder withFrozenState()
{
NbtCompound tickingState = new NbtCompound();
CompoundTag tickingState = new CompoundTag();
tickingState.putBoolean("is_paused", TickSpeed.isPaused());
tickingState.putBoolean("deepFreeze", TickSpeed.deeplyFrozen());
tag.put("TickingState", tickingState);
@@ -280,16 +278,16 @@ public class ServerNetworkHandler
}
private DataBuilder withRule(ParsedRule<?> rule)
{
NbtCompound rules = (NbtCompound) tag.get("Rules");
CompoundTag rules = (CompoundTag) tag.get("Rules");
if (rules == null)
{
rules = new NbtCompound();
rules = new CompoundTag();
tag.put("Rules", rules);
}
String identifier = rule.settingsManager.getIdentifier();
String key = rule.name;
while (rules.contains(key)) { key = key+"2";}
NbtCompound ruleNBT = new NbtCompound();
CompoundTag ruleNBT = new CompoundTag();
ruleNBT.putString("Value", rule.getAsString());
ruleNBT.putString("Manager",identifier);
ruleNBT.putString("Rule",rule.name);
@@ -297,15 +295,15 @@ public class ServerNetworkHandler
return this;
}
public DataBuilder withCustomNbt(String key, NbtElement value)
public DataBuilder withCustomNbt(String key, Tag value)
{
tag.put(key, value);
return this;
}
private PacketByteBuf build()
private FriendlyByteBuf build()
{
PacketByteBuf packetBuf = new PacketByteBuf(Unpooled.buffer());
FriendlyByteBuf packetBuf = new FriendlyByteBuf(Unpooled.buffer());
packetBuf.writeVarInt(CarpetClient.DATA);
packetBuf.writeNbt(tag);
return packetBuf;