Jump to content

Minecraft 1.11.2 Custom Packets


xCassyx

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;
	}
	
	@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.

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.

Guest
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.