/
FakeEquipCommand.java
226 lines (212 loc) · 10.7 KB
/
FakeEquipCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
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 org.bukkit.entity.LivingEntity;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.*;
public class FakeEquipCommand extends AbstractCommand {
public FakeEquipCommand() {
setName("fakeequip");
setSyntax("fakeequip [<entity>|...] (for:<player>|...) (duration:<duration>/reset) (hand:<item>) (offhand:<item>) (head:<item>) (chest:<item>) (legs:<item>) (boots:<item>)");
setRequiredArguments(1, 9);
isProcedural = false;
}
// <--[command]
// @Name FakeEquip
// @Syntax fakeequip [<entity>|...] (for:<player>|...) (duration:<duration>/reset) (hand:<item>) (offhand:<item>) (head:<item>) (chest:<item>) (legs:<item>) (boots:<item>)
// @Required 1
// @Maximum 9
// @Short Fake-equips items and armor on a list of entities for players to see without real change.
// @Group entity
//
// @Description
// This command fake-equips items and armor on a list of entities.
//
// The change doesn't happen on-server, and no armor effects will happen from it.
//
// The equipment can only be seen by certain players. By default, the linked player is used.
//
// The changes will remain in place for as long as the duration is specified (even if the real equipment is changed).
// The changes can be manually reset early by using the 'reset' argument.
// If you do not provide a duration, the fake equipment will last until manually reset.
// This does not persist across server restarts.
//
// Set the item to 'air' to unequip any slot.
//
// @Tags
// <EntityTag.equipment>
// <InventoryTag.equipment>
//
// @Usage
// Use to fake-equip a stone block on the player's head.
// - fakeequip <player> head:stone duration:10s
//
// @Usage
// Use to fake-equip an iron helmet on two defined players.
// - fakeequip <[player]>|<[someplayer]> head:iron_helmet duration:1m
//
// @Usage
// Use to fake-unequip all armor off the player.
// - fakeequip <player> head:air chest:air legs:air boots:air duration:5s
//
// @Usage
// Use to make all players within 30 blocks of an entity see it permanently equip a shield.
// - fakeequip <[entity]> offhand:shield for:<[entity].find.players.within[30]> duration:0
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
EquipmentOverride equipment = new EquipmentOverride();
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (arg.matchesPrefix("duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (arg.matches("reset")) {
scriptEntry.addObject("reset", new ElementTag(true));
}
else if (arg.matchesPrefix("for")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("for", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (arg.matchesPrefix("head", "helmet")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.head = arg.asType(ItemTag.class);
}
else if (arg.matchesPrefix("chest", "chestplate")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.chest = arg.asType(ItemTag.class);
}
else if (arg.matchesPrefix("legs", "leggings")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.legs = arg.asType(ItemTag.class);
}
else if (arg.matchesPrefix("boots", "feet")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.boots = arg.asType(ItemTag.class);
}
else if (arg.matchesPrefix("offhand")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.offhand = arg.asType(ItemTag.class);
}
else if (arg.matchesPrefix("hand")
&& arg.matchesArgumentType(ItemTag.class)) {
equipment.hand = arg.asType(ItemTag.class);
}
else {
arg.reportUnhandled();
}
}
if (equipment.isEmpty() && !scriptEntry.hasObject("reset")) {
throw new InvalidArgumentsException("Must specify equipment!");
}
if (!scriptEntry.hasObject("for")) {
PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
if (player == null) {
throw new InvalidArgumentsException("Must specify a for player!");
}
scriptEntry.addObject("for", Collections.singletonList(player));
}
scriptEntry.addObject("equipment", equipment);
}
public static class EquipmentOverride {
public ItemTag hand, offhand, head, chest, legs, boots;
public BukkitTask cancelTask;
public boolean isEmpty() {
return hand == null && offhand == null && head == null && chest == null && legs == null && boots == null;
}
@Override
public String toString() {
return "Equipment{hand=" + hand + ",offhand=" + offhand + ",head=" + head + ",chest=" + chest + ",legs=" + legs + ",boots=" + boots + "}";
}
public void copyFrom(EquipmentOverride override) {
hand = override.hand == null ? hand : override.hand;
offhand = override.offhand == null ? offhand : override.offhand;
head = override.head == null ? head : override.head;
chest = override.chest == null ? chest : override.chest;
legs = override.legs == null ? legs : override.legs;
boots = override.boots == null ? boots : override.boots;
}
}
public static HashMap<UUID, HashMap<UUID, EquipmentOverride>> overrides = new HashMap<>();
@Override
public void execute(ScriptEntry scriptEntry) {
NetworkInterceptHelper.enable();
EquipmentOverride equipment = (EquipmentOverride) scriptEntry.getObject("equipment");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
List<PlayerTag> playersFor = (List<PlayerTag>) scriptEntry.getObject("for");
ElementTag reset = scriptEntry.getElement("reset");
DurationTag duration = scriptEntry.getObjectTag("duration");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("entities", entities.toString()), db("equipment", equipment.toString()), reset, duration, db("for", playersFor));
}
boolean isReset = reset != null && reset.asBoolean();
for (PlayerTag player : playersFor) {
HashMap<UUID, EquipmentOverride> playersMap = overrides.computeIfAbsent(player.getUUID(), (k) -> new HashMap<>());
for (EntityTag entity : entities) {
if (entity.isGeneric()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot equip generic entity " + entity.identify() + "!");
continue;
}
LivingEntity livingEntity = entity.getLivingEntity();
if (livingEntity == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot equip invalid/non-living entity " + entity.identify() + "!");
continue;
}
EquipmentOverride entityData;
if (isReset) {
entityData = playersMap.remove(entity.getUUID());
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
}
else {
entityData = playersMap.computeIfAbsent(entity.getUUID(), (k) -> new EquipmentOverride());
entityData.copyFrom(equipment);
}
if (entityData != null) {
if (entityData.cancelTask != null && !entityData.cancelTask.isCancelled()) {
entityData.cancelTask.cancel();
entityData.cancelTask = null;
}
if (duration != null && duration.getTicks() > 0) {
entityData.cancelTask = new BukkitRunnable() {
@Override
public void run() {
entityData.cancelTask = null;
HashMap<UUID, EquipmentOverride> playersMap = overrides.get(player.getUUID());
if (playersMap != null) {
if (playersMap.remove(entity.getUUID()) != null) {
if (playersMap.isEmpty()) {
overrides.remove(player.getUUID());
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
}.runTaskLater(Denizen.getInstance(), duration.getTicks());
}
}
NMSHandler.getPacketHelper().resetEquipment(player.getPlayerEntity(), livingEntity);
}
}
}
}