-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
EntityAttributeBaseValues.java
191 lines (170 loc) · 8.64 KB
/
EntityAttributeBaseValues.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
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.objects.EntityTag;
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.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import org.bukkit.attribute.Attributable;
import org.bukkit.attribute.AttributeInstance;
import java.util.Map;
public class EntityAttributeBaseValues implements Property {
public static boolean describes(ObjectTag entity) {
return entity instanceof EntityTag && ((EntityTag) entity).getBukkitEntity() instanceof Attributable;
}
public static EntityAttributeBaseValues getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityAttributeBaseValues((EntityTag) entity);
}
}
public static final String[] handledTags = new String[] {
"has_attribute", "attribute_value", "attribute_base_value", "attribute_default_value"
};
public static final String[] handledMechs = new String[] {
"attribute_base_values"
};
private EntityAttributeBaseValues(EntityTag entity) {
this.entity = entity;
}
EntityTag entity;
public MapTag attributeBaseValues() {
MapTag result = new MapTag();
Attributable ent = (Attributable) entity.getBukkitEntity();
for (org.bukkit.attribute.Attribute attr : org.bukkit.attribute.Attribute.values()) {
AttributeInstance instance = ent.getAttribute(attr);
if (instance != null) {
result.putObject(attr.name(), new ElementTag(instance.getBaseValue()));
}
}
return result;
}
@Override
public String getPropertyString() {
MapTag map = attributeBaseValues();
if (map.map.isEmpty()) {
return null;
}
return map.savable();
}
@Override
public String getPropertyId() {
return "attribute_base_values";
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <EntityTag.has_attribute[<attribute>]>
// @returns ElementTag(Boolean)
// @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>
// See also <@link language attribute modifiers>.
// -->
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 EntityTag.attribute_base_values
// @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>
// See also <@link language attribute modifiers>.
// -->
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) {
attribute.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 EntityTag.attribute_base_values
// @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>
// See also <@link language attribute modifiers>.
// -->
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) {
attribute.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 EntityTag.attribute_base_values
// @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>
// See also <@link language attribute modifiers>.
// -->
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) {
attribute.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 attribute_base_values
// @input MapTag
// @description
// Sets the base value of an entity's attributes.
// Specify a MapTag where the keys are attribute names, and values are the new base values.
// Valid attribute names are listed at <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/attribute/Attribute.html>
// See also <@link language attribute modifiers>.
// @tags
// <EntityTag.has_attribute>
// <EntityTag.attribute_default_value>
// <EntityTag.attribute_base_value>
// <EntityTag.attribute_value>
// -->
if (mechanism.matches("attribute_base_values") && mechanism.requireObject(MapTag.class)) {
MapTag input = mechanism.valueAsType(MapTag.class);
Attributable ent = (Attributable) entity.getBukkitEntity();
for (Map.Entry<StringHolder, ObjectTag> subValue : input.map.entrySet()) {
org.bukkit.attribute.Attribute attr = org.bukkit.attribute.Attribute.valueOf(subValue.getKey().str.toUpperCase());
AttributeInstance instance = ent.getAttribute(attr);
if (instance == null) {
mechanism.echoError("Attribute " + attr.name() + " is not applicable to entity of type " + entity.getBukkitEntity().getType().name());
continue;
}
instance.setBaseValue(Double.parseDouble(subValue.getValue().toString()));
}
}
}
}