From ae614202ba4619b9a4553891f484d4939e080f07 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Sat, 1 Jan 2022 15:57:10 -0800 Subject: [PATCH] Improve GameRule API --- patches/api/0490-Improve-GameRule-API.patch | 103 ++++++++++++++++++ .../server/1059-Improve-GameRule-API.patch | 100 +++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 patches/api/0490-Improve-GameRule-API.patch create mode 100644 patches/server/1059-Improve-GameRule-API.patch diff --git a/patches/api/0490-Improve-GameRule-API.patch b/patches/api/0490-Improve-GameRule-API.patch new file mode 100644 index 0000000000..30cad62a7b --- /dev/null +++ b/patches/api/0490-Improve-GameRule-API.patch @@ -0,0 +1,103 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +Date: Sat, 1 Jan 2022 15:54:09 -0800 +Subject: [PATCH] Improve GameRule API + + +diff --git a/src/main/java/org/bukkit/GameRule.java b/src/main/java/org/bukkit/GameRule.java +index 4482e8f2c617c2f51b2b53762e775d118002363a..684c546479f0103c538b7af5d612fe53839c771a 100644 +--- a/src/main/java/org/bukkit/GameRule.java ++++ b/src/main/java/org/bukkit/GameRule.java +@@ -357,6 +357,16 @@ public final class GameRule implements net.kyori.adventure.translation.Transl + } + + // Paper start ++ /** ++ * Get the default value this {@link GameRule}. ++ * ++ * @return the default value ++ */ ++ @SuppressWarnings("deprecation") // using unsafe as bridge ++ public @NotNull T getDefaultValue() { ++ return Bukkit.getUnsafe().getGameRuleDefault(this); ++ } ++ + @Override + public @NotNull String translationKey() { + return "gamerule." + this.name; +diff --git a/src/main/java/org/bukkit/RegionAccessor.java b/src/main/java/org/bukkit/RegionAccessor.java +index 05ecf3cb38ff42c8b52405d900197e6bf47dd1e6..a19adcde803b6ae8da223ac8451b3d0a8c21cc5e 100644 +--- a/src/main/java/org/bukkit/RegionAccessor.java ++++ b/src/main/java/org/bukkit/RegionAccessor.java +@@ -539,4 +539,27 @@ public interface RegionAccessor extends Keyed, io.papermc.paper.world.flag.Featu + */ + boolean hasCollisionsIn(@NotNull org.bukkit.util.BoundingBox boundingBox); + // Paper end ++ ++ // Paper start - add gamerule methods here ++ /** ++ * Get the current value for a given {@link GameRule}. ++ * ++ * @param rule the GameRule to check ++ * @param the GameRule's type ++ * @return the current value ++ * @see GameRule#getValue(World) ++ */ ++ @NotNull T getGameRuleValue(@NotNull GameRule rule); ++ ++ /** ++ * Set the given {@link GameRule}'s new value. ++ * ++ * @param rule the GameRule to update ++ * @param newValue the new value ++ * @param the value type of the GameRule ++ * @return true if the value was successfully set ++ * @see GameRule#setValue(World, Object) ++ */ ++ boolean setGameRule(@NotNull GameRule rule, @NotNull T newValue); ++ // Paper end - add gamerule methods here + } +diff --git a/src/main/java/org/bukkit/UnsafeValues.java b/src/main/java/org/bukkit/UnsafeValues.java +index 10c87b7c19ed3eab28fdce5f225df3767292ee0a..961174b4a87b797b8dceffbe9cb91d8ce8758276 100644 +--- a/src/main/java/org/bukkit/UnsafeValues.java ++++ b/src/main/java/org/bukkit/UnsafeValues.java +@@ -316,4 +316,6 @@ public interface UnsafeValues { + io.papermc.paper.registry.tag.@Nullable Tag getTag(io.papermc.paper.registry.tag.@NotNull TagKey tagKey); // Paper - hack to get tags for non-server backed registries + + ItemStack createEmptyStack(); // Paper - proxy ItemStack ++ ++ @NotNull T getGameRuleDefault(@NotNull GameRule rule); // Paper - get default game rule value + } +diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java +index d8a23aa0d898ca3360757721e38ddb97387f7d21..4ffcba883ae71ce7b4079130a3d7c07da1a7cd60 100644 +--- a/src/main/java/org/bukkit/World.java ++++ b/src/main/java/org/bukkit/World.java +@@ -3509,8 +3509,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient + * @param rule the GameRule to check + * @param the GameRule's type + * @return the current value ++ * @see GameRule#getValue(World) + */ +- @Nullable ++ @NotNull // Paper - make not null + public T getGameRuleValue(@NotNull GameRule rule); + + /** +@@ -3520,7 +3521,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient + * @param rule the rule to return a default value for + * @param the type of GameRule + * @return the default value ++ * @deprecated use {@link GameRule#getDefaultValue()} instead + */ ++ @Deprecated(since = "1.21") // Paper + @Nullable + public T getGameRuleDefault(@NotNull GameRule rule); + +@@ -3531,6 +3534,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient + * @param newValue the new value + * @param the value type of the GameRule + * @return true if the value was successfully set ++ * @see GameRule#setValue(World, Object) + */ + public boolean setGameRule(@NotNull GameRule rule, @NotNull T newValue); + diff --git a/patches/server/1059-Improve-GameRule-API.patch b/patches/server/1059-Improve-GameRule-API.patch new file mode 100644 index 0000000000..e021a7c2d5 --- /dev/null +++ b/patches/server/1059-Improve-GameRule-API.patch @@ -0,0 +1,100 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Jake Potrebic +Date: Sat, 1 Jan 2022 15:53:52 -0800 +Subject: [PATCH] Improve GameRule API + + +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java b/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java +index c9ecec5da937bc5458f69736b68ff6ae50aa5ebc..b852ead129951eaeddd47c1e90b10f2aa5a5e58b 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftRegionAccessor.java +@@ -565,4 +565,27 @@ public abstract class CraftRegionAccessor implements RegionAccessor { + return io.papermc.paper.world.flag.PaperFeatureFlagProviderImpl.fromNms(this.getHandle().enabledFeatures()); + } + // Paper end - feature flag API ++ ++ // Paper start - move gamerule methods here ++ @Override ++ public T getGameRuleValue(org.bukkit.GameRule rule) { ++ Preconditions.checkArgument(rule != null, "GameRule cannot be null"); ++ return java.util.Objects.requireNonNull(CraftWorld.convert(rule, this.getHandle().getLevelData().getGameRules().getRule(CraftWorld.getGameRulesNMS().get(rule.getName()))), rule + " is an unrecognized game rule"); // Paper - make not null ++ } ++ ++ @Override ++ public boolean setGameRule(org.bukkit.GameRule rule, T newValue) { ++ Preconditions.checkArgument(rule != null, "GameRule cannot be null"); ++ Preconditions.checkArgument(newValue != null, "GameRule value cannot be null"); ++ ++ // Paper start - Add WorldGameRuleChangeEvent ++ io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(this.getHandle().getLevel().getWorld(), null, rule, String.valueOf(newValue)); ++ if (!event.callEvent()) return false; ++ // Paper end - Add WorldGameRuleChangeEvent ++ net.minecraft.world.level.GameRules.Value handle = this.getHandle().getLevelData().getGameRules().getRule(CraftWorld.getGameRulesNMS().get(rule.getName())); ++ handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent ++ handle.onChanged(this.getHandle().getLevel()); ++ return true; ++ } ++ // Paper end - move gamerule methods here + } +diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +index cd3381d8225e0322b3e285b3c4aa0a1265a91cc0..c1bdaaec30cdca995580c3d7efde60f56f526d71 100644 +--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java ++++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java +@@ -2046,11 +2046,7 @@ public class CraftWorld extends CraftRegionAccessor implements World { + return CraftWorld.getGameRulesNMS().containsKey(rule); + } + +- @Override +- public T getGameRuleValue(GameRule rule) { +- Preconditions.checkArgument(rule != null, "GameRule cannot be null"); +- return this.convert(rule, this.getHandle().getGameRules().getRule(CraftWorld.getGameRulesNMS().get(rule.getName()))); +- } ++ // Paper - move to CraftRegionAccessor + + @Override + public T getGameRuleDefault(GameRule rule) { +@@ -2058,24 +2054,9 @@ public class CraftWorld extends CraftRegionAccessor implements World { + return this.convert(rule, CraftWorld.getGameRuleDefinitions().get(rule.getName()).createRule()); + } + +- @Override +- public boolean setGameRule(GameRule rule, T newValue) { +- Preconditions.checkArgument(rule != null, "GameRule cannot be null"); +- Preconditions.checkArgument(newValue != null, "GameRule value cannot be null"); +- +- if (!this.isGameRule(rule.getName())) return false; +- +- // Paper start - Add WorldGameRuleChangeEvent +- io.papermc.paper.event.world.WorldGameRuleChangeEvent event = new io.papermc.paper.event.world.WorldGameRuleChangeEvent(this, null, rule, String.valueOf(newValue)); +- if (!event.callEvent()) return false; +- // Paper end - Add WorldGameRuleChangeEvent +- GameRules.Value handle = this.getHandle().getGameRules().getRule(CraftWorld.getGameRulesNMS().get(rule.getName())); +- handle.deserialize(event.getValue()); // Paper - Add WorldGameRuleChangeEvent +- handle.onChanged(this.getHandle()); +- return true; +- } ++ // Paper - move to CraftRegionAccessor + +- private T convert(GameRule rule, GameRules.Value value) { ++ public static T convert(GameRule rule, GameRules.Value value) { // Paper - make static + if (value == null) { + return null; + } +diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +index f78744b6d6075f584d9a88612661854f3f04aed1..5c98de39f9e55567d480ba4c01710fa3e6424ba4 100644 +--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java ++++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +@@ -719,6 +719,13 @@ public final class CraftMagicNumbers implements UnsafeValues { + } + // Paper end - proxy ItemStack + ++ // Paper start - game rule default ++ @Override ++ public T getGameRuleDefault(org.bukkit.GameRule rule) { ++ return java.util.Objects.requireNonNull(org.bukkit.craftbukkit.CraftWorld.convert(rule, org.bukkit.craftbukkit.CraftWorld.getGameRuleDefinitions().get(rule.getName()).createRule()), rule + " is an unrecognized game rule"); ++ } ++ // Paper end - game rule default ++ + /** + * This helper class represents the different NBT Tags. + *