/
ShowFakeCommand.java
131 lines (106 loc) · 5.1 KB
/
ShowFakeCommand.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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.blocks.FakeBlock;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import java.util.List;
public class ShowFakeCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
dList locations = new dList();
dList entities = new dList();
boolean added_entities = false;
// Iterate through arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matchesPrefix("to", "players")) {
for (String entity : dList.valueOf(arg.getValue())) {
if (dPlayer.matches(entity)) {
entities.add(entity);
}
}
added_entities = true; // TODO: handle lists properly
}
else if (arg.matchesArgumentList(dMaterial.class)) {
scriptEntry.addObject("materials", arg.asType(dList.class));
}
else if (locations.isEmpty()
&& arg.matchesArgumentType(dList.class)) {
for (String item : dList.valueOf(arg.getValue())) {
if (dLocation.matches(item)) {
locations.add(item);
}
}
}
else if (locations.isEmpty()
&& arg.matchesArgumentType(dLocation.class)) {
locations.add(arg.getValue());
}
else if (arg.matchesPrefix("d", "duration")
&& arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("duration", arg.asType(Duration.class));
}
else if (arg.matches("cancel")) {
scriptEntry.addObject("cancel", new Element(true));
}
else {
arg.reportUnhandled();
}
}
if (entities.isEmpty() && ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
entities.add(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().identify());
}
if (locations.isEmpty()) {
throw new InvalidArgumentsException("Must specify at least one valid location!");
}
if (!added_entities && (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()
|| !((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().isOnline())) {
throw new InvalidArgumentsException("Must have a valid, online player attached!");
}
if (entities.isEmpty() && added_entities) {
throw new InvalidArgumentsException("Must specify valid targets!");
}
if (!scriptEntry.hasObject("materials") && !scriptEntry.hasObject("cancel")) {
throw new InvalidArgumentsException("Must specify valid material(s)!");
}
scriptEntry.addObject("entities", entities);
scriptEntry.addObject("locations", locations);
scriptEntry.defaultObject("duration", new Duration(10)).defaultObject("cancel", new Element(false));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Duration duration = scriptEntry.getdObject("duration");
dList material_list = scriptEntry.getdObject("materials");
dList list = scriptEntry.getdObject("locations");
dList players = scriptEntry.getdObject("entities");
Element cancel = scriptEntry.getElement("cancel");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), (material_list != null ? material_list.debug() : "")
+ list.debug() + players.debug() + duration.debug() + cancel.debug());
}
boolean shouldCancel = cancel.asBoolean();
List<dMaterial> mats = null;
if (!shouldCancel) {
mats = material_list.filter(dMaterial.class);
}
int i = 0;
for (dLocation loc : list.filter(dLocation.class)) {
if (!shouldCancel) {
FakeBlock.showFakeBlockTo(players.filter(dPlayer.class), loc, mats.get(i % mats.size()), duration);
}
else {
FakeBlock.stopShowingTo(players.filter(dPlayer.class), loc);
}
i++;
}
}
}