Jump to content

[SOLVED /w SOLUTION] How to use Custom payload packets?


Shadow_Parallax

Recommended Posts

How do I use the Packet 250? I have sort of figured out how to send it, but how do I listen for it?

 

Also, the way I'm sending it atm is using Minecraft code not Forge code, since I don't know how to get a ForgeModContainer object...

I'd really appreciate it if you could point me in the right direction. I've already seen the article on the wiki about Netty packet handling but it seems to cause memory leaks. So I'd prefer to use the "native" way.

 

====[solution]====

@Mod class

public static SimpleNetworkWrapper network;

@EventHandler
public void init(FMLInitializationEvent event){
    network = NetworkRegistry.INSTANCE.newSimpleChannel("ChannelName");
    network.registerMessage(GenericMessage.Handle.class, GenericMessage.class, 84, Side.CLIENT); // I got the 84 from an exception I got when using 0, I'm not sure if this number is based on the channel name or not. Eitherway, start at 0 and then change it to the number from the exception.
// EDIT: This seems to be the byte(?) value of the 1st character in the payload. So if you want to send "Hello!" to the server you'd do for example "AHello!" and it would still be "Hello!"
}

 

GenericMessage.java (Sorry for the fail indentation here, I copy pasted it)

        public static class Handle implements IMessageHandler<GenericMessage, IMessage> {

	@Override
	public IMessage onMessage(GenericMessage message, MessageContext ctx) {
                    // Do stuff with the message.
	}
}

String s;

public GenericMessage() {
	// Empty constructor for incoming messages.
}

        // Constructor for outgoing messages
public GenericMessage(String s){
	this.s = s;
}

@Override
public void fromBytes(ByteBuf buf) {
                // Convert the ByteBuf object to a String object
	System.out.println("From Bytes");
	s = ByteBufUtils.readUTF8String(buf);
}

        // Just returns the message stored in the GenericMessage object
public String getMessage() {
	return s;
}

@Override
public void toBytes(ByteBuf buf) {
                // Converts the message from the outgoing constructor to bytes for sending.
	buf.writeBytes(s.getBytes());
}

 

Bukkit side

 

@Override
public void onEnable() {
    Bukkit.getMessenger().registerOutgoingPluginChannel(this, "YourChannel"); // Same as on the client!
    Bukkit.getMessenger().registerIncomingPluginChannel(this, "YourChannel", new MessageHandler()); // Create a class which will handle your messages
}

 

MessageHandler.java

public class MessageHandler implements PluginMessageListener {
    @Override
    public void onPluginMessageReceived(String string, Player player, byte[] bytes){
        // Do stuff like checking the contents etc.
    }
}

Link to comment
Share on other sites

There is this so called "Simple Impl" which provides you with an easy to use wrapper around the netty system. There is no tutorial on it as far as I know, but you create a handler with NetworkRegistry#newSimpleChannel.

You can see it used in our ModJam mod: https://github.com/diesieben07/Modjam-4/blob/master/src/main/java/mod/badores/BadOres.java#L150

 

I've been looking at that class for a while now yes, but I'm not sure if it supports communicating with a Bukkit plugin, as that is what I was hoping to do. I don't mean to sound lazy, but I'd rather know up front than spend days trying and finding out it wasn't possible in the first place.

 

I also had a good laugh at your ore names. :P "Nosleeptonite, Wannafite, Wantarite, Nopium"

Link to comment
Share on other sites

Yay! I got it to send something to the server. Now server to client... How will I make Bukkit send it in a way Forge can understand it? :/

 

EDIT: I am in need of an event that will fire when the player logs in to a server... Can't seem to find it by myself.

EDIT2: I figured it out! And it works now. I'll update the OP with my solution, so other can see it.

Link to comment
Share on other sites

Some problems:

Your onPlayerLogin is not needed. FML will automatically send REGISTER packets for the needed channels.

Also the ID you pass when registering the message is arbitrary, you should be able to just use 0 and then increment it every time. FML will write that packetID as a byte to the stream before your actual data.

 

The resulting error with just using any ID results from Bukkit, I suppose if you prefix it with a 0 instead of a letter it'll work. But if you do use a letter, for whatever reason it'll give you an exception and crash the client.

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.