Skip to content

Commit 42ece4b

Browse files
committed
feat player metrics
1 parent 17b0b85 commit 42ece4b

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/main/java/monkey/info/command/Prometheus.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.prometheus.metrics.model.registry.PrometheusRegistry;
66
import monkey.info.command.metrics.Metric;
77
import monkey.info.command.metrics.Mobcaps;
8+
import monkey.info.command.metrics.Player;
89
import monkey.info.command.metrics.Tick;
910
import org.jetbrains.annotations.Nullable;
1011

@@ -39,8 +40,9 @@ public void run() {
3940

4041
public void start() {
4142
this.stop();
42-
this.registerMetric(new Tick());
4343
this.registerMetric(new Mobcaps());
44+
this.registerMetric(new Player());
45+
this.registerMetric(new Tick());
4446

4547
if (metricUpdateLoop == null) metricUpdateLoop = new Timer("Metric Update Loop");
4648
metricUpdateLoop.scheduleAtFixedRate(new TimerTask() {

src/main/java/monkey/info/command/metrics/Mobcaps.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ public Mobcaps() {
1818
public void update(MinecraftServer server) {
1919
// edit from: https://github.com/gnembon/fabric-carpet/blob/7486670918f733aaf6ae3b595290947802024d02/src/main/java/carpet/utils/SpawnReporter.java#L105
2020
for (ServerWorld world : server.getWorlds()) {
21-
RegistryKey<World> dim = world.getRegistryKey();
2221
SpawnHelper.Info lastSpawner = world.getChunkManager().getSpawnInfo();
23-
int chunkcount = SpawnReporter.chunkCounts.getOrDefault(dim, -1);
2422

2523
if (lastSpawner != null) {
24+
RegistryKey<World> dim = world.getRegistryKey();
25+
String worldName = world.getRegistryKey().getValue().toString();
26+
int chunkcount = SpawnReporter.chunkCounts.getOrDefault(dim, -1);
27+
2628
for (SpawnGroup enumcreaturetype : SpawnGroup.values()) {
2729
Object2IntMap<SpawnGroup> dimCounts = lastSpawner.getGroupToCount();
2830
String enumcreature = enumcreaturetype.toString();
29-
String worldName = world.getRegistryKey().getValue().toString();
3031

3132
int cur = dimCounts.getOrDefault(enumcreaturetype, -1);
3233
int max = (int) (chunkcount * ((double) enumcreaturetype.getCapacity() / SpawnReporter.currentMagicNumber()));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package monkey.info.command.metrics;
2+
3+
import carpet.patches.EntityPlayerMPFake;
4+
import net.minecraft.server.MinecraftServer;
5+
import net.minecraft.server.network.ServerPlayerEntity;
6+
import net.minecraft.server.world.ServerWorld;
7+
import net.minecraft.util.registry.RegistryKey;
8+
import net.minecraft.world.World;
9+
10+
import java.util.List;
11+
12+
public class Player extends Metric {
13+
public Player() {
14+
super("players_online", "Online players", "type", "world");
15+
}
16+
17+
@Override
18+
public void update(MinecraftServer server) {
19+
// edit from: https://github.com/gnembon/fabric-carpet/blob/7486670918f733aaf6ae3b595290947802024d02/src/main/java/carpet/utils/SpawnReporter.java#L105
20+
for (ServerWorld world : server.getWorlds()) {
21+
RegistryKey<World> dim = world.getRegistryKey();
22+
List<ServerPlayerEntity> players = world.getPlayers();
23+
String worldName = world.getRegistryKey().getValue().toString();
24+
25+
long fakePlayers = players.stream().filter(player -> player instanceof EntityPlayerMPFake).count();
26+
long realPlayers = players.size() - fakePlayers;
27+
28+
this.getGauge().labelValues("FAKE", worldName).set(fakePlayers);
29+
this.getGauge().labelValues("REAL", worldName).set(realPlayers);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)