Solved Plugin Messaging

Discussion in 'BungeeCord Plugin Development' started by MakingTheMagic, Jun 29, 2016.

Thread Status:
Not open for further replies.
  1. Hi guys!

    I am trying to send a plugin message from my spigot server(s) to my BungeeCord server,
    this is my current code for the spigot plugin:

    Code (Text):
          public static void sendMessagePlugin(String channel, String message) {
                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                 DataOutputStream out = new DataOutputStream(stream);
               
                 try {
                    out.writeUTF(channel);
                    out.writeUTF(message);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 
                 
            JavaPlugin.getPlugin(MainClass.class).getServer().sendPluginMessage(JavaPlugin.getPlugin(MainClass.class), "Sent", stream.toByteArray());
                 
                       
          }
    for my bungeecord plugin code:

    Code (Text):
       @EventHandler
            public void onPluginMessage(PluginMessageEvent ev) {
               
              if(!ev.getTag().equals("Sent"))
              {
                  return;
              }
             
             
              if(!(ev.getSender() instanceof Server))
              {
                  return;
              }
             
              ByteArrayInputStream stream = new ByteArrayInputStream(ev.getData());
                 DataInputStream in = new DataInputStream(stream);
                     
                    try {
                   
                   
                    String sub = in.readUTF();
                       String cmd = in.readUTF(); // Command we gave in Bungee part.
                     if (sub.equals("command")) { // As in bungee part we gave the sub-channel name "command", here we're checking it sub-channel really is "command", if it is we do the rest of code      
                         System.out.println("[McDiscovery] Received a command message from Spigot, executing it.");
                         Map<String, ServerInfo> servers = BungeeCord.getInstance().getServers();
                         
                         for (Entry<String, ServerInfo> en : servers.entrySet()) { // Looping through each Server of Bungee.
                             String name = en.getKey();
                             ServerInfo all = BungeeCord.getInstance().getServerInfo(name);
                             sendToServer("command", cmd.toString(), all); // "command" is a sub-channel which will be used to determine the message is sent by this plugin.
                 //send to server sends the command to all the sub-servers
                         
                         }
                       
                     }
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
           
             }
    and what will happen is, depending on the amount of players on the Spigot server it will run how many times in the Bungee console. So, for example if there is 10 players on hub, it would run 10 times, or if there was 3 players on minigames it would run 3 times!

    How do I fix this duplication issue?
    Thanks,
    MTM.

    Any advice is appreciated!
    :D
     
    • Agree Agree x 1
  2. The getServer ().sendPluginMessage() actually sends the message for all players.
    If you only want 1 time. Just get a random player that Is online and send it through that player.
     
  3. Thanks!
     
Thread Status:
Not open for further replies.