Solved Brackets In Player Names

Discussion in 'Plugin Development' started by CodePlaysMinecraft, Jan 11, 2015.

Thread Status:
Not open for further replies.
  1. Sorry if the title is confusing, but I created a plugin that when a player joins it does a basic MOTD, and also tells them the names of the online ops. But, for some reason, when I loops through all online players, check if any of them are ops, I get the list of them, but they have brackets (http://gyazo.com/8bcb21dc5dbe9bfad8205e8dba27697a) around the list. Is there any way I can remove this? I have tried everything that I know to get rid of them, but I couldn't.
    PlayerJoin class:
    Code:
    List<String> ops = new ArrayList<String>();
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (p.isOp()) {
                    String pName = p.getName();
                    ops.add(pName + ", ");
                }
    Any help would be appreciated. :)
     
  2. Offline

    SuperOriginal

    Why are you calling the toString() method on the player's name when it's already a string..

    And you're not even showing the PlayerJoinEvent code.
     
  3. Offline

    WiseHollow

    Looks like you are treating the list as a string. You could do something like this...
    Code:
    String list = "Online Operators: ";
    for (String s : ops)
    {
        list = list + s + " ";
    }
    
    and then display the String list.
     
  4. @SuperOriginal Sorry, I don't know why that extra .toString() was there. Must have been an accident. I'm also not showing the full class because it's a big class. I just thought I'd post the relevant code.

    @WiseHollow I'm treating the list as a string because player names are technically strings. Plus, that was the best way I could do this.
     
    Last edited: Jan 11, 2015
  5. Offline

    mythbusterma

    @CodePlaysMinecraft

    There's no "technicality" about it, player's names are Strings. A List<String>, however, is not.

    Use a StringBuilder, not a List, as a List is not meant for what you're trying to do.
     
  6. @mythbusterma I must admit, I have never used a StringBuilder before. Could you give me some sample code?
     
  7. Offline

    teej107

  8. @teej107 Lol, thank you. I looked over it a bit and this is what I got out of it:
    Code:
    StringBuilder sb = new StringBuilder();
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (p.isOp()) {
                    String pName = p.getName();
                    StringBuilder onlineOps = sb.append(pName + ", ");
                }
    However, when I try to use it in a line of code like "player.sendMessage("Online Staff: " + onlineOps);" I get this error message under onlineOps, "onlineOps cannot be resolved to a variable."
     
  9. Offline

    Skionz

  10. Problem solved! Thank you everyone for replying! For future people having the same (or similar) problem here is the code that I used to solve the problem:
    Code:
    StringBuilder onlineOps = new StringBuilder();
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (p.isOp()) {
                    String pName = p.getName();
                    onlineOps.append(pName + ", ");
                }
    player.sendMessage(ChatColor.WHITE + "Online Staff: "
                        + ChatColor.AQUA + onlineOps);
     
Thread Status:
Not open for further replies.

Share This Page