Jump to content

Minecraft 1.11.2 Custom Packets


Recommended Posts

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;
	public void fromBytes(ByteBuf buf) {
		pos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt());
		luck = buf.readByte();
		type = buf.readByte();

	public void toBytes(ByteBuf buf) {
	public static class Handler extends ServerMessageHandler<CPacketUpdateLuckyBlock> {
		public IMessage handleServerMessage(EntityPlayer player, CPacketUpdateLuckyBlock message, MessageContext ctx) {
			World world = ctx.getServerHandler().playerEntity.getEntityWorld();
			TileEntity te = world.getTileEntity(message.pos);
			if(te instanceof TileEntityLuckyBlock) {
				TileEntityLuckyBlock telb = (TileEntityLuckyBlock)te;
			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);
	public IMessage onMessage(T message, MessageContext ctx) {
			return handleClientMessage(Minecraft.getMinecraft().player, message, ctx);
			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;



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) {
	public static void sendToAllAround(IMessage message, TargetPoint targetPoint) {
		DISPATCHER.sendToAllAround(message, targetPoint);
	public static void sendToDimension(IMessage message, int dim) {
		DISPATCHER.sendToDimension(message, dim);
	public static void sendToServer(IMessage 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));


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.

Edited by xCassyx
Fixing an error
Link to comment
Share on other sites

They are registered on both sides. I have CommonProxy, and ClientProxy which extends CommonProxy. The packets are registered in CommonProxy#registerHandlers which is always called. I copied that documentation exactly first, but it did not work, and registering packets on only one side (regardless of the side) caused an error with something along the lines of "I don't know what discriminator 0 means." Also, the code in the handler does not run at all, because nothing is logged in the console, and I plan to move the code to IThreadListener#addScheduledTask later once I get the handler to run.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.