/
DisplayItemCommand.java
176 lines (163 loc) · 7.16 KB
/
DisplayItemCommand.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
package com.denizenscript.denizen.scripts.commands.item;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.command.TabCompleteHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.entity.ItemMergeEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.util.Vector;
import java.util.HashSet;
import java.util.UUID;
public class DisplayItemCommand extends AbstractCommand implements Listener {
public DisplayItemCommand() {
setName("displayitem");
setSyntax("displayitem [<item>] [<location>] (duration:<value>)");
setRequiredArguments(2, 3);
Bukkit.getPluginManager().registerEvents(this, Denizen.getInstance());
isProcedural = false;
}
// <--[command]
// @Name DisplayItem
// @Syntax displayitem [<item>] [<location>] (duration:<value>)
// @Required 2
// @Maximum 3
// @Short Makes a non-touchable item spawn for players to view.
// @Group item
//
// @Description
// This command drops an item at the specified location which cannot be picked up by players.
//
// It accepts a duration which determines how long the item will stay for until disappearing.
// If no duration is specified the item will stay for 1 minute, after which the item will disappear.
// Use "duration:infinite" to indicate that the item should never remove itself.
//
// @Tags
// <EntityTag.item>
// <entry[saveName].dropped> returns a EntityTag of the spawned item.
//
// @Usage
// Use to display a stone block dropped at a players location.
// - displayitem stone <player.location>
//
// @Usage
// Use to display a diamond sword dropped at a relevant location.
// - displayitem diamond_sword <context.location>
//
// @Usage
// Use to display redstone dust dropped at a related location disappear after 10 seconds.
// - displayitem redstone <context.location> duration:10s
//
// @Usage
// Use to save the dropped item to save entry 'item_dropped'.
// - displayitem redstone <context.location> duration:10s save:item_dropped
// -->
@Override
public void addCustomTabCompletions(TabCompletionsBuilder tab) {
TabCompleteHelper.tabCompleteItems(tab);
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (arg.matchesArgumentType(DurationTag.class)
&& !scriptEntry.hasObject("duration")) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (arg.matchesArgumentType(LocationTag.class)
&& !scriptEntry.hasObject("location")) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (arg.matchesArgumentType(ItemTag.class)
&& !scriptEntry.hasObject("item")) {
scriptEntry.addObject("item", arg.asType(ItemTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("item")) {
throw new InvalidArgumentsException("Must specify an item to display.");
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
}
if (!scriptEntry.hasObject("duration")) {
scriptEntry.addObject("duration", new DurationTag(60));
}
}
public final HashSet<UUID> protectedEntities = new HashSet<>();
@EventHandler
public void onItemMerge(ItemMergeEvent event) {
if (protectedEntities.contains(event.getEntity().getUniqueId())
|| protectedEntities.contains(event.getTarget().getUniqueId())) {
event.setCancelled(true);
}
}
@EventHandler
public void onItemInventoryPickup(InventoryPickupItemEvent event) {
if (protectedEntities.contains(event.getItem().getUniqueId())) {
event.setCancelled(true);
}
}
@EventHandler
public void onItemEntityPickup(EntityPickupItemEvent event) {
if (protectedEntities.contains(event.getItem().getUniqueId())) {
event.setCancelled(true);
}
}
public void onItemDespawn(ItemDespawnEvent event) {
if (protectedEntities.contains(event.getEntity().getUniqueId())) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (event.getEntity().isValid() && !event.getEntity().isDead()) {
NMSHandler.entityHelper.setTicksLived(event.getEntity(), -1000);
}
}, 1);
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ItemTag item = scriptEntry.getObjectTag("item");
DurationTag duration = scriptEntry.getObjectTag("duration");
LocationTag location = scriptEntry.getObjectTag("location");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), item, duration, location);
}
// Drop the item
final Item dropped = location.getWorld().dropItem(location.getBlockLocation().clone().add(0.5, 1.5, 0.5), item.getItemStack());
dropped.setVelocity(new Vector(0, 0, 0));
dropped.setGravity(false);
dropped.setPickupDelay(32767);
int ticks = duration.getTicksAsInt();
NMSHandler.entityHelper.setTicksLived(dropped, ticks <= 0 ? -32768 : -ticks);
if (!dropped.isValid()) {
Debug.echoDebug(scriptEntry, "Item failed to spawned (likely blocked by some plugin).");
return;
}
final UUID itemUUID = dropped.getUniqueId();
protectedEntities.add(itemUUID);
scriptEntry.saveObject("dropped", new EntityTag(dropped));
if (ticks > 0) {
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(),
() -> {
protectedEntities.remove(itemUUID);
if (dropped.isValid() && !dropped.isDead()) {
dropped.remove();
}
}, ticks);
}
}
}