/
PlayerSwapsItemsScriptEvent.java
110 lines (98 loc) · 3.92 KB
/
PlayerSwapsItemsScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
public class PlayerSwapsItemsScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player swaps items
//
// @Regex ^on player swaps items$
//
// @Switch main:<item> to only process the event if the item being put into the main hand matches the input item.
// @Switch offhand:<item> to only process the event if the item being put into the off-hand matches the input item.
//
// @Cancellable true
//
// @Triggers when a player swaps the items in their main and off hands.
//
// @Context
// <context.main> returns the ItemTag switched to the main hand.
// <context.offhand> returns the ItemTag switched to the off hand.
//
// @Determine
// "MAIN:" + ItemTag to set the item in the main hand.
// "OFFHAND:" + ItemTag to set the item in the off hand.
//
// @Player Always.
//
// -->
public PlayerSwapsItemsScriptEvent() {
instance = this;
}
public static PlayerSwapsItemsScriptEvent instance;
public PlayerTag player;
public PlayerSwapHandItemsEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player swaps items");
}
@Override
public boolean matches(ScriptPath path) {
if (path.switches.containsKey("main") && !tryItem(new ItemTag(event.getMainHandItem()), path.switches.get("main"))) {
return false;
}
if (path.switches.containsKey("offhand") && !tryItem(new ItemTag(event.getOffHandItem()), path.switches.get("offhand"))) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerSwapsItems";
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("main:")) {
event.setMainHandItem(ItemTag.valueOf(determination.substring("main:".length()), path.container).getItemStack());
return true;
}
else if (lower.startsWith("offhand:")) {
event.setOffHandItem(ItemTag.valueOf(determination.substring("offhand:".length()), path.container).getItemStack());
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("main")) {
return new ItemTag(event.getMainHandItem());
}
else if (name.equals("offhand")) {
return new ItemTag(event.getOffHandItem());
}
return super.getContext(name);
}
@EventHandler
public void playerSwapsItems(PlayerSwapHandItemsEvent event) {
player = PlayerTag.mirrorBukkitPlayer(event.getPlayer());
this.event = event;
fire(event);
}
}