Skip to content

Commit 45eddad

Browse files
committed
Update gamerule handling
* Fixes commandModificationBlockLimit value type * Updates from deprecated Spigot methods * Updates gamerule value conversion
1 parent 490e3d8 commit 45eddad

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

src/main/java/com/laytonsmith/abstraction/MCWorld.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public interface MCWorld extends MCMetadatable {
5353

5454
String[] getGameRules();
5555

56-
String getGameRuleValue(String gameRule);
56+
Object getGameRuleValue(MCGameRule gameRule);
5757

58-
boolean setGameRuleValue(MCGameRule gameRule, String value);
58+
boolean setGameRuleValue(MCGameRule gameRule, Object value);
5959

6060
MCWorldBorder getWorldBorder();
6161

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCWorld.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.bukkit.Chunk;
5656
import org.bukkit.Effect;
5757
import org.bukkit.FireworkEffect;
58+
import org.bukkit.GameRule;
5859
import org.bukkit.HeightMap;
5960
import org.bukkit.Location;
6061
import org.bukkit.Particle;
@@ -181,13 +182,21 @@ public String[] getGameRules() {
181182
}
182183

183184
@Override
184-
public String getGameRuleValue(String gameRule) {
185-
return w.getGameRuleValue(gameRule);
185+
public Object getGameRuleValue(MCGameRule gameRule) {
186+
GameRule gr = GameRule.getByName(gameRule.getRuleName());
187+
if(gr == null) {
188+
return null;
189+
}
190+
return w.getGameRuleValue(gr);
186191
}
187192

188193
@Override
189-
public boolean setGameRuleValue(MCGameRule gameRule, String value) {
190-
return w.setGameRuleValue(gameRule.getRuleName(), value);
194+
public boolean setGameRuleValue(MCGameRule gameRule, Object value) {
195+
GameRule gr = GameRule.getByName(gameRule.getRuleName());
196+
if(gr == null) {
197+
return false;
198+
}
199+
return w.setGameRule(gr, value);
191200
}
192201

193202
@Override

src/main/java/com/laytonsmith/abstraction/enums/MCGameRule.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package com.laytonsmith.abstraction.enums;
22

33
import com.laytonsmith.annotations.MEnum;
4+
import com.laytonsmith.core.ArgumentValidation;
5+
import com.laytonsmith.core.MSLog;
46
import com.laytonsmith.core.constructs.CBoolean;
57
import com.laytonsmith.core.constructs.CInt;
8+
import com.laytonsmith.core.constructs.CString;
9+
import com.laytonsmith.core.constructs.Target;
610
import com.laytonsmith.core.natives.interfaces.Mixed;
711

812
@MEnum("com.commandhelper.GameRule")
913
public enum MCGameRule {
1014
ANNOUNCEADVANCEMENTS("announceAdvancements"),
1115
BLOCKEXPLOSIONDROPDECAY("blockExplosionDropDecay"),
1216
COMMANDBLOCKOUTPUT("commandBlockOutput"),
13-
COMMANDMODIFICATIONBLOCKLIMIT("commandModificationBlockLimit"),
17+
COMMANDMODIFICATIONBLOCKLIMIT("commandModificationBlockLimit", CInt.class),
1418
DISABLEELYTRAMOVEMENTCHECK("disableElytraMovementCheck"),
1519
DISABLEPLAYERMOVEMENTCHECK("disablePlayerMovementCheck"),
1620
DISABLERAIDS("disableRaids"),
@@ -87,7 +91,27 @@ public String getRuleName() {
8791
return this.gameRule;
8892
}
8993

90-
public Class<? extends Mixed> getRuleType() {
91-
return this.ruleType;
94+
public Object convertValue(Mixed value, Target t) {
95+
if(this.ruleType == CBoolean.class) {
96+
return ArgumentValidation.getBooleanish(value, t);
97+
} else if(this.ruleType == CInt.class) {
98+
return ArgumentValidation.getInt(value, t);
99+
}
100+
MSLog.GetLogger().e(MSLog.Tags.RUNTIME, "The gamerule \"" + this.gameRule + "\""
101+
+ " has an invalid type.", t);
102+
return null;
103+
}
104+
105+
public Mixed constructValue(Object value, Target t) {
106+
try {
107+
if(this.ruleType == CBoolean.class) {
108+
return CBoolean.get((Boolean) value);
109+
} else if(this.ruleType == CInt.class) {
110+
return new CInt((Integer) value, t);
111+
}
112+
} catch(ClassCastException ex) {}
113+
MSLog.GetLogger().e(MSLog.Tags.RUNTIME, "The gamerule \"" + this.gameRule + "\""
114+
+ " has an invalid type.", t);
115+
return new CString(value.toString(), t);
92116
}
93117
}

src/main/java/com/laytonsmith/core/functions/World.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.laytonsmith.annotations.api;
2222
import com.laytonsmith.annotations.hide;
2323
import com.laytonsmith.core.ArgumentValidation;
24+
import com.laytonsmith.core.MSLog;
2425
import com.laytonsmith.core.MSVersion;
2526
import com.laytonsmith.core.ObjectGenerator;
2627
import com.laytonsmith.core.Optimizable;
@@ -1811,20 +1812,25 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
18111812
}
18121813
if(args.length == 1) {
18131814
CArray gameRules = CArray.GetAssociativeArray(t);
1814-
for(String gameRule : world.getGameRules()) {
1815-
gameRules.set(new CString(gameRule, t),
1816-
Static.resolveConstruct(world.getGameRuleValue(gameRule), t), t);
1815+
for(String name : world.getGameRules()) {
1816+
try {
1817+
MCGameRule gameRule = MCGameRule.valueOf(name.toUpperCase());
1818+
gameRules.set(name, gameRule.constructValue(world.getGameRuleValue(gameRule), t), t);
1819+
} catch(IllegalArgumentException skip) {
1820+
MSLog.GetLogger().w(MSLog.Tags.RUNTIME, "The server gamerule \"" + name + "\""
1821+
+ " is missing in CommandHelper.", t);
1822+
}
18171823
}
18181824
return gameRules;
18191825
} else {
18201826
try {
18211827
MCGameRule gameRule = MCGameRule.valueOf(args[1].val().toUpperCase());
1822-
String value = world.getGameRuleValue(gameRule.getRuleName());
1823-
if(value.isEmpty()) {
1828+
Object value = world.getGameRuleValue(gameRule);
1829+
if(value == null) {
18241830
throw new CREFormatException("The gamerule \"" + args[1].val()
18251831
+ "\" does not exist in this version.", t);
18261832
}
1827-
return Static.resolveConstruct(value, t);
1833+
return gameRule.constructValue(value, t);
18281834
} catch (IllegalArgumentException exception) {
18291835
throw new CREFormatException("The gamerule \"" + args[1].val() + "\" does not exist.", t);
18301836
}
@@ -1862,9 +1868,9 @@ public Boolean runAsync() {
18621868

18631869
@Override
18641870
public String docs() {
1865-
return "boolean {[world], gameRule, value} Sets the value of the gamerule for the specified world. If world is"
1866-
+ " not given the value is set for all worlds. Returns true if successful. The gameRule can be "
1867-
+ StringUtils.Join(MCGameRule.getGameRules(), ", ", ", or ", " or ") + ".";
1871+
return "boolean {[world], gameRule, value} Sets the value of the gamerule for the specified world."
1872+
+ " If world is not given the value is set for all worlds. Returns true if successful."
1873+
+ " The gameRule can be " + StringUtils.Join(MCGameRule.getGameRules(), ", ", ", or ", " or ") + ".";
18681874
}
18691875

18701876
@Override
@@ -1883,7 +1889,10 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
18831889
throw new CREFormatException("The gamerule \"" + (args.length == 2 ? args[0].val() : args[1].val())
18841890
+ "\" does not exist.", t);
18851891
}
1886-
String value = ArgumentValidation.getObject(args[offset + 1], t, gameRule.getRuleType()).val();
1892+
Object value = gameRule.convertValue(args[offset + 1], t);
1893+
if(value == null) {
1894+
return CBoolean.FALSE;
1895+
}
18871896
if(args.length == 2) {
18881897
for(MCWorld world : Static.getServer().getWorlds()) {
18891898
success = world.setGameRuleValue(gameRule, value);

0 commit comments

Comments
 (0)