From 3f27bcf99946845f91f0185d45ac273ca9f7b929 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 23 Dec 2017 10:37:56 +1300 Subject: [PATCH] Add VillagerProfession and ZombieProfession because Villager.Profession is pure stupid --- .../libraryaddict/disguise/LibsDisguises.java | 52 +++++++++++-------- .../disguisetypes/VillagerProfession.java | 13 +++++ .../disguisetypes/ZombieProfession.java | 9 ++++ .../watchers/VillagerWatcher.java | 15 +++++- .../watchers/ZombieVillagerWatcher.java | 10 ++++ .../disguise/utilities/DisguiseParser.java | 17 ++++-- .../utilities/ReflectionFlagWatchers.java | 4 +- 7 files changed, 91 insertions(+), 29 deletions(-) create mode 100644 src/me/libraryaddict/disguise/disguisetypes/VillagerProfession.java create mode 100644 src/me/libraryaddict/disguise/disguisetypes/ZombieProfession.java diff --git a/src/me/libraryaddict/disguise/LibsDisguises.java b/src/me/libraryaddict/disguise/LibsDisguises.java index 9fcffb58..056497b6 100644 --- a/src/me/libraryaddict/disguise/LibsDisguises.java +++ b/src/me/libraryaddict/disguise/LibsDisguises.java @@ -251,7 +251,8 @@ public class LibsDisguises extends JavaPlugin { } /** - * Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their sound volume + * Here we create a nms entity for each disguise. Then grab their default values in their datawatcher. Then their + * sound volume * for mob noises. As well as setting their watcher class and entity size. */ private void registerValues() { @@ -283,6 +284,8 @@ public class LibsDisguises extends JavaPlugin { watcherClass = SpiderWatcher.class; break; case ZOMBIE_VILLAGER: + watcherClass = ZombieVillagerWatcher.class; + break; case PIG_ZOMBIE: case HUSK: watcherClass = ZombieWatcher.class; @@ -302,8 +305,9 @@ public class LibsDisguises extends JavaPlugin { watcherClass = IllagerWizardWatcher.class; break; default: - watcherClass = Class.forName("me.libraryaddict.disguise.disguisetypes.watchers." + toReadable( - disguiseType.name()) + "Watcher"); + watcherClass = Class.forName( + "me.libraryaddict.disguise.disguisetypes.watchers." + toReadable(disguiseType.name()) + + "Watcher"); break; } } @@ -438,10 +442,11 @@ public class LibsDisguises extends JavaPlugin { MetaIndex flagType = MetaIndex.getFlag(watcherClass, watch.getIndex()); if (flagType == null) { - System.err.println("Error finding the FlagType for " + disguiseType.name() + "! Index " + watch - .getIndex() + " can't be found!"); - System.err.println("Value is " + watch.getRawValue() + " (" + watch.getRawValue() - .getClass() + ") (" + nmsEntity.getClass() + ") & " + watcherClass.getSimpleName()); + System.err.println("Error finding the FlagType for " + disguiseType.name() + "! Index " + + watch.getIndex() + " can't be found!"); + System.err.println( + "Value is " + watch.getRawValue() + " (" + watch.getRawValue().getClass() + ") (" + + nmsEntity.getClass() + ") & " + watcherClass.getSimpleName()); System.err.println("Lib's Disguises will continue to load, but this will not work properly!"); continue; } @@ -452,21 +457,21 @@ public class LibsDisguises extends JavaPlugin { Object obj2 = ReflectionManager.convertInvalidItem(watch.getValue()); if (obj1 != obj2 && ((obj1 == null || obj2 == null) || obj1.getClass() != obj2.getClass())) { - System.err.println("Mismatch of " + "FlagType's for " + disguiseType.name() + "! Index " + watch - .getIndex() + " has the wrong classtype!"); - System.err.println( - "MetaIndex has the " + "default of " + flagType.getDefault() + " (" + flagType - .getDefault().getClass() + ") (" + nmsEntity.getClass() + ") & " + watcherClass - .getSimpleName()); - System.err.println("Where the internals is " + watch.getRawValue() + " (" + watch.getRawValue() - .getClass()); + System.err.println("Mismatch of " + "FlagType's for " + disguiseType.name() + "! Index " + + watch.getIndex() + " has the wrong classtype!"); + System.err.println("MetaIndex has the " + "default of " + flagType.getDefault() + " (" + + flagType.getDefault().getClass() + ") (" + nmsEntity.getClass() + ") & " + + watcherClass.getSimpleName()); + System.err.println("Where the internals is " + watch.getRawValue() + " (" + + watch.getRawValue().getClass()); System.err.println("Lib's Disguises will continue to load, but this will not work properly!"); } } for (MetaIndex index : indexes) { - System.out.println(disguiseType + " has MetaIndex remaining! " + index.getFlagWatcher() - .getSimpleName() + " at index " + index.getIndex()); + System.out.println( + disguiseType + " has MetaIndex remaining! " + index.getFlagWatcher().getSimpleName() + + " at index " + index.getIndex()); } DisguiseSound sound = DisguiseSound.getType(disguiseType.name()); @@ -495,12 +500,13 @@ public class LibsDisguises extends JavaPlugin { disguiseValues.setEntitySize(ReflectionManager.getSize(bukkitEntity)); } catch (SecurityException | IllegalArgumentException | IllegalAccessException | FieldAccessException ex) { - System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + disguiseType - .name() + "!"); - System.out - .print("[LibsDisguises] Before reporting this error, " + "please make sure you are using the latest version of LibsDisguises and ProtocolLib."); - System.out - .print("[LibsDisguises] Development builds are available at (ProtocolLib) " + "http://ci.dmulloy2.net/job/ProtocolLib/ and (LibsDisguises) http://server.o2gaming.com:8080/job/LibsDisguises%201.9+/"); + System.out.print("[LibsDisguises] Uh oh! Trouble while making values for the disguise " + + disguiseType.name() + "!"); + System.out.print("[LibsDisguises] Before reporting this error, " + + "please make sure you are using the latest version of LibsDisguises and ProtocolLib."); + System.out.print("[LibsDisguises] Development builds are available at (ProtocolLib) " + + "http://ci.dmulloy2.net/job/ProtocolLib/ and (LibsDisguises) http://server.o2gaming" + + ".com:8080/job/LibsDisguises%201.9+/"); ex.printStackTrace(); } diff --git a/src/me/libraryaddict/disguise/disguisetypes/VillagerProfession.java b/src/me/libraryaddict/disguise/disguisetypes/VillagerProfession.java new file mode 100644 index 00000000..c2cd4cc4 --- /dev/null +++ b/src/me/libraryaddict/disguise/disguisetypes/VillagerProfession.java @@ -0,0 +1,13 @@ +package me.libraryaddict.disguise.disguisetypes; + +/** + * Created by libraryaddict on 23/12/2017. + */ +public enum VillagerProfession { + FARMER, + LIBRARIAN, + PRIEST, + BLACKSMITH, + BUTCHER, + NITWIT; +} diff --git a/src/me/libraryaddict/disguise/disguisetypes/ZombieProfession.java b/src/me/libraryaddict/disguise/disguisetypes/ZombieProfession.java new file mode 100644 index 00000000..f89cba58 --- /dev/null +++ b/src/me/libraryaddict/disguise/disguisetypes/ZombieProfession.java @@ -0,0 +1,9 @@ +package me.libraryaddict.disguise.disguisetypes; + +/** + * Created by libraryaddict on 23/12/2017. + */ +public enum ZombieProfession { + NORMAL, + HUSK; +} diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/VillagerWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/VillagerWatcher.java index ea6717f1..56d0b692 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/VillagerWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/VillagerWatcher.java @@ -2,6 +2,7 @@ package me.libraryaddict.disguise.disguisetypes.watchers; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.MetaIndex; +import me.libraryaddict.disguise.disguisetypes.VillagerProfession; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import org.bukkit.entity.Villager.Profession; @@ -12,16 +13,26 @@ public class VillagerWatcher extends AgeableWatcher { setProfession(Profession.values()[DisguiseUtilities.random.nextInt(Profession.values().length)]); } + @Deprecated public Profession getProfession() { - return Profession.values()[getData(MetaIndex.VILLAGER_PROFESSION)]; + return Profession.values()[getData(MetaIndex.VILLAGER_PROFESSION) + 1]; + } + + public VillagerProfession getVillagerProfession() { + return VillagerProfession.values()[getData(MetaIndex.VILLAGER_PROFESSION)]; } @Deprecated public void setProfession(int professionId) { - setData(MetaIndex.VILLAGER_PROFESSION, Math.max(1, Math.min(professionId, Profession.BUTCHER.ordinal()) - 1)); + setData(MetaIndex.VILLAGER_PROFESSION, professionId); sendData(MetaIndex.VILLAGER_PROFESSION); } + public void setProfession(VillagerProfession profession) { + setProfession(Math.max(1, Math.min(profession.ordinal(), Profession.BUTCHER.ordinal()) - 1)); + } + + @Deprecated public void setProfession(Profession newProfession) { setProfession(newProfession.ordinal()); } diff --git a/src/me/libraryaddict/disguise/disguisetypes/watchers/ZombieVillagerWatcher.java b/src/me/libraryaddict/disguise/disguisetypes/watchers/ZombieVillagerWatcher.java index 6dfea7b2..9cb11134 100644 --- a/src/me/libraryaddict/disguise/disguisetypes/watchers/ZombieVillagerWatcher.java +++ b/src/me/libraryaddict/disguise/disguisetypes/watchers/ZombieVillagerWatcher.java @@ -2,6 +2,7 @@ package me.libraryaddict.disguise.disguisetypes.watchers; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.MetaIndex; +import me.libraryaddict.disguise.disguisetypes.ZombieProfession; import org.bukkit.entity.Villager.Profession; public class ZombieVillagerWatcher extends ZombieWatcher { @@ -42,6 +43,10 @@ public class ZombieVillagerWatcher extends ZombieWatcher { return Profession.NORMAL; } + public ZombieProfession getZombieProfession() { + return ZombieProfession.values()[getData(MetaIndex.ZOMBIE_VILLAGER_PROFESSION)]; + } + /** * Sets the profession of this zombie, in turn turning it into a Zombie Villager * @@ -58,7 +63,12 @@ public class ZombieVillagerWatcher extends ZombieWatcher { * * @param profession */ + @Deprecated public void setProfession(Profession profession) { setProfession(profession.ordinal()); } + + public void setProfession(ZombieProfession profession) { + setProfession(profession.ordinal()); + } } diff --git a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java index a72d7237..3c5a1ebd 100644 --- a/src/me/libraryaddict/disguise/utilities/DisguiseParser.java +++ b/src/me/libraryaddict/disguise/utilities/DisguiseParser.java @@ -759,6 +759,20 @@ public class DisguiseParser { // Parse to string value = ChatColor.translateAlternateColorCodes('&', valueString); } + } else if (param == ZombieProfession.class) { + try { + value = ZombieProfession.valueOf(valueString.toUpperCase()); + } + catch (Exception ex) { + throw parseToException(param, valueString, methodName); + } + } else if (param == VillagerProfession.class) { + try { + value = VillagerProfession.valueOf(valueString.toUpperCase()); + } + catch (Exception ex) { + throw parseToException(param, valueString, methodName); + } } else if (param == AnimalColor.class) { // Parse to animal color try { @@ -803,9 +817,6 @@ public class DisguiseParser { } else if (param == Horse.Style.class) { // Parse to horse style value = callValueOf(param, valueString, methodName); - } else if (param == Villager.Profession.class) { - // Parse to villager profession - value = callValueOf(param, valueString, methodName); } else if (param == Art.class) { // Parse to art type value = callValueOf(param, valueString, methodName); diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionFlagWatchers.java b/src/me/libraryaddict/disguise/utilities/ReflectionFlagWatchers.java index 5f29aeee..178c3d5f 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionFlagWatchers.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionFlagWatchers.java @@ -153,8 +153,10 @@ public class ReflectionFlagWatchers { new ParamInfo(Horse.Color.class, "Horse Color", "View all the colors you can use for a horses color"); new ParamInfo(Ocelot.Type.class, "Ocelot Type", "View all the ocelot types you can use for ocelots"); - new ParamInfo(Villager.Profession.class, "Villager Profession", + new ParamInfo(VillagerProfession.class, "Villager Profession", "View all the professions you can set on a villager"); + new ParamInfo(ZombieProfession.class, "Zombie Profession", + "View all the professions you can set on a zombie villager"); new ParamInfo(BlockFace.class, Arrays.copyOf(BlockFace.values(), 6), "Direction (North, East, South, West, " + "Up, Down)", "View the directions usable on player setSleeping and shulker direction");