Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add connect four #655

Merged
merged 41 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
3e9beca
added connect four
RealRTTV Aug 9, 2024
9ee553c
whoopsie fixes
RealRTTV Aug 10, 2024
a755a22
changed tie to 3
RealRTTV Aug 11, 2024
b3d3c62
changed to four in a row due to copyright purposes
RealRTTV Aug 11, 2024
2765e13
fine then intellij
RealRTTV Aug 12, 2024
a4c59f2
Minor style changes
xpple Sep 24, 2024
0eb97ee
Update en_us.json
RealRTTV Sep 24, 2024
a58d763
Update TwoPlayerGameType.java
RealRTTV Sep 24, 2024
9e87256
Merge branch 'Earthcomputer:fabric' into connectfour
RealRTTV Sep 25, 2024
75329bd
changed code in response to review
RealRTTV Sep 25, 2024
b3ddbb5
changed translates to `connectFour` and renamed `TwoPlayerGameType` t…
RealRTTV Sep 27, 2024
84cd6bb
changed field to not create new object every time
RealRTTV Sep 28, 2024
2e88d30
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Oct 5, 2024
1b96494
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Oct 22, 2024
f75a4e4
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 21, 2024
8c78bc0
updated to 1.21.3
RealRTTV Nov 21, 2024
b0ede26
fixed issue with color not rendering
RealRTTV Nov 21, 2024
715e13c
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 22, 2024
3a4b5e5
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 28, 2024
a086e1b
updated to new c2c system
RealRTTV Nov 28, 2024
2f490ba
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 30, 2024
2b91ac5
Update ConnectFourCommand.java
RealRTTV Nov 30, 2024
1e2ed4e
Update ConnectFourCommand.java
RealRTTV Nov 30, 2024
edf1ab6
made changes, added UUID to C2CFriendlyByteBuf
RealRTTV Nov 30, 2024
5ca7b4e
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 30, 2024
a98204b
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Nov 30, 2024
c0a0138
fix wildcard imports
RealRTTV Nov 30, 2024
6f6dc83
fixed failure of tests
RealRTTV Dec 1, 2024
de9e061
Merge remote-tracking branch 'upstream/fabric' into connectfour
RealRTTV Dec 2, 2024
b1f0687
updated translates
RealRTTV Dec 2, 2024
44247f2
i blame the fact that intellij kept crashing
RealRTTV Dec 2, 2024
6950604
push
RealRTTV Dec 2, 2024
207246c
push
RealRTTV Dec 2, 2024
5fd9a40
push
RealRTTV Dec 2, 2024
2318c96
push
RealRTTV Dec 2, 2024
dc3f4c2
push
RealRTTV Dec 2, 2024
4e29f1d
push
RealRTTV Dec 2, 2024
c7b371f
push
RealRTTV Dec 3, 2024
100c11c
Fix disconnect event
Earthcomputer Dec 3, 2024
ec26605
push
RealRTTV Dec 3, 2024
cf44b6f
push
RealRTTV Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading