Jump to content

Spigot server not receiving custom packets.


BadBoy6767

Recommended Posts

I have this custom packet:

public class MCJSMessage implements IMessage {

public static class Handler implements IMessageHandler<MCJSMessage, IMessage> {
	@Override
	public IMessage onMessage(MCJSMessage message, MessageContext ctx) {
		if (message.type == TYPE_LOAD_LUA) {
			MCJS.instance.getPython().exec(message.string);
		} else if (message.type == TYPE_HOOK_PLAYER_JOIN) {
			MCJS.instance.hooks.call("PlayerJoin",
					new Object[] { new MCJSPlayer(FMLCommonHandler.instance().getMinecraftServerInstance()
							.getPlayerList().getPlayerByUUID(UUID.fromString(message.string))) });
		}
		return null;
	}
}

public static final int TYPE_EMPTY = 0, TYPE_LOAD_LUA = 1, TYPE_HOOK_PLAYER_JOIN = 2,
		TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER = 3;

int type = TYPE_EMPTY;
String string = "";

public MCJSMessage() {
}

public MCJSMessage(int type, String string) {
	this.string = string;
	this.type = type;
}

@Override
public void fromBytes(ByteBuf buf) {
	type = buf.readByte();
	if (type != TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER)
		string = ByteBufUtils.readUTF8String(buf.readBytes(buf.readableBytes()));
}

@Override
public void toBytes(ByteBuf buf) {
	buf.writeByte(type);
	if (type != TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER)
		buf.writeBytes(string.getBytes());
}

}

And it's being registered with

		simpleNetworkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("MCJS");
	simpleNetworkWrapper.registerMessage(MCJSMessage.Handler.class, MCJSMessage.class, 84, Side.SERVER);

After connecting to a server, the client is supposed to send a packet to the server telling it that the client is modified with my mod.

If the server does not detect the packet, it's supposed to kick the player off the server.

I think I send it normally:

@SubscribeEvent
public void joinServer(FMLNetworkEvent.ClientConnectedToServerEvent e) {
	resetPython();
	System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
	simpleNetworkWrapper.sendToServer(new MCJSMessage(MCJSMessage.TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER, ""));
}

 

But the server is not detecting this packet. I think it's something wrong with my Forge code.

 

Am I doing something wrong here?

Link to comment
Share on other sites

  • Why are you having this
    type

    thing in the message? This is what multiple message types are for...

  • Why on earth is your message ID 84?
  • Are you seriously feeding the data that comes from the client directly into a python executor? Seriously? This can break your whole server.
  • You need to read the warning about thread-safety on the documentation page about the SimpleNetworkWrapper.
  • If I remember correctly you cannot send packets in
    ClientConnectedToServerEvent

    yet, you have to wait a tick.

  • What does this have to do with Spigot?

 

  • I don't like the idea of having multiple messages.
  • I saw another tutorial use 84, and I kind of automatically typed that in.
  • Spigot is the server, The client is what will execute Python code.
  • Already know about that.
  • Okay, thank you, will do!
  • Because Spigot is the server, it will not use Forge.

Link to comment
Share on other sites

The

Side

you pass to

SimpleNetworkWrapper#registerMessage

is the side that the packet is received and processed on, not the side it's sent from.

 

If the packet is being received on the client, pass

Side.CLIENT

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

The simplest way is to store a collection of

Runnable

s (possibly a

Queue

) to be executed next tick, then subscribe to

ClientTickEvent

and check that the

Phase

is

Phase.START

before executing and stored tasks and removing them from the collection.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

The simplest way is to store a collection of

Runnable

s (possibly a

Queue

) to be executed next tick, then subscribe to

ClientTickEvent

and check that the

Phase

is

Phase.START

before executing and stored tasks and removing them from the collection.

event.getPhase();

is returning an EventPriority for some reason.

Although I've managed to make a

List

of

Runnable

s and have the first one in the list be called, but the server still does not see the packet.

Link to comment
Share on other sites

The simplest way is to store a collection of

Runnable

s (possibly a

Queue

) to be executed next tick, then subscribe to

ClientTickEvent

and check that the

Phase

is

Phase.START

before executing and stored tasks and removing them from the collection.

event.getPhase();

is returning an EventPriority for some reason.

 

You actually want the

TickEvent#phase

field rather than the

Event#getPhase

method.

 

The method has a slightly confusing name, since it isn't immediately obvious that an

EventPriority

is a type of phase (but not the same type as

TickEvent.Phase

).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

After switching ClientTickEvent.getPhase to just the phase field, the server still does not receive the message.

 

Here is the main class code if it helps:

package com.example.examplemod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.script.ScriptException;

import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.util.PythonInterpreter;

import net.minecraft.util.Tuple;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

@Mod(modid = MCJS.MODID, version = MCJS.VERSION, name = "MCPY")
public class MCJS {

static MCJS instance;

public static final String MODID = "mcpy";
public static final String VERSION = "1.0";

private PythonInterpreter python;
public MCJSHook hooks;

public SimpleNetworkWrapper simpleNetworkWrapper;

public List<Runnable> tickQueue = new ArrayList<Runnable>();

public HashMap<String, MCJSPlayer> players = new HashMap<String, MCJSPlayer>();

@EventHandler
public void preInit(FMLPreInitializationEvent event) throws ScriptException {
	this.instance = this;
	prepareForServer();
	this.python.exec("def a():\n\tpass");
	this.python.exec("hook.add(\"PlayerJoin\", \"test\", a)");
	for (Tuple<String, Tuple<String, PyFunction>> tuple : hooks.hooks)
		System.out.println(tuple);
	MinecraftForge.EVENT_BUS.register(this);
	simpleNetworkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("MCJS");
	simpleNetworkWrapper.registerMessage(MCJSMessage.Handler.class, MCJSMessage.class, 84, Side.SERVER);
}

public PythonInterpreter prepareForServer() {
	this.python = new PythonInterpreter();
	hooks = new MCJSHook();
	this.python.set("hook", Py.java2py(hooks));
	return getPython();
}

public PythonInterpreter getPython() {
	return this.python == null ? prepareForServer() : this.python;
}

@SubscribeEvent
public void joinServer(FMLNetworkEvent.ClientConnectedToServerEvent e) {
	prepareForServer();
	tickQueue.add(new Runnable() {
		public void run() {
			System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
			simpleNetworkWrapper
					.sendToServer(new MCJSMessage(MCJSMessage.TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER, ""));
		}
	});
}

@SubscribeEvent
public void clientTick(ClientTickEvent e) {
	if (tickQueue.size() > 0 && e.phase == Phase.START)
		tickQueue.remove(0).run();
}

}

Link to comment
Share on other sites

How are you receiving the packet on the server?

 

Here is my Spigot code:

	Bukkit.getMessenger().registerIncomingPluginChannel(this, "MCJS", new PluginMessageListener() {
		@Override
		public void onPluginMessageReceived(String channel, Player player, byte[] bytes) {
			System.out.println("HAHA");
			ByteBuf buf = Unpooled.copiedBuffer(bytes);
			int type = buf.readByte();
			if (type == TYPE_THIS_IS_THE_MCLUA_MOD_SPEAKING_HERE___OVER) {
				verifiedMCJSUsers.add(player.getUniqueId());
			}
		}
	});

The plugin kicks the player off the server if my packet isn't received within 10 seconds.

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.