-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemMoveScriptEvent.java
127 lines (110 loc) · 4.61 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
123
124
125
126
127
package net.aufdemrand.denizen.events.scriptevents;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.events.ScriptEvent;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import java.util.*;
public class ItemMoveScriptEvent extends ScriptEvent 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>)
//
// @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(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
String itemName = item.getMaterialName();
String originType = origin.getInventoryType().name();
String destinationType = destination.getInventoryType().name();
return lower.equals("item moves from inventory")
|| lower.equals(itemName + " moves from inventory")
|| lower.equals(itemName + " moves from " + originType)
|| lower.equals(itemName + " moves from " + originType + " to " + destinationType)
|| lower.equals(itemName + " moves from inventory to " + destinationType)
|| lower.equals("item moves from " + originType)
|| lower.equals("item moves from " + originType + " to " + destinationType)
|| lower.equals("item moves from inventory to " + destinationType);
}
@Override
public String getName() {
return "ItemMoves";
}
@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void destroy() {
InventoryMoveItemEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
if (dItem.matches(determination)) {
item = dItem.valueOf(determination);
itemSet = true;
return true;
}
return super.applyDetermination(container, determination);
}
@Override
public HashMap<String, dObject> getContext() {
HashMap<String, dObject> context = super.getContext();
context.put("origin", origin);
context.put("destination", destination);
context.put("initiator", initiator);
context.put("item", item);
return context;
}
@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;
cancelled = event.isCancelled();
fire();
event.setCancelled(cancelled);
if (itemSet) {
event.setItem(item.getItemStack());
}
}
}