1.12.2 Plugin Message Channel help

Discussion in 'Spigot Plugin Development' started by MetalWarrior, Mar 11, 2020.

  1. I have this code whose purpose is to send part 0 of the list of servers in a Bungeecord network, I did it as specified in https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/
    However this does not work and does not send anything
    Code:
    Command Class:
    Code (Text):

    package com.theunclebarto.mein;

    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;

    public class Command implements CommandExecutor, PluginMessageListener{
       
        private final ClasePrincipal plugin;
        public Command(ClasePrincipal plugin) {
            this.plugin = plugin;
        }
        public String[] message;
        @Override
        public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(args[0].equalsIgnoreCase("message")) {
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("GetServers");
                player.sendMessage(message[0]);
            }
            return true;
        }
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if(!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);

            this.message = in.readUTF().split(", ");
        }
    }

     
    Main Class:
    Code (Text):

    package com.theunclebarto.mein;

    import org.bukkit.plugin.java.JavaPlugin;

    public class ClasePrincipal extends JavaPlugin{

        @Override
        public void onEnable() {
            Bukkit.getPluginCommand("menu").setExecutor(new Command(this));
            Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new Command(this));
        }
    }
     
     
  2. You never send a plugin message in your onCommand method. You need to call Player#sendPluginMessage to send the plugin message.
    You're not using plugin messaging correctly either. Plugin messaging is handled asynchronously, meaning that this is improper:
    Code (Java):
    out.writeUTF("GetServers");
    player.sendMessage(message[0]);
    The asynchronous task may not have finished and 'message' may not have been set yet, resulting in an NPE.
     
  3. In other words, first you would have to do player # sendPluginMessage (plugin, "BungeeCord", output.toByteArray)
    and then send the message to the player?
     
  4. Yes, but send the player the message in your onMessageReceived method instead of your onCommand method.
     
  5. Not work.
    CODE:
    Code (Text):

    package com.theunclebarto.mein;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;
    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    public class Command implements CommandExecutor, PluginMessageListener{
       
        private final ClasePrincipal plugin;
        public Command(ClasePrincipal plugin) {
            this.plugin = plugin;
        }
        public String[] message;
        @Override
        public boolean onCommand(CommandSender sender, org.bukkit.command.Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if(args[0].equalsIgnoreCase("message")) {
                this.getServers(player);
            }
            return true;
        }
       
        public void getServers(Player player) {
            try {
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("GetServers");
                player.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());  
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
       
        @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message) {
            if(!channel.equals("BungeeCord")) {
                return;
            }
            ByteArrayDataInput in = ByteStreams.newDataInput(message);
            this.message = in.readUTF().split(", ");
            player.sendMessage(this.message[0]);
        }
    }
     
     
  6. then something's wrong :^)

    "Not work" isn't enough. What's not working? What's happening instead? Is there any error?
     
  7. What happens is that when executing the command, the message is not sent, no errors