Jump to content

[1.12.2] Bidirectional packets with Spigot


Alcibiade

Recommended Posts

Hello everyone.

I'm trying to establish a bidirectional channel between my Spigot plugin and my Forge mod.

I just need to be able to send some data (in form of a byte array) from the client from the server. And send some data back (also byte array) from the server to the client

 

I have created a basic mod and a basic plugin to test things out. But I encountered an issue.

I can send from the client (forge), and receive into my plugin (spigot). But I can't send back from spigot to forge.

 

There are no errors.

I've tried debugging in wireshark, but it's pretty difficult as I'm flooded with protocol packets and I don't know what the headers are.

 

First, here is my source for the client side (forge)

PacketHandler:

import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class PacketHandler implements IMessageHandler<GenericMessage, IMessage> {
	@Override
	public IMessage onMessage(GenericMessage message, MessageContext ctx) {
		System.out.println("RECEIVED CL MESSAGE !");
		return message;
	}
}

 

HUDData:


import java.text.SimpleDateFormat;
import java.util.Date;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

public class HUDData {
	public SimpleNetworkWrapper network;

	public int ticksSinceLastPulse = 0;
	
	public void preInit()
	{
		network = NetworkRegistry.INSTANCE.newSimpleChannel("LKHud");
		PacketHandler handler = new PacketHandler();
		network.registerMessage(handler, GenericMessage.class, 0, Side.CLIENT);
	    System.out.println("LKHud registered as CLIENT Side with 0");
	}
	
	@SubscribeEvent
	public void onClientTick(TickEvent.ClientTickEvent event) {
		if (event.phase == TickEvent.Phase.START) {
			ticksSinceLastPulse++;

			if (ticksSinceLastPulse > 300) {
				this.sendHUDRefresh();
				ticksSinceLastPulse = 0;
			}
		}
	}

	void sendHUDRefresh() {
		System.out.println("Sending test message to server");
		network.sendToServer(new GenericMessage(new SimpleDateFormat("dd-MM-yyyy hh:mm:ss").format(new Date())));
		System.out.println("Sent test message to server");
	}
}

 

GenericMessage:


import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class GenericMessage implements IMessage {
	String s;

	public GenericMessage() {
		System.out.println("Incoming message");
		// 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());
	}
}

 

Then, Server Side (spigot):

Main:

 


import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

public class LKHudServer extends JavaPlugin {

	static LKHudServer plugin;

	// Fired when plugin is disabled
	@Override
	public void onDisable() {

	}

	@Override
	public void onEnable() {
		plugin = this;
		Logger logger = this.getLogger();
		logger.info("Initializing LKHudServer side");
		Bukkit.getMessenger().registerOutgoingPluginChannel(this, "LKHud");
		Bukkit.getMessenger().registerIncomingPluginChannel(this, "LKHud", new MessageListener());
	}

}

 

MessageListenener:


import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;

import java.util.Arrays;

public class MessageListener implements PluginMessageListener {
    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        System.out.println("Received message >" + channel + "<");
        if (channel.equals("LKHud")) {
            byte[] effectiveMessage = Arrays.copyOfRange(message, 1, message.length);
            System.out.println(new String(effectiveMessage));
            player.sendMessage("RECEIVED MESSAGE");
            byte[] output = new byte[8];
            output[0] = (byte)0;
            output[1] = (byte)0;
            output[2] = (byte)0;
            output[3] = (byte)0;
            output[4] = (byte)0;
            output[5] = (byte)0;
            output[6] = (byte)0;
            output[7] = (byte)0;
            player.sendPluginMessage(LKHudServer.plugin, "LKHud", output);
            System.out.println("Sent back to the client");
        }

    }
}

(I've read that the first byte must be equal to the packet ID, defined in HUDData)

 

Any help or suggestion is highly welcomed.

Thank you in advance

Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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