/
PlayerClicksBlockScriptEvent.java
204 lines (186 loc) · 7.69 KB
/
PlayerClicksBlockScriptEvent.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.Arrays;
import java.util.HashSet;
public class PlayerClicksBlockScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player clicks block
// player (right/left) clicks <material>
// player (right/left) clicks block
//
// @Cancellable true
//
// @Regex ^on player ([^\s]+ )?clicks [^\s]+$
//
// @Group Player
//
// @Switch with:<item> to only process the event if a specified item was held.
// @Switch using:hand/off_hand/either_hand to only process the event if the specified hand was used to click.
// @Switch in:<area> to only process the event if it occurred within a specified area.
//
// @Triggers when a player clicks on a block or in the air.
//
// @Context
// <context.item> returns the ItemTag the player is clicking with.
// <context.location> returns the LocationTag the player is clicking on.
// <context.relative> returns a LocationTag of the air block in front of the clicked block.
// <context.click_type> returns an ElementTag of the Spigot API click type <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/block/Action.html>.
// <context.hand> returns an ElementTag of the used hand.
//
// @Player Always.
//
// -->
public PlayerClicksBlockScriptEvent() {
instance = this;
}
public PlayerClicksBlockScriptEvent instance;
public PlayerInteractEvent event;
public ItemTag item;
public LocationTag location;
public ElementTag click_type;
public ElementTag hand;
public MaterialTag blockMaterial;
private boolean runUsingCheck(ScriptPath path) {
String using = path.switches.get("using");
if (using == null) {
int index;
for (index = 0; index < path.eventArgsLower.length; index++) {
if (path.eventArgsLower[index].equals("using")) {
break;
}
}
if (index >= path.eventArgsLower.length) {
using = "hand";
}
else {
using = path.eventArgLowerAt(index + 1);
}
}
if (!using.equals("hand") && !using.equals("off_hand") && !using.equals("either_hand")) {
Debug.echoError("Invalid USING hand in " + getName() + " for '" + path.event + "' in " + path.container.getName());
return false;
}
if (!using.equals("either_hand") && !runGenericCheck(using, hand.identify())) {
return false;
}
return true;
}
private static final HashSet<String> matchHelpList = new HashSet<>(Arrays.asList("at", "entity", "npc", "player", "vehicle", "projectile", "hanging", "fake"));
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgLowerAt(0).equals("player")) {
return false;
}
boolean clickFirst = path.eventArgLowerAt(1).equals("clicks");
if (!clickFirst && !path.eventArgLowerAt(2).equals("clicks")) {
return false;
}
if (!clickFirst && !path.eventArgLowerAt(1).equals("right") && !path.eventArgLowerAt(1).equals("left")) {
return false;
}
String clickedOn = path.eventArgLowerAt(clickFirst ? 2 : 3);
if (matchHelpList.contains(clickedOn)) {
return false;
}
if (!clickedOn.isEmpty() && !couldMatchBlock(clickedOn)
&& !clickedOn.equals("with") && !clickedOn.equals("in") && !clickedOn.equals("using")) { // Legacy format support
return false;
}
if (!couldMatchInArea(path.eventLower)) {
return false;
}
if (clickedOn.isEmpty()) {
Debug.echoError("'on player clicks:' is is not valid, use 'on player clicks block:' (for script '" + path.container.getName() + "').");
}
return true;
}
private static final HashSet<String> withHelpList = new HashSet<>(Arrays.asList("with", "using", "in"));
@Override
public boolean matches(ScriptPath path) {
int index = path.eventArgLowerAt(1).equals("clicks") ? 1 : 2;
if (index == 2 && !click_type.identify().startsWith(path.eventArgLowerAt(1).toUpperCase())) {
return false;
}
String mat = path.eventArgLowerAt(index + 1);
if (mat.length() > 0 && !withHelpList.contains(mat) && !tryMaterial(blockMaterial, mat)) {
return false;
}
if (!nonSwitchWithCheck(path, new ItemTag(event.getItem()))) {
return false;
}
if (!runWithCheck(path, new ItemTag(event.getItem()))) {
return false;
}
if (!runUsingCheck(path)) {
return false;
}
if (!runInCheck(path, location != null ? location : event.getPlayer().getLocation())) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "PlayerClicksBlock";
}
public boolean wasCancellationAltered;
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
wasCancellationAltered = true;
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(EntityTag.getPlayerFrom(event.getPlayer()), null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("item")) {
return item;
}
else if (name.equals("location")) {
return location;
}
else if (name.equals("click_type")) {
return click_type;
}
else if (name.equals("hand")) {
return hand;
}
else if (name.equals("relative")) {
return event.hasBlock() ? new LocationTag(event.getClickedBlock().getRelative(event.getBlockFace()).getLocation()) : null;
}
return super.getContext(name);
}
@Override
public void cancellationChanged() {
event.setCancelled(cancelled); // Workaround for Spigot=Dumb!
}
@EventHandler
public void playerClicksBlock(PlayerInteractEvent event) {
if (event.getAction() == Action.PHYSICAL) {
return;
}
blockMaterial = event.hasBlock() ? new MaterialTag(event.getClickedBlock()) : new MaterialTag(Material.AIR);
hand = new ElementTag(event.getHand().name());
item = new ItemTag(event.getItem());
location = event.hasBlock() ? new LocationTag(event.getClickedBlock().getLocation()) : null;
click_type = new ElementTag(event.getAction().name());
cancelled = event.isCancelled() && event.useItemInHand() == Event.Result.DENY; // Spigot is dumb!
this.event = event;
fire(); // Explicitly don't use `fire(event)` due to spigot bork
}
}