Skip to content

Commit

Permalink
feat player metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
a3510377 committed Nov 5, 2023
1 parent 17b0b85 commit 42ece4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/main/java/monkey/info/command/Prometheus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import monkey.info.command.metrics.Metric;
import monkey.info.command.metrics.Mobcaps;
import monkey.info.command.metrics.Player;
import monkey.info.command.metrics.Tick;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -39,8 +40,9 @@ public void run() {

public void start() {
this.stop();
this.registerMetric(new Tick());
this.registerMetric(new Mobcaps());
this.registerMetric(new Player());
this.registerMetric(new Tick());

if (metricUpdateLoop == null) metricUpdateLoop = new Timer("Metric Update Loop");
metricUpdateLoop.scheduleAtFixedRate(new TimerTask() {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/monkey/info/command/metrics/Mobcaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ public Mobcaps() {
public void update(MinecraftServer server) {
// edit from: https://github.com/gnembon/fabric-carpet/blob/7486670918f733aaf6ae3b595290947802024d02/src/main/java/carpet/utils/SpawnReporter.java#L105
for (ServerWorld world : server.getWorlds()) {
RegistryKey<World> dim = world.getRegistryKey();
SpawnHelper.Info lastSpawner = world.getChunkManager().getSpawnInfo();
int chunkcount = SpawnReporter.chunkCounts.getOrDefault(dim, -1);

if (lastSpawner != null) {
RegistryKey<World> dim = world.getRegistryKey();
String worldName = world.getRegistryKey().getValue().toString();
int chunkcount = SpawnReporter.chunkCounts.getOrDefault(dim, -1);

for (SpawnGroup enumcreaturetype : SpawnGroup.values()) {
Object2IntMap<SpawnGroup> dimCounts = lastSpawner.getGroupToCount();
String enumcreature = enumcreaturetype.toString();
String worldName = world.getRegistryKey().getValue().toString();

int cur = dimCounts.getOrDefault(enumcreaturetype, -1);
int max = (int) (chunkcount * ((double) enumcreaturetype.getCapacity() / SpawnReporter.currentMagicNumber()));
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/monkey/info/command/metrics/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package monkey.info.command.metrics;

import carpet.patches.EntityPlayerMPFake;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.World;

import java.util.List;

public class Player extends Metric {
public Player() {
super("players_online", "Online players", "type", "world");
}

@Override
public void update(MinecraftServer server) {
// edit from: https://github.com/gnembon/fabric-carpet/blob/7486670918f733aaf6ae3b595290947802024d02/src/main/java/carpet/utils/SpawnReporter.java#L105
for (ServerWorld world : server.getWorlds()) {
RegistryKey<World> dim = world.getRegistryKey();
List<ServerPlayerEntity> players = world.getPlayers();
String worldName = world.getRegistryKey().getValue().toString();

long fakePlayers = players.stream().filter(player -> player instanceof EntityPlayerMPFake).count();
long realPlayers = players.size() - fakePlayers;

this.getGauge().labelValues("FAKE", worldName).set(fakePlayers);
this.getGauge().labelValues("REAL", worldName).set(realPlayers);
}
}
}

0 comments on commit 42ece4b

Please sign in to comment.