Having issues with reading the PlayerList returned by BungeeCord. Please Help!

Discussion in 'BungeeCord Plugin Development' started by GODofCRAFTERS, Apr 14, 2015.

  1. Here's the variable declarations:
    Code (Text):
    public static String server = null;
    public static String[] playerList = null;
    Here's the registering of Outgoing and Incoming Plugin channels in the onEnable method:
    Code (Text):
    Bukkit.getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
            Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", this);
    Here'e the onPluginMessageReceived method:
    Code (Text):
    @Override
        public void onPluginMessageReceived(String channel, Player player, byte[] message){
               ByteArrayDataInput i = ByteStreams.newDataInput(message);
               String subchannel = i.readUTF();
            if (subchannel.equals("PlayerList")) {
                server = i.readUTF();
                playerList = i.readUTF().split(", ");
                player.sendMessage("so far so good");
            }
        }
    Here's where I give the output stream and call the subchannel:
    Code (Text):
    String msg = new String("string");
             
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
             
                out.writeUTF("PlayerList");
                out.writeUTF("ALL");
             
                for(String staff : playerList){
                    if(player.hasPermission("requests.recieve")){
                     
                        try{
                            out.writeUTF("Message");
                            out.writeUTF(staff);
                            out.writeUTF(msg);
                        }catch(Exception e){}
                    }
                }
    It gives me a NullPointerException at the line that contains the "for" loop. Probably because playerList is empty. But I don't know why it's empty. Please help.
     
  2. After you have all the out.writeUTF
    Try adding this code
    Code (Text):

    Player player = null;
    for(Player p: Bukkit.getOnlinePlayers())
    {
    player = p;
    }

      player.sendPluginMessage(this, "BungeeCord", out.toByteArray());
     
     
  3. @ztowne13
    The error is at the first for loop. Tell me if I'm wrong but, what would writing another for loop inside the erroneous one do?
     
  4. The reason I pointed it out is because I assumed that if you never send anything out, you'd never receive anything in to be able to use. Sorry if I dont completely understand I just noticed that that may be causing problems.
     
  5. Did you implement PluginMessageListener?
     
  6. Bump. I really need help on this. I don't know why it's not working. Please help.
     
  7. Can you post all the code to your classes so I can see how everything works together?
     
  8. I see your issue. Currently you have the loop that is the 'out':
    Code (Text):

    for(String staff : playerList){
                    if(player.hasPermission("requests.recieve")){
                     
                        try{
                            out.writeUTF("Message");
                            out.writeUTF(staff);
                            out.writeUTF(msg);
                        }catch(Exception e){}
                    }
                }
     
    The problem with this is you list through every player online
    "for(String staff : playerList){"
    Every single time you loop through, you create a new out.

    Let's say for example we have 3 people on the server: Me (Ztowne13), Notch, and you (GODofCrafters)

    Currently out 'out' is:
    Code (Text):

    out.writeUTF("PlayerList");
    out.writeUTF("ALL");
     
    Then we loop the first time...
    Does ztowne13 have permission requests.recieve (should be spelled receive :p)?
    Yes.
    Run the code:
    Code (Text):

    out.writeUTF("Message");
    out.writeUTF("Ztowne13");
    out.writeUTF(msg);
     
    What does our out look like now...
    Code (Text):

    out.writeUTF("PlayerList");
    out.writeUTF("ALL");
    out.writeUTF("Message");
    out.writeUTF("Ztowne13");
    out.writeUTF(msg);
     
    Then we loop again...
    Does Notch have permission? Yes.
    What does our new 'out' look like?
    Code (Text):

    out.writeUTF("PlayerList");
    out.writeUTF("ALL");
    out.writeUTF("Message");
    out.writeUTF("Ztowne13");
    out.writeUTF(msg);
    out.writeUTF("Message");
    out.writeUTF("Ztowne13");
    out.writeUTF(msg);
     
    Every time you loop you're adding 3 more 'outs' to your ByteStream...
    Here's what you need to do....
    Code (Text):

    String msg = new String("string");
             
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
             
                out.writeUTF("PlayerList");
                out.writeUTF("ALL");

                String playersOnline = "";

                for(String staff : playerList){
                    if(player.hasPermission("requests.recieve")){
                     
                        if(playersOnline.equalsIgnoreCase(""))
                        {
                              playersOnline = staff;
                        }
                        else
                        {
                              playersOnline += ", " + staff;
                        }
                    }
                }

                        out.writeUTF("Message");
                            out.writeUTF(playersOnline);
                            out.writeUTF(msg);
     
    On your in, you take in a list of names such as Ztowne13, Notch, _Jeb, GOD.....

    You don't currently create the list to sort through.

    Also, at least in the code currently, in your loop you never define what the 'playerList' is. So that needs to be fixed.
     
  9. @ztowne13
    public static String[] playerList = null;
    it's my first box of code... I thought the out.writeUTF("PlayerList") would return a string with the following: "ztowne13, Notch, GODofCRAFTERS" But can you send a string with multiple player names as an argument for out.writeUTF("Message")??

    P.S. Thanks about receive :p
     
  10. @GODofCRAFTERS So a few things. I think your concept of the plugin messaging channel is a little off, correct me if I'm wrong.
    Let me explain why.
    Here's is, CURRENTLY, how your code works...
    You need to first start by sending the out... which you do. However there is one issue. You sort though the players using "for(String staff : playerList){"
    The problem with this is that you are trying to sort through playerList, but, as state in your code... String[] playerList = null; It's null, so there's nothing to sort through.
    That would stop your plugin right there because there's no way to sorth through it all.

    You set playerList = i.readUTF().split(", ");
    the problem with this being is you NEVER create a list before you try to sort through a list...
    It's kinda hard to explain since I can't see all of your code.
    If you want, add me on skype: ztowne13. I've had some experience doing the same thing as you. If you don't want to add me please put your code so I can kinda visualize it all together...

    If you do think any of my posts are helpful a thumbs up would also be appreciated :p
     
  11. @ztowne13
    I think I slightly understood what you're saying, as I'm very good in C and C++. And in C there's always the issue with not being able to assign string arrays after initialization. I'm not sure if that's what is happening here but rest aside, all I really want to do is get all the players online from all the connected servers and then send them a message if they have a certain permission. If you could give a full code outline that does that I'd really appreciate it. You can put it up here or add me on skype: MineCraftGoC. Thanks !! :D
     
    #12 GODofCRAFTERS, Apr 15, 2015
    Last edited: Apr 15, 2015
  12. For anyone joining this late, we got in contact on Skype and have figured it out mostly :)
     
    • Like Like x 1