summaryrefslogtreecommitdiffstats
path: root/src/main/java
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/main/java
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/main/java')
-rw-r--r--src/main/java/org/bukkit/craftbukkit/CraftWorld.java41
1 files changed, 40 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);