how to work with PluginMessageReceived?

Discussion in 'BungeeCord Plugin Development' started by Dark4ty, Jun 26, 2015.

  1. Hey guys, I want to make a new Bukkit/BungeeCord plugin but I got some issues.
    When I want to send a new BungeeMessage (with my own channel and subchannel) it doesn't work at all (no Errors).
    But if I send with the Channel "BungeeCord" it does work perfect.
    I already tried to register my new channel, still not working.

    Code (Text):
        @Override
        public void onEnable() {
           
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
           
            this.getServer().getMessenger().registerOutgoingPluginChannel(this, "MyChannel");
            this.getServer().getMessenger().registerIncomingPluginChannel(this, "MyChannel", this);
           
        }
          @Override
          public void onPluginMessageReceived(String channel, Player player, byte[] message) {
         
                ByteArrayDataInput in = ByteStreams.newDataInput(message);
               
                String subChannel = in.readUTF();
               
                System.out.println("Channel : "     + channel);
                System.out.println("SubChannel : "     + subChannel);
               
          }
    Code (Text):
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
       
            if(commandLabel.equalsIgnoreCase("ssp")){

                ByteArrayDataOutput out = ByteStreams.newDataOutput();

                out.writeUTF("ExampleSub");
               
                ((Player) sender).sendPluginMessage(plugin, "MyChannel", out.toByteArray());      
               
                return true;
            }
           
           
           
            return false;
       
        }
     
  2. That's because bungeecord only searches for a channel with name "BungeeCord". You can however create custom subchannels for bungee to forward custom data. Take a look at https://www.spigotmc.org/wiki/bukkit-bungee-plugin-messaging-channel/#forward . Here's an example:
    Code (Text):
    //Sending
    ByteArrayDataOutput data = ByteStreams.newDataOutput();
    data.writeUTF("Forward"); //Plugin channel
    data.writeUTF("ALL"); //The server to send to, specified by the server name, ONLINE for online servers, or ALL for all servers
    data.writeUTF("MyChannel"); //Your subchannel's name, to detect if it's for you
    data.writeUTF("ExampleSub"); //Write some random data

    //Recieving
    ByteArrayDataInput data = ByteStreams.newDataInput(message);
    if(channel.equalsIgnoreCase("BungeeCord")) {
      String subchannel = data.readUTF();
      if(subchannel.equalsIgnoreCase("Forward")) {
        String mychannel = data.readUTF();
        if(mychannel.equals("MyChannel")) {
          String word = data.readUTF();
          System.out.println("Got a word! " + word);
        }
      }
    }
     
    #2 megamichiel, Jun 28, 2015
    Last edited: Jun 28, 2015