/
FakeSpawnCommand.java
133 lines (126 loc) · 5.93 KB
/
FakeSpawnCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.entity.FakeEntity;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.Arrays;
import java.util.List;
public class FakeSpawnCommand extends AbstractCommand {
public FakeSpawnCommand() {
setName("fakespawn");
setSyntax("fakespawn [<entity>] [cancel/<location>] (players:<player>|...) (d:<duration>{10s})");
setRequiredArguments(2, 4);
isProcedural = false;
}
// <--[command]
// @Name FakeSpawn
// @Syntax fakespawn [<entity>] [<location>] (players:<player>|...) (d:<duration>{10s})
// @Required 2
// @Maximum 4
// @Short Makes the player see a fake entity spawn that didn't actually happen.
// @Group player
//
// @Description
// Makes the player see a fake entity spawn that didn't actually happen.
// This means that the server will not track the entity,
// and players not included in the command will not see the entity.
//
// You must specify a location and an entity to spawn.
//
// Optionally, specify a list of players to show the entity to.
// If unspecified, will default to the linked player.
//
// Optionally, specify how long the fake entity should remain for.
// If unspecified, will default to 10 seconds.
// After the duration is up, the entity will be removed from the player(s).
//
// To remove a fake entity, specify the fake entity object and 'cancel' instead of a location.
//
// @Tags
// <PlayerTag.fake_entities>
// <entry[saveName].faked_entities> returns the list of spawned faked entities (one per player).
//
// @Usage
// Use to show a fake creeper in front of the attached player.
// - fakespawn creeper <player.forward[5]>
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("players")
&& arg.matchesPrefix("to", "players")) {
scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (arg.matchesPrefix("d", "duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("entity")
&& arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("entity", arg.asType(EntityTag.class));
}
else if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("cancel")
&& arg.matches("cancel")) {
scriptEntry.addObject("cancel", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("players") && Utilities.entryHasPlayer(scriptEntry)) {
scriptEntry.defaultObject("players", Arrays.asList(Utilities.getEntryPlayer(scriptEntry)));
}
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("cancel")) {
throw new InvalidArgumentsException("Must specify a valid location!");
}
if (!scriptEntry.hasObject("players")) {
throw new InvalidArgumentsException("Must have a valid, online player attached!");
}
if (!scriptEntry.hasObject("entity")) {
throw new InvalidArgumentsException("Must specify a valid entity!");
}
scriptEntry.defaultObject("duration", new DurationTag(10));
}
@Override
public void execute(ScriptEntry scriptEntry) {
EntityTag entity = scriptEntry.getObjectTag("entity");
LocationTag location = scriptEntry.getObjectTag("location");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
DurationTag duration = scriptEntry.getObjectTag("duration");
ElementTag cancel = scriptEntry.getElement("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), entity.debug()
+ (cancel != null ? cancel.debug() : location.debug() + duration.debug())
+ ArgumentHelper.debugList("players", players));
}
if (cancel != null && cancel.asBoolean()) {
if (entity.isFake) {
FakeEntity fakeEnt = FakeEntity.idsToEntities.get(entity.getUUID());
if (fakeEnt != null) {
fakeEnt.cancelEntity();
}
else {
Debug.echoDebug(scriptEntry, "Entity '" + entity + "' cannot be cancelled: not listed in fake-entity map.");
}
}
}
else {
ListTag created = FakeEntity.showFakeEntityTo(players, entity, location, duration);
scriptEntry.addObject("faked_entities", created);
}
}
}