/
MapCommand.java
175 lines (148 loc) · 7.81 KB
/
MapCommand.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package net.aufdemrand.denizen.scripts.commands.item;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.scripts.containers.core.MapScriptContainer;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.maps.DenizenMapManager;
import net.aufdemrand.denizen.utilities.maps.DenizenMapRenderer;
import net.aufdemrand.denizen.utilities.maps.MapAnimatedImage;
import net.aufdemrand.denizen.utilities.maps.MapImage;
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.objects.dScript;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import java.util.List;
public class MapCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("new")
&& arg.matchesPrefix("new")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("new", arg.asType(dWorld.class));
}
else if (!scriptEntry.hasObject("reset-loc")
&& arg.matchesPrefix("r", "reset")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("reset-loc", arg.asType(dLocation.class));
scriptEntry.addObject("reset", new Element(true));
}
else if (!scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new Element(true));
}
else if (!scriptEntry.hasObject("image")
&& arg.matchesPrefix("i", "img", "image")) {
scriptEntry.addObject("image", arg.asElement());
}
else if (!scriptEntry.hasObject("resize")
&& arg.matches("resize")) {
scriptEntry.addObject("resize", new Element(true));
}
else if (!scriptEntry.hasObject("width")
&& arg.matchesPrefix("width")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("width", arg.asElement());
}
else if (!scriptEntry.hasObject("height")
&& arg.matchesPrefix("height")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("height", arg.asElement());
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesPrefix("s", "script")
&& arg.matchesArgumentType(dScript.class)) {
scriptEntry.addObject("script", arg.asType(dScript.class));
}
else if (!scriptEntry.hasObject("x-value")
&& arg.matchesPrefix("x")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("x-value", arg.asElement());
}
else if (!scriptEntry.hasObject("y-value")
&& arg.matchesPrefix("y")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)) {
scriptEntry.addObject("y-value", arg.asElement());
}
else if (!scriptEntry.hasObject("map-id")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("map-id", arg.asElement());
}
}
if (!scriptEntry.hasObject("map-id") && !scriptEntry.hasObject("new"))
throw new InvalidArgumentsException("Must specify a map ID or create a new map!");
if (!scriptEntry.hasObject("reset")
&& !scriptEntry.hasObject("reset-loc")
&& !scriptEntry.hasObject("image")
&& !scriptEntry.hasObject("script"))
throw new InvalidArgumentsException("Must specify a valid action to perform!");
scriptEntry.defaultObject("reset", new Element(false)).defaultObject("resize", new Element(false))
.defaultObject("x-value", new Element(0)).defaultObject("y-value", new Element(0));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element id = scriptEntry.getElement("map-id");
dWorld create = scriptEntry.getdObject("new");
Element reset = scriptEntry.getElement("reset");
dLocation resetLoc = scriptEntry.getdObject("reset-loc");
Element image = scriptEntry.getElement("image");
dScript script = scriptEntry.getdObject("script");
Element resize = scriptEntry.getElement("resize");
Element width = scriptEntry.getElement("width");
Element height = scriptEntry.getElement("height");
Element x = scriptEntry.getElement("x-value");
Element y = scriptEntry.getElement("y-value");
dB.report(scriptEntry, getName(), (id != null ? id.debug() : "") + (create != null ? create.debug() : "")
+ reset.debug() + (resetLoc != null ? resetLoc.debug() : "") + (image != null ? image.debug() : "")
+ (script != null ? script.debug() : "") + resize.debug() + (width != null ? width.debug() : "")
+ (height != null ? height.debug() : "") + x.debug() + y.debug());
MapView map = null;
if (create != null) {
map = Bukkit.getServer().createMap(create.getWorld());
scriptEntry.addObject("created_map", new Element(map.getId()));
}
else if (id != null) {
map = Bukkit.getServer().getMap((short) id.asInt());
if (map == null)
throw new CommandExecutionException("No map found for ID '" + id.asInt() + "'!");
}
else {
throw new CommandExecutionException("The map command failed somehow! Report this to a developer!");
}
if (reset.asBoolean()) {
List<MapRenderer> oldRenderers = DenizenMapManager.removeDenizenRenderers(map);
for (MapRenderer renderer : oldRenderers) {
map.addRenderer(renderer);
}
if (resetLoc != null) {
map.setCenterX(resetLoc.getBlockX());
map.setCenterZ(resetLoc.getBlockZ());
map.setWorld(resetLoc.getWorld());
}
}
else if (script != null) {
DenizenMapManager.removeDenizenRenderers(map);
((MapScriptContainer) script.getContainer()).applyTo(map);
}
else {
DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
if (image != null) {
int wide = width != null ? width.asInt() : resize.asBoolean() ? 128 : 0;
int high = height != null ? height.asInt() : resize.asBoolean() ? 128 : 0;
if (CoreUtilities.toLowerCase(image.asString()).endsWith(".gif"))
dmr.addObject(new MapAnimatedImage(x.asString(), y.asString(), "true", false, image.asString(),
wide, high));
else
dmr.addObject(new MapImage(x.asString(), y.asString(), "true", false, image.asString(),
wide, high));
}
}
}
}