How to code a list. Need help.

Discussion in 'Plugin Development' started by Reptar_, Jul 19, 2012.

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

    Reptar_

    So I'm trying to code part of this plugin so when a player uses /online, they get a player count by group. Use this for example of how I want it:
    Code:
    ----------------------------
    Players: (10/35) Online
    Admins: (1) Online
    Moderators: (3) Online
    Owners: (0) Online
    ----------------------------
    What coding do I use to make this happen. I know how to show which player is in which group by:
    Code:
    if(player.hasPermission("BetterList.Admin"){
        //player is Admin
    } else if(player.hasPermission("BetterList.Moderator"){
        //player is Moderator
    } else if(player.hasPermission("BetterList.Owner"){
        //player is Owner
    }
    So far I have this:
    Code:
    package me.Reptar.BetterList;
     
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BetterList extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static BetterList plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + " Version" + pdfFile.getVersion() + " enabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player player = (Player) sender;
            if(commandLabel.equalsIgnoreCase("online")){       
                player.getPlayerListName();
            }
            return false;
        }
    }
    But the only problem I'm having is to make it display the way I want. This is my first plugin and I sorta know some Java, so I'm challenging myself right at the start. Please help.
     
  2. Offline

    bajsko

    Create a new list for the admins, mod etc... And check if the player(s) is online. ;)
     
  3. Offline

    Reptar_

    How do I do that? :(
     
  4. Offline

    bajsko

    I'm not home right now, wen I come home, I can post a code snippet.
     
  5. Offline

    Reptar_

    Thanks! I'll be waiting.
     
  6. Offline

    kamikazi3728

    well create a list of the admins and mods in a config file etc
    ok?
    so it would be...

    admin: 1, 2, anotherplayer
    mod: ssomeguy, dude, etc


    and you retrieve the lists each as a different String[]
    so for the first one, you would get the amount of players online, then you would create another mothod that returns an int, in that method you would check each player online and their names in accordance to the admins and mods in the config file. and if it matches a name, add one to the variables adminnumber and modnumber. and once it is done executing, then you simply display the statistics returned from each of the two methods along with the number of players online. :)

    sorry if that was a bit difficult to read, but i typed it as fast as i could
     
  7. Offline

    ZeusAllMighty11

    if player hasgroup admin.group
    player.put admins

    same for others

    bukkit get online players to get all players


    on the ascii menu you made, you would do total admins minus number offline to get online same with others
    player.sendMessage("") to send messages
     
  8. Offline

    Benni1000

    You could do it like this:
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("online")) {
                Player[] players = Bukkit.getOnlinePlayers();
                int admins = 0,owners = 0,mods = 0,users = 0;
                for (int i = 0; i < players.length; i++) {
                    if (players[i].hasPermission("BetterList.Admin")) {
                        admins++;
                    } else if (players[i].hasPermission("BetterList.Moderator")) {
                        mods++;
                    } else if (players[i].hasPermission("BetterList.Owner")) {
                        owners++;
                    }
                    else {
                        users++;
                    }
                }
                sender.sendMessage("----------------------------");
                sender.sendMessage("Players ("+""+users+"/"+Bukkit.getServer().getMaxPlayers()+") Online");
                sender.sendMessage("Admins: ("+admins+") Online");
                sender.sendMessage("Moderators: ("+mods+") Online");
                sender.sendMessage("Owners: ("+owners+") Online");
                sender.sendMessage("----------------------------");
            }
            return true;
        }
     
  9. Offline

    Reptar_

    @benni100
    Thanks.

    Also, how would I change the colors of the whole message the player gets when they use the command? Do I just put the &#?
     
  10. Offline

    Benni1000

    No you put: "§colorcode" to the message.
    For example:
    Code:
    sender.sendMessage("§4----------------------------");
     
  11. Offline

    VeryBIgCorp

    Or use ChatColor.
     
  12. Offline

    McLuke500

    I made a plugin like this for a request on forums and there was a issue that im sure you might want to fix in yours.

    Basically Owners Mostly have all permissions, Admins have all Mod permissions.
    Therefore The Owner has the permissions BetterList.Admin, BetterList.Moderator, BetterList.Owner
    Admin has BetterList.Admin and BetterList.Moderator

    Therefore it will repeat the numbers in the groups.

    Fix:
    if (players.hasPermission("BetterList.Admin") && (!players.hasPermission("BetterList.Owner")) {
    admins++;
    } else if (players.hasPermission("BetterList.Moderator") && (!players.hasPermission("BetterList.Admin")) {

    mods++;
    } else if (players.hasPermission("BetterList.Owner")) {
    owners++;
    }




    So it will only work if they have the right one
     
  13. Offline

    Reptar_

    McLuke500
    I just negated the permissions for the groups.
    So Owner had:
    permissions:
    - BetterList.Owner
    - - BetterList.Admin
    - - Better.List.Moderator
    And same for the Admins and Mods.
    Easier than fixing the code.
     
  14. Offline

    McLuke500

    Good of it works but the person I made it for said it didn't work for him
    http://forums.bukkit.org/threads/req-formatted-compasslist.82747/#post-1201490

    Anyway if negating works then great, I thought the person was doing it wrong anyway. :D
     
  15. Offline

    Reptar_

Thread Status:
Not open for further replies.

Share This Page