Skip to content

Commit

Permalink
Add back unnecessary int to CarpetPayload for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
altrisi committed Oct 31, 2023
1 parent 5b8e61c commit c259621
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.network.CarpetClient;
import carpet.network.CarpetPayload;
import carpet.network.ClientNetworkHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientCommonPacketListenerImpl;
Expand Down Expand Up @@ -34,9 +36,13 @@ private void onGameJoined(ClientboundLoginPacket packet, CallbackInfo info)
), cancellable = true)
private void onOnCustomPayload(CustomPacketPayload packet, CallbackInfo ci)
{
if (packet instanceof CarpetClient.CarpetPayload cpp)
if (packet instanceof CarpetPayload cpp)
{
ClientNetworkHandler.onServerData(cpp.data(), minecraft.player);
if (cpp.command() == CarpetPayload.DATA) {
ClientNetworkHandler.onServerData(cpp.data(), minecraft.player);
} else {
CarpetSettings.LOG.info("Invalid carpet-like packet received");
}
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@


import carpet.network.CarpetClient;
import carpet.network.ClientNetworkHandler;
import net.minecraft.client.Minecraft;
import carpet.network.CarpetPayload;
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;
Expand All @@ -25,7 +23,7 @@ private static void onOnCustomPayloadR(ResourceLocation resourceLocation, Friend
{
if (resourceLocation.equals(CarpetClient.CARPET_CHANNEL))
{
cir.setReturnValue(new CarpetClient.CarpetPayload(friendlyByteBuf));
cir.setReturnValue(new CarpetPayload(friendlyByteBuf));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carpet.mixins;

import carpet.network.CarpetClient;
import carpet.CarpetSettings;
import carpet.network.CarpetPayload;
import carpet.network.ServerNetworkHandler;
import net.minecraft.network.protocol.PacketUtils;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
Expand All @@ -19,12 +20,16 @@ public class ServerCommonPacketListenerimpl_connectionMixin
private void onCustomCarpetPayload(ServerboundCustomPayloadPacket serverboundCustomPayloadPacket, CallbackInfo ci)
{
Object thiss = this;
if (thiss instanceof ServerGamePacketListenerImpl impl && serverboundCustomPayloadPacket.payload() instanceof CarpetClient.CarpetPayload cpp) {
if (thiss instanceof ServerGamePacketListenerImpl impl && serverboundCustomPayloadPacket.payload() instanceof CarpetPayload 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.onClientData(impl.player, cpp.data());
if (cpp.command() == CarpetPayload.DATA) {
ServerNetworkHandler.onClientData(impl.player, cpp.data());
} else {
CarpetSettings.LOG.info("Invalid carpet-like packet received");
}
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package carpet.mixins;

import carpet.network.CarpetClient;
import carpet.network.CarpetPayload;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
Expand All @@ -21,7 +22,7 @@ private static void onOnCustomPayload(ResourceLocation resourceLocation, Friendl
{
if (CarpetClient.CARPET_CHANNEL.equals(resourceLocation))
{
cir.setReturnValue(new CarpetClient.CarpetPayload(friendlyByteBuf));
cir.setReturnValue(new CarpetPayload(friendlyByteBuf));
}
}
}
22 changes: 0 additions & 22 deletions src/main/java/carpet/network/CarpetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,11 @@
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 CarpetPayload(FriendlyByteBuf input)
{
this(input.readNbt());
}

@Override
public void write(FriendlyByteBuf output)
{
output.writeNbt(data);
}

@Override
public ResourceLocation id()
{
return CARPET_CHANNEL;
}
}

public static final String HI = "69";
public static final String HELLO = "420";

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/carpet/network/CarpetPayload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package carpet.network;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;

public record CarpetPayload(int command, CompoundTag data) implements CustomPacketPayload
{
public static final int DATA = 1; // The only command, in the packet for backwards compat

public CarpetPayload(CompoundTag data)
{
this(DATA, data);
}

public CarpetPayload(FriendlyByteBuf input)
{
this(input.readInt(), input.readNbt());
}

@Override
public void write(FriendlyByteBuf output)
{
output.writeInt(command);
output.writeNbt(data);
}

@Override
public ResourceLocation id()
{
return CarpetClient.CARPET_CHANNEL;
}
}
4 changes: 2 additions & 2 deletions src/main/java/carpet/network/ClientNetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static void respondHello()
CompoundTag data = new CompoundTag();
data.putString(CarpetClient.HELLO, CarpetSettings.carpetVersion);
CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket(
new CarpetClient.CarpetPayload(data)
new CarpetPayload(data)
));
}

Expand Down Expand Up @@ -144,7 +144,7 @@ public static void clientCommand(String command)
CompoundTag outer = new CompoundTag();
outer.put("clientCommand", tag);
CarpetClient.getPlayer().connection.send(new ServerboundCustomPayloadPacket(
new CarpetClient.CarpetPayload(outer)
new CarpetPayload(CarpetPayload.DATA, outer)
));
}
}
4 changes: 2 additions & 2 deletions src/main/java/carpet/network/ServerNetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void onPlayerJoin(ServerPlayer playerEntity)
{
CompoundTag data = new CompoundTag();
data.putString(CarpetClient.HI, CarpetSettings.carpetVersion);
playerEntity.connection.send(new ClientboundCustomPayloadPacket(new CarpetClient.CarpetPayload(data)));
playerEntity.connection.send(new ClientboundCustomPayloadPacket(new CarpetPayload(data)));
}
else
{
Expand Down Expand Up @@ -244,7 +244,7 @@ public DataBuilder withCustomNbt(String key, Tag value)

private ClientboundCustomPayloadPacket build()
{
return new ClientboundCustomPayloadPacket(new CarpetClient.CarpetPayload(tag));
return new ClientboundCustomPayloadPacket(new CarpetPayload(tag));
}
}
}

0 comments on commit c259621

Please sign in to comment.