/
ViewerCommand.java
259 lines (199 loc) · 9.27 KB
/
ViewerCommand.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.events.SavesReloadEvent;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
/**
* Creates special signs that auto-update with information.
* - viewer ({create <location>}/modify/remove) [id:<name>] (type:{sign_post}/wall_sign) (display:{location}/score/logged_in)
* @author Morphan1
*/
public class ViewerCommand extends AbstractCommand {
private enum Action { CREATE, MODIFY, REMOVE }
private enum Type { SIGN_POST, WALL_SIGN }
private enum Display { LOCATION, SCORE }
static Map<String, Viewer> viewers = new ConcurrentHashMap<String, Viewer>();
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values()))
// add Action
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
else if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values()))
// add Action
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
else if (!scriptEntry.hasObject("display")
&& arg.matchesEnum(Display.values()))
// add Action
scriptEntry.addObject("display", Display.valueOf(arg.getValue().toUpperCase()));
else if (arg.matchesPrefix("i, id"))
scriptEntry.addObject("id", arg.asElement());
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class))
// Location arg
scriptEntry.addObject("location", arg.asType(dLocation.class).setPrefix("location"));
}
if (!scriptEntry.hasObject("action"))
scriptEntry.addObject("action", Action.CREATE);
if (!scriptEntry.hasObject("display"))
scriptEntry.addObject("display", Display.LOCATION);
if (!scriptEntry.hasObject("id"))
throw new InvalidArgumentsException("Must specify a Viewer ID!");
if (!scriptEntry.hasObject("location") && scriptEntry.getObject("action").equals(Action.CREATE))
throw new InvalidArgumentsException("Must specify a Sign location!");
if (!scriptEntry.hasObject("type"))
scriptEntry.addObject("type", Type.SIGN_POST);
}
@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Action action = (Action) scriptEntry.getObject("action");
Type type = (Type) scriptEntry.getObject("type");
Display display = (Display) scriptEntry.getObject("display");
String id = (String) scriptEntry.getObject("id").toString();
dLocation location = (dLocation) scriptEntry.getObject("location");
String content = display.toString() + "; " + scriptEntry.getPlayer().getName();
switch (action) {
case CREATE:
if (viewers.containsKey(id)) {
dB.echoDebug("Viewer ID " + id + " already exists!");
return;
}
Viewer viewer = new Viewer(id, content, location);
viewers.put(id, viewer);
Block sign = location.getBlock();
sign.setType(Material.valueOf(type.name()));
final BlockState signState = sign.getState();
Utilities.setSignRotation(signState);
final Player player = Bukkit.getPlayerExact(scriptEntry.getPlayer().getName());
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
if (player == null)
Utilities.setSignLines((Sign) signState, new String[]{"", scriptEntry.getPlayer().getName(), "is offline.", ""});
else
Utilities.setSignLines((Sign) signState, new String[]{String.valueOf(scriptEntry.getPlayer().getLocation().getX()), String.valueOf(scriptEntry.getPlayer().getLocation().getY()), String.valueOf(scriptEntry.getPlayer().getLocation().getZ()), scriptEntry.getPlayer().getWorld().getName()});
}
}, 0, 20);
viewer.setTask(task);
viewer.save();
break;
case MODIFY:
// Insert stuff
break;
case REMOVE:
if (!viewers.containsKey(id)) {
dB.echoDebug("Viewer ID " + id + " doesn't exist!");
return;
}
Block block = viewers.get(id).getLocation().getBlock();
block.setType(Material.AIR);
Bukkit.getScheduler().cancelTask(viewers.get(id).getTask());
viewers.get(id).remove();
viewers.remove(id);
}
}
private static class Viewer {
private String id;
private String content;
private dLocation location;
private int task;
private Viewer(String id) {
this.id = id;
}
private Viewer(String id, String content, dLocation location) {
this.id = id;
this.content = content;
this.location = location;
}
void setContent(String content) {
this.content = content;
}
void setLocation(dLocation location) {
this.location = location;
}
void setTask(int task) {
this.task = task;
}
private String getContent() {
return this.content;
}
private dLocation getLocation() {
return this.location;
}
private int getTask() {
return this.task;
}
void save() {
FileConfiguration saves = DenizenAPI.getCurrentInstance().getSaves();
// Save content
saves.set("Viewers." + id.toLowerCase() + ".content", content);
// Save location
saves.set("Viewers." + id.toLowerCase() + ".location", location.identify());
}
void remove() {
FileConfiguration saves = DenizenAPI.getCurrentInstance().getSaves();
saves.set("Viewers." + id.toLowerCase(), null);
}
}
@EventHandler
public static void reloadViewers(SavesReloadEvent event) {
for (Viewer viewer : viewers.values()) {
Bukkit.getScheduler().cancelTask(viewer.getTask());
}
viewers.clear();
FileConfiguration saves = DenizenAPI.getCurrentInstance().getSaves();
for (String key : saves.getConfigurationSection("Viewers").getKeys(false)) {
Viewer viewer = new Viewer(key, saves.getString("Viewers." + key.toLowerCase() + ".content"), dLocation.valueOf(saves.getString("Viewers." + key.toLowerCase() + ".dLocation")));
viewers.put(key, viewer);
final Sign sign = (Sign) viewer.getLocation().getBlock().getState();
final String[] content = viewer.getContent().split("; ");
final Player player = Bukkit.getPlayerExact(content[1]);
if (viewer.getContent().startsWith("location")) {
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
if (player == null)
Utilities.setSignLines((Sign) sign, new String[]{"", content[1], "is offline.",""});
else
Utilities.setSignLines((Sign) sign, new String[]{String.valueOf(player.getLocation().getX()), String.valueOf(player.getLocation().getY()), String.valueOf(player.getLocation().getZ()), player.getWorld().getName()});
}
}, 0, 20);
viewer.setTask(task);
}
}
}
@EventHandler
public static void blockBreaks(BlockBreakEvent event) {
for (Viewer viewer : viewers.values())
if (event.getBlock().getLocation().equals(viewer.getLocation())) {
event.getPlayer().sendMessage(ChatColor.RED + "You're not allowed to break that sign.");
event.setCancelled(true);
}
}
@Override
public void onDisable() {
for (Viewer viewer : viewers.values())
viewer.save();
}
}