List Plugin

Discussion in 'Spigot Plugin Development' started by minecrft, May 29, 2016.

  1. Hello all!
    Code (Text):
    StringBuilder sb = new StringBuilder();
                ArrayList<String> all = new ArrayList<String>();
                for (Player p1 : Bukkit.getOnlinePlayers()) {
                    String name = p1.getName();
                    all.add(name);
                }
                    StringBuilder msg = sb.append(all);
                    p.sendMessage(ChatColor.LIGHT_PURPLE + msg.toString());
               
    I'm working on a fairly simple list plugin that will be integrated into other things. I've created the list with all the players and it works fine, except it puts '[' ']'s around the players because its an arraylist, any other methods to do this?
     
  2. Code (Text):
    String str = msg.toString();
    str = str.substring(1, str.length - 1);
     
  3. Code (Text):
    String names = Joiner.on(", ").join(Bukkit.getOnlinePlayers().stream().map(HumanEntity::getName).collect(Collectors.toList()));
    That's my way doing it... you could also just append all names to the StringBuilder with a space between all names :)
     
  4. Ahh, thanks mate! Sometimes I just have the biggest brain farts haha
    @Xilixir
     
  5. @1byte thanks for the idea! Looks more efficient!
     
  6. Hello again,

    I have done some with my code, and it does seem to be work (picture posted). Well, it works just the '[]'s are all screwed up. Ignore the bad color coding :p

    Code (Text):
    StringBuilder sb = new StringBuilder();
                ArrayList<String> members = new ArrayList<String>();
                ArrayList<String> donors = new ArrayList<String>();
                ArrayList<String> staff = new ArrayList<String>();
                for (Player p1 : Bukkit.getOnlinePlayers()) {
                    if (!(p1.isOp())) {
                    if (p1.hasPermission("list.member")) {
                        members.add(p1.getName());
                    } else if (p1.hasPermission("list.donor")) {
                        donors.add(p1.getName());
                    } else if (p1.hasPermission("list.staff")) {
                        staff.add(p1.getName());
                    }
                    } else if (p1.isOp()) {
                        staff.add(p1.getName());
                    }
                }
                String mem = sb.append(members).toString();
                if (mem.length() > 0) {
                mem = mem.substring(1, mem.length() - 1);
                }
             
             
                String don = sb.append(donors).toString();
                if (don.length() > 0) {
                don = don.substring(1, don.length() - 1);
                }
                String sta = sb.append(staff).toString();
                if (sta.length() > 0) {
                sta = sta.substring(1, sta.length() - 1);
                }
                p.sendMessage(color("&2Staff Online &8(&7" + staff.size() + "&8) &b> " + sta));
                p.sendMessage(color("&aDonators Online &8(&7" + donors.size() + "&8) &b> " + don));
                p.sendMessage(color("&aMembers Online &8(&7" + members.size() + "&8) &b> " + mem));
    (I'm op rn)
    [​IMG]
     
  7. what's with the pic ? its dark
     
  8. @MadMaxCookie haha i don't really know mate. I can take another if you need?
     
  9. I changed my code to
    Code (Text):
    StringBuilder sb = new StringBuilder();
                ArrayList<String> members = new ArrayList<String>();
                ArrayList<String> donors = new ArrayList<String>();
                ArrayList<String> staff = new ArrayList<String>();
                for (Player p1 : Bukkit.getOnlinePlayers()) {
                    if (!(p1.isOp())) {
                    if (p1.hasPermission("list.member")) {
                        members.add(p1.getName());
                    } else if (p1.hasPermission("list.donor")) {
                        donors.add(p1.getName());
                    } else if (p1.hasPermission("list.staff")) {
                        staff.add(p1.getName());
                    }
                    } else if (p1.isOp()) {
                        staff.add(p1.getName());
                    }
                }
                StringBuilder mem = sb.append(members);
                String mems = mem.toString();
                if (members.size() > 0) {
                mems = mem.substring(1, mem.length() - 1);
                } else {
                    mems = mem.substring(1, 1);
                }
                StringBuilder don = sb.append(donors);
                String dons = don.toString();
                if (donors.size() > 0) {
                dons = don.substring(1, don.length() - 1);
                } else {
                    dons = don.substring(1, 1);
                }
                StringBuilder sta = sb.append(staff);
                String stas = mem.toString();
                if (staff.size() > 0) {
                stas = sta.substring(1, sta.length() - 1);
                } else {
                    stas = sta.substring(1, 1);
                }
                p.sendMessage(color("&2Staff Online &8(&7" + staff.size() + "&8) &b> " + stas));
                p.sendMessage(color("&aDonators Online &8(&7" + donors.size() + "&8) &b> " + dons));
                p.sendMessage(color("&aMembers Online &8(&7" + members.size() + "&8) &b> " + mems));
    It looks like this now -
    [​IMG]
    (look at the very last one down)
     
  10. Okay so I have done some testing, and found out it only bugs out when I am op. Any help? @Xilixir