Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void onInitializeClient() {
public static void registerCommands(CommandDispatcher<FabricClientCommandSource> dispatcher, CommandRegistryAccess registryAccess) {
AuditMixinsCommand.register(dispatcher);
BookCommand.register(dispatcher);
DiceRollCommand.register(dispatcher);
LookCommand.register(dispatcher);
NoteCommand.register(dispatcher);
ShrugCommand.register(dispatcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package net.earthcomputer.clientcommands.c2c;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.network.PacketByteBuf;

public interface C2CPacket {
void write(PacketByteBuf buf);

void apply(CCPacketListener listener);
void apply(CCPacketListener listener) throws CommandSyntaxException;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is out of the scope of command syntax exceptions. Perhaps we should create our own exception class or just return instead of throwing an exception.

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.logging.LogUtils;
import io.netty.buffer.Unpooled;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import.

Suggested change
import io.netty.buffer.Unpooled;

import net.earthcomputer.clientcommands.c2c.packets.DiceRollC2CPackets;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.earthcomputer.clientcommands.command.DiceRollCommand;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.PlayerListEntry;
Expand Down Expand Up @@ -97,4 +100,19 @@ public void onMessageC2CPacket(MessageC2CPacket packet) {
Text text = prefix.append(Text.translatable("ccpacket.messageC2CPacket.incoming", sender, message).formatted(Formatting.GRAY));
MinecraftClient.getInstance().inGameHud.getChatHud().addMessage(text);
}

@Override
public void onCoinflipInitC2CPacket(DiceRollC2CPackets.DiceRollInitC2CPacket packet) throws CommandSyntaxException {
DiceRollCommand.initDiceroll(packet);
}

@Override
public void onCoinflipAcceptedC2CPacket(DiceRollC2CPackets.DiceRollAcceptedC2CPacket packet) throws CommandSyntaxException {
DiceRollCommand.acceptDiceroll(packet);
}

@Override
public void onCoinflipResultC2CPacket(DiceRollC2CPackets.DiceRollResultC2CPacket packet) {
DiceRollCommand.completeDiceroll(packet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.earthcomputer.clientcommands.c2c.packets.DiceRollC2CPackets;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Util;
Expand All @@ -18,6 +19,7 @@ public class CCPacketHandler {

static {
CCPacketHandler.register(MessageC2CPacket.class, MessageC2CPacket::new);
DiceRollC2CPackets.register();
}

public static <P extends C2CPacket> void register(Class<P> packet, Function<PacketByteBuf, P> packetFactory) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package net.earthcomputer.clientcommands.c2c;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.earthcomputer.clientcommands.c2c.packets.DiceRollC2CPackets;
import net.earthcomputer.clientcommands.c2c.packets.MessageC2CPacket;

public interface CCPacketListener {
void onMessageC2CPacket(MessageC2CPacket packet);

void onCoinflipInitC2CPacket(DiceRollC2CPackets.DiceRollInitC2CPacket packet) throws CommandSyntaxException;

void onCoinflipAcceptedC2CPacket(DiceRollC2CPackets.DiceRollAcceptedC2CPacket packet) throws CommandSyntaxException;

void onCoinflipResultC2CPacket(DiceRollC2CPackets.DiceRollResultC2CPacket packet);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package net.earthcomputer.clientcommands.c2c.packets;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.earthcomputer.clientcommands.c2c.C2CPacket;
import net.earthcomputer.clientcommands.c2c.CCPacketHandler;
import net.earthcomputer.clientcommands.c2c.CCPacketListener;
import net.minecraft.network.PacketByteBuf;

import java.math.BigInteger;
import java.util.BitSet;

public class DiceRollC2CPackets {
// use a diffie hellman key exchange in order to ensure that the coinflip is fair

public static class DiceRollInitC2CPacket implements C2CPacket {
public final String sender;
public final int sides;
public final byte[] ABHash;

public DiceRollInitC2CPacket(String sender, int sides, byte[] ABHash) {
this.sender = sender;
this.sides = sides;
this.ABHash = ABHash;
}

public DiceRollInitC2CPacket(PacketByteBuf raw) {
this.sender = raw.readString();
this.sides = raw.readInt();
this.ABHash = raw.readByteArray();
}

@Override
public void write(PacketByteBuf buf) {
buf.writeString(this.sender);
buf.writeInt(this.sides);
buf.writeByteArray(this.ABHash);
}

@Override
public void apply(CCPacketListener listener) throws CommandSyntaxException {
listener.onCoinflipInitC2CPacket(this);
}
}

public static class DiceRollAcceptedC2CPacket implements C2CPacket {
public final String sender;
public final BigInteger AB;

public DiceRollAcceptedC2CPacket(String sender, BigInteger publicKey) {
this.sender = sender;
this.AB = publicKey;
}

public DiceRollAcceptedC2CPacket(PacketByteBuf stringBuf) {
this.sender = stringBuf.readString();
this.AB = new BigInteger(stringBuf.readBitSet().toByteArray());
}

@Override
public void write(PacketByteBuf buf) {
buf.writeString(this.sender);
buf.writeBitSet(BitSet.valueOf(this.AB.toByteArray()));
}

@Override
public void apply(CCPacketListener listener) throws CommandSyntaxException {
listener.onCoinflipAcceptedC2CPacket(this);
}
}

public static class DiceRollResultC2CPacket implements C2CPacket {
public final String sender;
public final BigInteger s;

public DiceRollResultC2CPacket(String sender, BigInteger s) {
this.sender = sender;
this.s = s;
}

public DiceRollResultC2CPacket(PacketByteBuf stringBuf) {
this.sender = stringBuf.readString();
this.s = new BigInteger(stringBuf.readBitSet().toByteArray());
}

@Override
public void write(PacketByteBuf buf) {
buf.writeString(this.sender);
buf.writeBitSet(BitSet.valueOf(this.s.toByteArray()));
}

@Override
public void apply(CCPacketListener listener) {
listener.onCoinflipResultC2CPacket(this);
}
}

public static void register() {
CCPacketHandler.register(DiceRollInitC2CPacket.class, DiceRollInitC2CPacket::new);
CCPacketHandler.register(DiceRollAcceptedC2CPacket.class, DiceRollAcceptedC2CPacket::new);
CCPacketHandler.register(DiceRollResultC2CPacket.class, DiceRollResultC2CPacket::new);
}
Comment on lines +97 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's necessary to register them here. I don't know why CCPacketHandler#register wasn't private in the first place.

}
Loading