From 30cc40a6a3315f08f43cdcd1ac35e7b3ec3f70c8 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Wed, 1 Jul 2020 17:44:29 +1200 Subject: [PATCH] Fix isRightClicking() and add isSpinning() --- .../disguise/disguisetypes/FlagWatcher.java | 15 ++++-- .../watchers/InsentientWatcher.java | 5 +- .../disguisetypes/watchers/LivingWatcher.java | 49 +++++++++++++++++++ .../PacketHandlerEquipment.java | 30 +++++++----- .../utilities/parser/DisguiseParser.java | 5 +- 5 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index 491b8ff6..a8199eb5 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -18,6 +18,7 @@ import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue; import me.libraryaddict.disguise.utilities.reflection.NmsAddedIn; +import me.libraryaddict.disguise.utilities.reflection.NmsRemovedIn; import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import net.md_5.bungee.api.chat.BaseComponent; @@ -52,7 +53,6 @@ public class FlagWatcher { private boolean[] modifiedEntityAnimations = new boolean[8]; private transient List watchableObjects; private boolean sleeping; - private boolean swimming; private transient boolean previouslySneaking; @Getter private boolean upsideDown; @@ -88,7 +88,6 @@ public class FlagWatcher { cloned.modifiedEntityAnimations = Arrays.copyOf(modifiedEntityAnimations, modifiedEntityAnimations.length); cloned.addEntityAnimations = addEntityAnimations; cloned.upsideDown = upsideDown; - cloned.swimming = swimming; cloned.sleeping = sleeping; return cloned; @@ -559,11 +558,17 @@ public class FlagWatcher { sendData(MetaIndex.ENTITY_NO_GRAVITY); } + //@NmsRemovedIn(val = NmsVersion.v1_13) public boolean isRightClicking() { - return getEntityFlag(4); + return !NmsVersion.v1_13.isSupported() && getEntityFlag(4); } + //@NmsRemovedIn(val = NmsVersion.v1_13) public void setRightClicking(boolean setRightClicking) { + if (NmsVersion.v1_13.isSupported()) { + return; + } + setEntityFlag(4, setRightClicking); sendData(MetaIndex.ENTITY_META); } @@ -796,7 +801,7 @@ public class FlagWatcher { @NmsAddedIn(val = NmsVersion.v1_14) public boolean isSwimming() { - return swimming; + return getEntityFlag(4); } @NmsAddedIn(val = NmsVersion.v1_14) @@ -805,7 +810,7 @@ public class FlagWatcher { return; } - this.swimming = swimming; + setEntityFlag(4, swimming); updatePose(); } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/InsentientWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/InsentientWatcher.java index 7f814b9d..5c72fb47 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/InsentientWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/InsentientWatcher.java @@ -17,7 +17,6 @@ public class InsentientWatcher extends LivingWatcher { public void setMainHand(MainHand mainHand) { setInsentientFlag(2, mainHand == MainHand.RIGHT); - sendData(MetaIndex.INSENTIENT_META); } public boolean isAI() { @@ -26,7 +25,6 @@ public class InsentientWatcher extends LivingWatcher { public void setAI(boolean ai) { setInsentientFlag(1, ai); - sendData(MetaIndex.INSENTIENT_META); } private void setInsentientFlag(int i, boolean flag) { @@ -37,6 +35,8 @@ public class InsentientWatcher extends LivingWatcher { } else { setData(MetaIndex.INSENTIENT_META, (byte) (b0 & i)); } + + sendData(MetaIndex.INSENTIENT_META); } private boolean getInsentientFlag(int i) { @@ -51,6 +51,5 @@ public class InsentientWatcher extends LivingWatcher { @NmsAddedIn(val = NmsVersion.v1_14) public void setEnraged(boolean enraged) { setInsentientFlag(4, enraged); - sendData(MetaIndex.INSENTIENT_META); } } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java index b040dc99..11031a5a 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java @@ -4,6 +4,7 @@ import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.BlockPosition; +import com.comphenix.protocol.wrappers.EnumWrappers; import com.comphenix.protocol.wrappers.WrappedAttribute; import com.comphenix.protocol.wrappers.WrappedAttribute.Builder; import me.libraryaddict.disguise.DisguiseAPI; @@ -11,11 +12,13 @@ import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; import me.libraryaddict.disguise.disguisetypes.MetaIndex; import me.libraryaddict.disguise.utilities.DisguiseUtilities; +import me.libraryaddict.disguise.utilities.parser.RandomDefaultValue; import me.libraryaddict.disguise.utilities.reflection.NmsAddedIn; import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import org.bukkit.Color; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.inventory.MainHand; import org.bukkit.potion.PotionEffectType; import java.lang.reflect.InvocationTargetException; @@ -68,6 +71,52 @@ public class LivingWatcher extends FlagWatcher { sendData(MetaIndex.LIVING_HEALTH); } + /*@NmsAddedIn(val = NmsVersion.v1_13) + public MainHand getMainHand() { + return getHandFlag(0) ? MainHand.RIGHT : MainHand.LEFT; + } + + @NmsAddedIn(val = NmsVersion.v1_13) + public void setMainHand(MainHand hand) { + setHandFlag(0, hand == MainHand.RIGHT); + }*/ + + private boolean getHandFlag(int byteValue) { + return (getData(MetaIndex.LIVING_HAND) & 1 << byteValue) != 0; + } + + private void setHandFlag(int byteValue, boolean flag) { + byte b0 = getData(MetaIndex.LIVING_HAND); + + if (flag) { + setData(MetaIndex.LIVING_HAND, (byte) (b0 | 1 << byteValue)); + } else { + setData(MetaIndex.LIVING_HAND, (byte) (b0 & ~(1 << byteValue))); + } + + sendData(MetaIndex.LIVING_HAND); + } + + @NmsAddedIn(val = NmsVersion.v1_13) + public boolean isRightClicking() { + return getHandFlag(0); + } + + @NmsAddedIn(val = NmsVersion.v1_13) + public void setRightClicking(boolean setRightClicking) { + setHandFlag(0, setRightClicking); + } + + @NmsAddedIn(val = NmsVersion.v1_13) + public boolean isSpinning() { + return getHandFlag(2); + } + + @NmsAddedIn(val = NmsVersion.v1_13) + public void setSpinning(boolean setSpinning) { + setHandFlag(2, setSpinning); + } + public double getMaxHealth() { return maxHealth; } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java index 59ccc78a..37c7c512 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/packets/packethandlers/PacketHandlerEquipment.java @@ -84,16 +84,17 @@ public class PacketHandlerEquipment implements IPacketHandler { newSlots.add(pair); } - if (disguise.getWatcher().isRightClicking() && slot == EquipmentSlot.HAND) { - ItemStack heldItem = packets.getPackets().get(0).getItemModifier().read(0); + if (disguise.getWatcher().isRightClicking() && + (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { + itemStack = ReflectionManager.getBukkitItem(pair.getSecond()); - if (heldItem != null && heldItem.getType() != Material.AIR) { + if (itemStack != null && itemStack.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); if (DisguiseConfig.isMetaPacketsEnabled()) { - WrappedWatchableObject watch = ReflectionManager.createWatchable(MetaIndex.ENTITY_META, - WrappedDataWatcher.getEntityWatcher(entity).getByte(0)); + WrappedWatchableObject watch = ReflectionManager.createWatchable(MetaIndex.LIVING_HAND, + WrappedDataWatcher.getEntityWatcher(entity).getByte(MetaIndex.LIVING_HAND.getIndex())); if (watch != null) list.add(watch); @@ -101,7 +102,7 @@ public class PacketHandlerEquipment implements IPacketHandler { list = disguise.getWatcher().convert(list); } else { for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects()) { - if (obj.getIndex() == 0) { + if (obj.getIndex() == MetaIndex.LIVING_HAND.getIndex()) { list.add(obj); break; } @@ -117,7 +118,7 @@ public class PacketHandlerEquipment implements IPacketHandler { PacketContainer packetUnblock = packetBlock.deepClone(); // Make a packet to send the 'unblock' for (WrappedWatchableObject watcher : packetUnblock.getWatchableCollectionModifier().read(0)) { - watcher.setValue((byte) ((byte) watcher.getValue() & ~(1 << 4))); + watcher.setValue((byte) 0); } // Send the unblock before the itemstack change so that the 2nd metadata packet works. Why? @@ -158,16 +159,17 @@ public class PacketHandlerEquipment implements IPacketHandler { .write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack)); } - if (disguise.getWatcher().isRightClicking() && slot == EquipmentSlot.HAND) { + if (disguise.getWatcher().isRightClicking() && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { ItemStack heldItem = packets.getPackets().get(0).getItemModifier().read(0); if (heldItem != null && heldItem.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); + MetaIndex toUse = NmsVersion.v1_13.isSupported() ? MetaIndex.LIVING_HAND : MetaIndex.ENTITY_META; if (DisguiseConfig.isMetaPacketsEnabled()) { - WrappedWatchableObject watch = ReflectionManager.createWatchable(MetaIndex.ENTITY_META, - WrappedDataWatcher.getEntityWatcher(entity).getByte(0)); + WrappedWatchableObject watch = ReflectionManager.createWatchable(toUse, + WrappedDataWatcher.getEntityWatcher(entity).getByte(toUse.getIndex())); if (watch != null) list.add(watch); @@ -175,7 +177,7 @@ public class PacketHandlerEquipment implements IPacketHandler { list = disguise.getWatcher().convert(list); } else { for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects()) { - if (obj.getIndex() == 0) { + if (obj.getIndex() == toUse.getIndex()) { list.add(obj); break; } @@ -191,7 +193,11 @@ public class PacketHandlerEquipment implements IPacketHandler { PacketContainer packetUnblock = packetBlock.deepClone(); // Make a packet to send the 'unblock' for (WrappedWatchableObject watcher : packetUnblock.getWatchableCollectionModifier().read(0)) { - watcher.setValue((byte) ((byte) watcher.getValue() & ~(1 << 4))); + if (NmsVersion.v1_13.isSupported()) { + watcher.setValue((byte) 0); + } else { + watcher.setValue((byte) ((byte) watcher.getValue() & ~(1 << 4))); + } } // Send the unblock before the itemstack change so that the 2nd metadata packet works. Why? diff --git a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java index d7281f73..7a44f0b9 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/parser/DisguiseParser.java @@ -235,9 +235,10 @@ public class DisguiseParser { if (!Objects.deepEquals(dObj, object)) { throw new IllegalStateException(String.format( - "%s has conflicting values! This means it expected the same value again but received a " + + "%s has conflicting values in class %s! This means it expected the same value again but " + + "received a " + "different value on a different disguise! %s is not the same as %s!", - setMethod.getName(), object, dObj)); + setMethod.getName(), setMethod.getDeclaringClass().getName(), object, dObj)); } return;