/
EntityPotionEffectScriptEvent.java
145 lines (133 loc) · 5.72 KB
/
EntityPotionEffectScriptEvent.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
package com.denizenscript.denizen.events.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.properties.item.ItemPotion;
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 com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPotionEffectEvent;
public class EntityPotionEffectScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// <entity> potion effects modified
// <entity> potion effects <'change_action'>
//
// @Group Entity
//
// @Location true
// @Switch cause:<cause> to only process the event when it came from a specified cause.
// @Switch effect:<effect type> to only process the event when a specified potion effect is applied.
//
// @Cancellable true
//
// @Triggers when an entity's potion effects change.
//
// @Context
// <context.entity> returns the EntityTag.
// <context.cause> returns the cause of the effect change, based on <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/entity/EntityPotionEffectEvent.Cause.html>
// <context.action> returns the action of the effect changed, which can be 'added', 'changed', 'cleared', or 'removed'
// <context.override> returns whether the new potion effect will override the old.
// <context.new_effect> returns the new potion effect (in the same format as <@link tag EntityTag.list_effects>) (if any).
// <context.old_effect> returns the old potion effect (in the same format as <@link tag EntityTag.list_effects>) (if any).
// <context.effect_type> returns the name of the modified potion effect type.
//
// @Determine
// "OVERRIDE:" + ElementTag(Boolean) to set whether the new potion effect should override.
//
// @Player when the entity that has changed is a player.
//
// @NPC when the entity that has changed is an NPC.
//
// -->
public EntityPotionEffectScriptEvent() {
instance = this;
registerCouldMatcher("<entity> potion effects modified");
registerCouldMatcher("<entity> potion effects <'change_action'>");
registerSwitches("cause", "effect");
}
public static EntityPotionEffectScriptEvent instance;
public EntityTag entity;
public EntityPotionEffectEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!super.couldMatch(path)) {
return false;
}
String change = path.eventArgAt(3);
if (!change.equals("modified") && !couldMatchEnum(change, EntityPotionEffectEvent.Action.values())) {
return false;
}
return true;
}
@Override
public boolean matches(ScriptPath path) {
String target = path.eventArgLowerAt(0);
String change = path.eventArgAt(3);
if (!change.equals("modified") && !runGenericCheck(change, CoreUtilities.toLowerCase(event.getAction().name()))) {
return false;
}
if (!entity.tryAdvancedMatcher(target)) {
return false;
}
if (!runInCheck(path, entity.getLocation())) {
return false;
}
if (!runGenericSwitchCheck(path, "cause", CoreUtilities.toLowerCase(event.getCause().name()))) {
return false;
}
if (!runGenericSwitchCheck(path, "effect", CoreUtilities.toLowerCase(event.getModifiedType().getName()))) {
return false;
}
return super.matches(path);
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String lower = CoreUtilities.toLowerCase(determinationObj.toString());
if (lower.startsWith("override:")) {
event.setOverride(lower.substring("override".length()).equals("true"));
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(entity);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("entity")) {
return entity.getDenizenObject();
}
else if (name.equals("cause")) {
return new ElementTag(event.getCause().name());
}
else if (name.equals("action")) {
return new ElementTag(event.getAction().name());
}
else if (name.equals("effect_type")) {
return new ElementTag(event.getModifiedType().getName());
}
else if (name.equals("override")) {
return new ElementTag(event.isOverride());
}
else if (name.equals("new_effect") && event.getNewEffect() != null) {
return new ElementTag(ItemPotion.stringifyEffect(event.getNewEffect()));
}
else if (name.equals("old_effect") && event.getOldEffect() != null) {
return new ElementTag(ItemPotion.stringifyEffect(event.getOldEffect()));
}
return super.getContext(name);
}
@EventHandler
public void onEntityPotionEffect(EntityPotionEffectEvent event) {
entity = new EntityTag(event.getEntity());
this.event = event;
fire(event);
}
}