/
EntityColor.java
255 lines (225 loc) · 10.6 KB
/
EntityColor.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
249
250
251
252
253
254
255
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.TropicalFishHelper;
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 com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.DyeColor;
import org.bukkit.entity.*;
public class EntityColor implements Property {
public static boolean describes(ObjectTag entity) {
if (!(entity instanceof EntityTag)) {
return false;
}
EntityType type = ((EntityTag) entity).getBukkitEntityType();
return type == EntityType.SHEEP ||
type == EntityType.HORSE ||
type == EntityType.WOLF ||
type == EntityType.OCELOT ||
type == EntityType.RABBIT ||
type == EntityType.LLAMA ||
type == EntityType.PARROT ||
type == EntityType.SHULKER ||
type == EntityType.MUSHROOM_COW ||
(NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2) && type == EntityType.TROPICAL_FISH);
}
public static EntityColor getFrom(ObjectTag entity) {
if (!describes(entity)) {
return null;
}
else {
return new EntityColor((EntityTag) entity);
}
}
public static final String[] handledTags = new String[] {
"color"
};
public static final String[] handledMechs = new String[] {
"color"
};
///////////////////
// Instance Fields and Methods
/////////////
private EntityColor(EntityTag entity) {
colored = entity;
}
EntityTag colored;
private String getColor() {
EntityType type = colored.getBukkitEntityType();
if (type == EntityType.HORSE) {
Horse horse = (Horse) colored.getBukkitEntity();
return horse.getColor().name() + "|" + horse.getStyle().name();
}
else if (type == EntityType.SHEEP) {
return ((Sheep) colored.getBukkitEntity()).getColor().name();
}
else if (type == EntityType.WOLF) {
return ((Wolf) colored.getBukkitEntity()).getCollarColor().name();
}
else if (type == EntityType.OCELOT) {
return ((Ocelot) colored.getBukkitEntity()).getCatType().name();
}
else if (type == EntityType.RABBIT) {
return ((Rabbit) colored.getBukkitEntity()).getRabbitType().name();
}
else if (type == EntityType.LLAMA) {
return ((Llama) colored.getBukkitEntity()).getColor().name();
}
else if (type == EntityType.PARROT) {
return ((Parrot) colored.getBukkitEntity()).getVariant().name();
}
else if (type == EntityType.SHULKER) {
DyeColor color = ((Shulker) colored.getBukkitEntity()).getColor();
return color == null ? null : color.name();
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14_R1) && type == EntityType.MUSHROOM_COW) {
return ((MushroomCow) colored.getBukkitEntity()).getVariant().name();
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2) && type == EntityType.TROPICAL_FISH) {
return TropicalFishHelper.getColor(colored);
}
else // Should never happen
{
return null;
}
}
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
String color = getColor();
return color == null ? null : CoreUtilities.toLowerCase(color);
}
@Override
public String getPropertyId() {
return "color";
}
///////////
// ObjectTag Attributes
////////
// <--[language]
// @name Horse Types
// @group Properties
// @description
// This is a quick rundown of the styling information used to create a horse,
// used for both <@link tag EntityTag.color> and <@link mechanism EntityTag.color>.
//
// The output/input is formatted as COLOR|STYLE(|VARIANT)
// Where color is:
// BLACK, BROWN, CHESTNUT, CREAMY, DARK_BROWN, GRAY, or WHITE.
// and where style is:
// WHITE, WHITE_DOTS, WHITEFIELD, BLACK_DOTS, or NONE.
// and where variant is:
// DONKEY, MULE, SKELETON_HORSE, UNDEAD_HORSE, or HORSE.
// NOTE: HORSE VARIANTS DEPRECATED SINCE 1.11, use spawn instead
// -->
// <--[language]
// @name Rabbit Types
// @group Properties
// @description
// Denizen includes its own user-friendly list of rabbit type names, instead
// of relying on Bukkit names which did not exist at the time of writing.
//
// Types currently available:
// BROWN, WHITE, BLACK, WHITE_SPLOTCHED, GOLD, SALT, KILLER.
//
// Note: The KILLER rabbit type is a hostile rabbit type. It will attempt to kill
// nearby players and wolves. Use at your own risk.
// -->
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <EntityTag.color>
// @returns ElementTag
// @mechanism EntityTag.color
// @group properties
// @description
// If the entity can have a color, returns the entity's color. A few entity types can have colors:
// For horses, the output is COLOR|STYLE(|VARIANT), see <@link language horse types>.
// NOTE: HORSE VARIANTS DEPRECATED SINCE 1.11, use spawn instead
// For ocelots, the types are BLACK_CAT, RED_CAT, SIAMESE_CAT, or WILD_OCELOT.
// For rabbit types, see <@link language rabbit types>.
// For parrots, the types are BLUE, CYAN, GRAY, GREEN, or RED.
// For llamas, the types are CREAMY, WHITE, BROWN, and GRAY.
// For MushroomCows, the types are RED and BROWN.
// For Tropical_Fish, the input is Pattern|BodyColor|PatternColor, see <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/TropicalFish.Pattern.html>
// For sheep, wolf, and shulker entities, see <@link url https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/DyeColor.html>
// -->
if (attribute.startsWith("color")) {
return new ElementTag(CoreUtilities.toLowerCase(getColor()))
.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name color
// @input Element
// @description
// Changes the entity's color. Refer to <@link tag EntityTag.color>.
// @tags
// <EntityTag.color>
// <EntityTag.is_colorable>
// -->
if (mechanism.matches("color")) {
EntityType type = colored.getBukkitEntityType();
if (type == EntityType.HORSE) {
ListTag horse_info = mechanism.valueAsType(ListTag.class);
if (horse_info.size() > 0 && new ElementTag(horse_info.get(0)).matchesEnum(Horse.Color.values())) {
((Horse) colored.getBukkitEntity())
.setColor(Horse.Color.valueOf(horse_info.get(0).toUpperCase()));
}
if (horse_info.size() > 1 && new ElementTag(horse_info.get(1)).matchesEnum(Horse.Style.values())) {
((Horse) colored.getBukkitEntity())
.setStyle(Horse.Style.valueOf(horse_info.get(1).toUpperCase()));
}
}
else if (type == EntityType.SHEEP && mechanism.getValue().matchesEnum(DyeColor.values())) {
((Sheep) colored.getBukkitEntity())
.setColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.WOLF && mechanism.getValue().matchesEnum(DyeColor.values())) {
((Wolf) colored.getBukkitEntity())
.setCollarColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.OCELOT && mechanism.getValue().matchesEnum(Ocelot.Type.values())) {
((Ocelot) colored.getBukkitEntity())
.setCatType(Ocelot.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.RABBIT && mechanism.getValue().matchesEnum(Rabbit.Type.values())) {
((Rabbit) colored.getBukkitEntity()).setRabbitType(Rabbit.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.RABBIT && mechanism.getValue().matchesEnum(Rabbit.Type.values())) {
((Rabbit) colored.getBukkitEntity()).setRabbitType(Rabbit.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.LLAMA) {
((Llama) colored.getBukkitEntity()).setColor(Llama.Color.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.PARROT) {
((Parrot) colored.getBukkitEntity()).setVariant(Parrot.Variant.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.SHULKER && mechanism.getValue().matchesEnum(DyeColor.values())) {
((Shulker) colored.getBukkitEntity()).setColor(DyeColor.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14_R1) && type == EntityType.MUSHROOM_COW) {
((MushroomCow) colored.getBukkitEntity()).setVariant(MushroomCow.Variant.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else {
Debug.echoError("Could not apply color '" + mechanism.getValue().toString() + "' to entity of type " + type.name() + ".");
}
}
}
}