javamavenminecraftvelocityspigot

onPluginMessageReceived not working when sending from velocity to spigot


I'm learning how velocity plugins work for my minecraft network, and I wanted to make a simple /screenshare (player) command to bring both players onto the screenshare server. Since i want the spigot plugin (that only goes on the screenshare server) to know which player is the staffer and which is the suspect, I found out about plugin messaging channel. I kinda understood how it works, when I send a message from spigot to velocity, the velocity servers gets it and prints it, but when sending from the velocity to spigot it won't.

I know that velocity will only message spigot if there's at least one player online, i even had the plugin to wait 2seconds before running the code, but still nothing. This is the code


    @Override
    public void onEnable() {
        getServer().getMessenger().registerOutgoingPluginChannel(this, "test:screenshare");
        getServer().getMessenger().registerIncomingPluginChannel(this, "test:screenshare", this);

        // Registra l'evento PlayerJoin
        getServer().getPluginManager().registerEvents(this, this);
    }

    @Override
    public void onPluginMessageReceived(String channel, Player player, byte[] message) {
        if (!(channel.equals("test:screenshare"))) {
            getLogger().info("Wrong channel: " + channel);
            return;
        }

        new BukkitRunnable() {
            @Override
            public void run() {
                ByteArrayDataInput in = ByteStreams.newDataInput(message);
                String subchannel = in.readUTF();

                if (subchannel.equals("RoleUpdate")) {
                    String stafferRole = in.readUTF();
                    String stafferName = in.readUTF();
                    String suspectRole = in.readUTF();
                    String suspectName = in.readUTF();

                    getLogger().info("Received roles: " + stafferRole + " for " + stafferName + " and " + suspectRole + " for " + suspectName);
                } else {
                    getLogger().info("Subchannel not found: " + subchannel);
                }
            }
        }.runTaskLater(this, 40L);
    }

and this is the velocity part of the code:

public static final MinecraftChannelIdentifier IDENTIFIER = MinecraftChannelIdentifier.from("test:screenshare");

sendPluginMessage(staffer.getUsername(), suspect.getUsername(), "Staffer", "Suspect"); function called upon running the /ss command on velocity.

    private void sendPluginMessage(String stafferName, String suspectName, String stafferRole, String suspectRole) {
            ByteArrayDataOutput out = ByteStreams.newDataOutput();

            out.writeUTF("RoleUpdate");
            out.writeUTF(stafferRole);
            out.writeUTF(stafferName);
            out.writeUTF(suspectRole);
            out.writeUTF(suspectName);

            Optional<Player> anyConnectedPlayer = proxy.getAllPlayers().stream().findAny();
            if (anyConnectedPlayer.isPresent()) {
                Optional<ServerConnection> connection = anyConnectedPlayer.get().getCurrentServer();
                if (connection.isPresent()) {
                    connection.get().sendPluginMessage(IDENTIFIER, out.toByteArray());
                    logger.info("Message sent to spigot, staffer name: " + stafferName + " (role: " + stafferRole + ") and suspect: " + suspectName + " (role: " + suspectRole + ")");
                } else {
                    logger.warn("No connection was found for player: " + anyConnectedPlayer.get().getUsername());
                }
            } else {
                logger.warn("No players connected to send message to the spigot server.");
            }
        }

the logger ""Message sent to spigot, staffer name: " + stafferName + " (role: " + stafferRole + ") and suspect: " + suspectName + " (role: " + suspectRole + ")")" shows up in console and everything's fine, but spigot won't say anything. What am I missing?

Tried using bungee's channel instead of custom one, tried adding more time before having the spigot's server to receive the message from velocity, searched online but beside these 2 issues I didn't found anyone else having the same problem.

It's like velocity can read messages sent from my spigot server, but my spigot server can't read messages from velocity.


Solution

  • You are trying to send a message to the player, and intercept it by the spigot, which could not work.

    To explain, here is how it works:

    Spigot    <=>   Velocity   <=>   Player
    

    Spigot sendPluginMessage(), it will do: spigot -> velocity -> player. Actually, you intercept it on Velocity (I suggest you to cancel it).

    Your Velocity code is trying to do: velocity -> player. Which will not works. It should do the opposite: spigot <- velocity (send to "child", as spigot is below Velocity)

    You have to get the server where you want to send the information, then use sendPluginMessage from the server object, to send it to the server itself.

    Example code:

    private void sendPluginMessage(String stafferName, String suspectName, String stafferRole, String suspectRole) {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
    
        out.writeUTF("RoleUpdate");
        out.writeUTF(stafferRole);
        out.writeUTF(stafferName);
        out.writeUTF(suspectRole);
        out.writeUTF(suspectName);
        byte[] data = out.toByteArray();
    
        for(RegisteredServer server : proxy.getAllServers()) {
            server.sendPluginMessage(IDENTIFIER, data);
            logger.info("Message sent to server " + server.getServerInfo().getName() + ", staffer name: " + stafferName + " (role: " + stafferRole + ") and suspect: " + suspectName + " (role: " + suspectRole + ")");
        }
    }