How can I get the average player balance?

Discussion in 'Spigot Plugin Development' started by EpicBlue11, Jun 13, 2018.

  1. Hey, so I'm working on an addition to my server's economy plugin and I basically need to figure out what the average player balance is on my server (I use essentialsX for my economy). Naturally, I should take the total balance of the server and divide it by the amount of people who have joined my server, but vault api doesn't give me an option to get the total balance or the total amount of people who've joined. So what I did is I made player loops that work perfectly fine on my test server, but since I have over 10,000 people who've joined my public server, there were tens of thousands of lines of code being run every time I called an update:

    Code (Text):
        public static int getTotalPlayers() {
            return Bukkit.getOfflinePlayers().length;
        public static double getServerTotal() {
            double output = 0;
            for(OfflinePlayer player : Bukkit.getOfflinePlayers()) {
                output = output + Main.getEconomy().getBalance(player);
            return output;
        public static double getRatio() {
            double output = 0;
            if((getServerTotal()/getTotalPlayers())/25000 < 0.6) {
                output = 0.6;
            }else {
                output = (getServerTotal()/getTotalPlayers())/25000;
            return output;
    So I need to find an alternative way of doing this that is much more optimized for my server.

    Thanks for any help!
  2. short1der

    short1der Previously shyguy224

    Assuming you are using Vault:
    Code (Text):
    public static double getServerAverage() {
        double amount = 0;

        for (String s : Main.getEconomy().getBanks()) {
            amount += Main.getEconomy().getBalance(s);

        return (amount / Main.getEconomy().getBanks().size());
  3. Unfortunately, that doesn't work, the "getBanks" method returns empty every time. Thanks tho :p
  4. Can't test that right now, but isn't it possible to run that methode async? I mean.. it's not interacting with players directly, just reading the balance. Maybe that works.
  5. MiniDigger


    You can use essentials UserMap class to load all users into memory and then check their balance.

    ideally you wouldn't recalc the balance every time. only calc it once (async) and then adjust it when the balance of users changes. you know the total number of users and the old avg balance, so the new avg balance is oldAvg + balanceChange/totalNumberOfUsers.
    Thats way you don't do the expensive operation every time and instead just do many small operations whenever smth happens.
  6. Given that player to player transactions will not impact the average then an alternative is to track when memory is added/removed from the economy. As you are not doing this from day 1 you would still need a way to find what the total amount of money is in the economy though and use it as your starting point.

Share This Page