AppleDash / SaneEconomy

Finally, a sane economy plugin for Bukkit.
https://www.spigotmc.org/resources/saneeconomy-simple-but-featureful-economy.26223/
GNU General Public License v3.0
19 stars 24 forks source link

baltop prints out 11 players and ballot <page> doesn't work #24

Closed foosratt closed 7 years ago

foosratt commented 7 years ago

Here is the fix for this in MapUtil.java

You never increment the counter i if it is less than the offset.

You check if newMap.size() > amount I have fixed this to be >= which prints out 10 now instead of 11.

I have tested the code below on our pizzagamers.com test server. I will compile and move the fix I have to pizzagamers.com soon. Next fix needs to be the list output so as to show a continued list number as opposed to always starting at 0

public static <K, V> Map<K, V> takeFromMap(Map<K, V> map, int amount, int offset) { Map<K, V> newMap = new LinkedHashMap<>();

    //System.out.println("Amount: " + amount + " mapsize: " + map.size() + " Offset: "  + offset);
    if (offset > map.size()) {
        System.out.println("Returning newMap");
        return newMap;
    }

    int i = 0;

    for (Map.Entry<K, V> entry : map.entrySet()) {
        //System.out.println("I = " + i);
        if (i < offset) {
            i++;
            continue;
        }

        if (newMap.size() >= amount) {
            break;
        }

        newMap.put(entry.getKey(), entry.getValue());
        i++;
    }

    return newMap;
}
foosratt commented 7 years ago

Update: I have also implemented a fix for the list numbering. It now prints out 1-10 on baltop and then 11-10 on page 2 etc. ThIs fix is implemented in BalanaceTopCommand.java. All the fixes mentioned here and above are compiled and are running on pizzagamers.com if you wish to see them working.

public void onCommand(CommandSender sender, String[] args) throws CommandException { if (args.length > 1) { throw new TooManyArgumentsException(); }

    int offset = 0;

    if (args.length == 1) {
        int page = Integer.parseInt(args[0]);
        if (page >  1) {
            page = page - 1;
            //System.out.println("Page = " + page);
            try {
                offset = 10 * Math.abs(page);
            } catch (NumberFormatException e) {
                MessageUtils.sendMessage(sender, "%s is not a valid number.");
                return;
            }
        }
    }

    Map<OfflinePlayer, Double> topBalances = saneEconomy.getEconomyManager().getTopPlayerBalances(10, offset);
    AtomicInteger index = new AtomicInteger(offset + 1); /* I know it's stupid, but you can't do some_int++ from within the lambda. */

    MessageUtils.sendMessage(sender, "Top %d players (page %s):", topBalances.size(), args.length == 1 ? args[0] : "1");
    topBalances.forEach((player, balance) -> MessageUtils.sendMessage(sender, "[%02d] %s - %s", index.getAndIncrement(), player.getName(), SaneEconomy.getInstance().getEconomyManager().getCurrency().formatAmount(balance)));
}
AppleDash commented 7 years ago

Please submit a pull request, then I will merge it.

foosratt commented 7 years ago

I can push it direct or have to make a fork? Not supper familiar with github.

AppleDash commented 7 years ago

Fork, clone the fork, edit the fork, push to the fork, then open a PR from the fork to the original.

foosratt commented 7 years ago

OK I will work on getting that done this weekend

AppleDash commented 7 years ago

Fixed in 0.9.2 because you took too long :P

foosratt commented 7 years ago

Sorry crazy weekend with the family. Have 2 kids will do that to you :). I hope at least the code snippets I provided helped.