top 10?

Discussion in 'Spigot Plugin Development' started by Yazio, Jun 28, 2015.

  1. Code (Text):

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            TreeMap<String, Double> tokens = new TreeMap<String, Double>();
     
            // fill your treemap here from config data
            // so i'm not totally spoonfeeding you, I'll leave this to you ^_^
     
            if (tokens.isEmpty()) {
                // treemap is empty (no data found from config)
                if (!(sender instanceof Player)) {
                    // command sender is not a player (possibly console or command block)
                    sender.sendMessage("No data found.");
                    return true; // end it here in that case.
                }
         
                // command sender is a player, so lets just add them to the treemap.
                // Since the above section didnt add this player from config, I am adding the
                // default value of 0 as it tries again to pull data from the config.
                tokens.put(sender.getName(), getConfig().getDouble("Players." + ((Player) sender).getUniqueId().toString(), 0D));
            }
     
            // now iterate over the treemap and display the results.
            int i = 1; // this is the counter
            for (Iterator<Map.Entry<String, Double>> iter = tokens.entrySet().iterator(); iter.hasNext() && i <= 10; /* no need for 3rd statement here */) {
                Map.Entry<String, Double> entry = iter.next(); // get the entry
                String name = entry.getKey(); // get player name
                Double amount = entry.getValue(); // get amount of tokens
                sender.sendMessage(ChatColor.GRAY + "" + i + ". " + ChatColor.LIGHT_PURPLE + name + " " + amount); // display this entry
                i++; // update the counter.
            }
     
            // done.
            return true;
        }
     
     
    • Winner Winner x 1
  2. @BillyGalbreath so i logged on with a other account and.. it doesn't say the top fx: my account (T0X1C) has 5 tokens.. then i type /tokens top and it says my name with 5 tokens.. then i login with my other account and it says i have 1 token when i type /tokens.. but when i type /tokens top it says only my other account's name with 0 tokens.. (and if i go to my main account then it only says me)

    EDIT: if anybody gets a new tokens amount then it won't update unless i do /rl
     
  3. Do you make sure whenever a player gains or loses a token, the config is updated using
    PHP:
    getConfig().set("path to it", getConfig().getDouble("path to it") <+ or - how much it's been changed>)
    and saved using
    PHP:
    saveConfig();
    Could you give us your code so far and how you used Billy's code template he gave you?
     
  4. i don't quite understand the first part.. but i am saving the config everytime they gain or lose a token..

    code:
    PHP:
    TreeMap<String, Integer> tokens = new TreeMap<String, Integer>();
                        tokens.get(getConfig().get("Players." + p.getUniqueId()));
                     
                        if (tokens.isEmpty()) {
                            if (!(sender instanceof Player)) {
                                sender.sendMessage(prefix + " §7No data found.");
                                return true;
                            }
                            tokens.put(sender.getName(), (int) getConfig().getDouble("Players." + ((Player) sender).getUniqueId().toString(), 0D));
                        }
                        int I = 1;
                        for (Iterator<Entry<String, Integer>> iter = tokens.entrySet().iterator(); iter.hasNext() && I <= 10;) {
                            Entry<String, Integer> entry = iter.next();
                            String name = entry.getKey();
                            Integer amount = entry.getValue();
                            sender.sendMessage(prefix + " §7Top 10");
                            sender.sendMessage("");
                            sender.sendMessage("§7" + I + ". §d" + name + " §7with §b" + amount + " §7token(s)");
                            I++;
                        }
                     
                        return true;
     
  5. Why do you use an Iterator when you can use a perfectly good enhanced for loop? Also, if your TreeMap stores an Integer, you should just use getConfig().getInt() instead of getDouble(). This is just better than casting it to an int when you don't have to.

    Could I see the code where you are saving the config everytime they gain or lose a token?
    Also, btw. prefix + " §7Top 10" will be sent multiple times. You should put it before the for loop.

    You may also want to do debug messages through your code that prints out the values of all your variables now and then so you can see what's actually happening.

    There's much more easy ways to do what you're trying to do. If you still can't figure the problem in your code, I wouldn't mind creating a small class for you :)
     
  6. Not exactly sure why you are bumping. Your question has been answered, and even re-answered in greater detail.

    If you are expecting someone to write you code that will work 100% by copy-pasting it into your project, you're looking in the wrong place.
     
    • Agree Agree x 1
  7. Thought I would leave this here:
    Use a TreeMultimap<Integer, Player> combined with a HashMap<Player, Integer>. The TreeMultimap would do the ordering, whilst the HashMap would tell you which Integer has Player in the TreeMultimap.