/
EntityPotionEffects.java
161 lines (142 loc) · 6.3 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
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.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
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[] handledTags = new String[] {
"list_effects", "has_effect"
};
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 (entity.getBukkitEntity() instanceof Arrow) {
return ((Arrow) entity.getBukkitEntity()).getCustomEffects();
}
return new ArrayList<>();
}
public String getPropertyString() {
Collection<PotionEffect> effects = getEffectsList();
if (effects.isEmpty()) {
return null;
}
ListTag returnable = new ListTag();
for (PotionEffect effect : effects) {
returnable.add(ItemPotion.stringifyEffect(effect));
}
return returnable.identify();
}
public String getPropertyId() {
return "potion_effects";
}
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <EntityTag.list_effects>
// @returns ListTag
// @group attribute
// @mechanism EntityTag.potion_effects
// @description
// Returns the list of active potion effects on the entity, in the format: TYPE,AMPLIFIER,DURATION,IS_AMBIENT,HAS_PARTICLES,HAS_ICON|...
// Note that AMPLIFIER is a number representing the level, and DURATION is a number representing the time, in ticks, it will last for.
// IS_AMBIENT, HAS_PARTICLES, and HAS_ICON are booleans.
// The effect type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
if (attribute.startsWith("list_effects")) {
ListTag effects = new ListTag();
for (PotionEffect effect : getEffectsList()) {
effects.add(ItemPotion.stringifyEffect(effect));
}
return effects.getObjectAttribute(attribute.fulfill(1));
}
// <--[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>.
// -->
if (attribute.startsWith("has_effect")) {
boolean returnElement = false;
if (attribute.hasContext(1)) {
PotionEffectType effectType = PotionEffectType.getByName(attribute.getContext(1));
for (org.bukkit.potion.PotionEffect effect : getEffectsList()) {
if (effect.getType().equals(effectType)) {
returnElement = true;
}
}
}
else if (!getEffectsList().isEmpty()) {
returnElement = true;
}
return new ElementTag(returnElement).getObjectAttribute(attribute.fulfill(1));
}
return null;
}
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 is formatted as: TYPE,AMPLIFIER,DURATION,IS_AMBIENT,HAS_PARTICLES,HAS_ICON
// Note that AMPLIFIER is a number representing the level, and DURATION is a number representing the time, in ticks, it will last for.
// IS_AMBIENT, HAS_PARTICLES, and HAS_ICON are booleans.
// For example: SPEED,0,120,false,true,true would give the entity a swiftness potion for 120 ticks.
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// @tags
// <EntityTag.list_effects>
// <EntityTag.has_effect[<effect>]>
// -->
if (mechanism.matches("potion_effects")) {
ListTag effects = ListTag.valueOf(mechanism.getValue().asString(), mechanism.context);
for (String effectStr : effects) {
PotionEffect effect = ItemPotion.parseEffect(effectStr);
if (entity.isLivingEntity()) {
entity.getLivingEntity().addPotionEffect(effect);
}
else if (entity.getBukkitEntity() instanceof Arrow) {
((Arrow) entity.getBukkitEntity()).addCustomEffect(effect, true);
}
}
}
}
}