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

Open
wants to merge 12 commits into
base: fabric
Choose a base branch
from
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);
FourInARowCommand.register(dispatcher);
RealRTTV marked this conversation as resolved.
Show resolved Hide resolved
CParticleCommand.register(dispatcher, context);
CPlaySoundCommand.register(dispatcher);
CrackRNGCommand.register(dispatcher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import com.mojang.logging.LogUtils;
import io.netty.buffer.Unpooled;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutFourInARowPieceC2CPacket;
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.FourInARowCommand;
import net.earthcomputer.clientcommands.command.ListenCommand;
import net.earthcomputer.clientcommands.features.TwoPlayerGameType;
import net.earthcomputer.clientcommands.interfaces.IClientPacketListener_C2C;
import net.earthcomputer.clientcommands.command.TicTacToeCommand;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
Expand Down Expand Up @@ -47,8 +50,9 @@ public class C2CPacketHandler implements C2CPacketListener {

public static final ProtocolInfo.Unbound<C2CPacketListener, RegistryFriendlyByteBuf> PROTOCOL_UNBOUND = ProtocolInfoBuilder.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(PutFourInARowPieceC2CPacket.ID, PutFourInARowPieceC2CPacket.CODEC)
);

public static final String C2C_PACKET_HEADER = "CCΕNC:";
Expand Down Expand Up @@ -198,15 +202,20 @@ public void onMessageC2CPacket(MessageC2CPacket packet) {
}

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

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

@Override
public void onPutFourInARowPieceC2CPacket(PutFourInARowPieceC2CPacket packet) {
FourInARowCommand.onPutFourInARowPieceC2CPacket(packet);
}

@Nullable
public static ProtocolInfo<C2CPacketListener> getCurrentProtocolInfo() {
ClientPacketListener connection = Minecraft.getInstance().getConnection();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package net.earthcomputer.clientcommands.c2c;

import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.c2c.packets.PutFourInARowPieceC2CPacket;
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;
import net.minecraft.network.PacketListener;

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

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

void onPutTicTacToeMarkC2CPacket(PutTicTacToeMarkC2CPacket packet);

void onPutFourInARowPieceC2CPacket(PutFourInARowPieceC2CPacket packet);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
import net.minecraft.network.protocol.PacketType;
import net.minecraft.resources.ResourceLocation;

public record StartTicTacToeGameC2CPacket(String sender, boolean accept) implements C2CPacket {
public static final StreamCodec<RegistryFriendlyByteBuf, StartTicTacToeGameC2CPacket> CODEC = Packet.codec(StartTicTacToeGameC2CPacket::write, StartTicTacToeGameC2CPacket::new);
public static final PacketType<StartTicTacToeGameC2CPacket> ID = new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("clientcommands", "start_tic_tac_toe_game"));
public record PutFourInARowPieceC2CPacket(String sender, int x) implements C2CPacket {
public static final StreamCodec<RegistryFriendlyByteBuf, PutFourInARowPieceC2CPacket> CODEC = Packet.codec(PutFourInARowPieceC2CPacket::write, PutFourInARowPieceC2CPacket::new);
public static final PacketType<PutFourInARowPieceC2CPacket> ID = new PacketType<>(PacketFlow.CLIENTBOUND, ResourceLocation.fromNamespaceAndPath("clientcommands", "put_four_in_a_row_piece"));

public StartTicTacToeGameC2CPacket(FriendlyByteBuf buf) {
this(buf.readUtf(), buf.readBoolean());
public PutFourInARowPieceC2CPacket(FriendlyByteBuf buf) {
this(buf.readUtf(), buf.readVarInt());
}

public void write(FriendlyByteBuf buf) {
buf.writeUtf(this.sender);
buf.writeBoolean(this.accept);
buf.writeUtf(sender);
buf.writeVarInt(x);
}

@Override
public void handle(C2CPacketListener handler) {
handler.onStartTicTacToeGameC2CPacket(this);
public PacketType<? extends Packet<C2CPacketListener>> type() {
return ID;
}

@Override
public PacketType<? extends Packet<C2CPacketListener>> type() {
return ID;
public void handle(C2CPacketListener handler) {
handler.onPutFourInARowPieceC2CPacket(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.earthcomputer.clientcommands.c2c.packets;

import net.earthcomputer.clientcommands.c2c.C2CPacket;
import net.earthcomputer.clientcommands.c2c.C2CPacketListener;
import net.earthcomputer.clientcommands.features.TwoPlayerGame;
import net.earthcomputer.clientcommands.features.TwoPlayerGameType;
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;

public record StartTwoPlayerGameC2CPacket(String sender, boolean accept, TwoPlayerGameType<?> game) implements C2CPacket {
public static final StreamCodec<RegistryFriendlyByteBuf, 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(FriendlyByteBuf buf) {
this(buf.readUtf(), buf.readBoolean(), TwoPlayerGameType.getById(buf.readResourceLocation()));
}

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

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

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