Implements better online player counter. (#1791)

Current one is based on online player count when bStats sends data to the server.
This one will send data about amount of players who logged in the server.
This commit is contained in:
BONNe 2021-07-06 16:22:44 +03:00 committed by GitHub
parent 19ddd73204
commit 3a1ec0a570
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -1,7 +1,10 @@
package world.bentobox.bentobox;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.AdvancedPie;
@ -29,6 +32,13 @@ public class BStats {
*/
private int islandsCreatedCount = 0;
/**
* Contains the amount of connected players since last data send.
* @since 1.17.1
*/
private final Set<UUID> connectedPlayerSet = new HashSet<>();
BStats(BentoBox plugin) {
this.plugin = plugin;
}
@ -86,6 +96,15 @@ public class BStats {
islandsCreatedCount++;
}
/**
* Adds given UUID to the connected player set.
* @param uuid UUID of a player who logins.
* @since 1.17.1
*/
public void addPlayer(UUID uuid) {
this.connectedPlayerSet.add(uuid);
}
/**
* Sends the enabled addons (except GameModeAddons) of this server.
* @since 1.1
@ -132,7 +151,9 @@ public class BStats {
*/
private void registerPlayersPerServerChart() {
metrics.addCustomChart(new SimplePie("playersPerServer", () -> {
int players = Bukkit.getOnlinePlayers().size();
int players = this.connectedPlayerSet.size();
this.connectedPlayerSet.clear();
if (players <= 0) return "0";
else if (players <= 10) return "1-10";
else if (players <= 30) return "11-30";

View File

@ -102,6 +102,9 @@ public class JoinLeaveListener implements Listener {
plugin.getIslands().getMaxMembers(i, RanksManager.TRUSTED_RANK);
plugin.getIslands().getMaxHomes(i);
});
// Add a player to the bStats cache.
plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID));
}