/
WeatherCommand.java
145 lines (125 loc) · 5.5 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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.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.Bukkit;
import org.bukkit.WeatherType;
public class WeatherCommand extends AbstractCommand {
// <--[command]
// @Name Weather
// @Syntax weather [type:{global}/player] [sunny/storm/thunder] (world:<name>)
// @Required 1
// @Short Changes the current weather in the minecraft world.
// @Group world
//
// @Description
// Changes the weather in the specified world.
// You can also set weather for the attached player, where that player will experience personal
// weather that is different from the global weather.
// Logging off will reset personal weather.
//
// @Tags
// <b@biome.downfall_type>
// <p@player.weather>
// <w@world.has_storm>
// <w@world.weather_duration>
// <w@world.thundering>
// <w@world.thunder_duration>
//
// @Usage
// Makes the weather sunny
// - weather sunny
//
// @Usage
// Makes the weather storm in world "cookies"
// - weather storm world:cookies
//
// @Usage
// Make the weather storm for the attached player.
// - weather type:player storm
//
// -->
private enum Type {GLOBAL, PLAYER}
private enum Value {SUNNY, STORM, THUNDER}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
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,
Bukkit.getWorlds().get(0));
}
@Override
public void execute(ScriptEntry scriptEntry) {
// 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
if (scriptEntry.dbCallShouldDebug()) {
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;
}
}
}