Jump to content

Where to register network messages?


MrJake

Recommended Posts

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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++;

 

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.

Announcements



×
×
  • Create New...

Important Information

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