-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
WeatherCommand.java
111 lines (89 loc) · 4.46 KB
/
WeatherCommand.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
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;
import org.bukkit.WeatherType;
public class WeatherCommand extends AbstractCommand {
private enum Type {GLOBAL, PLAYER}
private enum Value {SUNNY, STORM, THUNDER}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
else if (!scriptEntry.hasObject("value")
&& arg.matchesEnum(Value.values())) {
scriptEntry.addObject("value", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if ((!scriptEntry.hasObject("value"))) {
throw new InvalidArgumentsException("Must specify a value!");
}
// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary
scriptEntry.defaultObject("world",
((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? new dWorld(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getWorld()) : null,
((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? new dWorld(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getWorld()) : null,
dWorld.valueOf("world"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
Value value = Value.valueOf(((Element) scriptEntry.getObject("value"))
.asString().toUpperCase());
dWorld world = (dWorld) scriptEntry.getObject("world");
Type type = scriptEntry.hasObject("type") ?
(Type) scriptEntry.getObject("type") : Type.GLOBAL;
// Report to dB
dB.report(scriptEntry, getName(), aH.debugObj("type", type.name()) +
(type.name().equalsIgnoreCase("player") ?
aH.debugObj("player", ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()) : "") +
(type.name().equalsIgnoreCase("global") ?
aH.debugObj("world", world) : "") +
aH.debugObj("value", value));
switch (value) {
case SUNNY:
if (type.equals(Type.GLOBAL)) {
world.getWorld().setStorm(false);
world.getWorld().setThundering(false);
}
else {
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setPlayerWeather(WeatherType.CLEAR);
}
break;
case STORM:
if (type.equals(Type.GLOBAL)) {
world.getWorld().setStorm(true);
}
else {
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setPlayerWeather(WeatherType.DOWNFALL);
}
break;
case THUNDER:
// Note: setThundering always creates a storm
if (type.equals(Type.GLOBAL)) {
world.getWorld().setThundering(true);
}
else {
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setPlayerWeather(WeatherType.DOWNFALL);
}
break;
}
}
}