/
ShowFakeCommand.java
166 lines (140 loc) · 6.47 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
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
package net.aufdemrand.denizen.scripts.commands.player;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dMaterial;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.blocks.FakeBlock;
import net.aufdemrand.denizen.utilities.debugging.dB;
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 {
// <--[command]
// @Name ShowFake
// @Syntax showfake [<material>|.../cancel] [<location>|...] (players:<player>|...) (d:<duration>{10s})
// @Required 2
// @Short Makes the player see a block change that didn't actually happen.
// @Group player
//
// @Description
// Shows a fake block for a player which is not affected on server-side.
// You can show a block for a player without anyone else can see it.
//
// If a player stands on a showfake block which is originally and air block,
// then the server will treat this as the player is flying/falling.
//
// If a player tries to interact with the block (usually by right-clicking or left-click),
// the server will then update the block for the player with the original block and the
// effect of showfake is lost.
//
// If no duration is specefied, then it assumes the default duration of 10 seconds.
//
// @Tags
// <l@location.block.material>
//
// @Usage
// Use to place a fake gold block at where the player is looking
// - showfake GOLD_BLOCK <player.location.cursor_on> players:<player> duration:1m
//
// @Usage
// Use to place a stone block right on player's face
// - showfake STONE <player.location.add[0,1,0]> players:<player> duration:5s
//
// @Usage
// Use to place fake lava (shows visual fire if standing in it)
// - showfake LAVA <player.location> players:<server.list_online_players> duration:5s
// -->
@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.interpretArguments(scriptEntry.aHArgs)) {
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() && Utilities.entryHasPlayer(scriptEntry)) {
entities.add(Utilities.getEntryPlayer(scriptEntry).identify());
}
if (locations.isEmpty()) {
throw new InvalidArgumentsException("Must specify at least one valid location!");
}
if (!added_entities && (!Utilities.entryHasPlayer(scriptEntry)
|| !Utilities.getEntryPlayer(scriptEntry).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) {
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, scriptEntry);
}
int i = 0;
for (dLocation loc : list.filter(dLocation.class, scriptEntry)) {
if (!shouldCancel) {
FakeBlock.showFakeBlockTo(players.filter(dPlayer.class, scriptEntry), loc, mats.get(i % mats.size()), duration);
}
else {
FakeBlock.stopShowingTo(players.filter(dPlayer.class, scriptEntry), loc);
}
i++;
}
}
}