-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PlayerDragsInInvScriptEvent.java
169 lines (153 loc) · 5.95 KB
/
PlayerDragsInInvScriptEvent.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
package net.aufdemrand.denizen.events.player;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.events.BukkitScriptEvent;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dInventory;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.objects.notable.NotableManager;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.scripts.ScriptEntryData;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.scheduler.BukkitRunnable;
public class PlayerDragsInInvScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player drags in inventory (in_area <area>)
// player drags (<item>) (in <inventory>) (in_area <area>)
//
// @Regex ^on player drags( ^[\s]+)?(in [^\s]+)?( in_area ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Cancellable true
//
// @Triggers when a player drags in an inventory.
//
// @Context
// <context.item> returns the dItem the player has dragged.
// <context.inventory> returns the dInventory.
// <context.slots> returns a dList of the slot numbers dragged through.
// <context.raw_slots> returns a dList of the raw slot numbers dragged through.
//
// -->
public PlayerDragsInInvScriptEvent() {
instance = this;
}
public static PlayerDragsInInvScriptEvent instance;
public Inventory inventory;
public dList slots;
public dItem item;
public dList raw_slots;
private dPlayer entity;
private dInventory dInv;
private boolean manual_cancelled;
public InventoryDragEvent event;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.startsWith("player drags");
}
@Override
public boolean matches(ScriptPath path) {
String s = path.event;
String lower = path.eventLower;
String arg2 = CoreUtilities.getXthArg(2, lower);
String arg3 = CoreUtilities.getXthArg(3, lower);
String arg4 = CoreUtilities.getXthArg(4, lower);
String inv = arg2.equals("in") ? arg3 : arg3.equals("in") ? arg4 : "";
String nname = NotableManager.isSaved(dInv) ?
CoreUtilities.toLowerCase(NotableManager.getSavedId(dInv)) :
"\0";
if (!inv.equals("") && !inv.equals("inventory")
&& !inv.equals(CoreUtilities.toLowerCase(dInv.getInventoryType().name()))
&& !inv.equals(CoreUtilities.toLowerCase(dInv.bestName()))
&& !(inv.equals("notable") && !nname.equals("\0"))
&& !inv.equals(nname)) {
return false;
}
if (!arg2.equals("in") && !tryItem(item, arg2)) {
return false;
}
return runInCheck(scriptContainer, s, lower, entity.getLocation(), "in_area");
}
@Override
public String getName() {
return "PlayerDragsInInventory";
}
@Override
public void init() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
@Override
public void destroy() {
InventoryDragEvent.getHandlerList().unregister(this);
}
@Override
public boolean applyDetermination(ScriptContainer container, String determination) {
return super.applyDetermination(container, determination);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity, null);
}
@Override
public dObject getContext(String name) {
if (name.equals("inventory")) {
return dInv;
}
else if (name.equals("slots")) {
return slots;
}
else if (name.equals("raw_slots")) {
return raw_slots;
}
else if (name.equals("item")) {
return item;
}
return super.getContext(name);
}
@EventHandler
public void onPlayerDragsInInv(InventoryDragEvent event) {
if (dEntity.isCitizensNPC(event.getWhoClicked())) {
return;
}
entity = dEntity.getPlayerFrom(event.getWhoClicked());
inventory = event.getInventory();
dInv = dInventory.mirrorBukkitInventory(inventory);
item = new dItem(event.getOldCursor());
slots = new dList();
for (Integer slot : event.getInventorySlots()) {
slots.add(String.valueOf(slot + 1));
}
raw_slots = new dList();
for (Integer raw_slot : event.getRawSlots()) {
raw_slots.add(String.valueOf(raw_slot + 1));
}
cancelled = event.isCancelled();
boolean wasCancelled = cancelled;
this.event = event;
fire();
if (cancelled && !wasCancelled) {
final InventoryHolder holder = inventory.getHolder();
new BukkitRunnable() {
@Override
public void run() {
entity.getPlayerEntity().updateInventory();
if (holder != null && holder instanceof Player) {
((Player) holder).updateInventory();
}
}
}.runTaskLater(DenizenAPI.getCurrentInstance(), 1);
}
event.setCancelled(cancelled);
}
}