Expand GameRule API

This commit is contained in:
Senmori 2018-08-11 11:24:33 +10:00 committed by md_5
parent 50e6858b72
commit 29e7564874
2 changed files with 96 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot; import org.bukkit.ChunkSnapshot;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
import org.bukkit.Effect; import org.bukkit.Effect;
import org.bukkit.GameRule;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Particle; import org.bukkit.Particle;
import org.bukkit.Sound; import org.bukkit.Sound;
@ -41,7 +42,6 @@ import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.metadata.BlockMetadataStore; import org.bukkit.craftbukkit.metadata.BlockMetadataStore;
import org.bukkit.craftbukkit.potion.CraftPotionUtil; import org.bukkit.craftbukkit.potion.CraftPotionUtil;
import org.bukkit.craftbukkit.util.CraftMagicNumbers; import org.bukkit.craftbukkit.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.util.LongHash;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.minecart.CommandMinecart; import org.bukkit.entity.minecart.CommandMinecart;
@ -1465,9 +1465,48 @@ public class CraftWorld implements World {
} }
public boolean isGameRule(String rule) { public boolean isGameRule(String rule) {
Validate.isTrue(rule != null && !rule.isEmpty(), "Rule cannot be null nor empty");
return GameRules.getGameRules().containsKey(rule); 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 @Override
public WorldBorder getWorldBorder() { public WorldBorder getWorldBorder() {
if (this.worldBorder == null) { if (this.worldBorder == null) {

View File

@ -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"));
}
}