-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
PlayerStatistics.java
84 lines (67 loc) · 2.81 KB
/
PlayerStatistics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package de.sldk.mc.metrics;
import io.prometheus.client.Gauge;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class PlayerStatistics extends PlayerMetric {
private static final Gauge PLAYER_STATS = Gauge.build()
.name(prefix("player_statistic"))
.help("Player statistics")
.labelNames("player_name", "player_uid", "statistic")
.create();
public PlayerStatistics(Plugin plugin) {
super(plugin, PLAYER_STATS);
}
@Override
public void collect(OfflinePlayer player) {
Map<String, Integer> statistics = getStatistics(player.getPlayer());
final String playerNameLabel = getNameOrUid(player);
final String playerUidLabel = getUid(player);
statistics.forEach((stat, value) -> PLAYER_STATS.labels(playerNameLabel, playerUidLabel, stat).set(value));
}
private static Map<String, Integer> getStatistics(Player player) {
if (player == null) {
return Collections.emptyMap();
}
EntityType[] entityTypes = EntityType.values();
Material[] materials = Material.values();
Statistic[] statistics = Statistic.values();
return Arrays.stream(statistics).collect(Collectors.toMap(Enum::name, statistic -> {
if (Statistic.Type.UNTYPED == statistic.getType()) {
return player.getStatistic(statistic);
} else if (Statistic.Type.ENTITY == statistic.getType()) {
return Arrays.stream(entityTypes).map(type -> getSafeStatistic(player, statistic, type))
.filter(Objects::nonNull)
.mapToInt(Integer::intValue).sum();
} else if (Statistic.Type.ITEM == statistic.getType()
|| Statistic.Type.BLOCK == statistic.getType()) {
return Arrays.stream(materials).map(material -> getSafeStatistic(player, statistic, material))
.filter(Objects::nonNull)
.mapToInt(Integer::intValue).sum();
}
return 0;
}));
}
private static Integer getSafeStatistic(Player player, Statistic statistic, Material material) {
try {
return player.getStatistic(statistic, material);
} catch (Exception e) {
return null;
}
}
private static Integer getSafeStatistic(Player player, Statistic statistic, EntityType type) {
try {
return player.getStatistic(statistic, type);
} catch (Exception e) {
return null;
}
}
}