/
GameRuleCommand.java
66 lines (53 loc) · 2.31 KB
/
GameRuleCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
public class GameRuleCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
else if (!scriptEntry.hasObject("gamerule")) {
scriptEntry.addObject("gamerule", arg.asElement());
}
else if (!scriptEntry.hasObject("value")) {
scriptEntry.addObject("value", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("world")) {
throw new InvalidArgumentsException("Must specify a world!");
}
if (!scriptEntry.hasObject("gamerule")) {
throw new InvalidArgumentsException("Must specify a gamerule!");
}
if (!scriptEntry.hasObject("value")) {
throw new InvalidArgumentsException("Must specify a value!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Fetch objects
dWorld world = scriptEntry.getdObject("world");
Element gamerule = scriptEntry.getElement("gamerule");
Element value = scriptEntry.getElement("value");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), world.debug() + gamerule.debug() + value.debug());
}
// Execute
if (!world.getWorld().setGameRuleValue(gamerule.asString(), value.asString())) {
dB.echoError(scriptEntry.getResidingQueue(), "Invalid gamerule!");
}
}
}