/
EntityAttributes.java
248 lines (221 loc) · 10.8 KB
/
EntityAttributes.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.inventory.EquipmentSlot;
import java.util.List;
import java.util.UUID;
public class EntityAttributes implements Property {
public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag && ((EntityTag) entity).getBukkitEntity() instanceof Attributable;
}
public static EntityAttributes getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityAttributes((EntityTag) entity);
}
}
public static final String[] handledTags = new String[] {
"has_attribute", "attribute_value", "attribute_base_value", "attribute_default_value", "attributes"
};
public static final String[] handledMechs = new String[] {
"attributes"
};
///////////////////
// Instance Fields and Methods
/////////////
private EntityAttributes(EntityTag entity) {
this.entity = entity;
}
EntityTag entity;
/////////
// Property Methods
///////
public static String stringify(AttributeModifier modifier) {
return EscapeTagBase.escape(modifier.getName()) + "/" + modifier.getAmount() + "/" + modifier.getOperation().name()
+ "/" + (modifier.getSlot() == null ? "any" : modifier.getSlot().name());
}
public ListTag getAttributes() {
ListTag list = new ListTag();
for (org.bukkit.attribute.Attribute attribute : org.bukkit.attribute.Attribute.values()) {
AttributeInstance instance = ((Attributable) entity.getBukkitEntity()).getAttribute(attribute);
if (instance == null) {
continue;
}
StringBuilder modifiers = new StringBuilder();
for (AttributeModifier modifier : instance.getModifiers()) {
modifiers.append("/").append(stringify(modifier));
}
list.add(EscapeTagBase.escape(attribute.name()) + "/" + instance.getBaseValue() + modifiers.toString());
}
return list;
}
@Override
public String getPropertyString() {
ListTag list = getAttributes();
if (list.size() > 0) {
return list.identify();
}
return null;
}
@Override
public String getPropertyId() {
return "attributes";
}
///////////
// ObjectTag Attributes
////////
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <EntityTag.attributes>
// @returns ListTag
// @mechanism attributes
// @group properties
// @description
// Returns a list of all attributes on the entity, formatted in a way that can be sent back into the 'attributes' mechanism.
// -->
if (attribute.startsWith("attributes")) {
return getAttributes().getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <EntityTag.has_attribute[<attribute>]>
// @returns ElementTag(Boolean)
// @mechanism attributes
// @group properties
// @description
// Returns whether the entity has the named attribute.
// Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
// -->
if (attribute.startsWith("has_attribute") && attribute.hasContext(1)) {
AttributeInstance instance = ((Attributable) entity.getBukkitEntity()).getAttribute(
org.bukkit.attribute.Attribute.valueOf(attribute.getContext(1).toUpperCase()));
return new ElementTag(instance != null).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <EntityTag.attribute_value[<attribute>]>
// @returns ElementTag(Decimal)
// @mechanism attributes
// @group properties
// @description
// Returns the final calculated value of the named attribute for the entity.
// See also <@link tag EntityTag.has_attribute>.
// Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
// -->
if (attribute.startsWith("attribute_value") && attribute.hasContext(1)) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(attribute.getContext(1).toUpperCase());
AttributeInstance instance = ((Attributable) entity.getBukkitEntity()).getAttribute(attr);
if (instance == null) {
if (!attribute.hasAlternative()) {
Debug.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntity().getType().name());
}
return null;
}
return new ElementTag(instance.getValue()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <EntityTag.attribute_base_value[<attribute>]>
// @returns ElementTag(Decimal)
// @mechanism attributes
// @group properties
// @description
// Returns the base value of the named attribute for the entity.
// See also <@link tag EntityTag.has_attribute>.
// Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
// -->
if (attribute.startsWith("attribute_base_value") && attribute.hasContext(1)) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(attribute.getContext(1).toUpperCase());
AttributeInstance instance = ((Attributable) entity.getBukkitEntity()).getAttribute(attr);
if (instance == null) {
if (!attribute.hasAlternative()) {
Debug.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntity().getType().name());
}
return null;
}
return new ElementTag(instance.getBaseValue()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <EntityTag.attribute_default_value[<attribute>]>
// @returns ElementTag(Decimal)
// @mechanism attributes
// @group properties
// @description
// Returns the default value of the named attribute for the entity.
// See also <@link tag EntityTag.has_attribute>.
// Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
// -->
if (attribute.startsWith("attribute_default_value") && attribute.hasContext(1)) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(attribute.getContext(1).toUpperCase());
AttributeInstance instance = ((Attributable) entity.getBukkitEntity()).getAttribute(attr);
if (instance == null) {
if (!attribute.hasAlternative()) {
Debug.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntity().getType().name());
}
return null;
}
return new ElementTag(instance.getDefaultValue()).getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name attributes
// @input ListTag
// @description
// Changes the attributes of an entity.
// Specify a list of attributes in the format: name/base/value/(modifier_name/mod_amount/mod_operation/mod_slot|...)
// For example: GENERIC_ARMOR/5/boost/3/add_number/any/reduction/-5/add_number/chest
// Note that the 'slot' value is probably just ignored.
//
// Valid operations are ADD_NUMBER, ADD_SCALAR, and MULTIPLY_SCALAR_1
// @tags
// <EntityTag.has_attribute>
// <EntityTag.attributes>
// <EntityTag.attribute_default_value>
// <EntityTag.attribute_base_value>
// <EntityTag.attribute_value>
// -->
if (mechanism.matches("attributes") && mechanism.hasValue()) {
Attributable ent = (Attributable) entity.getBukkitEntity();
ListTag list = mechanism.valueAsType(ListTag.class);
for (String str : list) {
List<String> subList = CoreUtilities.split(str, '/');
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(EscapeTagBase.unEscape(subList.get(0)).toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
Debug.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntity().getType().name());
continue;
}
instance.setBaseValue(ArgumentHelper.getDoubleFrom(subList.get(1)));
for (AttributeModifier modifier : instance.getModifiers()) {
instance.removeModifier(modifier);
}
for (int x = 2; x < subList.size(); x += 4) {
String slot = subList.get(x + 3).toUpperCase();
AttributeModifier modifier = new AttributeModifier(UUID.randomUUID(), EscapeTagBase.unEscape(subList.get(x)),
ArgumentHelper.getDoubleFrom(subList.get(x + 1)), AttributeModifier.Operation.valueOf(subList.get(x + 2).toUpperCase()),
slot.equals("ANY") ? null : EquipmentSlot.valueOf(slot));
instance.addModifier(modifier);
}
}
}
}
}