/
EntityLoadCrossbowScriptEvent.java
120 lines (107 loc) · 3.83 KB
/
EntityLoadCrossbowScriptEvent.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
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.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import io.papermc.paper.event.entity.EntityLoadCrossbowEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
public class EntityLoadCrossbowScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// entity loads crossbow
// <entity> loads crossbow
//
// @Regex ^on [^\s]+ loads crossbow$
//
// @Location true
//
// @Plugin Paper
//
// @Group Paper
//
// @Cancellable true
//
// @Triggers when a living entity loads a crossbow with a projectile.
//
// @Context
// <context.entity> returns the EntityTag that is loading the crossbow.
// <context.crossbow> returns the ItemTag of the crossbow.
// <context.consumes> returns true if the loading will consume a projectile item, otherwise false.
// <context.hand> returns "HAND" or "OFF_HAND" depending on which hand is holding the crossbow item.
//
// @Determine
// "KEEP_ITEM" to keep the projectile item in the shooter's inventory.
//
// @Player when the entity is a player.
//
// @NPC when the entity is an NPC.
//
// -->
public EntityLoadCrossbowScriptEvent() {
instance = this;
}
public static EntityLoadCrossbowScriptEvent instance;
public EntityLoadCrossbowEvent event;
public EntityTag entity;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgLowerAt(1).equals("loads") || !path.eventArgLowerAt(2).equals("crossbow")) {
return false;
}
if (!couldMatchEntity(path.eventArgLowerAt(0))) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
if (!tryEntity(entity, path.eventArgLowerAt(0))) {
return false;
}
return super.matches(path);
}
@Override
public String getName() {
return "EntityLoadsCrossbow";
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag && !isDefaultDetermination(determinationObj)) {
String lower = CoreUtilities.toLowerCase(determinationObj.toString());
if (lower.equals("keep_item")) {
event.setConsumeItem(false);
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "entity":
return entity.getDenizenObject();
case "crossbow":
return new ItemTag(event.getCrossbow());
case "hand":
return new ElementTag(event.getHand().name());
case "consumes":
return new ElementTag(event.shouldConsumeItem());
}
return super.getContext(name);
}
@EventHandler
public void onLoadCrossbow(EntityLoadCrossbowEvent event) {
this.event = event;
this.entity = new EntityTag(event.getEntity());
fire(event);
}
}