I've been trying to add custom packets to my mod, but I've been having trouble getting the handler of a custom packet to run. For now I'll just post the packet code and the registration code, and I can post more if necessary.
Packet class & Handler:
public class CPacketUpdateLuckyBlock implements IMessage {
private BlockPos pos;
private int luck;
private int type;
public CPacketUpdateLuckyBlock() { }
public CPacketUpdateLuckyBlock(BlockPos pos, int luck, int type) {
this.pos = pos;
this.luck = luck;
this.type = type;
}
@Override
public void fromBytes(ByteBuf buf) {
pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
luck = buf.readByte();
type = buf.readByte();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(pos.getX());
buf.writeInt(pos.getY());
buf.writeInt(pos.getZ());
buf.writeByte(luck);
buf.writeByte(type);
}
public static class Handler extends ServerMessageHandler<CPacketUpdateLuckyBlock> {
@Override
public IMessage handleServerMessage(EntityPlayer player, CPacketUpdateLuckyBlock message, MessageContext ctx) {
System.out.println("here");
World world = ctx.getServerHandler().playerEntity.getEntityWorld();
TileEntity te = world.getTileEntity(message.pos);
if(te instanceof TileEntityLuckyBlock) {
TileEntityLuckyBlock telb = (TileEntityLuckyBlock)te;
telb.setLuck(message.luck);
telb.setType(message.type);
}
return null;
}
}
}
The handler tree:
public abstract class MessageHandler<T extends IMessage> implements IMessageHandler<T, IMessage> {
public abstract IMessage handleClientMessage(EntityPlayer player, T message, MessageContext ctx);
public abstract IMessage handleServerMessage(EntityPlayer player, T message, MessageContext ctx);
@Override
public IMessage onMessage(T message, MessageContext ctx) {
if(ctx.side.isClient())
return handleClientMessage(Minecraft.getMinecraft().player, message, ctx);
else
return handleServerMessage(ctx.getServerHandler().playerEntity, message, ctx);
}
}
public abstract class ClientMessageHandler<T extends IMessage> extends MessageHandler<T> {
public final IMessage handleServerMessage(EntityPlayer player, T message, MessageContext ctx) {
return null;
}
}
public abstract class ServerMessageHandler<T extends IMessage> extends MessageHandler<T> {
public final IMessage handleClientMessage(EntityPlayer player, T message, MessageContext ctx) {
return null;
}
}
Registration:
public final class PacketDispatcher {
public static final SimpleNetworkWrapper DISPATCHER = NetworkRegistry.INSTANCE.newSimpleChannel(Reference.MOD_ID);
private static int packetID = 0;
private PacketDispatcher() { }
public static void registerPackets() {
DISPATCHER.registerMessage(CPacketUpdateLuckyBlock.Handler.class, CPacketUpdateLuckyBlock.class, packetID++, Side.CLIENT);
DISPATCHER.registerMessage(CPacketGiveLuckyBlock.Handler.class, CPacketGiveLuckyBlock.class, packetID++, Side.CLIENT);
}
public static void sendTo(IMessage message, EntityPlayerMP player) {
DISPATCHER.sendTo(message, player);
}
public static void sendToAll(IMessage message) {
DISPATCHER.sendToAll(message);
}
public static void sendToAllAround(IMessage message, TargetPoint targetPoint) {
DISPATCHER.sendToAllAround(message, targetPoint);
}
public static void sendToDimension(IMessage message, int dim) {
DISPATCHER.sendToDimension(message, dim);
}
@SideOnly(Side.CLIENT)
public static void sendToServer(IMessage message) {
DISPATCHER.sendToServer(message);
}
}
Where I send the packet (in Gui method on client side):
blockLuck = blockLuck < -100 ? -100 : (blockLuck > 100 ? 100 : blockLuck);
blockType = blockType < 0 ? 0 : (blockType > 1 ? 1 : blockType);
PacketDispatcher.sendToServer(new CPacketUpdateLuckyBlock(luckyBlock.getPos(), blockLuck, blockType));
luckyBlock.setPlayerEditing(false);
mc.displayGuiScreen(null);
I invoke PacketDispatcher#registerPackets in preInit. I get no errors in chat, nothing. The trace of "System.out.println("here")" simply stops after I send the packet. I have a feeling I'm getting something wrong with the registering process.