From 27baf53073f3c8f249759377ae211d274a2121c5 Mon Sep 17 00:00:00 2001 From: NavidK0 Date: Sun, 13 Mar 2016 21:27:46 -0400 Subject: [PATCH] Fixed Wolves, Endermen, and Ocelots All mob and misc disguises work now --- .../disguise/disguisetypes/FlagWatcher.java | 7 ++++-- .../watchers/EndermanWatcher.java | 24 +++++++++++++++++-- .../disguise/utilities/DisguiseSound.java | 5 ++-- .../disguise/utilities/PacketsManager.java | 14 +++++++---- .../disguise/utilities/ReflectionManager.java | 12 ++++++++++ 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index bf267123..9f8e983a 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -124,8 +124,7 @@ public class FlagWatcher { } } // Here we check for if there is a health packet that says they died. - if (getDisguise().isSelfDisguiseVisible() && getDisguise().getEntity() != null - && getDisguise().getEntity() instanceof Player) { + if (getDisguise().isSelfDisguiseVisible() && getDisguise().getEntity() != null && getDisguise().getEntity() instanceof Player) { for (WrappedWatchableObject watch : newList) { // Its a health packet if (watch.getIndex() == 6) { @@ -261,6 +260,9 @@ public class FlagWatcher { } Object value = entityValues.get(data); if (isEntityAnimationsAdded() && DisguiseConfig.isMetadataPacketsEnabled() && data == 0) { + if (disguise.getType() != DisguiseType.WOLF && + disguise.getType() != DisguiseType.OCELOT && + disguise.getType() != DisguiseType.ENDERMAN) value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0)); } WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(data, value)); @@ -341,6 +343,7 @@ public class FlagWatcher { /** * Don't use this, use setItemInMainHand instead + * * @param itemstack */ @Deprecated diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/EndermanWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/EndermanWatcher.java index d2b8521e..7f49b2a3 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/EndermanWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/EndermanWatcher.java @@ -1,6 +1,9 @@ package me.libraryaddict.disguise.disguisetypes.watchers; +import com.google.common.base.Optional; import me.libraryaddict.disguise.disguisetypes.Disguise; +import me.libraryaddict.disguise.utilities.ReflectionManager; +import org.apache.commons.lang3.tuple.Pair; import org.bukkit.inventory.ItemStack; public class EndermanWatcher extends LivingWatcher { @@ -11,12 +14,29 @@ public class EndermanWatcher extends LivingWatcher { @Override public ItemStack getItemInMainHand() { - return new ItemStack((int) getValue(11, 1), 1, (short) 0); + Optional value = (Optional) getValue(11, Optional.of(1)); + if (value.isPresent()) { + Pair pair = ReflectionManager.getFromCombinedId(value.get()); + int id = pair.getLeft(); + int data = pair.getRight(); + return new ItemStack(id, 1, (short) data); + } else { + return null; + } } @Override public void setItemInMainHand(ItemStack itemstack) { - setValue(11, itemstack.getTypeId()); + setItemInMainHand(itemstack.getTypeId(), itemstack.getDurability()); + } + + public void setItemInMainHand(int typeId) { + setItemInMainHand(typeId, 0); + } + + public void setItemInMainHand(int typeId, int data) { + int combined = ReflectionManager.getCombinedId(typeId, data); + setValue(11, Optional.of(combined)); } public boolean isAggressive() { diff --git a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java index 4ea1a3dd..cca56e24 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/DisguiseSound.java @@ -25,8 +25,9 @@ public enum DisguiseSound { ELDER_GUARDIAN("entity.guardian.elder.hit", null, "entity.guardian.elder.death", "entity.guardian.elder.death"), ENDER_DRAGON("entity.enderdragon.hit", null, "entity.enderdragon.end", "entity.enderdragon.growl", "damage.fallsmall", "entity.enderdragon.wings", "damage.fallbig"), - ENDERMAN("entity.endermen.hit", "step.grass", "entity.endermen.death", "entity.endermen.idle", "entity.endermen.scream", - "entity.endermen.portal", "entity.endermen.stare"), + //TODO: Fix enderman sounds +// ENDERMAN("entity.endermen.hit", "step.grass", "entity.endermen.death", "entity.endermen.idle", "entity.endermen.scream", +// "entity.endermen.portal", "entity.endermen.stare"), ENDERMITE("entity.silverfish.hit", "entity.silverfish.step", "entity.silverfish.kill", "entity.silverfish.ambient"), GHAST("entity.ghast.scream", null, "entity.ghast.death", "entity.ghast.moan", "damage.fallsmall", "entity.ghast.fireball", "damage.fallbig", "entity.ghast.affectionate_scream", "entity.ghast.charge"), diff --git a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java index bb973d87..6411dee9 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -284,7 +284,7 @@ public class PacketsManager { int objectId = disguise.getType().getObjectId(); int data = ((MiscDisguise) disguise).getData(); if (disguise.getType() == DisguiseType.FALLING_BLOCK) { - data = ((MiscDisguise) disguise).getId() + (data << 12); + data = ReflectionManager.getCombinedId(((MiscDisguise) disguise).getId(), data); } else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == 0) { // If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners entity id data = disguisedEntity.getEntityId(); @@ -326,9 +326,9 @@ public class PacketsManager { private static WrappedDataWatcher createDataWatcher(WrappedDataWatcher watcher, FlagWatcher flagWatcher) { WrappedDataWatcher newWatcher = new WrappedDataWatcher(); try { - List list = DisguiseConfig.isMetadataPacketsEnabled() ? - flagWatcher.convert(watcher.getWatchableObjects()) : flagWatcher.getWatchableObjects(); + List list = DisguiseConfig.isMetadataPacketsEnabled() ? flagWatcher.convert(watcher.getWatchableObjects()) : flagWatcher.getWatchableObjects(); for (WrappedWatchableObject watchableObject : list) { + if (watchableObject == null) continue; if (watchableObject.getValue() == null) continue; if (Registry.get(watchableObject.getValue().getClass()) == null) continue; WrappedDataWatcherObject obj = new WrappedDataWatcherObject(watchableObject.getIndex(), Registry.get(watchableObject.getValue().getClass())); @@ -1281,7 +1281,9 @@ public class PacketsManager { // Else if the packet is sending entity metadata else if (sentPacket.getType() == Server.ENTITY_METADATA) { - if (DisguiseConfig.isMetadataPacketsEnabled()) { + if (DisguiseConfig.isMetadataPacketsEnabled() && (disguise.getType() != DisguiseType.WOLF && + disguise.getType() != DisguiseType.OCELOT && + disguise.getType() != DisguiseType.ENDERMAN)) { List watchableObjects = disguise.getWatcher().convert( packets[0].getWatchableCollectionModifier().read(0)); packets[0] = new PacketContainer(sentPacket.getType()); @@ -1394,7 +1396,9 @@ public class PacketsManager { if (heldItem != null && heldItem.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); - if (DisguiseConfig.isMetadataPacketsEnabled()) { + if (DisguiseConfig.isMetadataPacketsEnabled() && (disguise.getType() != DisguiseType.WOLF && + disguise.getType() != DisguiseType.OCELOT && + disguise.getType() != DisguiseType.ENDERMAN)) { WrappedWatchableObject watch = new WrappedWatchableObject(ReflectionManager.createDataWatcherItem(0, WrappedDataWatcher.getEntityWatcher(entity).getByte(0))); list.add(watch); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java index 27534b61..7af6c513 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -6,6 +6,8 @@ import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; import com.comphenix.protocol.wrappers.WrappedDataWatcher.WrappedDataWatcherObject; import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.google.common.collect.ImmutableMap; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; import org.bukkit.Art; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -667,4 +669,14 @@ public class ReflectionManager { } return null; } + + public static int getCombinedId(int id, int data) { + return id + (data << 12); + } + + public static Pair getFromCombinedId(int combinedId) { + int j = combinedId & 4095; + int k = combinedId >> 12 & 15; + return new ImmutablePair<>(j, k); + } }