/
DisplayItemCommand.java
168 lines (144 loc) · 6.33 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
package com.denizenscript.denizen.scripts.commands.item;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.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.ItemMergeEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import java.util.HashSet;
import java.util.UUID;
public class DisplayItemCommand extends AbstractCommand implements Listener {
// <--[command]
// @Name DisplayItem
// @Syntax displayitem [<item>] [<location>] (duration:<value>)
// @Required 2
// @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.
//
// @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 -54,100,-87 in world creative disappear after 10 seconds.
// - displayitem redstone l@-54,100,-87,creative duration:10s
//
// @Usage
// Use to save the dropped item to save entry 'item_dropped'.
// - displayitem redstone l@-54,100,-87,creative duration:10s save:item_dropped
// -->
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
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();
}
}
// Check required args
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", DurationTag.valueOf("1m"));
}
}
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);
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ItemTag item = (ItemTag) scriptEntry.getObject("item");
DurationTag duration = (DurationTag) scriptEntry.getObject("duration");
LocationTag location = (LocationTag) scriptEntry.getObject("location");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
item.debug()
+ duration.debug()
+ location.debug());
}
// Drop the item
final Item dropped = location.getWorld()
.dropItem(location.getBlock().getLocation().clone().add(0.5, 1.5, 0.5), item.getItemStack());
dropped.setVelocity(dropped.getVelocity().multiply(0));
dropped.setPickupDelay(duration.getTicksAsInt() + 1000);
dropped.setTicksLived(duration.getTicksAsInt() + 1000);
if (!dropped.isValid()) {
return;
}
final UUID itemUUID = dropped.getUniqueId();
protectedEntities.add(itemUUID);
// Remember the item entity
scriptEntry.addObject("dropped", new EntityTag(dropped));
// Remove it later
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
if (dropped.isValid() && !dropped.isDead()) {
dropped.remove();
protectedEntities.remove(itemUUID);
}
}
}, duration.getTicks());
}
}