diff --git a/gradle.properties b/gradle.properties index 89464393..202bc782 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,13 +3,13 @@ org.gradle.jvmargs=-Xmx1G # Fabric Properties # check https://fabricmc.net/develop/ - minecraft_version=1.20.1 - loader_version=0.14.21 + minecraft_version=23w31a + loader_version=0.14.22 jsr305_version=3.0.2 fabric_version=0.83.0+1.20.1 # Mod Properties - mod_version = 1.4.112 + mod_version = 1.4.113 maven_group = carpet archives_base_name = fabric-carpet diff --git a/src/main/java/carpet/fakes/CarpetPacketPayload.java b/src/main/java/carpet/fakes/CarpetPacketPayload.java new file mode 100644 index 00000000..d1576759 --- /dev/null +++ b/src/main/java/carpet/fakes/CarpetPacketPayload.java @@ -0,0 +1,11 @@ +package carpet.fakes; + +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.network.protocol.common.custom.DiscardedPayload; +import net.minecraft.resources.ResourceLocation; + +public interface CarpetPacketPayload extends CustomPacketPayload +{ + FriendlyByteBuf data(); +} diff --git a/src/main/java/carpet/helpers/FertilizableCoral.java b/src/main/java/carpet/helpers/FertilizableCoral.java index c1116af6..9c8bd43d 100644 --- a/src/main/java/carpet/helpers/FertilizableCoral.java +++ b/src/main/java/carpet/helpers/FertilizableCoral.java @@ -33,7 +33,7 @@ public interface FertilizableCoral extends BonemealableBlock { boolean isEnabled(); @Override - public default boolean isValidBonemealTarget(LevelReader world, BlockPos pos, BlockState state, boolean var4) + public default boolean isValidBonemealTarget(LevelReader world, BlockPos pos, BlockState state) { return isEnabled() && state.getValue(BaseCoralPlantTypeBlock.WATERLOGGED) diff --git a/src/main/java/carpet/mixins/ClientCommonPacketListenerImpl_customPacketMixin.java b/src/main/java/carpet/mixins/ClientCommonPacketListenerImpl_customPacketMixin.java new file mode 100644 index 00000000..3cd91bf6 --- /dev/null +++ b/src/main/java/carpet/mixins/ClientCommonPacketListenerImpl_customPacketMixin.java @@ -0,0 +1,28 @@ +package carpet.mixins; + +import carpet.network.CarpetClient; +import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ClientCommonPacketListenerImpl.class) +public class ClientCommonPacketListenerImpl_customPacketMixin +{ + @Inject(method = "onDisconnect", at = @At("HEAD")) + private void onCMDisconnected(Component reason, CallbackInfo ci) + { + CarpetClient.disconnect(); + } + + @Inject(method = "handleCustomPayload(Lnet/minecraft/network/protocol/common/ClientboundCustomPayloadPacket;)V", + at = @At("HEAD")) + private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci) + { + //System.out.println("CustomPayload of : " + packet.payload()); + } + +} diff --git a/src/main/java/carpet/mixins/ClientPacketListener_clientCommandMixin.java b/src/main/java/carpet/mixins/ClientPacketListener_clientCommandMixin.java index 61ba0dd6..6690ea49 100644 --- a/src/main/java/carpet/mixins/ClientPacketListener_clientCommandMixin.java +++ b/src/main/java/carpet/mixins/ClientPacketListener_clientCommandMixin.java @@ -3,8 +3,11 @@ package carpet.mixins; import carpet.CarpetServer; import carpet.network.CarpetClient; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl; import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.client.multiplayer.CommonListenerCookie; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.network.Connection; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -13,9 +16,13 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ClientPacketListener.class) -public class ClientPacketListener_clientCommandMixin +public abstract class ClientPacketListener_clientCommandMixin extends ClientCommonPacketListenerImpl { - @Shadow @Final private Minecraft minecraft; + + protected ClientPacketListener_clientCommandMixin(final Minecraft minecraft, final Connection connection, final CommonListenerCookie commonListenerCookie) + { + super(minecraft, connection, commonListenerCookie); + } @Inject(method = "sendCommand", at = @At("HEAD")) private void inspectMessage(String string, CallbackInfo ci) diff --git a/src/main/java/carpet/mixins/ClientPacketListener_customPacketsMixin.java b/src/main/java/carpet/mixins/ClientPacketListener_customPacketsMixin.java index ce073645..d159ba30 100644 --- a/src/main/java/carpet/mixins/ClientPacketListener_customPacketsMixin.java +++ b/src/main/java/carpet/mixins/ClientPacketListener_customPacketsMixin.java @@ -3,41 +3,39 @@ package carpet.mixins; import carpet.network.CarpetClient; import carpet.network.ClientNetworkHandler; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl; import net.minecraft.client.multiplayer.ClientPacketListener; +import net.minecraft.client.multiplayer.CommonListenerCookie; +import net.minecraft.network.Connection; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; -import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.ClientCommonPacketListener; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.network.protocol.common.custom.DiscardedPayload; import net.minecraft.network.protocol.game.ClientboundLoginPacket; +import net.minecraft.resources.ResourceLocation; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ClientPacketListener.class) -public abstract class ClientPacketListener_customPacketsMixin +public abstract class ClientPacketListener_customPacketsMixin extends ClientCommonPacketListenerImpl { - @Final @Shadow private Minecraft minecraft; - @Inject(method = "handleCustomPayload", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/protocol/game/ClientboundCustomPayloadPacket;getIdentifier()Lnet/minecraft/resources/ResourceLocation;"), cancellable = true) - private void onOnCustomPayload(ClientboundCustomPayloadPacket packet, CallbackInfo ci) + protected ClientPacketListener_customPacketsMixin(final Minecraft minecraft, final Connection connection, final CommonListenerCookie commonListenerCookie) { - if (CarpetClient.CARPET_CHANNEL.equals(packet.getIdentifier())) - { - ClientNetworkHandler.handleData(packet.getData(), minecraft.player); - ci.cancel(); - } + super(minecraft, connection, commonListenerCookie); } @Inject(method = "handleLogin", at = @At("RETURN")) private void onGameJoined(ClientboundLoginPacket packet, CallbackInfo info) { - CarpetClient.gameJoined(minecraft.player); + CarpetClient.gameJoined( minecraft.player); } - @Inject(method = "onDisconnect", at = @At("HEAD")) - private void onCMDisconnected(Component reason, CallbackInfo ci) - { - CarpetClient.disconnect(); - } } diff --git a/src/main/java/carpet/mixins/ClientboundCustomPayloadPacket_customPacketMixin.java b/src/main/java/carpet/mixins/ClientboundCustomPayloadPacket_customPacketMixin.java new file mode 100644 index 00000000..bb2f41e7 --- /dev/null +++ b/src/main/java/carpet/mixins/ClientboundCustomPayloadPacket_customPacketMixin.java @@ -0,0 +1,35 @@ +package carpet.mixins; + + +import carpet.network.CarpetClient; +import carpet.network.ClientNetworkHandler; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.network.protocol.common.custom.DiscardedPayload; +import net.minecraft.resources.ResourceLocation; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(ClientboundCustomPayloadPacket.class) +public class ClientboundCustomPayloadPacket_customPacketMixin +{ + @Inject(method = "readPayload", at = @At("HEAD")) + private static void onOnCustomPayloadR(final ResourceLocation resourceLocation, final FriendlyByteBuf friendlyByteBuf, final CallbackInfoReturnable cir) + { + } + + @Inject(method = "readUnknownPayload", at = @At(value = "HEAD"), cancellable = true) + private static void onOnCustomPayload(ResourceLocation resourceLocation, FriendlyByteBuf friendlyByteBuf, CallbackInfoReturnable cir) + { + if (true) return; + if (CarpetClient.CARPET_CHANNEL.equals(resourceLocation) && Minecraft.getInstance().isSameThread()) + { + ClientNetworkHandler.handleData(friendlyByteBuf, Minecraft.getInstance().player); + cir.setReturnValue(new DiscardedPayload(resourceLocation)); + } + } +} diff --git a/src/main/java/carpet/mixins/Connection_packetCounterMixin.java b/src/main/java/carpet/mixins/Connection_packetCounterMixin.java index f83380ab..c7159e73 100644 --- a/src/main/java/carpet/mixins/Connection_packetCounterMixin.java +++ b/src/main/java/carpet/mixins/Connection_packetCounterMixin.java @@ -28,7 +28,7 @@ public abstract class Connection_packetCounterMixin implements ClientConnectionI // Add to the packet counter whenever a packet is sent. @Inject(method = "sendPacket", at = @At("HEAD")) - private void packetOutCount(Packet packet, @Nullable PacketSendListener packetSendListener, CallbackInfo ci) + private void packetOutCount(final Packet packet, final PacketSendListener packetSendListener, final boolean bl, final CallbackInfo ci) { PacketCounter.totalOut++; } diff --git a/src/main/java/carpet/mixins/MinecraftMixin.java b/src/main/java/carpet/mixins/MinecraftMixin.java index 7941cc25..c7105a35 100644 --- a/src/main/java/carpet/mixins/MinecraftMixin.java +++ b/src/main/java/carpet/mixins/MinecraftMixin.java @@ -20,7 +20,7 @@ public class MinecraftMixin implements MinecraftInterface { @Shadow public ClientLevel level; - @Inject(method = "clearLevel(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At("HEAD")) + @Inject(method = "disconnect(Lnet/minecraft/client/gui/screens/Screen;)V", at = @At("HEAD")) private void onCloseGame(Screen screen, CallbackInfo ci) { CarpetClient.disconnect(); diff --git a/src/main/java/carpet/mixins/PlayerList_coreMixin.java b/src/main/java/carpet/mixins/PlayerList_coreMixin.java index e689d153..d52e0c40 100644 --- a/src/main/java/carpet/mixins/PlayerList_coreMixin.java +++ b/src/main/java/carpet/mixins/PlayerList_coreMixin.java @@ -16,7 +16,7 @@ public class PlayerList_coreMixin { @Inject(method = "placeNewPlayer", at = @At("RETURN")) - private void onPlayerConnected(Connection connection, ServerPlayer player, CallbackInfo ci) + private void onPlayerConnected(Connection connection, ServerPlayer player, int i, CallbackInfo ci) { CarpetServer.onPlayerLoggedIn(player); } diff --git a/src/main/java/carpet/mixins/PlayerList_fakePlayersMixin.java b/src/main/java/carpet/mixins/PlayerList_fakePlayersMixin.java index 0792a728..2cfdae22 100644 --- a/src/main/java/carpet/mixins/PlayerList_fakePlayersMixin.java +++ b/src/main/java/carpet/mixins/PlayerList_fakePlayersMixin.java @@ -34,21 +34,21 @@ public abstract class PlayerList_fakePlayersMixin } } - @Redirect(method = "placeNewPlayer", at = @At(value = "NEW", target = "net/minecraft/server/network/ServerGamePacketListenerImpl")) - private ServerGamePacketListenerImpl replaceNetworkHandler(MinecraftServer server, Connection clientConnection, ServerPlayer playerIn) + @Redirect(method = "placeNewPlayer", at = @At(value = "NEW", target = "(Lnet/minecraft/server/MinecraftServer;Lnet/minecraft/network/Connection;Lnet/minecraft/server/level/ServerPlayer;I)Lnet/minecraft/server/network/ServerGamePacketListenerImpl;")) + private ServerGamePacketListenerImpl replaceNetworkHandler( MinecraftServer server, Connection clientConnection, ServerPlayer playerIn, int i) { if (playerIn instanceof EntityPlayerMPFake fake) { - return new NetHandlerPlayServerFake(this.server, clientConnection, fake); + return new NetHandlerPlayServerFake(this.server, clientConnection, fake, i); } else { - return new ServerGamePacketListenerImpl(this.server, clientConnection, playerIn); + return new ServerGamePacketListenerImpl(this.server, clientConnection, playerIn, i); } } - @Redirect(method = "respawn", at = @At(value = "NEW", target = "net/minecraft/server/level/ServerPlayer")) - public ServerPlayer makePlayerForRespawn(MinecraftServer minecraftServer, ServerLevel serverLevel, GameProfile gameProfile, ServerPlayer serverPlayer, boolean bl) + @Redirect(method = "respawn", at = @At(value = "NEW", target = "(Lnet/minecraft/server/MinecraftServer;Lnet/minecraft/server/level/ServerLevel;Lcom/mojang/authlib/GameProfile;)Lnet/minecraft/server/level/ServerPlayer;")) + public ServerPlayer makePlayerForRespawn(MinecraftServer minecraftServer, ServerLevel serverLevel, GameProfile gameProfile, ServerPlayer serverPlayer, boolean i) { if (serverPlayer instanceof EntityPlayerMPFake) { return EntityPlayerMPFake.respawnFake(minecraftServer, serverLevel, gameProfile); diff --git a/src/main/java/carpet/mixins/ServerCommonPacketListenerimpl_connectionMixin.java b/src/main/java/carpet/mixins/ServerCommonPacketListenerimpl_connectionMixin.java new file mode 100644 index 00000000..b606e0b3 --- /dev/null +++ b/src/main/java/carpet/mixins/ServerCommonPacketListenerimpl_connectionMixin.java @@ -0,0 +1,33 @@ +package carpet.mixins; + +import carpet.fakes.CarpetPacketPayload; +import carpet.network.ServerNetworkHandler; +import net.minecraft.network.protocol.PacketUtils; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.game.ServerGamePacketListener; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerCommonPacketListenerImpl.class) +public class ServerCommonPacketListenerimpl_connectionMixin +{ + @Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true) + private void onCustomCarpetPayload(ServerboundCustomPayloadPacket serverboundCustomPayloadPacket, CallbackInfo ci) + { + if (true) return; + Object thiss = this; + if (thiss instanceof ServerGamePacketListenerImpl impl && serverboundCustomPayloadPacket.payload() instanceof CarpetPacketPayload cpp) { + // We should force onto the main thread here + // ServerNetworkHandler.handleData can possibly mutate data that isn't + // thread safe, and also allows for client commands to be executed + PacketUtils.ensureRunningOnSameThread(serverboundCustomPayloadPacket, (ServerGamePacketListener) this, impl.player.serverLevel()); + ServerNetworkHandler.handleData(cpp.data(), impl.player); + ci.cancel(); + } + ci.cancel(); + } +} diff --git a/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java b/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java index 53788619..06aaa474 100644 --- a/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java +++ b/src/main/java/carpet/mixins/ServerGamePacketListenerImplMixin.java @@ -3,8 +3,8 @@ package carpet.mixins; import carpet.network.CarpetClient; import carpet.network.ServerNetworkHandler; import net.minecraft.network.protocol.PacketUtils; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; import net.minecraft.network.protocol.game.ServerGamePacketListener; -import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; @@ -19,18 +19,5 @@ public class ServerGamePacketListenerImplMixin { @Shadow public ServerPlayer player; - @Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true) - private void onCustomCarpetPayload(ServerboundCustomPayloadPacket packet, CallbackInfo ci) - { - ResourceLocation channel = packet.getIdentifier(); - if (CarpetClient.CARPET_CHANNEL.equals(channel)) - { - // We should force onto the main thread here - // ServerNetworkHandler.handleData can possibly mutate data that isn't - // thread safe, and also allows for client commands to be executed - PacketUtils.ensureRunningOnSameThread(packet, (ServerGamePacketListener) this, player.serverLevel()); - ServerNetworkHandler.handleData(packet.getData(), player); - ci.cancel(); - } - } + } diff --git a/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_antiCheatDisabledMixin.java b/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_antiCheatDisabledMixin.java index c49186d9..0b0ac162 100644 --- a/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_antiCheatDisabledMixin.java +++ b/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_antiCheatDisabledMixin.java @@ -1,7 +1,10 @@ package carpet.mixins; import carpet.CarpetSettings; +import net.minecraft.network.Connection; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; import net.minecraft.server.network.ServerGamePacketListenerImpl; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -11,13 +14,18 @@ import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ServerGamePacketListenerImpl.class) -public abstract class ServerGamePacketListenerImpl_antiCheatDisabledMixin +public abstract class ServerGamePacketListenerImpl_antiCheatDisabledMixin extends ServerCommonPacketListenerImpl { @Shadow private int aboveGroundTickCount; @Shadow private int aboveGroundVehicleTickCount; - @Shadow protected abstract boolean isSingleplayerOwner(); + public ServerGamePacketListenerImpl_antiCheatDisabledMixin(final MinecraftServer minecraftServer, final Connection connection, final int i) + { + super(minecraftServer, connection, i); + } + + //@Shadow protected abstract boolean isSingleplayerOwner(); @Inject(method = "tick", at = @At("HEAD")) private void restrictFloatingBits(CallbackInfo ci) diff --git a/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_coreMixin.java b/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_coreMixin.java index d9a8efcf..3d23a71c 100644 --- a/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_coreMixin.java +++ b/src/main/java/carpet/mixins/ServerGamePacketListenerImpl_coreMixin.java @@ -4,7 +4,9 @@ import carpet.CarpetServer; import carpet.fakes.ServerGamePacketListenerImplInterface; import net.minecraft.network.Connection; import net.minecraft.network.chat.Component; +import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; import net.minecraft.server.network.ServerGamePacketListenerImpl; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -14,11 +16,14 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(ServerGamePacketListenerImpl.class) -public class ServerGamePacketListenerImpl_coreMixin implements ServerGamePacketListenerImplInterface { +public abstract class ServerGamePacketListenerImpl_coreMixin extends ServerCommonPacketListenerImpl implements ServerGamePacketListenerImplInterface { @Shadow public ServerPlayer player; - @Shadow @Final private Connection connection; + public ServerGamePacketListenerImpl_coreMixin(final MinecraftServer minecraftServer, final Connection connection, final int i) + { + super(minecraftServer, connection, i); + } @Inject(method = "onDisconnect", at = @At("HEAD")) private void onPlayerDisconnect(Component reason, CallbackInfo ci) { diff --git a/src/main/java/carpet/mixins/ServerboundCustomPayloadPacket_mixin.java b/src/main/java/carpet/mixins/ServerboundCustomPayloadPacket_mixin.java new file mode 100644 index 00000000..a7a1301d --- /dev/null +++ b/src/main/java/carpet/mixins/ServerboundCustomPayloadPacket_mixin.java @@ -0,0 +1,52 @@ +package carpet.mixins; + +import carpet.fakes.CarpetPacketPayload; +import carpet.network.CarpetClient; +import carpet.network.ClientNetworkHandler; +import net.minecraft.client.Minecraft; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.network.protocol.common.custom.DiscardedPayload; +import net.minecraft.resources.ResourceLocation; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(ServerboundCustomPayloadPacket.class) +public class ServerboundCustomPayloadPacket_mixin +{ + @Inject(method = "readPayload", at = @At( + value = "INVOKE", + target = "Lnet/minecraft/network/protocol/common/ServerboundCustomPayloadPacket;readUnknownPayload(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/network/FriendlyByteBuf;)Lnet/minecraft/network/protocol/common/custom/DiscardedPayload;" + ), cancellable = true) + private static void onOnCustomPayload(ResourceLocation resourceLocation, FriendlyByteBuf friendlyByteBuf, CallbackInfoReturnable cir) + { + if (true) return; + if (CarpetClient.CARPET_CHANNEL.equals(resourceLocation)) + { + //ClientNetworkHandler.handleData(friendlyByteBuf, Minecraft.getInstance().player); + cir.setReturnValue(new CarpetPacketPayload() + { + @Override + public FriendlyByteBuf data() + { + return friendlyByteBuf; + } + + @Override + public void write(final FriendlyByteBuf friendlyByteBuf) + { + // noop - its a client side and we handle it server side only + } + + @Override + public ResourceLocation id() + { + return resourceLocation; + } + }); + } + } +} diff --git a/src/main/java/carpet/network/CarpetClient.java b/src/main/java/carpet/network/CarpetClient.java index 2bb3f0c8..8b268c4e 100644 --- a/src/main/java/carpet/network/CarpetClient.java +++ b/src/main/java/carpet/network/CarpetClient.java @@ -7,11 +7,32 @@ 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.FriendlyByteBuf; import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; public class CarpetClient { + public record CarpetPayload(CompoundTag data) implements CustomPacketPayload + { + public static final ResourceLocation ID = CARPET_CHANNEL; + + public CarpetPayload(final FriendlyByteBuf input) { + this(input.readNbt()); + } + + @Override + public void write(final FriendlyByteBuf output) { + output.writeNbt(data); + } + + @Override + public ResourceLocation id() { + return ID; + } + } + public static final int HI = 69; public static final int HELLO = 420; public static final int DATA = 1; diff --git a/src/main/java/carpet/network/ClientNetworkHandler.java b/src/main/java/carpet/network/ClientNetworkHandler.java index 0973b7e8..6b8ed288 100644 --- a/src/main/java/carpet/network/ClientNetworkHandler.java +++ b/src/main/java/carpet/network/ClientNetworkHandler.java @@ -19,7 +19,9 @@ 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; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; public class ClientNetworkHandler { @@ -129,8 +131,20 @@ public class ClientNetworkHandler public static void respondHello() { CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - (new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HELLO).writeUtf(CarpetSettings.carpetVersion) + new CustomPacketPayload() + { + @Override + public void write(final FriendlyByteBuf friendlyByteBuf) + { + friendlyByteBuf.writeVarInt(CarpetClient.HELLO).writeUtf(CarpetSettings.carpetVersion); + } + + @Override + public ResourceLocation id() + { + return CarpetClient.CARPET_CHANNEL; + } + } )); } @@ -162,8 +176,20 @@ public class ClientNetworkHandler CompoundTag outer = new CompoundTag(); outer.put("clientCommand", tag); CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - (new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.DATA).writeNbt(outer) + new CustomPacketPayload() + { + @Override + public void write( FriendlyByteBuf friendlyByteBuf) + { + friendlyByteBuf.writeVarInt(CarpetClient.DATA).writeNbt(outer); + } + + @Override + public ResourceLocation id() + { + return CarpetClient.CARPET_CHANNEL; + } + } )); } } diff --git a/src/main/java/carpet/network/ServerNetworkHandler.java b/src/main/java/carpet/network/ServerNetworkHandler.java index c2cf113d..9a429ab2 100644 --- a/src/main/java/carpet/network/ServerNetworkHandler.java +++ b/src/main/java/carpet/network/ServerNetworkHandler.java @@ -4,6 +4,7 @@ import carpet.CarpetServer; import carpet.CarpetSettings; import carpet.api.settings.CarpetRule; import carpet.api.settings.RuleHelper; +import carpet.fakes.CarpetPacketPayload; import carpet.fakes.MinecraftServerInterface; import carpet.fakes.ServerGamePacketListenerImplInterface; import carpet.helpers.ServerTickRateManager; @@ -16,19 +17,39 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; +import java.util.function.Consumer; + 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.game.ClientboundCustomPayloadPacket; +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; public class ServerNetworkHandler { + @FunctionalInterface + public interface CarpetCustomPayload extends CustomPacketPayload + { + void writeData(FriendlyByteBuf output); + + @Override + default void write(FriendlyByteBuf output) { + writeData(output); + } + + @Override + default ResourceLocation id() + { + return CarpetClient.CARPET_CHANNEL; + } + } private static final Map remoteCarpetPlayers = new HashMap<>(); private static final Set validCarpetPlayers = new HashSet<>(); @@ -52,24 +73,39 @@ public class ServerNetworkHandler + private static ClientboundCustomPayloadPacket make(Consumer accepter) { + return new ClientboundCustomPayloadPacket( + new CarpetCustomPayload() + { + @Override + public void writeData(final FriendlyByteBuf t) + { + accepter.accept(t); + } + } + ); + } + public static void onPlayerJoin(ServerPlayer playerEntity) { + if (true) return; if (!((ServerGamePacketListenerImplInterface)playerEntity.connection).getConnection().isMemoryConnection()) { - playerEntity.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - (new FriendlyByteBuf(Unpooled.buffer())).writeVarInt(CarpetClient.HI).writeUtf(CarpetSettings.carpetVersion) + playerEntity.connection.send( make( output -> { + output.writeVarInt(CarpetClient.HI); + output.writeUtf(CarpetSettings.carpetVersion); + } )); } else { validCarpetPlayers.add(playerEntity); } - } public static void onHello(ServerPlayer playerEntity, FriendlyByteBuf packetData) { + if (true) return; validCarpetPlayers.add(playerEntity); String clientVersion = packetData.readUtf(64); remoteCarpetPlayers.put(playerEntity, clientVersion); @@ -79,13 +115,13 @@ public class ServerNetworkHandler CarpetSettings.LOG.warn("Player "+playerEntity.getName().getString()+" joined with another carpet version: "+clientVersion); 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(new ClientboundCustomPayloadPacket(CarpetClient.CARPET_CHANNEL, data.build() )); + playerEntity.connection.send(make(data::build)); } public static void sendPlayerLevelData(ServerPlayer player, ServerLevel level) { if (CarpetSettings.superSecretSetting || !validCarpetPlayers.contains(player)) return; DataBuilder data = DataBuilder.create(player.server).withTickRate().withFrozenState().withTickPlayerActiveTimeout(); // .withSuperHotState() - player.connection.send(new ClientboundCustomPayloadPacket(CarpetClient.CARPET_CHANNEL, data.build() )); + player.connection.send(make(data::build)); } @@ -113,9 +149,8 @@ public class ServerNetworkHandler ListTag outputResult = new ListTag(); for (Component line: output) outputResult.add(StringTag.valueOf(Component.Serializer.toJson(line))); if (!output.isEmpty()) result.put("output", outputResult); - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withCustomNbt("clientCommand", result).build() + player.connection.send(make( butebuf -> + DataBuilder.create(player.server).withCustomNbt("clientCommand", result).build(butebuf) )); // run command plug to command output, } @@ -139,9 +174,8 @@ public class ServerNetworkHandler if (CarpetSettings.superSecretSetting) return; for (ServerPlayer player : remoteCarpetPlayers.keySet()) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withRule(rule).build() + player.connection.send(make( output -> + DataBuilder.create(player.server).withRule(rule).build(output) )); } } @@ -151,9 +185,8 @@ public class ServerNetworkHandler if (CarpetSettings.superSecretSetting) return; for (ServerPlayer player : validCarpetPlayers) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withTickRate().build() + player.connection.send(make( output -> + DataBuilder.create(player.server).withTickRate().build(output) )); } } @@ -163,9 +196,8 @@ public class ServerNetworkHandler if (CarpetSettings.superSecretSetting) return; for (ServerPlayer player : validCarpetPlayers) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withFrozenState().build() + player.connection.send(make( output -> + DataBuilder.create(player.server).withFrozenState().build(output) )); } } @@ -175,9 +207,8 @@ public class ServerNetworkHandler if(CarpetSettings.superSecretSetting) return; for (ServerPlayer player : validCarpetPlayers) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withSuperHotState().build() + player.connection.send(make(output -> + DataBuilder.create(player.server).withSuperHotState().build(output) )); } } @@ -187,9 +218,8 @@ public class ServerNetworkHandler if (CarpetSettings.superSecretSetting) return; for (ServerPlayer player : validCarpetPlayers) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withTickPlayerActiveTimeout().build() + player.connection.send(make(output -> + DataBuilder.create(player.server).withTickPlayerActiveTimeout().build(output) )); } } @@ -199,9 +229,8 @@ public class ServerNetworkHandler if (CarpetSettings.superSecretSetting) return; for (ServerPlayer player : validCarpetPlayers) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withCustomNbt(command, data).build() + player.connection.send(make(output -> + DataBuilder.create(player.server).withCustomNbt(command, data).build(output) )); } } @@ -210,9 +239,8 @@ public class ServerNetworkHandler { if (isValidCarpetPlayer(player)) { - player.connection.send(new ClientboundCustomPayloadPacket( - CarpetClient.CARPET_CHANNEL, - DataBuilder.create(player.server).withCustomNbt(command, data).build() + player.connection.send(make(output -> + DataBuilder.create(player.server).withCustomNbt(command, data).build(output) )); } } @@ -310,12 +338,10 @@ public class ServerNetworkHandler return this; } - private FriendlyByteBuf build() + private void build(FriendlyByteBuf packetBuf) { - FriendlyByteBuf packetBuf = new FriendlyByteBuf(Unpooled.buffer()); packetBuf.writeVarInt(CarpetClient.DATA); packetBuf.writeNbt(tag); - return packetBuf; } diff --git a/src/main/java/carpet/patches/EntityPlayerMPFake.java b/src/main/java/carpet/patches/EntityPlayerMPFake.java index 617e718b..6c8cb8d8 100644 --- a/src/main/java/carpet/patches/EntityPlayerMPFake.java +++ b/src/main/java/carpet/patches/EntityPlayerMPFake.java @@ -63,12 +63,12 @@ public class EntityPlayerMPFake extends ServerPlayer if (gameprofile.getProperties().containsKey("textures")) { AtomicReference result = new AtomicReference<>(); - SkullBlockEntity.updateGameprofile(gameprofile, result::set); - gameprofile = result.get(); + //SkullBlockEntity.updateGameprofile(gameprofile, result::set); + //gameprofile = result.get(); } EntityPlayerMPFake instance = new EntityPlayerMPFake(server, worldIn, gameprofile, false); instance.fixStartingPosition = () -> instance.moveTo(pos.x, pos.y, pos.z, (float) yaw, (float) pitch); - server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), instance); + server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), instance, 0); instance.teleportTo(worldIn, pos.x, pos.y, pos.z, (float) yaw, (float) pitch); instance.setHealth(20.0F); instance.unsetRemoved(); @@ -90,7 +90,7 @@ public class EntityPlayerMPFake extends ServerPlayer GameProfile gameprofile = player.getGameProfile(); EntityPlayerMPFake playerShadow = new EntityPlayerMPFake(server, worldIn, gameprofile, true); playerShadow.setChatSession(player.getChatSession()); - server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), playerShadow); + server.getPlayerList().placeNewPlayer(new FakeClientConnection(PacketFlow.SERVERBOUND), playerShadow, 0); playerShadow.setHealth(player.getHealth()); playerShadow.connection.teleport(player.getX(), player.getY(), player.getZ(), player.getYRot(), player.getXRot()); diff --git a/src/main/java/carpet/patches/NetHandlerPlayServerFake.java b/src/main/java/carpet/patches/NetHandlerPlayServerFake.java index a8e5b852..7eee43fe 100644 --- a/src/main/java/carpet/patches/NetHandlerPlayServerFake.java +++ b/src/main/java/carpet/patches/NetHandlerPlayServerFake.java @@ -5,15 +5,16 @@ import net.minecraft.network.chat.Component; import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.network.protocol.Packet; import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.network.ServerGamePacketListenerImpl; import net.minecraft.world.entity.RelativeMovement; import java.util.Set; public class NetHandlerPlayServerFake extends ServerGamePacketListenerImpl { - public NetHandlerPlayServerFake(MinecraftServer server, Connection cc, EntityPlayerMPFake playerIn) + public NetHandlerPlayServerFake(final MinecraftServer minecraftServer, final Connection connection, final ServerPlayer serverPlayer, final int i) { - super(server, cc, playerIn); + super(minecraftServer, connection, serverPlayer, i); } @Override diff --git a/src/main/java/carpet/script/api/Scoreboards.java b/src/main/java/carpet/script/api/Scoreboards.java index dd275e72..293e4c24 100644 --- a/src/main/java/carpet/script/api/Scoreboards.java +++ b/src/main/java/carpet/script/api/Scoreboards.java @@ -61,7 +61,7 @@ public class Scoreboards return ListValue.wrap(scoreboard.getObjectiveNames().stream().map(StringValue::new)); } String objectiveName = lv.get(0).getString(); - Objective objective = scoreboard.getOrCreateObjective(objectiveName); + Objective objective = scoreboard.getObjective(objectiveName); if (objective == null) { return Value.NULL; @@ -103,7 +103,7 @@ public class Scoreboards CarpetContext cc = (CarpetContext) c; Scoreboard scoreboard = cc.server().getScoreboard(); String objectiveName = lv.get(0).getString(); - Objective objective = scoreboard.getOrCreateObjective(objectiveName); + Objective objective = scoreboard.getObjective(objectiveName); if (objective == null) { return Value.FALSE; @@ -151,7 +151,7 @@ public class Scoreboards } } - Objective objective = scoreboard.getOrCreateObjective(objectiveName); + Objective objective = scoreboard.getObjective(objectiveName); if (objective != null) { c.host.issueDeprecation("reading or modifying an objective's criterion with scoreboard_add"); @@ -181,7 +181,7 @@ public class Scoreboards } CarpetContext cc = (CarpetContext) c; Scoreboard scoreboard = cc.server().getScoreboard(); - Objective objective = scoreboard.getOrCreateObjective(lv.get(0).getString()); + Objective objective = scoreboard.getObjective(lv.get(0).getString()); if (objective == null) { return Value.NULL; @@ -225,7 +225,7 @@ public class Scoreboards } return new FormattedTextValue(objective.getDisplayName()); } - case "display_slot" -> { + case "display_slot" -> { /* if (modify) { int slotId = Scoreboard.getDisplaySlotByName(setValue.getString()); @@ -249,7 +249,8 @@ public class Scoreboards slots.add(StringValue.of(slotName)); } } - return ListValue.wrap(slots); + return ListValue.wrap(slots); */ + return Value.NULL; } case "render_type" -> { if (modify) @@ -269,7 +270,7 @@ public class Scoreboards }); expression.addContextFunction("scoreboard_display", 2, (c, t, lv) -> - { + { /* CarpetContext cc = (CarpetContext) c; Scoreboard scoreboard = cc.server().getScoreboard(); String location = lv.get(0).getString(); @@ -291,7 +292,8 @@ public class Scoreboards return Value.NULL; } scoreboard.setDisplayObjective(slot, objective); - return new NumericValue(slot); + return new NumericValue(slot); */ + return Value.NULL; }); expression.addContextFunction("team_list", -1, (c, t, lv) -> diff --git a/src/main/java/carpet/script/command/CommandArgument.java b/src/main/java/carpet/script/command/CommandArgument.java index 93276819..24f09160 100644 --- a/src/main/java/carpet/script/command/CommandArgument.java +++ b/src/main/java/carpet/script/command/CommandArgument.java @@ -258,9 +258,9 @@ public abstract class CommandArgument true ), new ScoreholderArgument(), - new VanillaUnconfigurableArgument("scoreboardslot", ScoreboardSlotArgument::displaySlot, - (c, p) -> StringValue.of(Scoreboard.getDisplaySlotName(ScoreboardSlotArgument.getDisplaySlot(c, p))), false - ), + //new VanillaUnconfigurableArgument("scoreboardslot", ScoreboardSlotArgument::displaySlot, + // (c, p) -> StringValue.of(Scoreboard.getDisplaySlotName(ScoreboardSlotArgument.getDisplaySlot(c, p))), false + //), new VanillaUnconfigurableArgument("swizzle", SwizzleArgument::swizzle, (c, p) -> StringValue.of(SwizzleArgument.getSwizzle(c, p).stream().map(Direction.Axis::getSerializedName).collect(Collectors.joining())), true ), diff --git a/src/main/java/carpet/script/value/EntityValue.java b/src/main/java/carpet/script/value/EntityValue.java index 24565a1d..140c30e6 100644 --- a/src/main/java/carpet/script/value/EntityValue.java +++ b/src/main/java/carpet/script/value/EntityValue.java @@ -633,7 +633,7 @@ public class EntityValue extends Value put("client_brand", (e, a) -> e instanceof final ServerPlayer sp ? StringValue.of(Carpet.getPlayerStatus(sp)) : Value.NULL); put("team", (e, a) -> e.getTeam() == null ? Value.NULL : new StringValue(e.getTeam().getName())); - put("ping", (e, a) -> e instanceof final ServerPlayer sp ? new NumericValue(sp.latency) : Value.NULL); + put("ping", (e, a) -> e instanceof final ServerPlayer sp ? new NumericValue(sp.connection.latency()) : Value.NULL); //spectating_entity // isGlowing diff --git a/src/main/resources/carpet.mixins.json b/src/main/resources/carpet.mixins.json index 72dc6392..f9daa620 100644 --- a/src/main/resources/carpet.mixins.json +++ b/src/main/resources/carpet.mixins.json @@ -181,7 +181,11 @@ "BlockBehaviourBlockStateBase_mixin", "ChainBlock_customStickyMixin", "ChestBlock_customStickyMixin", - "PistonStructureResolver_customStickyMixin" + "PistonStructureResolver_customStickyMixin", + + "ServerboundCustomPayloadPacket_mixin", + "ServerCommonPacketListenerimpl_connectionMixin", + "ClientboundCustomPayloadPacket_customPacketMixin" ], "client": [ @@ -200,7 +204,10 @@ "LevelRenderer_creativeNoClipMixin", "ClientPacketListener_customPacketsMixin", "DebugRenderer_scarpetRenderMixin", - "LevelRenderer_scarpetRenderMixin" + "LevelRenderer_scarpetRenderMixin", + + + "ClientCommonPacketListenerImpl_customPacketMixin" ], "injectors": { "defaultRequire": 1