-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
GameRuleCommand.java
90 lines (76 loc) · 3.21 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.dWorld;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
public class GameRuleCommand extends AbstractCommand {
// <--[command]
// @Name Gamerule
// @Syntax gamerule [<world>] [<rule>] [<value>]
// @Required 3
// @Short Sets a gamerule on the world.
// @Group world
//
// @Description
// Sets a gamerule on the world. A list of valid gamerules can be found here: http://minecraft.gamepedia.com/Commands#gamerule
// Note: Be careful, gamerules are CASE SENSITIVE.
//
// @Tags
// TODO: Add tags and then document them!
//
// @Usage
// Use to disable fire spreading in world "Adventure".
// - gamerule w@Adventure doFireTick false
//
// @Usage
// Use to avoid mobs from destroying blocks (creepers, endermen...) and picking items up (zombies, skeletons...) in world "Adventure".
// - gamerule w@Adventure mobGriefing false
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
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()) {
Debug.report(scriptEntry, getName(), world.debug() + gamerule.debug() + value.debug());
}
// Execute
if (!world.getWorld().setGameRuleValue(gamerule.asString(), value.asString())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid gamerule!");
}
}
}