Bukkit Plugin Messaging

Discussion in 'Spigot Plugin Development' started by AwesomeFishh, May 25, 2018.

  1. Hey guys, I'm trying to get into a bit of cross-server communication, but I don't really understand the plugin-messaging system. My code atm:

    Main.java:
    Code (Text):

    package me.AwesomeFishh.PluginMessageTest;

    import org.bukkit.ChatColor;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

       public void onEnable(){
           this.getServer().getMessenger().registerOutgoingPluginChannel(this, "PluginMessage");
           this.getServer().getMessenger().registerIncomingPluginChannel(this, "PluginMessage", new PluginMessage(this));

           this.getCommand("serverbc").setExecutor(new BroadcastCommand(this));
           this.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[PLUGINMESSAGE] Plugin enabled!");
       }

       public void onDisable(){
           this.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[PLUGINMESSAGE] Plugin disabled!");
       }

    }
     
    PluginMessage.java:
    Code (Text):

    package me.AwesomeFishh.PluginMessageTest;

    import com.google.common.io.ByteArrayDataInput;
    import com.google.common.io.ByteArrayDataOutput;
    import com.google.common.io.ByteStreams;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.messaging.PluginMessageListener;

    public class PluginMessage implements PluginMessageListener {

       Main plugin;

       public PluginMessage(Main plugin) {
           this.plugin = plugin;
       }

       @Override
       public void onPluginMessageReceived(String channel, Player player, byte[] message) {

           System.out.println(channel + " " + player.getName());
           if (!channel.equals("PluginMessage")) return;

           ByteArrayDataInput input = ByteStreams.newDataInput(message);
           String subchannel = input.readUTF();
           if (subchannel.equals("Broadcast")) {
               String bcmessage = input.readUTF();
               plugin.getServer().broadcastMessage(bcmessage);
           }

       }

       public void serverBroadcast(String message, Player player) {
           ByteArrayDataOutput output = ByteStreams.newDataOutput();
           output.writeUTF("Broadcast");
           output.writeUTF(ChatColor.translateAlternateColorCodes('&', message));
           player.sendPluginMessage(plugin, "PluginMessage", output.toByteArray());
       }


    }
     
    BroadcastCommand.java:

    Code (Text):

    package me.AwesomeFishh.PluginMessageTest;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class BroadcastCommand implements CommandExecutor {

       Main plugin;
       PluginMessage pluginMessage;

       public BroadcastCommand(Main plugin){
           this.plugin = plugin;
           this.pluginMessage = new PluginMessage(plugin);

       }

       @Override
       public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {

           if(cmd.getName().equalsIgnoreCase("serverbc")) {
               if(sender instanceof Player){
                   if(args.length == 1){
                       pluginMessage.serverBroadcast(args[0], (Player) sender);
                       sender.sendMessage(ChatColor.GREEN + "Message sent!");
                   }
               }
           }

           return false;
       }
    }
     
    The message to the sender (ChatColor.GREEN + "Message sent") is sent but not the actual broadcast message, the other server doesn't receive anything. What am I doing wrong?
     
  2. This is a beginners mistake, you created a new instance of the object and none of the servers are set in the new object. Try creating a public instance in your main class and grabbing it from there.
     
  3. You mean like in the main class doing this:
    Code (Text):
    public PluginMessage pluginMessage;

    public void onEnable(){
       this.getServer().getMessenger().registerIncomingPluginChannel(this, "PluginMessage", pluginMessage = new PluginMessage(this));
    }
    And then in every class's constructor say
    Code (Text):
    this.pluginMessage = plugin.pluginMessage;
    That doesn't work either :(