-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemMoveScriptEvent.java
122 lines (110 loc) · 4.04 KB
/
ItemMoveScriptEvent.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
package net.aufdemrand.denizen.events.world;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
public class ItemMoveScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// item moves from inventory (to <inventory type>)
// item moves from <inventory type> (to <inventory type>)
// <item> moves from inventory (to <inventory type>)
// <item> moves from <inventory type> (to <inventory type>)
//
// @Regex ^on [^\s]+ moves from [^\s]+( to [^\s]+)?$
// @Switch in <area>
//
// @Cancellable true
//
// @Triggers when an entity or block moves an item from one inventory to another. (Hopper-style movement, not player-induced movement).
//
// @Context
// <context.origin> returns the origin dInventory.
// <context.destination> returns the destination dInventory.
// <context.initiator> returns the dInventory that initiatied the item's transfer.
// <context.item> returns the dItem that was moved.
//
// @Determine
// dItem to set a different item to be moved. NOTE: The original item will not be moved!
//
// -->
public ItemMoveScriptEvent() {
instance = this;
}
public static ItemMoveScriptEvent instance;
public dInventory origin;
public dInventory destination;
public dInventory initiator;
public dItem item;
public boolean itemSet;
public InventoryMoveItemEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.contains("moves from");
}
@Override
public boolean matches(ScriptPath path) {
if (!tryItem(item, CoreUtilities.getXthArg(0, path.eventLower))) {
return false;
}
if (!tryInventory(origin, CoreUtilities.getXthArg(3, path.eventLower))) {
return false;
}
if (CoreUtilities.xthArgEquals(4, path.eventLower, "to")) {
if (!tryInventory(destination, CoreUtilities.getXthArg(5, path.eventLower))) {
return false;
}
}
if (!runInCheck(path, origin.getLocation())) {
return false;
}
return true;
}
@Override
public String getName() {
return "ItemMoves";
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
item = dItem.valueOf(determination, container);
itemSet = true;
return true;
}
return super.applyDetermination(container, determination);
}
@Override
public dObject getContext(String name) {
if (name.equals("origin")) {
return origin;
}
else if (name.equals("destination")) {
return destination;
}
else if (name.equals("initiator")) {
return initiator;
}
else if (name.equals("item")) {
return item;
}
return super.getContext(name);
}
@EventHandler
public void onInventoryMoveItemEvent(InventoryMoveItemEvent event) {
origin = dInventory.mirrorBukkitInventory(event.getSource());
destination = dInventory.mirrorBukkitInventory(event.getDestination());
initiator = dInventory.mirrorBukkitInventory(event.getInitiator());
item = new dItem(event.getItem());
itemSet = false;
fire(event);
if (itemSet) {
event.setItem(item.getItemStack());
}
}
}