mirror of
https://github.com/PaperMC/Paper.git
synced 2024-11-22 07:08:47 +01:00
Improve GameRule API
This commit is contained in:
parent
6d7a438fad
commit
ae614202ba
2 changed files with 203 additions and 0 deletions
103
patches/api/0490-Improve-GameRule-API.patch
Normal file
103
patches/api/0490-Improve-GameRule-API.patch
Normal file
|
@ -0,0 +1,103 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Jake Potrebic <jake.m.potrebic@gmail.com>
|
||||
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<T> 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 <T> the GameRule's type
|
||||
+ * @return the current value
|
||||
+ * @see GameRule#getValue(World)
|
||||
+ */
|
||||
+ <T> @NotNull T getGameRuleValue(@NotNull GameRule<T> rule);
|
||||
+
|
||||
+ /**
|
||||
+ * Set the given {@link GameRule}'s new value.
|
||||
+ *
|
||||
+ * @param rule the GameRule to update
|
||||
+ * @param newValue the new value
|
||||
+ * @param <T> the value type of the GameRule
|
||||
+ * @return true if the value was successfully set
|
||||
+ * @see GameRule#setValue(World, Object)
|
||||
+ */
|
||||
+ <T> boolean setGameRule(@NotNull GameRule<T> 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 {
|
||||
<A extends Keyed, M> io.papermc.paper.registry.tag.@Nullable Tag<A> getTag(io.papermc.paper.registry.tag.@NotNull TagKey<A> tagKey); // Paper - hack to get tags for non-server backed registries
|
||||
|
||||
ItemStack createEmptyStack(); // Paper - proxy ItemStack
|
||||
+
|
||||
+ <T> @NotNull T getGameRuleDefault(@NotNull GameRule<T> 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 <T> the GameRule's type
|
||||
* @return the current value
|
||||
+ * @see GameRule#getValue(World)
|
||||
*/
|
||||
- @Nullable
|
||||
+ @NotNull // Paper - make not null
|
||||
public <T> T getGameRuleValue(@NotNull GameRule<T> rule);
|
||||
|
||||
/**
|
||||
@@ -3520,7 +3521,9 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
|
||||
* @param rule the rule to return a default value for
|
||||
* @param <T> the type of GameRule
|
||||
* @return the default value
|
||||
+ * @deprecated use {@link GameRule#getDefaultValue()} instead
|
||||
*/
|
||||
+ @Deprecated(since = "1.21") // Paper
|
||||
@Nullable
|
||||
public <T> T getGameRuleDefault(@NotNull GameRule<T> rule);
|
||||
|
||||
@@ -3531,6 +3534,7 @@ public interface World extends RegionAccessor, WorldInfo, PluginMessageRecipient
|
||||
* @param newValue the new value
|
||||
* @param <T> the value type of the GameRule
|
||||
* @return true if the value was successfully set
|
||||
+ * @see GameRule#setValue(World, Object)
|
||||
*/
|
||||
public <T> boolean setGameRule(@NotNull GameRule<T> rule, @NotNull T newValue);
|
||||
|
100
patches/server/1059-Improve-GameRule-API.patch
Normal file
100
patches/server/1059-Improve-GameRule-API.patch
Normal file
|
@ -0,0 +1,100 @@
|
|||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Jake Potrebic <jake.m.potrebic@gmail.com>
|
||||
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> T getGameRuleValue(org.bukkit.GameRule<T> 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 <T> boolean setGameRule(org.bukkit.GameRule<T> 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> T getGameRuleValue(GameRule<T> 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> T getGameRuleDefault(GameRule<T> rule) {
|
||||
@@ -2058,24 +2054,9 @@ public class CraftWorld extends CraftRegionAccessor implements World {
|
||||
return this.convert(rule, CraftWorld.getGameRuleDefinitions().get(rule.getName()).createRule());
|
||||
}
|
||||
|
||||
- @Override
|
||||
- public <T> boolean setGameRule(GameRule<T> 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> T convert(GameRule<T> rule, GameRules.Value<?> value) {
|
||||
+ public static <T> T convert(GameRule<T> 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> T getGameRuleDefault(org.bukkit.GameRule<T> 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.
|
||||
* <p>
|
Loading…
Reference in a new issue