From 49e5c012b3ec6199f34a54f4652c0a35943bf8d6 Mon Sep 17 00:00:00 2001 From: Ghoulboy Date: Mon, 7 Dec 2020 01:32:29 +0100 Subject: [PATCH] Adding Gamerule modification to scarpet (#610) --- docs/scarpet/api/Auxiliary.md | 6 ++++-- src/main/java/carpet/script/api/Auxiliary.java | 3 --- src/main/java/carpet/script/utils/SystemInfo.java | 12 ++++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/docs/scarpet/api/Auxiliary.md b/docs/scarpet/api/Auxiliary.md index 10544321..06fa8784 100644 --- a/docs/scarpet/api/Auxiliary.md +++ b/docs/scarpet/api/Auxiliary.md @@ -487,7 +487,9 @@ Available options in the scarpet app space: * `world_path` - full path to the world saves folder * `world_folder` - name of the direct folder in the saves that holds world files * `world_carpet_rules` - returns all Carpet rules in a map form (`rule`->`value`). Note that the values are always returned as strings, so you can't do boolean comparisons directly. Includes rules from extensions with their namespace (`namespace:rule`->`value`). You can later listen to rule changes with the `on_carpet_rule_changes(rule, newValue)` event. - + * `world_gamerules` - returns all gamerules in a map form (`rule`->`value`). Like carpet rules, values are returned as strings, so you can use appropriate value conversions using `bool()` or `number()` to convert them to other values. Gamerules are read-only to discourage app programmers to mess up with the settings intentionally applied by server admins. Isn't that just super annoying when a datapack messes up with your gamerule settings? It is still possible to change them though using `run('gamerule ...`. + + Relevant gameplay related properties * `game_difficulty` - current difficulty of the game: `'peacefu'`, `'easy'`, `'normal'`, or `'hard'` * `game_hardcore` - boolean whether the game is in hardcore mode @@ -517,4 +519,4 @@ Available options in the scarpet app space: * `java_process_cpu_load` - current percentage of CPU used by JVM Scarpet related properties - * `scarpet_version` - returns the version of the carpet your scarpet comes with. \ No newline at end of file + * `scarpet_version` - returns the version of the carpet your scarpet comes with. diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index 73ce0323..367fe2d1 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -35,7 +35,6 @@ import carpet.script.value.ValueConversions; import carpet.utils.Messenger; import com.google.common.collect.ImmutableMap; import net.minecraft.block.BlockState; -import net.minecraft.command.argument.EntityAnchorArgumentType; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.entity.EquipmentSlot; @@ -50,7 +49,6 @@ import net.minecraft.network.packet.s2c.play.TitleS2CPacket; import net.minecraft.network.packet.s2c.play.TitleS2CPacket.Action; import net.minecraft.particle.ParticleEffect; import net.minecraft.server.MinecraftServer; -import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.world.ServerWorld; @@ -64,7 +62,6 @@ import net.minecraft.util.InvalidIdentifierException; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.util.math.EulerAngle; -import net.minecraft.util.math.Vec2f; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.DynamicRegistryManager; import net.minecraft.util.registry.Registry; diff --git a/src/main/java/carpet/script/utils/SystemInfo.java b/src/main/java/carpet/script/utils/SystemInfo.java index 86b13735..e5984e7b 100644 --- a/src/main/java/carpet/script/utils/SystemInfo.java +++ b/src/main/java/carpet/script/utils/SystemInfo.java @@ -13,6 +13,7 @@ import carpet.settings.ParsedRule; import carpet.settings.SettingsManager; import com.sun.management.OperatingSystemMXBean; import net.minecraft.util.WorldSavePath; +import net.minecraft.world.GameRules; import java.lang.management.ManagementFactory; import java.nio.file.Path; @@ -124,6 +125,17 @@ public class SystemInfo { }); return carpetRules; }); + put("world_gamerules", c->{ + Map rules = new HashMap<>(); + final GameRules gameRules = c.s.getWorld().getGameRules(); + GameRules.accept(new GameRules.Visitor() { + @Override + public > void visit(GameRules.Key key, GameRules.Type type) { + rules.put(StringValue.of(key.getName()), StringValue.of(gameRules.get(key).toString())); + } + }); + return MapValue.wrap(rules); + }); put("scarpet_version", c -> StringValue.of(CarpetSettings.carpetVersion)); }};