Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

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.

  • Author

Never mind, I misunderstood what you meant. Switching the side to Side.SERVER allowed the handler to run, and I can fix the bit with the thread task scheduler. Thank you, diesieben07!

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.