Add llama colour support; improve support for old game versions

Fixes issues with /spawnmob on older versions of the game.

(Also add llama colours, which were missing from the 1.11 update.)
This commit is contained in:
md678685 2019-05-18 20:56:34 +01:00
parent 09598d0173
commit 60ebe9e738
2 changed files with 174 additions and 107 deletions

View File

@ -1,6 +1,7 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.utils.EnumUtil; import com.earth2me.essentials.utils.EnumUtil;
import net.ess3.nms.refl.ReflUtil;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Ocelot; import org.bukkit.entity.Ocelot;
@ -14,6 +15,7 @@ public class MobCompat {
public static final EntityType CAT = EnumUtil.getEntityType("CAT", "OCELOT"); public static final EntityType CAT = EnumUtil.getEntityType("CAT", "OCELOT");
public enum CatType { public enum CatType {
// These are (loosely) Mojang names for the cats
SIAMESE("SIAMESE", "SIAMESE_CAT"), SIAMESE("SIAMESE", "SIAMESE_CAT"),
WHITE("WHITE", "SIAMESE_CAT"), WHITE("WHITE", "SIAMESE_CAT"),
RED("RED", "RED_CAT"), RED("RED", "RED_CAT"),
@ -37,6 +39,7 @@ public class MobCompat {
} }
public enum VillagerProfession { public enum VillagerProfession {
// These are 1.14+ villager professions mapped to their respective pre-V&P profession and career
NONE("FARMER", "FARMER", "NONE"), NONE("FARMER", "FARMER", "NONE"),
ARMORER("BLACKSMITH", "ARMORER"), ARMORER("BLACKSMITH", "ARMORER"),
BUTCHER("FARMER", "BUTCHER"), BUTCHER("FARMER", "BUTCHER"),
@ -75,34 +78,17 @@ public class MobCompat {
} }
} }
public enum VillagerType { // Older cats are Ocelots, whereas 1.14+ cats are Cats
DESERT, private static Class catClass = ReflUtil.getClassCached("org.bukkit.entity.Cat");
JUNGLE, private static Class catTypeClass = ReflUtil.getClassCached("org.bukkit.entity.Cat.Type");
PLAINS, private static Method catSetTypeMethod = (catClass == null || catTypeClass == null) ? null : ReflUtil.getMethodCached(catClass, "setCatType", catTypeClass);
SAVANNA,
SNOWY,
SWAMP,
TAIGA
}
private static Class catClass = null; private static boolean isNewCat() {
private static Class catTypeClass = null; return (catClass != null && catTypeClass != null && catSetTypeMethod != null);
private static Method catSetTypeMethod = null; }
private static Boolean isNewCat = null;
public static void setCatType(final Entity entity, final CatType type) { public static void setCatType(final Entity entity, final CatType type) {
if (isNewCat == null) { if (isNewCat()) {
try {
catClass = Class.forName("org.bukkit.entity.Cat");
catTypeClass = Class.forName("org.bukkit.entity.Cat.Type");
catSetTypeMethod = catClass.getDeclaredMethod("setCatType", catTypeClass);
isNewCat = true;
} catch (ClassNotFoundException | NoSuchMethodException e) {
isNewCat = false;
}
}
if (isNewCat) {
try { try {
catSetTypeMethod.invoke(entity, EnumUtil.valueOf(catTypeClass, type.catTypeName)); catSetTypeMethod.invoke(entity, EnumUtil.valueOf(catTypeClass, type.catTypeName));
} catch (IllegalAccessException | InvocationTargetException e) { } catch (IllegalAccessException | InvocationTargetException e) {
@ -113,58 +99,114 @@ public class MobCompat {
} }
} }
private static Boolean isNewVillager = null; // Older villagers have professions and careers, 1.14+ villagers only have professions
private static Class villagerCareerClass = null; private static Class villagerCareerClass = ReflUtil.getClassCached("org.bukkit.entity.Villager.Career");
private static Method villagerSetCareerMethod = null; private static Method villagerSetCareerMethod = (villagerCareerClass == null) ? null : ReflUtil.getMethodCached(Villager.class, "setCareer", villagerCareerClass);
private static Class villagerTypeClass = null;
private static Method villagerSetTypeMethod = null;
private static boolean isCareerVillager() {
private static void checkVillagerEnums() { return (villagerCareerClass != null && villagerSetCareerMethod != null);
try {
villagerCareerClass = Class.forName("org.bukkit.entity.Villager.Career");
villagerSetCareerMethod = Villager.class.getDeclaredMethod("setCareer", villagerCareerClass);
isNewVillager = false;
} catch (ClassNotFoundException | NoSuchMethodException e) {
try {
villagerTypeClass = Class.forName("org.bukkit.entity.Villager.Type");
villagerSetTypeMethod = Villager.class.getDeclaredMethod("setVillagerType", villagerTypeClass);
isNewVillager = true;
} catch (ClassNotFoundException | NoSuchMethodException e1) {
e1.printStackTrace();
}
}
} }
public static void setVillagerProfession(final Entity entity, final VillagerProfession profession) { public static void setVillagerProfession(final Entity entity, final VillagerProfession profession) {
if (isNewVillager == null) { if (!isCareerVillager()) {
checkVillagerEnums();
}
if (isNewVillager) {
((Villager) entity).setProfession(profession.asEnum()); ((Villager) entity).setProfession(profession.asEnum());
} else { } else {
((Villager) entity).setProfession(profession.asEnum()); ((Villager) entity).setProfession(profession.asEnum());
try { try {
villagerSetCareerMethod.invoke(entity, EnumUtil.valueOf(villagerCareerClass, profession.oldCareer)); villagerSetCareerMethod.invoke(entity, EnumUtil.valueOf(villagerCareerClass, profession.oldCareer));
} catch (IllegalAccessException | InvocationTargetException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
public static void setVillagerType(final Entity entity, final VillagerType type) { // Only 1.14+ villagers have biome variants
if (isNewVillager == null) { public static void setVillagerType(final Entity entity, final String type) {
checkVillagerEnums(); Class typeEnum = ReflUtil.getClassCached("org.bukkit.entity.Villager.Type");
if (typeEnum == null) return;
Method villagerSetTypeMethod = ReflUtil.getMethodCached(Villager.class, "setVillagerType", typeEnum);
try {
villagerSetTypeMethod.invoke(entity, EnumUtil.valueOf(typeEnum, type));
} catch (Exception e) {
e.printStackTrace();
}
} }
if (!isNewVillager) { // Llamas only exist in 1.11+
return; public static void setLlamaColor(final Entity entity, final String color) {
Class llamaClass = ReflUtil.getClassCached("org.bukkit.entity.Llama");
if (llamaClass == null) return;
Class colorEnum = ReflUtil.getClassCached("org.bukkit.entity.Llama.Color");
Method setVariantMethod = ReflUtil.getMethodCached(llamaClass, "setColor");
try {
setVariantMethod.invoke(entity, EnumUtil.valueOf(colorEnum, color));
} catch (Exception e) {
e.printStackTrace();
}
}
// Parrots only exist in 1.12+
public static void setParrotVariant(final Entity entity, final String variant) {
Class parrotClass = ReflUtil.getClassCached("org.bukkit.entity.Parrot");
if (parrotClass == null) return;
Class variantEnum = ReflUtil.getClassCached("org.bukkit.entity.Parrot.Variant");
Method setVariantMethod = ReflUtil.getMethodCached(parrotClass, "setVariant");
try {
setVariantMethod.invoke(entity, EnumUtil.valueOf(variantEnum, variant));
} catch (Exception e) {
e.printStackTrace();
}
}
// Tropical fish only exist in 1.13+
public static void setTropicalFishPattern(final Entity entity, final String pattern) {
Class tropicalFishClass = ReflUtil.getClassCached("org.bukkit.entity.TropicalFish");
if (tropicalFishClass == null) return;
Class patternEnum = ReflUtil.getClassCached("org.bukkit.entity.TropicalFish.Pattern");
Method setPatternMethod = ReflUtil.getMethodCached(tropicalFishClass, "setPattern");
try {
setPatternMethod.invoke(entity, EnumUtil.valueOf(patternEnum, pattern));
} catch (Exception e) {
e.printStackTrace();
}
}
// Mushroom cow variant API only exists in 1.14+
public static void setMooshroomVariant(final Entity entity, final String variant) {
Class mushroomCowClass = ReflUtil.getClassCached("org.bukkit.entity.MushroomCow");
Class variantEnum = ReflUtil.getClassCached("org.bukkit.entity.MushroomCow.Variant");
if (mushroomCowClass == null || variantEnum == null) return;
Method setVariantMethod = ReflUtil.getMethodCached(mushroomCowClass, "setVariant");
try {
setVariantMethod.invoke(entity, EnumUtil.valueOf(variantEnum, variant));
} catch (Exception e) {
e.printStackTrace();
}
}
// Pandas only exists in 1.14+
public static void setPandaGene(final Entity entity, final String gene, final boolean mainGene) {
Class pandaClass = ReflUtil.getClassCached("org.bukkit.entity.Panda");
if (pandaClass == null) return;
Class geneEnum = ReflUtil.getClassCached("org.bukkit.entity.Panda.Gene");
Method setGeneMethod;
if (mainGene) {
setGeneMethod = ReflUtil.getMethodCached(pandaClass, "setMainGene");
} else {
setGeneMethod = ReflUtil.getMethodCached(pandaClass, "setHiddenGene");
} }
try { try {
villagerSetTypeMethod.invoke(entity, EnumUtil.valueOf(villagerTypeClass, type.name())); setGeneMethod.invoke(entity, EnumUtil.valueOf(geneEnum, gene));
} catch (IllegalAccessException | InvocationTargetException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -106,44 +106,54 @@ public enum MobData {
SHEPHERD_VILLAGER("shepherd", EntityType.VILLAGER, MobCompat.VillagerProfession.SHEPHERD, true), SHEPHERD_VILLAGER("shepherd", EntityType.VILLAGER, MobCompat.VillagerProfession.SHEPHERD, true),
TOOLSMITH_VILLAGER("toolsmith", EntityType.VILLAGER, MobCompat.VillagerProfession.TOOLSMITH, true), TOOLSMITH_VILLAGER("toolsmith", EntityType.VILLAGER, MobCompat.VillagerProfession.TOOLSMITH, true),
WEAPONSMITH_VILLAGER("weaponsmith", EntityType.VILLAGER, MobCompat.VillagerProfession.WEAPONSMITH, true), WEAPONSMITH_VILLAGER("weaponsmith", EntityType.VILLAGER, MobCompat.VillagerProfession.WEAPONSMITH, true),
DESERT_VILLAGER("desert", EntityType.VILLAGER, MobCompat.VillagerType.DESERT, true), DESERT_VILLAGER("desert", EntityType.VILLAGER, "villagertype:DESERT", true),
JUNGLE_VILLAGER("jungle", EntityType.VILLAGER, MobCompat.VillagerType.JUNGLE, true), JUNGLE_VILLAGER("jungle", EntityType.VILLAGER, "villagertype:JUNGLE", true),
PLAINS_VILLAGER("plains", EntityType.VILLAGER, MobCompat.VillagerType.PLAINS, true), PLAINS_VILLAGER("plains", EntityType.VILLAGER, "villagertype:PLAINS", true),
SAVANNA_VILLAGER("savanna", EntityType.VILLAGER, MobCompat.VillagerType.SAVANNA, true), SAVANNA_VILLAGER("savanna", EntityType.VILLAGER, "villagertype:SAVANNA", true),
SNOWY_VILLAGER("snowy", EntityType.VILLAGER, MobCompat.VillagerType.SNOWY, true), SNOWY_VILLAGER("snowy", EntityType.VILLAGER, "villagertype:SNOWY", true),
SWAMP_VILLAGER("swamp", EntityType.VILLAGER, MobCompat.VillagerType.SWAMP, true), SWAMP_VILLAGER("swamp", EntityType.VILLAGER, "villagertype:SWAMP", true),
TAIGA_VILLAGER("taiga", EntityType.VILLAGER, MobCompat.VillagerType.TAIGA, true), TAIGA_VILLAGER("taiga", EntityType.VILLAGER, "villagertype:TAIGA", true),
SIZE_SLIME("", "<1-100>", EntityType.SLIME.getEntityClass(), Data.SIZE, true), SIZE_SLIME("", "<1-100>", EntityType.SLIME.getEntityClass(), Data.SIZE, true),
NUM_EXPERIENCE_ORB("", "<1-2000000000>", EntityType.EXPERIENCE_ORB, Data.EXP, true), NUM_EXPERIENCE_ORB("", "<1-2000000000>", EntityType.EXPERIENCE_ORB, Data.EXP, true),
RED_PARROT("red", EntityType.PARROT, Parrot.Variant.RED, true), RED_PARROT("red", EntityType.PARROT, "parrot:RED", true),
GREEN_PARROT("green", EntityType.PARROT, Parrot.Variant.GREEN, true), GREEN_PARROT("green", EntityType.PARROT, "parrot:GREEN", true),
BLUE_PARROT("blue", EntityType.PARROT, Parrot.Variant.BLUE, true), BLUE_PARROT("blue", EntityType.PARROT, "parrot:BLUE", true),
CYAN_PARROT("cyan", EntityType.PARROT, Parrot.Variant.CYAN, true), CYAN_PARROT("cyan", EntityType.PARROT, "parrot:CYAN", true),
GRAY_PARROT("gray", EntityType.PARROT, Parrot.Variant.GRAY, true), GRAY_PARROT("gray", EntityType.PARROT, "parrot:GRAY", true),
KOB_TROPICAL_FISH("kob", EntityType.TROPICAL_FISH, TropicalFish.Pattern.KOB, true), KOB_TROPICAL_FISH("kob", EntityType.TROPICAL_FISH, "tropicalfish:KOB", true),
SUNSTREAK_TROPICAL_FISH("sunstreak", EntityType.TROPICAL_FISH, TropicalFish.Pattern.SUNSTREAK, true), SUNSTREAK_TROPICAL_FISH("sunstreak", EntityType.TROPICAL_FISH, "tropicalfish:SUNSTREAK", true),
SNOOPER_TROPICAL_FISH("snooper", EntityType.TROPICAL_FISH, TropicalFish.Pattern.SNOOPER, true), SNOOPER_TROPICAL_FISH("snooper", EntityType.TROPICAL_FISH, "tropicalfish:SNOOPER", true),
DASHER_TROPICAL_FISH("dasher", EntityType.TROPICAL_FISH, TropicalFish.Pattern.DASHER, true), DASHER_TROPICAL_FISH("dasher", EntityType.TROPICAL_FISH, "tropicalfish:DASHER", true),
BRINELY_TROPICAL_FISH("brinely", EntityType.TROPICAL_FISH, TropicalFish.Pattern.BRINELY, true), BRINELY_TROPICAL_FISH("brinely", EntityType.TROPICAL_FISH, "tropicalfish:BRINELY", true),
SPOTTY_TROPICAL_FISH("spotty", EntityType.TROPICAL_FISH, TropicalFish.Pattern.SPOTTY, true), SPOTTY_TROPICAL_FISH("spotty", EntityType.TROPICAL_FISH, "tropicalfish:SPOTTY", true),
FLOPPER_TROPICAL_FISH("flopper", EntityType.TROPICAL_FISH, TropicalFish.Pattern.FLOPPER, true), FLOPPER_TROPICAL_FISH("flopper", EntityType.TROPICAL_FISH, "tropicalfish:FLOPPER", true),
STRIPEY_TROPICAL_FISH("stripey", EntityType.TROPICAL_FISH, TropicalFish.Pattern.STRIPEY, true), STRIPEY_TROPICAL_FISH("stripey", EntityType.TROPICAL_FISH, "tropicalfish:STRIPEY", true),
GLITTER_TROPICAL_FISH("glitter", EntityType.TROPICAL_FISH, TropicalFish.Pattern.GLITTER, true), GLITTER_TROPICAL_FISH("glitter", EntityType.TROPICAL_FISH, "tropicalfish:GLITTER", true),
BLOCKFISH_TROPICAL_FISH("blockfish", EntityType.TROPICAL_FISH, TropicalFish.Pattern.BLOCKFISH, true), BLOCKFISH_TROPICAL_FISH("blockfish", EntityType.TROPICAL_FISH, "tropicalfish:BLOCKFISH", true),
BETTY_TROPICAL_FISH("betty", EntityType.TROPICAL_FISH, TropicalFish.Pattern.BETTY, true), BETTY_TROPICAL_FISH("betty", EntityType.TROPICAL_FISH, "tropicalfish:BETTY", true),
CLAYFISH_TROPICAL_FISH("clayfish", EntityType.TROPICAL_FISH, TropicalFish.Pattern.CLAYFISH, true), CLAYFISH_TROPICAL_FISH("clayfish", EntityType.TROPICAL_FISH, "tropicalfish:CLAYFISH", true),
BROWN_MUSHROOM_COW("brown", EntityType.MUSHROOM_COW, MushroomCow.Variant.BROWN, true), BROWN_MUSHROOM_COW("brown", EntityType.MUSHROOM_COW, "mooshroom:BROWN", true),
RED_MUSHROOM_COW("red", EntityType.MUSHROOM_COW, MushroomCow.Variant.RED, true), RED_MUSHROOM_COW("red", EntityType.MUSHROOM_COW, "mooshroom:RED", true),
AGGRESSIVE_PANDA("aggressive", EntityType.PANDA, Panda.Gene.AGGRESSIVE, true), AGGRESSIVE_PANDA_MAIN("aggressive", EntityType.PANDA, "pandamain:AGGRESSIVE", true),
LAZY_PANDA("lazy", EntityType.PANDA, Panda.Gene.LAZY, true), LAZY_PANDA_MAIN("lazy", EntityType.PANDA, "pandamain:LAZY", true),
WORRIED_PANDA("worried", EntityType.PANDA, Panda.Gene.WORRIED, true), WORRIED_PANDA_MAIN("worried", EntityType.PANDA, "pandamain:WORRIED", true),
PLAYFUL_PANDA("playful", EntityType.PANDA, Panda.Gene.PLAYFUL, true), PLAYFUL_PANDA_MAIN("playful", EntityType.PANDA, "pandamain:PLAYFUL", true),
BROWN_PANDA("brown", EntityType.PANDA, Panda.Gene.BROWN, true), BROWN_PANDA_MAIN("brown", EntityType.PANDA, "pandamain:BROWN", true),
WEAK_PANDA("weak", EntityType.PANDA, Panda.Gene.WEAK, true), WEAK_PANDA_MAIN("weak", EntityType.PANDA, "pandamain:WEAK", true),
CREAMY_TRADER_LLAMA("creamy", EntityType.TRADER_LLAMA, TraderLlama.Color.CREAMY, true), AGGRESSIVE_PANDA_HIDDEN("aggressive_hidden", EntityType.PANDA, "pandahidden:AGGRESSIVE", true),
WHITE_TRADER_LLAMA("white", EntityType.TRADER_LLAMA, TraderLlama.Color.WHITE, true), LAZY_PANDA_HIDDEN("lazy_hidden", EntityType.PANDA, "pandahidden:LAZY", true),
BROWN_TRADER_LLAMA("brown", EntityType.TRADER_LLAMA, TraderLlama.Color.BROWN, true), WORRIED_PANDA_HIDDEN("worried_hidden", EntityType.PANDA, "pandahidden:WORRIED", true),
GRAY_TRADER_LLAMA("gray", EntityType.TRADER_LLAMA, TraderLlama.Color.GRAY, true) PLAYFUL_PANDA_HIDDEN("playful_hidden", EntityType.PANDA, "pandahidden:PLAYFUL", true),
BROWN_PANDA_HIDDEN("brown_hidden", EntityType.PANDA, "pandahidden:BROWN", true),
WEAK_PANDA_HIDDEN("weak_hidden", EntityType.PANDA, "pandahidden:WEAK", true),
CREAMY_LLAMA("creamy", EntityType.LLAMA, "llama:CREAMY", true),
WHITE_LLAMA("white", EntityType.LLAMA, "llama:WHITE", true),
BROWN_LLAMA("brown", EntityType.LLAMA, "llama:BROWN", true),
GRAY_LLAMA("gray", EntityType.LLAMA, "llama:GRAY", true),
CREAMY_TRADER_LLAMA("creamy", EntityType.TRADER_LLAMA, "llama:CREAMY", true),
WHITE_TRADER_LLAMA("white", EntityType.TRADER_LLAMA, "llama:WHITE", true),
BROWN_TRADER_LLAMA("brown", EntityType.TRADER_LLAMA, "llama:BROWN", true),
GRAY_TRADER_LLAMA("gray", EntityType.TRADER_LLAMA, "llama:GRAY", true)
; ;
@ -292,8 +302,6 @@ public enum MobData {
MobCompat.setCatType(spawned, (MobCompat.CatType) this.value); MobCompat.setCatType(spawned, (MobCompat.CatType) this.value);
} else if (this.value instanceof MobCompat.VillagerProfession) { } else if (this.value instanceof MobCompat.VillagerProfession) {
MobCompat.setVillagerProfession(spawned, (MobCompat.VillagerProfession) this.value); MobCompat.setVillagerProfession(spawned, (MobCompat.VillagerProfession) this.value);
} else if (this.value instanceof MobCompat.VillagerType) {
MobCompat.setVillagerType(spawned, (MobCompat.VillagerType) this.value);
} else if (this.value instanceof Material) { } else if (this.value instanceof Material) {
if (this.type.equals(EntityType.HORSE)) { if (this.type.equals(EntityType.HORSE)) {
((Horse) spawned).setTamed(true); ((Horse) spawned).setTamed(true);
@ -303,16 +311,33 @@ public enum MobData {
InventoryWorkaround.setItemInMainHand(invent, new ItemStack((Material) this.value, 1)); InventoryWorkaround.setItemInMainHand(invent, new ItemStack((Material) this.value, 1));
InventoryWorkaround.setItemInMainHandDropChance(invent, 0.1f); InventoryWorkaround.setItemInMainHandDropChance(invent, 0.1f);
} }
} else if (this.value instanceof Parrot.Variant) { // TODO: MobCompat } else if (this.value instanceof String) {
((Parrot) spawned).setVariant((Parrot.Variant) this.value); final String[] split = ((String) this.value).split(":");
} else if (this.value instanceof TropicalFish.Pattern) { // TODO: MobCompat switch (split[0]) {
((TropicalFish) spawned).setPattern((TropicalFish.Pattern) this.value); case "parrot":
} else if (this.value instanceof MushroomCow.Variant) { // TODO: MobCompat MobCompat.setParrotVariant(spawned, split[1]);
((MushroomCow) spawned).setVariant((MushroomCow.Variant) this.value); break;
} else if (this.value instanceof Panda.Gene) { // TODO: MobCompat case "tropicalfish":
((Panda) spawned).setMainGene((Panda.Gene) this.value); MobCompat.setTropicalFishPattern(spawned, split[1]);
} else if (this.value instanceof TraderLlama.Color) { // TODO: MobCompat break;
((TraderLlama) spawned).setColor((TraderLlama.Color) this.value); case "mooshroom":
MobCompat.setMooshroomVariant(spawned, split[1]);
break;
case "pandamain":
MobCompat.setPandaGene(spawned, split[1], true);
break;
case "pandahidden":
MobCompat.setPandaGene(spawned, split[1], false);
break;
case "llama":
MobCompat.setLlamaColor(spawned, split[1]);
break;
case "villagertype":
MobCompat.setVillagerType(spawned, split[1]);
break;
}
} else {
logger.warning("Unknown mob data type: " + this.toString());
} }
} }
} }