From ac3666129e668cf6716449ba02aa1b365150620c Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 20 Jul 2024 20:06:54 +0100 Subject: [PATCH] Include gamerule settings in metadata --- .../spark/bukkit/BukkitWorldInfoProvider.java | 18 ++++++++ .../world/AsyncWorldInfoProvider.java | 8 ++++ .../platform/world/WorldInfoProvider.java | 25 +++++++++++ .../world/WorldStatisticsProvider.java | 12 ++++- spark-common/src/main/proto/spark/spark.proto | 6 +++ .../spark/fabric/FabricWorldInfoProvider.java | 44 ++++++++++++++++++- .../spark/forge/ForgeWorldInfoProvider.java | 44 ++++++++++++++++++- .../neoforge/NeoForgeWorldInfoProvider.java | 44 ++++++++++++++++++- .../lucko/spark/paper/PaperSparkPlugin.java | 1 - .../spark/paper/PaperWorldInfoProvider.java | 18 ++++++++ .../sponge/Sponge7WorldInfoProvider.java | 12 +++++ .../sponge/Sponge8WorldInfoProvider.java | 13 ++++++ 12 files changed, 237 insertions(+), 8 deletions(-) diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java index 8d2ea513..51cd17ce 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitWorldInfoProvider.java @@ -24,6 +24,7 @@ import me.lucko.spark.common.platform.world.CountMap; import me.lucko.spark.common.platform.world.WorldInfoProvider; import org.bukkit.Chunk; +import org.bukkit.GameRule; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.block.BlockState; @@ -32,6 +33,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class BukkitWorldInfoProvider implements WorldInfoProvider { private static final boolean SUPPORTS_PAPER_COUNT_METHODS; @@ -110,6 +112,22 @@ public ChunksResult pollChunks() { return data; } + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (World world : this.server.getWorlds()) { + for (String gameRule : world.getGameRules()) { + GameRule ruleObj = GameRule.getByName(gameRule); + if (ruleObj == null) { + continue; + } + Object value = world.getGameRuleValue(ruleObj); + data.put(world.getName(), gameRule, Objects.toString(value)); + } + } + return data; + } + static final class BukkitChunkInfo extends AbstractChunkInfo { private final CountMap entityCounts; diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/world/AsyncWorldInfoProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/world/AsyncWorldInfoProvider.java index 82cddeff..0f48b2e1 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/world/AsyncWorldInfoProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/world/AsyncWorldInfoProvider.java @@ -80,6 +80,10 @@ public CompletableFuture>> return async(WorldInfoProvider::pollChunks); } + public CompletableFuture pollGameRules() { + return async(WorldInfoProvider::pollGameRules); + } + public WorldInfoProvider.CountsResult getCounts() { return get(pollCounts()); } @@ -87,4 +91,8 @@ public WorldInfoProvider.CountsResult getCounts() { public WorldInfoProvider.ChunksResult> getChunks() { return get(pollChunks()); } + + public WorldInfoProvider.GameRulesResult getGameRules() { + return get(pollGameRules()); + } } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldInfoProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldInfoProvider.java index 7fb581db..ece35b9e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldInfoProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldInfoProvider.java @@ -39,6 +39,11 @@ public CountsResult pollCounts() { public ChunksResult> pollChunks() { return null; } + + @Override + public GameRulesResult pollGameRules() { + return null; + } }; /** @@ -55,6 +60,13 @@ public ChunksResult> pollChunks() { */ ChunksResult> pollChunks(); + /** + * Polls for game rules. + * + * @return the game rules + */ + GameRulesResult pollGameRules(); + default boolean mustCallSync() { return true; } @@ -101,4 +113,17 @@ public int chunks() { } } + final class GameRulesResult { + // game rule --> world name --> value + private final Map> rules = new HashMap<>(); + + public void put(String worldName, String gameRuleName, String value) { + this.rules.computeIfAbsent(gameRuleName, k -> new HashMap<>()).put(worldName, value); + } + + public Map> getRules() { + return this.rules; + } + } + } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldStatisticsProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldStatisticsProvider.java index 7e63222c..1b41f689 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldStatisticsProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/world/WorldStatisticsProvider.java @@ -38,7 +38,7 @@ public WorldStatisticsProvider(AsyncWorldInfoProvider provider) { } public WorldStatistics getWorldStatistics() { - WorldInfoProvider.ChunksResult> result = provider.getChunks(); + WorldInfoProvider.ChunksResult> result = this.provider.getChunks(); if (result == null) { return null; } @@ -70,6 +70,16 @@ public WorldStatistics getWorldStatistics() { stats.setTotalEntities(combinedTotal.get()); combined.asMap().forEach((key, value) -> stats.putEntityCounts(key, value.get())); + WorldInfoProvider.GameRulesResult gameRules = this.provider.getGameRules(); + if (gameRules != null) { + gameRules.getRules().forEach((ruleName, worldValues) -> { + WorldStatistics.GameRule.Builder builder = WorldStatistics.GameRule.newBuilder(); + builder.setName(ruleName); + worldValues.forEach(builder::putWorldValues); + stats.addGameRules(builder.build()); + }); + } + return stats.build(); } diff --git a/spark-common/src/main/proto/spark/spark.proto b/spark-common/src/main/proto/spark/spark.proto index 53750d72..0165dab5 100644 --- a/spark-common/src/main/proto/spark/spark.proto +++ b/spark-common/src/main/proto/spark/spark.proto @@ -140,6 +140,7 @@ message WorldStatistics { int32 total_entities = 1; map entity_counts = 2; repeated World worlds = 3; + repeated GameRule game_rules = 4; message World { string name = 1; @@ -158,6 +159,11 @@ message WorldStatistics { int32 total_entities = 3; map entity_counts = 4; } + + message GameRule { + string name = 1; + map world_values = 2; + } } message WindowStatistics { diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java index 926e8a36..84c306fc 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricWorldInfoProvider.java @@ -37,6 +37,7 @@ import net.minecraft.server.world.ServerEntityManager; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.GameRules; import net.minecraft.world.entity.ClientEntityManager; import net.minecraft.world.entity.EntityIndex; import net.minecraft.world.entity.EntityTrackingSection; @@ -101,6 +102,23 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (ServerWorld world : this.server.getWorlds()) { + String worldName = world.getRegistryKey().getValue().getPath(); + GameRules gameRules = world.getGameRules(); + GameRules.accept(new GameRules.Visitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.get(key).serialize(); + data.put(worldName, key.getName(), value); + } + }); + } + return data; + } } public static final class Client extends FabricWorldInfoProvider { @@ -128,13 +146,13 @@ public CountsResult pollCounts() { @Override public ChunksResult pollChunks() { - ChunksResult data = new ChunksResult<>(); - ClientWorld world = this.client.world; if (world == null) { return null; } + ChunksResult data = new ChunksResult<>(); + ClientEntityManager entityManager = ((ClientWorldAccessor) world).getEntityManager(); SectionedEntityCache cache = ((ClientEntityManagerAccessor) entityManager).getCache(); @@ -143,6 +161,28 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + ClientWorld world = this.client.world; + if (world == null) { + return null; + } + + GameRulesResult data = new GameRulesResult(); + + String worldName = world.getRegistryKey().getValue().getPath(); + GameRules gameRules = world.getGameRules(); + GameRules.accept(new GameRules.Visitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.get(key).serialize(); + data.put(worldName, key.getName(), value); + } + }); + + return data; + } } static final class FabricChunkInfo extends AbstractChunkInfo> { diff --git a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java index e10a0c87..537998fb 100644 --- a/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java +++ b/spark-forge/src/main/java/me/lucko/spark/forge/ForgeWorldInfoProvider.java @@ -32,6 +32,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.GameRules; import net.minecraft.world.level.entity.EntityLookup; import net.minecraft.world.level.entity.EntitySection; import net.minecraft.world.level.entity.EntitySectionStorage; @@ -97,6 +98,23 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (ServerLevel level : this.server.getAllLevels()) { + String levelName = level.dimension().location().getPath(); + GameRules gameRules = level.getGameRules(); + GameRules.visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.getRule(key).serialize(); + data.put(levelName, key.getId(), value); + } + }); + } + return data; + } } public static final class Client extends ForgeWorldInfoProvider { @@ -124,13 +142,13 @@ public CountsResult pollCounts() { @Override public ChunksResult pollChunks() { - ChunksResult data = new ChunksResult<>(); - ClientLevel level = this.client.level; if (level == null) { return null; } + ChunksResult data = new ChunksResult<>(); + TransientEntitySectionManager entityManager = level.entityStorage; EntitySectionStorage cache = entityManager.sectionStorage; @@ -139,6 +157,28 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + ClientLevel level = this.client.level; + if (level == null) { + return null; + } + + GameRulesResult data = new GameRulesResult(); + + String levelName = level.dimension().location().getPath(); + GameRules gameRules = level.getGameRules(); + GameRules.visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.getRule(key).serialize(); + data.put(levelName, key.getId(), value); + } + }); + + return data; + } } static final class ForgeChunkInfo extends AbstractChunkInfo> { diff --git a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java index ef76646c..b1f06467 100644 --- a/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java +++ b/spark-neoforge/src/main/java/me/lucko/spark/neoforge/NeoForgeWorldInfoProvider.java @@ -32,6 +32,7 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.GameRules; import net.minecraft.world.level.entity.EntityLookup; import net.minecraft.world.level.entity.EntitySection; import net.minecraft.world.level.entity.EntitySectionStorage; @@ -97,6 +98,23 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (ServerLevel level : this.server.getAllLevels()) { + String levelName = level.dimension().location().getPath(); + GameRules gameRules = level.getGameRules(); + GameRules.visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.getRule(key).serialize(); + data.put(levelName, key.getId(), value); + } + }); + } + return data; + } } public static final class Client extends NeoForgeWorldInfoProvider { @@ -124,13 +142,13 @@ public CountsResult pollCounts() { @Override public ChunksResult pollChunks() { - ChunksResult data = new ChunksResult<>(); - ClientLevel level = this.client.level; if (level == null) { return null; } + ChunksResult data = new ChunksResult<>(); + TransientEntitySectionManager entityManager = level.entityStorage; EntitySectionStorage cache = entityManager.sectionStorage; @@ -139,6 +157,28 @@ public ChunksResult pollChunks() { return data; } + + @Override + public GameRulesResult pollGameRules() { + ClientLevel level = this.client.level; + if (level == null) { + return null; + } + + GameRulesResult data = new GameRulesResult(); + + String levelName = level.dimension().location().getPath(); + GameRules gameRules = level.getGameRules(); + GameRules.visitGameRuleTypes(new GameRules.GameRuleTypeVisitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + String value = gameRules.getRule(key).serialize(); + data.put(levelName, key.getId(), value); + } + }); + + return data; + } } public static final class ForgeChunkInfo extends AbstractChunkInfo> { diff --git a/spark-paper/src/main/java/me/lucko/spark/paper/PaperSparkPlugin.java b/spark-paper/src/main/java/me/lucko/spark/paper/PaperSparkPlugin.java index eb595ccd..eb82f8e6 100644 --- a/spark-paper/src/main/java/me/lucko/spark/paper/PaperSparkPlugin.java +++ b/spark-paper/src/main/java/me/lucko/spark/paper/PaperSparkPlugin.java @@ -39,7 +39,6 @@ import org.bukkit.Server; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.ServicePriority; import java.nio.file.Path; import java.util.Arrays; diff --git a/spark-paper/src/main/java/me/lucko/spark/paper/PaperWorldInfoProvider.java b/spark-paper/src/main/java/me/lucko/spark/paper/PaperWorldInfoProvider.java index 29ab1ef4..e6f6e2df 100644 --- a/spark-paper/src/main/java/me/lucko/spark/paper/PaperWorldInfoProvider.java +++ b/spark-paper/src/main/java/me/lucko/spark/paper/PaperWorldInfoProvider.java @@ -24,6 +24,7 @@ import me.lucko.spark.common.platform.world.CountMap; import me.lucko.spark.common.platform.world.WorldInfoProvider; import org.bukkit.Chunk; +import org.bukkit.GameRule; import org.bukkit.Server; import org.bukkit.World; import org.bukkit.entity.Entity; @@ -31,6 +32,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class PaperWorldInfoProvider implements WorldInfoProvider { private final Server server; @@ -75,6 +77,22 @@ public ChunksResult pollChunks() { return data; } + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (World world : this.server.getWorlds()) { + for (String gameRule : world.getGameRules()) { + GameRule ruleObj = GameRule.getByName(gameRule); + if (ruleObj == null) { + continue; + } + Object value = world.getGameRuleValue(ruleObj); + data.put(world.getName(), gameRule, Objects.toString(value)); + } + } + return data; + } + static final class PaperChunkInfo extends AbstractChunkInfo { private final CountMap entityCounts; diff --git a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java index 8c2e412f..a80cb795 100644 --- a/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java +++ b/spark-sponge7/src/main/java/me/lucko/spark/sponge/Sponge7WorldInfoProvider.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class Sponge7WorldInfoProvider implements WorldInfoProvider { private final Server server; @@ -76,6 +77,17 @@ public ChunksResult pollChunks() { return data; } + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (World world : this.server.getWorlds()) { + for (Map.Entry entry : world.getGameRules().entrySet()) { + data.put(world.getName(), entry.getKey(), entry.getValue()); + } + } + return data; + } + static final class Sponge7ChunkInfo extends AbstractChunkInfo { private final CountMap entityCounts; diff --git a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java index fadd629a..9e9cde0a 100644 --- a/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java +++ b/spark-sponge8/src/main/java/me/lucko/spark/sponge/Sponge8WorldInfoProvider.java @@ -30,11 +30,13 @@ import org.spongepowered.api.entity.EntityType; import org.spongepowered.api.entity.EntityTypes; import org.spongepowered.api.world.chunk.WorldChunk; +import org.spongepowered.api.world.gamerule.GameRule; import org.spongepowered.api.world.server.ServerWorld; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class Sponge8WorldInfoProvider implements WorldInfoProvider { private final Server server; @@ -77,6 +79,17 @@ public ChunksResult pollChunks() { return data; } + @Override + public GameRulesResult pollGameRules() { + GameRulesResult data = new GameRulesResult(); + for (ServerWorld world : this.server.worldManager().worlds()) { + for (Map.Entry, ?> entry : world.properties().gameRules().entrySet()) { + data.put(world.key().value(), entry.getKey().name(), entry.getValue().toString()); + } + } + return data; + } + static final class Sponge7ChunkInfo extends AbstractChunkInfo> { private final CountMap> entityCounts;