fix(stats): use compatible Bukkit registry keys
This commit is contained in:
@@ -12,6 +12,7 @@ As a **server analyst**, I want periodic player location and statistic snapshots
|
|||||||
## Acceptance criteria
|
## Acceptance criteria
|
||||||
|
|
||||||
- [x] Each configured interval captures every online player's world identity, coordinates, orientation, game mode, and biome.
|
- [x] Each configured interval captures every online player's world identity, coordinates, orientation, game mode, and biome.
|
||||||
|
- [x] Registry-backed snapshot values use Bukkit's stable `Keyed` contract so collection remains compatible with the supported Purpur runtime.
|
||||||
- [x] Every Bukkit untyped, block, item, and entity statistic combination accepted by Bukkit is captured, including zero values.
|
- [x] Every Bukkit untyped, block, item, and entity statistic combination accepted by Bukkit is captured, including zero values.
|
||||||
- [x] Invalid statistic subtype combinations are skipped without stopping the snapshot.
|
- [x] Invalid statistic subtype combinations are skipped without stopping the snapshot.
|
||||||
- [x] Statistic reads are spread across ticks according to a configurable operation budget and run through Bukkit scheduling.
|
- [x] Statistic reads are spread across ticks according to a configurable operation budget and run through Bukkit scheduling.
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package games.dmg.spigotevents.stats;
|
||||||
|
|
||||||
|
import org.bukkit.Keyed;
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
|
||||||
|
final class BukkitRegistryKeys {
|
||||||
|
private BukkitRegistryKeys() {}
|
||||||
|
|
||||||
|
static NamespacedKey get(Keyed value) {
|
||||||
|
return value.getKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
static String format(Keyed value, String fallback) {
|
||||||
|
NamespacedKey key = get(value);
|
||||||
|
return key == null ? fallback : key.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,8 +44,7 @@ public final class PlayerSnapshotCollector {
|
|||||||
data.put("pitch", location.getPitch());
|
data.put("pitch", location.getPitch());
|
||||||
data.put("game_mode", player.getGameMode().name().toLowerCase());
|
data.put("game_mode", player.getGameMode().name().toLowerCase());
|
||||||
var biome = location.getBlock().getBiome();
|
var biome = location.getBlock().getBiome();
|
||||||
var biomeKey = biome.getKeyOrNull();
|
data.put("biome", BukkitRegistryKeys.format(biome, biome.toString()));
|
||||||
data.put("biome", biomeKey == null ? biome.toString() : biomeKey.toString());
|
|
||||||
publish("player.location", identity(player), data);
|
publish("player.location", identity(player), data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,9 +156,13 @@ public final class PlayerSnapshotCollector {
|
|||||||
if ((blocksOnly && !material.isBlock()) || (!blocksOnly && !material.isItem())) {
|
if ((blocksOnly && !material.isBlock()) || (!blocksOnly && !material.isItem())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
var materialKey = BukkitRegistryKeys.get(material);
|
||||||
|
if (materialKey == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
target.put(
|
target.put(
|
||||||
statistic.getKey() + "/" + material.getKeyOrThrow(),
|
statistic.getKey() + "/" + materialKey,
|
||||||
currentPlayer.getStatistic(statistic, material));
|
currentPlayer.getStatistic(statistic, material));
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
// Bukkit rejects combinations not represented by the game.
|
// Bukkit rejects combinations not represented by the game.
|
||||||
@@ -180,9 +183,13 @@ public final class PlayerSnapshotCollector {
|
|||||||
while (operations < budget && subtypeIndex < ENTITY_TYPES.length) {
|
while (operations < budget && subtypeIndex < ENTITY_TYPES.length) {
|
||||||
EntityType entityType = ENTITY_TYPES[subtypeIndex++];
|
EntityType entityType = ENTITY_TYPES[subtypeIndex++];
|
||||||
operations++;
|
operations++;
|
||||||
|
var entityTypeKey = BukkitRegistryKeys.get(entityType);
|
||||||
|
if (entityTypeKey == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
target.put(
|
target.put(
|
||||||
statistic.getKey() + "/" + entityType.getKeyOrThrow(),
|
statistic.getKey() + "/" + entityTypeKey,
|
||||||
currentPlayer.getStatistic(statistic, entityType));
|
currentPlayer.getStatistic(statistic, entityType));
|
||||||
} catch (IllegalArgumentException ignored) {
|
} catch (IllegalArgumentException ignored) {
|
||||||
// Bukkit rejects unsupported entity types.
|
// Bukkit rejects unsupported entity types.
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package games.dmg.spigotevents.stats;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import org.bukkit.NamespacedKey;
|
||||||
|
import org.bukkit.Keyed;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class BukkitRegistryKeysTest {
|
||||||
|
@Test
|
||||||
|
void formatsARegisteredKey() {
|
||||||
|
Keyed keyed = () -> NamespacedKey.minecraft("plains");
|
||||||
|
|
||||||
|
assertEquals("minecraft:plains", BukkitRegistryKeys.format(keyed, "fallback"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void reportsAnUnavailableKey() {
|
||||||
|
Keyed keyed = () -> null;
|
||||||
|
|
||||||
|
assertNull(BukkitRegistryKeys.get(keyed));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user