Skip to content

Commit

Permalink
made changes, added UUID to C2CFriendlyByteBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Nov 30, 2024
1 parent 1e2ed4e commit edf1ab6
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.RegistryFriendlyByteBuf;

import java.util.UUID;

public class C2CFriendlyByteBuf extends RegistryFriendlyByteBuf {
private final String sender;
private final UUID senderUUID;

public C2CFriendlyByteBuf(ByteBuf source, RegistryAccess registryAccess, String sender) {
public C2CFriendlyByteBuf(ByteBuf source, RegistryAccess registryAccess, String sender, UUID senderUUID) {
super(source, registryAccess);
this.sender = sender;
this.senderUUID = senderUUID;
}

public String getSender() {
return this.sender;
}

public UUID getSenderUUID() {
return this.senderUUID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.security.PublicKey;
import java.util.Arrays;
import java.util.Optional;
import java.util.UUID;

public class C2CPacketHandler implements C2CPacketListener {
private static final Logger LOGGER = LogUtils.getLogger();
Expand Down Expand Up @@ -76,7 +77,7 @@ public void sendPacket(Packet<C2CPacketListener> packet, PlayerInfo recipient) t
throw PUBLIC_KEY_NOT_FOUND_EXCEPTION.create();
}
PublicKey key = ppk.data().key();
FriendlyByteBuf buf = wrapByteBuf(PacketByteBufs.create(), null);
FriendlyByteBuf buf = wrapByteBuf(PacketByteBufs.create(), null, null);
if (buf == null) {
return;
}
Expand Down Expand Up @@ -119,7 +120,7 @@ public void sendPacket(Packet<C2CPacketListener> packet, PlayerInfo recipient) t
OutgoingPacketFilter.addPacket(packetString);
}

public static boolean handleC2CPacket(String content, String sender) {
public static boolean handleC2CPacket(String content, String sender, UUID senderUUID) {
byte[] encrypted = ConversionHelper.BaseUTF8.fromUnicode(content);
// round down to multiple of 256 bytes
int length = encrypted.length & ~0xFF;
Expand Down Expand Up @@ -156,7 +157,7 @@ public static boolean handleC2CPacket(String content, String sender) {
if (uncompressed == null) {
return false;
}
C2CFriendlyByteBuf buf = wrapByteBuf(Unpooled.wrappedBuffer(uncompressed), sender);
C2CFriendlyByteBuf buf = wrapByteBuf(Unpooled.wrappedBuffer(uncompressed), sender, senderUUID);
if (buf == null) {
return false;
}
Expand Down Expand Up @@ -213,12 +214,12 @@ public void onPutConnectFourPieceC2CPacket(PutConnectFourPieceC2CPacket packet)
ConnectFourCommand.onPutConnectFourPieceC2CPacket(packet);
}

public static @Nullable C2CFriendlyByteBuf wrapByteBuf(ByteBuf buf, String sender) {
public static @Nullable C2CFriendlyByteBuf wrapByteBuf(ByteBuf buf, String sender, UUID senderUUID) {
ClientPacketListener connection = Minecraft.getInstance().getConnection();
if (connection == null) {
return null;
}
return new C2CFriendlyByteBuf(buf, connection.registryAccess(), sender);
return new C2CFriendlyByteBuf(buf, connection.registryAccess(), sender, senderUUID);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

public record PutConnectFourPieceC2CPacket(String sender, int x) implements C2CPacket {
import java.util.UUID;

public record PutConnectFourPieceC2CPacket(String sender, UUID senderUUID, int x) implements C2CPacket {
public static final StreamCodec<C2CFriendlyByteBuf, PutConnectFourPieceC2CPacket> CODEC = Packet.codec(PutConnectFourPieceC2CPacket::write, PutConnectFourPieceC2CPacket::new);
public static final PacketType<PutConnectFourPieceC2CPacket> ID = new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("clientcommands", "put_connect_four_piece"));

public PutConnectFourPieceC2CPacket(C2CFriendlyByteBuf buf) {
this(buf.getSender(), buf.readVarInt());
this(buf.getSender(), buf.getSenderUUID(), buf.readVarInt());
}

public void write(C2CFriendlyByteBuf buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

public record PutTicTacToeMarkC2CPacket(String sender, byte x, byte y) implements C2CPacket {
import java.util.UUID;

public record PutTicTacToeMarkC2CPacket(String sender, UUID senderUUID, byte x, byte y) implements C2CPacket {
public static final StreamCodec<C2CFriendlyByteBuf, PutTicTacToeMarkC2CPacket> CODEC = Packet.codec(PutTicTacToeMarkC2CPacket::write, PutTicTacToeMarkC2CPacket::new);
public static final PacketType<PutTicTacToeMarkC2CPacket> ID = new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("clientcommands", "put_tic_tac_toe_mark"));

public PutTicTacToeMarkC2CPacket(C2CFriendlyByteBuf buf) {
this(buf.getSender(), buf.readByte(), buf.readByte());
this(buf.getSender(), buf.getSenderUUID(), buf.readByte(), buf.readByte());
}

public void write(C2CFriendlyByteBuf buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

public record StartTwoPlayerGameC2CPacket(String sender, boolean accept, TwoPlayerGame<?, ?> game) implements C2CPacket {
import java.util.UUID;

public record StartTwoPlayerGameC2CPacket(String sender, UUID senderUUID, boolean accept, TwoPlayerGame<?, ?> game) implements C2CPacket {
public static final StreamCodec<C2CFriendlyByteBuf, StartTwoPlayerGameC2CPacket> CODEC = Packet.codec(StartTwoPlayerGameC2CPacket::write, StartTwoPlayerGameC2CPacket::new);
public static final PacketType<StartTwoPlayerGameC2CPacket> ID = new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("clientcommands", "start_two_player_game"));

public StartTwoPlayerGameC2CPacket(C2CFriendlyByteBuf buf) {
this(buf.getSender(), buf.readBoolean(), TwoPlayerGame.getById(buf.readResourceLocation()));
this(buf.getSender(), buf.getSenderUUID(), buf.readBoolean(), TwoPlayerGame.getById(buf.readResourceLocation()));
}

public void write(C2CFriendlyByteBuf buf) {
Expand Down
Loading

0 comments on commit edf1ab6

Please sign in to comment.