/
EntityColor.java
263 lines (243 loc) · 12.5 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
256
257
258
259
260
261
262
263
package com.denizenscript.denizen.objects.properties.entity;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.entity.CatHelper;
import com.denizenscript.denizen.utilities.entity.FoxHelper;
import com.denizenscript.denizen.utilities.entity.PandaHelper;
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_14) && type == EntityType.CAT) ||
(NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.PANDA) ||
(NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.ARROW) ||
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"
};
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) && type == EntityType.MUSHROOM_COW) {
return ((MushroomCow) colored.getBukkitEntity()).getVariant().name();
}
else if (type == EntityType.TROPICAL_FISH) {
return TropicalFishHelper.getColor(colored);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.FOX) {
return FoxHelper.getColor(colored);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.CAT) {
return CatHelper.getColor(colored);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.PANDA) {
return PandaHelper.getColor(colored);
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.ARROW) {
try {
return new ColorTag(((Arrow) colored.getBukkitEntity()).getColor()).identify();
}
catch (Exception e) {
return null;
}
}
else { // Should never happen
return null;
}
}
@Override
public String getPropertyString() {
String color = getColor();
return color == null ? null : CoreUtilities.toLowerCase(color);
}
@Override
public String getPropertyId() {
return "color";
}
// <--[language]
// @name Entity Color Types
// @group Properties
// @description
// This is a quick rundown of the styling information used to handle the coloration of a mob,
// in both <@link tag EntityTag.color> and <@link mechanism EntityTag.color>.
//
// For horses, the format is COLOR|STYLE,
// where COLOR is BLACK, BROWN, CHESTNUT, CREAMY, DARK_BROWN, GRAY, or WHITE.
// and where STYLE is WHITE, WHITE_DOTS, WHITEFIELD, BLACK_DOTS, or NONE.
// For rabbits, the types are BROWN, WHITE, BLACK, BLACK_AND_WHITE, GOLD, SALT_AND_PEPPER, or THE_KILLER_BUNNY.
// For ocelots, the types are BLACK_CAT, RED_CAT, SIAMESE_CAT, or WILD_OCELOT. (NOTE: Deprecated since 1.14 - now 'cat' entity type is separate)
// For cats, the format is TYPE|COLOR (see below).
// The types are TABBY, BLACK, RED, SIAMESE, BRITISH_SHORTHAIR, CALICO, PERSIAN, RAGDOLL, WHITE, JELLIE, and ALL_BLACK.
// For parrots, the types are BLUE, CYAN, GRAY, GREEN, or RED.
// For llamas, the types are CREAMY, WHITE, BROWN, and GRAY.
// For mushroom_cows, the types are RED and BROWN.
// For foxes, the types are RED and SNOW.
// For pandas, the format is MAIN_GENE|HIDDEN_GENE.
// The gene types are NORMAL, LAZY, WORRIED, PLAYFUL, BROWN, WEAK, and AGGRESSIVE.
// For tropical_fish, the input is PATTERN|BODYCOLOR|PATTERNCOLOR, where BodyColor and PatterenColor are both DyeColor (see below),
// and PATTERN is KOB, SUNSTREAK, SNOOPER, DASHER, BRINELY, SPOTTY, FLOPPER, STRIPEY, GLITTER, BLOCKFISH, BETTY, is CLAYFISH.
// For sheep, wolf, and shulker entities, the input is a Dye Color.
// For Tipped Arrow entities, the input is a ColorTag.
//
// For all places where a DyeColor is needed, the options are:
// BLACK, BLUE, BROWN, CYAN, GRAY, GREEN, LIGHT_BLUE, LIGHT_GRAY, LIME, MAGENTA, ORANGE, PINK, PURPLE, RED, WHITE, or YELLOW.
// -->
@Override
public ObjectTag getObjectAttribute(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.
// For the available color options, refer to <@link language Entity Color Types>.
// -->
if (attribute.startsWith("color")) {
return new ElementTag(CoreUtilities.toLowerCase(getColor()))
.getObjectAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object EntityTag
// @name color
// @input Element
// @description
// Changes the entity's color.
// For the available color options, refer to <@link language Entity Color Types>.
// @tags
// <EntityTag.color>
// <EntityTag.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) && type == EntityType.MUSHROOM_COW) {
((MushroomCow) colored.getBukkitEntity()).setVariant(MushroomCow.Variant.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.TROPICAL_FISH) {
TropicalFishHelper.setColor(colored, mechanism.getValue().asString());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.FOX) {
FoxHelper.setColor(colored, mechanism.getValue().asString());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.CAT) {
CatHelper.setColor(colored, mechanism.getValue().asString());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.PANDA) {
PandaHelper.setColor(colored, mechanism.getValue().asString());
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_14) && type == EntityType.ARROW) {
((Arrow) colored.getBukkitEntity()).setColor(mechanism.valueAsType(ColorTag.class).getColor());
}
else { // Should never happen
Debug.echoError("Could not apply color '" + mechanism.getValue().toString() + "' to entity of type " + type.name() + ".");
}
}
}
}