/
EntityPotionEffects.java
198 lines (177 loc) · 7.54 KB
/
EntityPotionEffects.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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.objects.properties.item.ItemPotion;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.entity.Arrow;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.Collection;
public class EntityPotionEffects implements Property {
public static boolean describes(ObjectTag object) {
if (!(object instanceof EntityTag)) {
return false;
}
return ((EntityTag) object).isLivingEntity()
|| ((EntityTag) object).getBukkitEntity() instanceof Arrow;
}
public static EntityPotionEffects getFrom(ObjectTag object) {
if (!describes(object)) {
return null;
}
else {
return new EntityPotionEffects((EntityTag) object);
}
}
public static final String[] handledMechs = new String[] {
"potion_effects"
};
private EntityPotionEffects(EntityTag entity) {
this.entity = entity;
}
EntityTag entity;
public Collection<PotionEffect> getEffectsList() {
if (entity.isLivingEntity()) {
return entity.getLivingEntity().getActivePotionEffects();
}
else if (isArrow()) {
return getArrow().getCustomEffects();
}
return new ArrayList<>();
}
public ListTag getEffectsListTag() {
ListTag result = new ListTag();
for (PotionEffect effect : getEffectsList()) {
result.add(ItemPotion.stringifyEffect(effect));
}
return result;
}
public ListTag getEffectsMapTag() {
ListTag result = new ListTag();
for (PotionEffect effect : getEffectsList()) {
result.addObject(ItemPotion.effectToMap(effect));
}
return result;
}
public boolean isArrow() {
return entity.getBukkitEntity() instanceof Arrow;
}
public Arrow getArrow() {
return (Arrow) entity.getBukkitEntity();
}
public String getPropertyString() {
ListTag effects = getEffectsMapTag();
return effects.isEmpty() ? null : effects.identify();
}
public String getPropertyId() {
return "potion_effects";
}
public static void registerTags() {
// <--[tag]
// @attribute <EntityTag.list_effects>
// @returns ListTag
// @group attribute
// @mechanism EntityTag.potion_effects
// @deprecated use 'effects_data' instead
// @description
// Deprecated in favor of <@link tag EntityTag.effects_data>
// -->
PropertyParser.<EntityPotionEffects, ListTag>registerTag(ListTag.class, "list_effects", (attribute, object) -> {
Deprecations.oldPotionEffects.warn(attribute.context);
return object.getEffectsListTag();
});
// <--[tag]
// @attribute <EntityTag.effects_data>
// @returns ListTag(MapTag)
// @group attribute
// @mechanism EntityTag.potion_effects
// @description
// Returns the active potion effects on the entity, in the MapTag format of the mechanism.
// -->
PropertyParser.<EntityPotionEffects, ListTag>registerTag(ListTag.class, "effects_data", (attribute, object) -> {
return object.getEffectsMapTag();
});
// <--[tag]
// @attribute <EntityTag.has_effect[<effect>]>
// @returns ElementTag(Boolean)
// @group attributes
// @mechanism EntityTag.potion_effects
// @description
// Returns whether the entity has a specified effect.
// If no effect is specified, returns whether the entity has any effect.
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
PropertyParser.<EntityPotionEffects, ElementTag>registerTag(ElementTag.class, "has_effect", (attribute, object) -> {
boolean returnElement = false;
if (attribute.hasParam()) {
PotionEffectType effectType = PotionEffectType.getByName(attribute.getParam());
if (effectType == null) {
attribute.echoError("Invalid effect type specified: " + attribute.getParam());
return null;
}
if (object.entity.isLivingEntity()) {
returnElement = object.entity.getLivingEntity().hasPotionEffect(effectType);
}
else if (object.isArrow()) {
returnElement = object.getArrow().hasCustomEffect(effectType);
}
}
else if (!object.getEffectsList().isEmpty()) {
returnElement = true;
}
return new ElementTag(returnElement);
});
}
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name potion_effects
// @input ListTag
// @description
// Set the entity's active potion effects.
// Each item in the list must be a MapTag with keys:
// "type" - from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>
// "amplifier" - number to increase the level by (0 for default level 1)
// "duration" - DurationTag, how long it lasts
// "ambient", "particles", "icon" - booleans
//
// For example: [type=SPEED;amplifier=0;duration=120t;ambient=false;particles=true;icon=true]
// This example would be a level 1 swiftness potion that lasts 120 ticks.
// @tags
// <EntityTag.effects_data>
// <EntityTag.list_effects>
// <EntityTag.has_effect[<effect>]>
// -->
if (mechanism.matches("potion_effects")) {
for (ObjectTag effectObj : CoreUtilities.objectToList(mechanism.value, mechanism.context)) {
PotionEffect effect;
if (effectObj.canBeType(MapTag.class)) {
MapTag effectMap = effectObj.asType(MapTag.class, mechanism.context);
effect = ItemPotion.parseEffect(effectMap, mechanism.context);
}
else {
String effectStr = effectObj.toString();
effect = ItemPotion.parseEffect(effectStr, mechanism.context);
}
if (effect == null) {
mechanism.echoError("Invalid potion effect '" + effectObj + "'");
continue;
}
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(effect);
}
else if (isArrow()) {
getArrow().addCustomEffect(effect, true);
}
}
}
}
}