summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSenmori <thesenmori@gmail.com>2018-08-11 11:24:33 +1000
committermd_5 <git@md-5.net>2018-08-11 11:25:09 +1000
commit29e75648741b877c06165d82241b6b3a417474f6 (patch)
treefc774019f25e93d6d20ffdfd5f9ab57417f883aa /src
parent50e6858b72343827ddc8ca15a387fa950d459e3a (diff)
downloadcraftbukkit-29e75648741b877c06165d82241b6b3a417474f6.tar
craftbukkit-29e75648741b877c06165d82241b6b3a417474f6.tar.gz
craftbukkit-29e75648741b877c06165d82241b6b3a417474f6.tar.lz
craftbukkit-29e75648741b877c06165d82241b6b3a417474f6.tar.xz
craftbukkit-29e75648741b877c06165d82241b6b3a417474f6.zip
Expand GameRule API
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftWorld.java41
-rw-r--r--src/test/java/org/bukkit/GameRuleTest.java56
2 files changed, 96 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 9b295cdc..e4b88cb7 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -22,6 +22,7 @@ import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Difficulty;
import org.bukkit.Effect;
+import org.bukkit.GameRule;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.Sound;
@@ -41,7 +42,6 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.metadata.BlockMetadataStore;
import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.craftbukkit.util.CraftMagicNumbers;
-import org.bukkit.craftbukkit.util.LongHash;
import org.bukkit.entity.*;
import org.bukkit.entity.Entity;
import org.bukkit.entity.minecart.CommandMinecart;
@@ -1465,10 +1465,49 @@ public class CraftWorld implements World {
}
public boolean isGameRule(String rule) {
+ Validate.isTrue(rule != null && !rule.isEmpty(), "Rule cannot be null nor empty");
return GameRules.getGameRules().containsKey(rule);
}
@Override
+ public <T> T getGameRuleValue(GameRule<T> rule) {
+ Validate.notNull(rule, "GameRule cannot be null");
+ return convert(rule, getHandle().getGameRules().get(rule.getName()));
+ }
+
+ @Override
+ public <T> T getGameRuleDefault(GameRule<T> rule) {
+ Validate.notNull(rule, "GameRule cannot be null");
+ return convert(rule, GameRules.getGameRules().get(rule.getName()).a());
+ }
+
+ @Override
+ public <T> boolean setGameRule(GameRule<T> rule, T newValue) {
+ Validate.notNull(rule, "GameRule cannot be null");
+ Validate.notNull(newValue, "GameRule value cannot be null");
+
+ if (!isGameRule(rule.getName())) return false;
+
+ getHandle().getGameRules().set(rule.getName(), newValue.toString(), getHandle().getMinecraftServer());
+ return true;
+ }
+
+ private <T> T convert(GameRule<T> rule, GameRules.GameRuleValue value) {
+ if (value == null) {
+ return null;
+ }
+
+ switch (value.e()) {
+ case BOOLEAN_VALUE:
+ return rule.getType().cast(value.b());
+ case NUMERICAL_VALUE:
+ return rule.getType().cast(value.c());
+ default:
+ throw new IllegalArgumentException("Invalid GameRule type (" + value.e() + ") for GameRule " + rule.getName());
+ }
+ }
+
+ @Override
public WorldBorder getWorldBorder() {
if (this.worldBorder == null) {
this.worldBorder = new CraftWorldBorder(this);
diff --git a/src/test/java/org/bukkit/GameRuleTest.java b/src/test/java/org/bukkit/GameRuleTest.java
new file mode 100644
index 00000000..883f5081
--- /dev/null
+++ b/src/test/java/org/bukkit/GameRuleTest.java
@@ -0,0 +1,56 @@
+package org.bukkit;
+
+import java.util.Map;
+import java.util.TreeMap;
+import net.minecraft.server.GameRules;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class GameRuleTest {
+
+ @Test
+ public void testBukkitRules() {
+ GameRule<?>[] rules = GameRule.values();
+
+ for (GameRule<?> rule : rules) {
+ GameRule<?> registeredRule = GameRule.getByName(rule.getName());
+ Assert.assertNotNull("Null GameRule", registeredRule);
+ Assert.assertEquals("Invalid GameRule equality", rule, registeredRule);
+ }
+ }
+
+ @Test
+ public void testMinecraftRules() {
+ TreeMap<String, GameRules.b> minecraftRules = GameRules.getGameRules();
+
+ for (Map.Entry<String, GameRules.b> entry : minecraftRules.entrySet()) {
+ GameRule<?> bukkitRule = GameRule.getByName(entry.getKey());
+
+ Assert.assertNotNull(bukkitRule);
+ Assert.assertEquals("Invalid GameRule Name", bukkitRule.getName(), entry.getKey());
+ }
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void nullGameRuleName() {
+ GameRule.getByName(null);
+ }
+
+ @Test
+ public void emptyGameRuleName() {
+ Assert.assertNull(GameRule.getByName(""));
+ }
+
+ @Test
+ public void incorrectGameRuleName() {
+ Assert.assertNull(GameRule.getByName("doAnnounceAdvancements"));
+ Assert.assertNull(GameRule.getByName("sendCommandBlockFeedback"));
+ }
+
+ @Test
+ public void invalidCasing() {
+ Assert.assertNull(GameRule.getByName("CommandBlockOutput"));
+ Assert.assertNull(GameRule.getByName("spAwnRadius"));
+ Assert.assertNull(GameRule.getByName("rand0mTickSpeEd"));
+ }
+}