Skip to content

Commit

Permalink
add connect four (#655)
Browse files Browse the repository at this point in the history
* added connect four

* whoopsie fixes

* changed tie to 3

* changed to four in a row due to copyright purposes

* fine then intellij

* Minor style changes

* Update en_us.json

Co-authored-by: Frederik van der Els <[email protected]>

* Update TwoPlayerGameType.java

* changed code in response to review

* changed translates to `connectFour` and renamed `TwoPlayerGameType` to `TwoPlayerGame`

* changed field to not create new object every time

* updated to 1.21.3

* fixed issue with color not rendering

* updated to new c2c system

* Update ConnectFourCommand.java

Co-authored-by: Joseph Burton <[email protected]>

* Update ConnectFourCommand.java

Co-authored-by: Joseph Burton <[email protected]>

* made changes, added UUID to C2CFriendlyByteBuf

* fix wildcard imports

* fixed failure of tests

* updated translates

* i blame the fact that intellij kept crashing

* push

* push

* push

* push

* push

* push

* push

* Fix disconnect event

* push

* push

---------

Co-authored-by: Frederik van der Els <[email protected]>
Co-authored-by: Frederik van der Els <[email protected]>
Co-authored-by: Joseph Burton <[email protected]>
  • Loading branch information
4 people authored Dec 3, 2024
1 parent 549d41d commit 64b6948
Show file tree
Hide file tree
Showing 21 changed files with 850 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
CGameModeCommand.register(dispatcher);
CGiveCommand.register(dispatcher, context);
ChorusCommand.register(dispatcher);
ConnectFourCommand.register(dispatcher);
CParticleCommand.register(dispatcher, context);
CPlaySoundCommand.register(dispatcher);
CrackRNGCommand.register(dispatcher);
Expand Down
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 @@ -7,9 +7,12 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutConnectFourPieceC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutTicTacToeMarkC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.StartTicTacToeGameC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.StartTwoPlayerGameC2CPacket;
import net.earthcomputer.clientcommands.command.ConnectFourCommand;
import net.earthcomputer.clientcommands.command.ListenCommand;
import net.earthcomputer.clientcommands.features.TwoPlayerGame;
import net.earthcomputer.clientcommands.command.TicTacToeCommand;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.ChatFormatting;
Expand All @@ -36,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 All @@ -46,8 +50,9 @@ public class C2CPacketHandler implements C2CPacketListener {

public static final ProtocolInfo<C2CPacketListener> C2C = ProtocolInfoBuilder.<C2CPacketListener, C2CFriendlyByteBuf>clientboundProtocol(ConnectionProtocol.PLAY, builder -> builder
.addPacket(MessageC2CPacket.ID, MessageC2CPacket.CODEC)
.addPacket(StartTicTacToeGameC2CPacket.ID, StartTicTacToeGameC2CPacket.CODEC)
.addPacket(StartTwoPlayerGameC2CPacket.ID, StartTwoPlayerGameC2CPacket.CODEC)
.addPacket(PutTicTacToeMarkC2CPacket.ID, PutTicTacToeMarkC2CPacket.CODEC)
.addPacket(PutConnectFourPieceC2CPacket.ID, PutConnectFourPieceC2CPacket.CODEC)
).bind(b -> (C2CFriendlyByteBuf) b);

public static final String C2C_PACKET_HEADER = "CCΕNC:";
Expand All @@ -72,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 @@ -115,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 @@ -152,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 @@ -195,21 +200,26 @@ public void onMessageC2CPacket(MessageC2CPacket packet) {
}

@Override
public void onStartTicTacToeGameC2CPacket(StartTicTacToeGameC2CPacket packet) {
TicTacToeCommand.onStartTicTacToeGameC2CPacket(packet);
public void onStartTwoPlayerGameC2CPacket(StartTwoPlayerGameC2CPacket packet) {
TwoPlayerGame.onStartTwoPlayerGame(packet);
}

@Override
public void onPutTicTacToeMarkC2CPacket(PutTicTacToeMarkC2CPacket packet) {
TicTacToeCommand.onPutTicTacToeMarkC2CPacket(packet);
}

public static @Nullable C2CFriendlyByteBuf wrapByteBuf(ByteBuf buf, String sender) {
@Override
public void onPutConnectFourPieceC2CPacket(PutConnectFourPieceC2CPacket packet) {
ConnectFourCommand.onPutConnectFourPieceC2CPacket(packet);
}

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
@@ -1,14 +1,17 @@
package net.earthcomputer.clientcommands.c2c;

import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutConnectFourPieceC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutTicTacToeMarkC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.StartTicTacToeGameC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.StartTwoPlayerGameC2CPacket;
import net.minecraft.network.ClientboundPacketListener;

public interface C2CPacketListener extends ClientboundPacketListener {
void onMessageC2CPacket(MessageC2CPacket packet);

void onStartTicTacToeGameC2CPacket(StartTicTacToeGameC2CPacket packet);
void onStartTwoPlayerGameC2CPacket(StartTwoPlayerGameC2CPacket packet);

void onPutTicTacToeMarkC2CPacket(PutTicTacToeMarkC2CPacket packet);

void onPutConnectFourPieceC2CPacket(PutConnectFourPieceC2CPacket packet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.earthcomputer.clientcommands.c2c.packets;

import net.earthcomputer.clientcommands.c2c.C2CFriendlyByteBuf;
import net.earthcomputer.clientcommands.c2c.C2CPacket;
import net.earthcomputer.clientcommands.c2c.C2CPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

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.getSenderUUID(), buf.readVarInt());
}

public void write(C2CFriendlyByteBuf buf) {
buf.writeVarInt(this.x);
}

@Override
public PacketType<? extends Packet<C2CPacketListener>> type() {
return ID;
}

@Override
public void handle(C2CPacketListener handler) {
handler.onPutConnectFourPieceC2CPacket(this);
}
}
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.earthcomputer.clientcommands.c2c.packets;

import net.earthcomputer.clientcommands.c2c.C2CFriendlyByteBuf;
import net.earthcomputer.clientcommands.c2c.C2CPacket;
import net.earthcomputer.clientcommands.c2c.C2CPacketListener;
import net.earthcomputer.clientcommands.features.TwoPlayerGame;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

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.getSenderUUID(), buf.readBoolean(), TwoPlayerGame.getById(buf.readResourceLocation()));
}

public void write(C2CFriendlyByteBuf buf) {
buf.writeBoolean(this.accept);
buf.writeResourceLocation(this.game.getId());
}

@Override
public void handle(C2CPacketListener handler) {
handler.onStartTwoPlayerGameC2CPacket(this);
}

@Override
public PacketType<? extends Packet<C2CPacketListener>> type() {
return ID;
}
}
Loading

0 comments on commit 64b6948

Please sign in to comment.