Posted July 29, 20187 yr Hi, I find forge docs missing really crucial info. It mentions calling INSTANCE.registerMessage(MyMessageHandler.class, MyMessage.class, 0, Side.Server); but it never tells where to run it or on which side to call it. I found some other mods calling it in FMLPreInitializationEvent but this event is called on client side only(Is it supposed to?). So, where and when should I register my custom packets?
July 29, 20187 yr 11 minutes ago, MrJake said: but this event is called on client side only(Is it supposed to?). This event is the FML lifecycle event. All FML lifecycle events are called on both the physical client and the server since they are common. What you are seeing is the event called at the physical client since you are launching the client and not the dedicated server. By the time most FML lifecycle events fire there is no logical server for them to be fired at. Read this for futher documentation. 13 minutes ago, MrJake said: where to run it As far as I can tell any common code that is executed before the server is started will do. I personaly do it in the pre-init phase.
July 29, 20187 yr Author I was doing it right, so there's another reason my packet is not registered... This is what I have. Any help appreciated On client side I call this: DHDButtonClickedToServer message = new DHDButtonClickedToServer(button, pos); AunisPacketHandler.INSTANCE.sendToServer(message); Aunis.info("DHDButtonClickedToServer sent"); And this works: [16:09:22] [main/INFO] [aunis]: DHDButtonClickedToServer sent But it doesnt get received: package mrjake.aunis.packet; import io.netty.buffer.ByteBuf; import mrjake.aunis.Aunis; import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; // Send from client to server public class DHDButtonClickedToServer implements IMessage { public DHDButtonClickedToServer() {} private int buttonID; private BlockPos dhdPos; public DHDButtonClickedToServer(int buttonID, BlockPos pos) { this.buttonID = buttonID; this.dhdPos = pos; } @Override public void toBytes(ByteBuf buf) { buf.writeInt(buttonID); buf.writeInt(dhdPos.getX()); buf.writeInt(dhdPos.getY()); buf.writeInt(dhdPos.getZ()); } @Override public void fromBytes(ByteBuf buf) { buttonID = buf.readInt(); dhdPos = new BlockPos(buf.readInt(), buf.readInt(), buf.readInt()); } public static class DHDButtonClickedHandler implements IMessageHandler<DHDButtonClickedToServer, IMessage>{ @Override public IMessage onMessage(DHDButtonClickedToServer message, MessageContext ctx) { Aunis.info("DHDButtonClickedToServer received buttonID:"+message.buttonID+" dhdPos:"+message.dhdPos.toString()); ctx.getServerHandler().player.getServerWorld().addScheduledTask(new Runnable() { @Override public void run() { Aunis.info("DHDButtonClickedToServer received buttonID:"+message.buttonID+" dhdPos:"+message.dhdPos.toString()); } }); BlockPos pos = message.dhdPos; DHDActivateButtonToClient activateMessage = new DHDActivateButtonToClient(message.buttonID, pos); TargetPoint point = new TargetPoint(ctx.getServerHandler().player.getEntityWorld().provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 64); AunisPacketHandler.INSTANCE.sendToAllAround(activateMessage, point); return null; } } } Registrations is done as follows: package mrjake.aunis.packet; import mrjake.aunis.Aunis; import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.relauncher.Side; public class AunisPacketHandler { public static SimpleNetworkWrapper INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel("aunis"); public static void registerPackets() { int id = 0; INSTANCE.registerMessage(DHDButtonClickedToServer.DHDButtonClickedHandler.class, DHDButtonClickedToServer.class, id, Side.SERVER); INSTANCE.registerMessage(DHDActivateButtonToClient.DHDActivateButtonHandler.class, DHDActivateButtonToClient.class, id, Side.CLIENT); id++; Aunis.info("Registering packets for AUNIS"); } } And in main mod class: @EventHandler public void preInit(FMLPreInitializationEvent event) { logger = event.getModLog(); proxy.registerRenderers(); AunisPacketHandler.registerPackets(); } [16:09:03] [main/INFO] [aunis]: Registering packets for AUNIS
July 29, 20187 yr Author Gah, nevermind... Quote The fourth and final parameter is the side that your packet will be received on. If you are planning to send the packet to both sides, it must be registered twice, once to each side. Discriminators can be the same between sides, but are not required to be. This is what mislead me... Now it works INSTANCE.registerMessage(DHDButtonClickedToServer.DHDButtonClickedHandler.class, DHDButtonClickedToServer.class, id, Side.SERVER); id++; INSTANCE.registerMessage(DHDActivateButtonToClient.DHDActivateButtonHandler.class, DHDActivateButtonToClient.class, id, Side.CLIENT); id++;
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.