Skip to content

Commit

Permalink
add EntityTag.allowed_colors, clean up EntityColor
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 17, 2020
1 parent 37b4fb7 commit 67d5d51
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 113 deletions.
Expand Up @@ -2,10 +2,6 @@

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;
Expand All @@ -17,6 +13,8 @@
import org.bukkit.DyeColor;
import org.bukkit.entity.*;

import java.util.Arrays;

public class EntityColor implements Property {

public static boolean describes(ObjectTag entity) {
Expand Down Expand Up @@ -52,7 +50,7 @@ public static EntityColor getFrom(ObjectTag entity) {
}

public static final String[] handledTags = new String[] {
"color"
"color", "allowed_colors"
};

public static final String[] handledMechs = new String[] {
Expand All @@ -65,9 +63,8 @@ private EntityColor(EntityTag entity) {

EntityTag colored;

private String getColor() {
public String getColor(boolean includeDeprecated) {
EntityType type = colored.getBukkitEntityType();

if (type == EntityType.HORSE) {
Horse horse = (Horse) colored.getBukkitEntity();
return horse.getColor().name() + "|" + horse.getStyle().name();
Expand All @@ -78,7 +75,7 @@ else if (type == EntityType.SHEEP) {
else if (type == EntityType.WOLF) {
return ((Wolf) colored.getBukkitEntity()).getCollarColor().name();
}
else if (type == EntityType.OCELOT) {
else if (type == EntityType.OCELOT && includeDeprecated) {
return ((Ocelot) colored.getBukkitEntity()).getCatType().name();
}
else if (type == EntityType.RABBIT) {
Expand All @@ -98,16 +95,19 @@ else if (type == EntityType.MUSHROOM_COW) {
return ((MushroomCow) colored.getBukkitEntity()).getVariant().name();
}
else if (type == EntityType.TROPICAL_FISH) {
return TropicalFishHelper.getColor(colored);
TropicalFish fish = ((TropicalFish) colored.getBukkitEntity());
return new ListTag(Arrays.asList(fish.getPattern().name(), fish.getBodyColor().name(), fish.getPatternColor().name())).identify();
}
else if (type == EntityType.FOX) {
return FoxHelper.getColor(colored);
return ((Fox) colored.getBukkitEntity()).getFoxType().name();
}
else if (type == EntityType.CAT) {
return CatHelper.getColor(colored);
Cat cat = (Cat) colored.getBukkitEntity();
return cat.getCatType().name() + "|" + cat.getCollarColor().name();
}
else if (type == EntityType.PANDA) {
return PandaHelper.getColor(colored);
Panda panda = (Panda) colored.getBukkitEntity();
return panda.getMainGene().name() + "|" + panda.getHiddenGene().name();
}
else if (type == EntityType.VILLAGER) {
return ((Villager) colored.getBukkitEntity()).getVillagerType().name();
Expand All @@ -120,14 +120,72 @@ else if (type == EntityType.ARROW) {
return null;
}
}
else { // Should never happen
else {
return null;
}
}

public static ListTag listForEnum(Enum<?>[] values) {
ListTag list = new ListTag(values.length);
for (Enum<?> obj : values) {
list.addObject(new ElementTag(obj.name()));
}
return list;
}

public ListTag getAllowedColors() {
EntityType type = colored.getBukkitEntityType();
if (type == EntityType.HORSE) {
ListTag toRet = listForEnum(Horse.Color.values());
toRet.addAll(listForEnum(Horse.Style.values()));
return toRet;
}
else if (type == EntityType.SHEEP) {
return listForEnum(DyeColor.values());
}
else if (type == EntityType.WOLF) {
return listForEnum(DyeColor.values());
}
else if (type == EntityType.RABBIT) {
return listForEnum(Rabbit.Type.values());
}
else if (type == EntityType.LLAMA || type == EntityType.TRADER_LLAMA) {
return listForEnum(Llama.Color.values());
}
else if (type == EntityType.PARROT) {
return listForEnum(Parrot.Variant.values());
}
else if (type == EntityType.SHULKER) {
return listForEnum(DyeColor.values());
}
else if (type == EntityType.MUSHROOM_COW) {
return listForEnum(MushroomCow.Variant.values());
}
else if (type == EntityType.TROPICAL_FISH) {
ListTag toRet = listForEnum(TropicalFish.Pattern.values());
toRet.addAll(listForEnum(DyeColor.values()));
return toRet;
}
else if (type == EntityType.FOX) {
return listForEnum(Fox.Type.values());
}
else if (type == EntityType.CAT) {
return listForEnum(Cat.Type.values());
}
else if (type == EntityType.PANDA) {
return listForEnum(Panda.Gene.values());
}
else if (type == EntityType.VILLAGER) {
return listForEnum(Villager.Type.values());
}
else { // includes Ocelot (deprecated) and arrow (ColorTag)
return null;
}
}

@Override
public String getPropertyString() {
String color = getColor();
String color = getColor(false);
return color == null ? null : CoreUtilities.toLowerCase(color);
}

Expand All @@ -142,13 +200,13 @@ public String getPropertyId() {
// @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>.
// The list of values can be gotten in-script via <@link tag EntityTag.allowed_colors>.
//
// 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).
// For cats (not ocelots), 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.
Expand All @@ -172,6 +230,24 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
return null;
}

// <--[tag]
// @attribute <EntityTag.allowed_colors>
// @returns ElementTag
// @mechanism EntityTag.color
// @group properties
// @description
// If the entity can have a color, returns the list of allowed colors.
// See also <@link language Entity Color Types>.
// -->
if (attribute.startsWith("allowed_colors")) {
ListTag colors = getAllowedColors();
if (colors == null) {
return null;
}
return colors
.getObjectAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <EntityTag.color>
// @returns ElementTag
Expand All @@ -182,7 +258,11 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
// For the available color options, refer to <@link language Entity Color Types>.
// -->
if (attribute.startsWith("color")) {
return new ElementTag(CoreUtilities.toLowerCase(getColor()))
String color = CoreUtilities.toLowerCase(getColor(true));
if (color == null) {
return null;
}
return new ElementTag(color)
.getObjectAttribute(attribute.fulfill(1));
}

Expand Down Expand Up @@ -223,7 +303,7 @@ else if (type == EntityType.SHEEP && mechanism.getValue().matchesEnum(DyeColor.v
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())) {
else if (type == EntityType.OCELOT && mechanism.getValue().matchesEnum(Ocelot.Type.values())) { // TODO: Deprecate?
((Ocelot) colored.getBukkitEntity()).setCatType(Ocelot.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.RABBIT && mechanism.getValue().matchesEnum(Rabbit.Type.values())) {
Expand All @@ -242,16 +322,32 @@ else if (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());
ListTag list = ListTag.valueOf(mechanism.getValue().asString(), CoreUtilities.basicContext);
TropicalFish fish = ((TropicalFish) colored.getBukkitEntity());
fish.setPattern(TropicalFish.Pattern.valueOf(list.get(0).toUpperCase()));
if (list.size() > 1) {
fish.setBodyColor(DyeColor.valueOf(list.get(1).toUpperCase()));
}
if (list.size() > 2) {
fish.setPatternColor(DyeColor.valueOf(list.get(2).toUpperCase()));
}
}
else if (type == EntityType.FOX) {
FoxHelper.setColor(colored, mechanism.getValue().asString());
((Fox) colored.getBukkitEntity()).setFoxType(Fox.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (type == EntityType.CAT) {
CatHelper.setColor(colored, mechanism.getValue().asString());
Cat cat = (Cat) colored.getBukkitEntity();
ListTag list = ListTag.valueOf(mechanism.getValue().asString(), CoreUtilities.basicContext);
cat.setCatType(Cat.Type.valueOf(list.get(0).toUpperCase()));
if (list.size() > 1) {
cat.setCollarColor(DyeColor.valueOf(list.get(1).toUpperCase()));
}
}
else if (type == EntityType.PANDA) {
PandaHelper.setColor(colored, mechanism.getValue().asString());
Panda panda = (Panda) colored.getBukkitEntity();
ListTag list = ListTag.valueOf(mechanism.getValue().asString(), CoreUtilities.basicContext);
panda.setMainGene(Panda.Gene.valueOf(list.get(0).toUpperCase()));
panda.setHiddenGene(Panda.Gene.valueOf(list.get(1).toUpperCase()));
}
else if (type == EntityType.VILLAGER) {
((Villager) colored.getBukkitEntity()).setVillagerType(Villager.Type.valueOf(mechanism.getValue().asString().toUpperCase()));
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 67d5d51

Please sign in to comment.