-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
PlayerRightClicksEntityScriptEvent.java
144 lines (127 loc) · 5.14 KB
/
PlayerRightClicksEntityScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.EquipmentSlot;
import java.util.HashSet;
import java.util.UUID;
public class PlayerRightClicksEntityScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// player right clicks entity
// player right clicks <entity>
//
// @Regex ^on player right clicks [^\s]+$
//
// @Switch in <area>
// @Switch with <item>
//
// @Cancellable true
//
// @Triggers when a player right clicks on an entity.
//
// @Context
// <context.entity> returns the EntityTag the player is clicking on.
// <context.item> returns the ItemTag the player is clicking with.
// <context.location> returns a LocationTag of the clicked entity. NOTE: DEPRECATED IN FAVOR OF <context.entity.location>
// <context.click_position> returns a LocationTag of the click position (as a world-less vector, relative to the entity's center). This is only available when clicking armor stands.
//
// -->
public static PlayerRightClicksEntityScriptEvent instance;
PlayerInteractEntityEvent event;
EntityTag entity;
ItemTag item;
LocationTag location;
@Override
public boolean couldMatch(ScriptContainer scriptContainer, String s) {
String lower = CoreUtilities.toLowerCase(s);
return lower.startsWith("player right clicks");
}
@Override
public boolean matches(ScriptPath path) {
boolean isAt = path.eventArgLowerAt(3).equals("at");
if (!tryEntity(entity, path.eventArgLowerAt(isAt ? 4 : 3))) {
return false;
}
if (!runInCheck(path, event.getPlayer().getLocation())) {
return false;
}
if (!runWithCheck(path, item)) {
return false;
}
// Deprecated in favor of with: format
if (path.eventArgLowerAt(isAt ? 5 : 4).equals("with") && !tryItem(item, path.eventArgLowerAt(isAt ? 6 : 5))) {
return false;
}
return true;
}
@Override
public String getName() {
return "PlayerRightClicksEntity";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(PlayerTag.mirrorBukkitPlayer(event.getPlayer()), entity.isNPC() ? entity.getDenizenNPC() : null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("item")) {
return item;
}
else if (name.equals("location")) {
return location;
}
else if (name.equals("click_position") && event instanceof PlayerInteractAtEntityEvent) {
return new LocationTag(((PlayerInteractAtEntityEvent) event).getClickedPosition());
}
else if (name.equals("cuboids")) {
Debug.echoError("context.cuboids tag is deprecated in " + getName() + " script event");
ListTag cuboids = new ListTag();
for (CuboidTag cuboid : CuboidTag.getNotableCuboidsContaining(location)) {
cuboids.addObject(cuboid);
}
return cuboids;
}
return super.getContext(name);
}
@EventHandler
public void playerRightClicksAtEntity(PlayerInteractAtEntityEvent event) {
playerRightClicksEntityHandler(event);
}
@EventHandler
public void playerRightClicksEntity(PlayerInteractEntityEvent event) {
if (event instanceof PlayerInteractAtEntityEvent) {
return;
}
playerRightClicksEntityHandler(event);
}
public HashSet<UUID> clickedThisFrame = new HashSet<>();
public void playerRightClicksEntityHandler(PlayerInteractEntityEvent event) {
if (event.getHand() == EquipmentSlot.OFF_HAND) {
return;
}
// Event tends to double-fire, use cooldown to prevent that
if (clickedThisFrame.contains(event.getPlayer().getUniqueId())) {
return;
}
clickedThisFrame.add(event.getPlayer().getUniqueId());
entity = new EntityTag(event.getRightClicked());
item = new ItemTag(event.getPlayer().getItemInHand());
location = new LocationTag(event.getRightClicked().getLocation());
this.event = event;
fire(event);
}
}