-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PlayerInventorySlotChangeScriptEvent.java
99 lines (90 loc) · 3.63 KB
/
PlayerInventorySlotChangeScriptEvent.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
package com.denizenscript.denizen.paper.events;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.utilities.inventory.SlotHelper;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import io.papermc.paper.event.player.PlayerInventorySlotChangeEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class PlayerInventorySlotChangeScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player inventory slot changes
//
// @Group Paper
//
// @Location true
//
// @Plugin Paper
//
// @Switch from:<item> to only process the event if the previous item in the slot matches the specified item.
// @Switch to:<item> to only process the event if the new item in the slot matches the specified item.
// @Switch slot:<slot> to only process the event if a specific slot was clicked.
//
// @Triggers when the item in a slot of a player's inventory changes.
// Note that this fires for every item in the player's inventory when they join.
//
// @Context
// <context.new_item> returns an ItemTag of the new item in the slot.
// <context.old_item> returns an ItemTag of the previous item in the slot.
// <context.slot> returns an ElementTag(Number) of the slot that was changed.
// <context.raw_slot> returns an ElementTag(Number) of the raw number of the slot that was changed.
//
// @Player Always.
//
// -->
public PlayerInventorySlotChangeScriptEvent() {
registerCouldMatcher("player inventory slot changes");
registerSwitches("from", "to", "slot");
}
public PlayerInventorySlotChangeEvent event;
public ItemTag oldItem;
public ItemTag newItem;
@Override
public boolean matches(ScriptPath path) {
if (!runGenericSwitchCheck(path, "slot", String.valueOf(event.getSlot() + 1))) {
int altIndex = SlotHelper.nameToIndex(path.switches.get("slot"), event.getPlayer());
if (event.getSlot() != altIndex) {
return false;
}
}
if (!runWithCheck(path, oldItem, "from")) {
return false;
}
if (!runWithCheck(path, newItem, "to")) {
return false;
}
if (!runInCheck(path, event.getPlayer().getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "new_item": return newItem;
case "old_item": return oldItem;
case "slot": return new ElementTag(event.getSlot() + 1);
case "raw_slot": return new ElementTag(event.getRawSlot());
}
return super.getContext(name);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(event.getPlayer());
}
@EventHandler
public void onPlayerInventorySlotChange(PlayerInventorySlotChangeEvent event) {
if (EntityTag.isNPC(event.getPlayer())) {
return;
}
oldItem = new ItemTag(event.getOldItemStack());
newItem = new ItemTag(event.getNewItemStack());
this.event = event;
fire(event);
}
}