Staff List thingy.

Discussion in 'Plugin Development' started by DomThePotato, Apr 18, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    DomThePotato

    Hello, so I am wanting to make a plugin that gives online staff. And I am not sure where to start. I have implemented Vault thinking that could help me but I'm not too sure anymore.

    Code:
    //Staff: Helper, Mentor, Moderator.
    //Vanish: All vanished staff
    //Mangement: Developer, Admin, Management, Manager, Owner.
    
           
                for(Player all  : Bukkit.getOnlinePlayers()){
                    List<String> staff = new ArrayList();
                    List<String> vanish = new ArrayList();
                    List<String> manage = new ArrayList();
    Is a bit of what I have attempted the other bit is trash. This is how I want it to be formatted

    Code:
           
                p.sendMessage("§e-------------------------------------------");
                p.sendMessage("§aStaff online now:");
               //Staff p.sendMessage("     §e[§aFactions§e] §3Example123");
                //Vanish p.sendMessage("     §e[§aCreative§e] §7Example1234");
                //Mangement p.sendMessage("     §e[§aHub§e] §cExample12345");
                p.sendMessage("§e-------------------------------------------");
    Sorry if this a bit hard to read and also quite vague but ask me if you need anymore info.

    Thanks

    ~DomThePotato :D
     
  2. @DomThePotato Loop through all online players and check if they're op, or have a specific permission.
     
    Tyler Christensen and mine-care like this.
  3. Offline

    mine-care

    Why are the lists implemented in the loop that will only run once per player? You may want to yse a map instead :p
    The concept i am thinking of is:
    1. a map holding player names as key and an enum with their rank as staff (if they are fixed) or even a string (if ranks are passed in i.e. from the config.
    2. loop through all online players, and based on their permission add them in the map
    3. present the contents of the map with their values with any fomat you want :p

    Ofc. there are many other ways to implement it without a map and so on i just picked one.
     
    CodePlaysMinecraft likes this.
  4. Offline

    Zombie_Striker

    If "Staff " Means op
    • Loop through all players online
    • if player isOP()
    • Send message with the name to players.
    If "Staff" Means having perms
    • Loop through all players
    • if player has perms
    • Send messsage
    If "Staff" means a player specified in a file/config/map/Array
    • Loop through all players
    • If the file/config ect. contains player
    • send message
     
  5. Offline

    DomThePotato

    @CodePlaysMinecraft @mine-care @Zombie_Striker Hello so what I have done is this:

    Code:
     for(Player all  : Bukkit.getOnlinePlayers()){
                   
                     p.sendMessage(Main.pr+"§4Please note: This command has not yet been finished.");
                        p.sendMessage("§e-------------------------------------------");
                        p.sendMessage("§aStaff online now:");
                        if(all.hasPermission(new Permissions().lstaff)){
                            p.sendMessage("     §e[§a"+all.getWorld().getName()+"§e] §3"+all.getName());
                        }
                       
                        p.sendMessage("     §e[§aCreative§e] §7Example1234");
                        if(all.hasPermission(new Permissions().lmanage)){
                            p.sendMessage("     §e[§a"+all.getWorld().getName()+"§e] §c"+all.getName());
                        }
                        p.sendMessage("§e-------------------------------------------");
                }
    This almosts works just it lists a new msg for each player so if 3 staff were on it would sent you the msg 3 times. Sorry if I didn't follow what some of you said. But I took a small bit from each of them.

    ~DomThePotato :D
     
  6. @DomThePotato I suggest you use a String Builder instead of an Array List.
     
  7. Offline

    DomThePotato

    There is no array lists there. And even so how would you do a String Builder?
     
  8. Offline

    mythbusterma

    @DomThePotato

    Why are you creating a new instance of "Permissions" each time? Make the fields you're going after public static final fields.

    Also, it's very clear why you're sending the message more than once, so make it not send the message more than once. It's not difficult.
     
  9. @DomThePotato
    You have lists right here. Anyway, this is a proper way to use a StringBuilder:
    Code:
    StringBuilder onlineOps = new StringBuilder(); // Create a new StringBuilder.
    
    for (Player p : Bukkit.getOnlinePlayers()) {     // Loop through all online players.
                if (p.hasPermission("permission.name")) { // Check if they have correct permissions
                    String pName = p.getName(); // Get the player's name.
                    onlineOps.append(pName + ", "); // Add them to the StringBuilder with a comma after their name. Format: "CodePlaysMinecraft, otherPlayer, otherPlayer2, "
                }
            }
     
  10. Offline

    DomThePotato

    Okay thank you. But what if I wanted to have more than one list but not have it so one player shows up more than once.

    Also I would prefer it as

    [WorldName1] Staff1
    [WorldName2] Staff2

    Thanks C:
     
  11. @DomThePotato Get all players in a world, check if they have the permission, add them to the StringBuilder
     
  12. Offline

    DomThePotato

    So that is what I did and that apparently didn't work.

    Code:
               
                    StringBuilder Staff = new StringBuilder();
    for(Player all  : Bukkit.getOnlinePlayers()){
                        if(all.hasPermission(new Permissions().lstaff)){
                             String pName = all.getName();
                             String pWorld = all.getWorld().getName();
                             Staff.append("     §e[§a"+pWorld+"§e] §3"+pName+""
                                     + "");
    
                        }
    Also do I need to sent the player Staff or should I do it automatically do it?

    Sorry if I sound annoying :/

    ~DomThePotato C:


    --------------Edit----------------------
    Nevermind. I did some research on StringBuilders and it worked! Thanks alot everyone! :D.

    Although how would you make it check whether someone appears twice?
     
    Last edited by a moderator: Apr 18, 2015
  13. @DomThePotato Why would you need to check if a player is in 2 worlds at the same time?
     
  14. Offline

    DomThePotato

    No not the same world. If a person appears once on one list and then again on the other list (list as in the StringBuilders)
     
  15. @DomThePotato Check if the string builder contains the players name already. If so, remove the players name from the rest of the string builders.
     
  16. Offline

    DomThePotato

    @CodePlaysMinecraft Okay so I have done everything, it all works and it's great. Although the command is a bit laggy. For example if you do the command initially it will just send the player this:
    Code:
    p.sendMessage("§e-------------------------------------------");
                        p.sendMessage("§aStaff online now:");
    Then a couple seconds later it will show this:
    Code:
                 for(Player all  : Bukkit.getOnlinePlayers()){
                     String pName = all.getName();
                     String pWorld = all.getWorld().getName();
                        if(all.hasPermission(new Permissions().lstaff)&&!Main.ess.getVanishedPlayers().contains(all)){
                             Staff.append("     §e[§a"+pWorld+"§e] §3"+pName);
                        }else
                                if(all.hasPermission(new Permissions().lmanage)&&!Main.ess.getVanishedPlayers().contains(all)){    
                                     Manage.append("     §e[§a"+pWorld+"§e] §c"+pName);
                                }
                                else{
                            p.sendMessage(Main.pr+"There is currently no staff online.");
                        }
                   
    
                }
                 p.sendMessage(Staff.toString());
                 p.sendMessage(Manage.toString());
                    p.sendMessage("§e-------------------------------------------");
    I know it's not waiting it's just a bit slow. But can anyone recommend a way to make it a bit for efficient, also I've had 1 report that when you do it, it lags the server a bit.

    Thanks :D

    ~DomThePotato c:
     
  17. That is not the proper way to use a StringBuilder. You're still concatenating Strings when you do pName + ", " :)


    @DomThePotato It's clear from your code you're fairly new. Please learn the basics of Java before trying to make Bukkit plugins, otherwise you tun into problems like this. Bukkit, and Minecraft itself, run on Java, so you can't make plugins before learning it. I recommend the Oracle Java tutorials, or a Java book.
     
  18. @AdamQpzm And how does that make it wrong? I'm adding a string to a string.
     
  19. @CodePlaysMinecraft The reason we tend to use StringBuilder is that by concatenating Strings, you're actually creating new Strings internally, one for each concatenation option. This is especially prominent in loops. It is therefore more efficient to use a StringBuilder.

    What you're doing, however, is using the worst of both worlds - you have the efficiency of a manual concatenation, but the simplicity of a StringBuilder :p So you're much better off with something like this:

    Code:
    onlineOps.append(pName).append(", ");
    Also, this method would result in a tailing ", ".
     
    CodePlaysMinecraft likes this.
  20. Offline

    DomThePotato

    I'm not exactly new, although I jump to extreme before really learning what I should. And I have made several plugins although I just have problems and are new to some aspects (Such as this) as I got into Bukkit too quickly. And yes I see what you mean, but I prefer to learn practically and make something 'productive', I know that isn't really the right attitude but that is just how I roll.
    ------------------------------------------------------------------------------------
    Either way, does anyone know how to make the command less laggier?
    Thanks :)

    ~DomThePotato c:
     
Thread Status:
Not open for further replies.

Share This Page