From 3e6e4b39328f72848b0f4e9d803b225a2dd4668b Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 11 Sep 2021 01:28:01 +1200 Subject: [PATCH] Add isLeftClicking and fix isRightClicking, didn't check backwards compatibility, may need to be renamed. Fixes #617 --- .../libraryaddict/disguise/DisguiseAPI.java | 19 +++++- .../disguise/disguisetypes/FlagWatcher.java | 18 ++++-- .../disguise/disguisetypes/MetaIndex.java | 2 +- .../disguisetypes/watchers/LivingWatcher.java | 58 +++++++++++++++++-- .../utilities/mineskin/MineSkinAPI.java | 5 +- .../PacketHandlerEquipment.java | 11 ++-- 6 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java index 57317a42..d9a41a65 100644 --- a/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java +++ b/src/main/java/me/libraryaddict/disguise/DisguiseAPI.java @@ -6,10 +6,12 @@ import com.comphenix.protocol.wrappers.WrappedWatchableObject; import lombok.Getter; import me.libraryaddict.disguise.disguisetypes.*; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise.TargetType; +import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.parser.DisguiseParseException; import me.libraryaddict.disguise.utilities.parser.DisguiseParser; import me.libraryaddict.disguise.utilities.parser.DisguisePerm; +import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import org.bukkit.Material; import org.bukkit.command.CommandSender; @@ -138,22 +140,35 @@ public class DisguiseAPI { continue; } - watcher.setUnsafeData(index, obj.getRawValue()); + watcher.setUnsafeData(index, obj.getRawValue()); // Update the meta for 0, otherwise boolean be weird if (index == MetaIndex.ENTITY_META) { watcher.setSprinting(watcher.isSprinting() && displayExtraAnimations); watcher.setFlyingWithElytra(watcher.isFlyingWithElytra() && displayExtraAnimations); - watcher.setRightClicking(watcher.isRightClicking() && displayExtraAnimations); watcher.setSneaking(watcher.isSneaking() && displayExtraAnimations); watcher.setSwimming(watcher.isSwimming() && displayExtraAnimations); + if (!NmsVersion.v1_13.isSupported()) { + watcher.setRightClicking(watcher.isRightClicking() && displayExtraAnimations); + } + if (!displayExtraAnimations) { Arrays.fill(watcher.getModifiedEntityAnimations(), false); } watcher.setGlowing(watcher.isGlowing()); watcher.setInvisible(watcher.isInvisible()); + } else if (index == MetaIndex.LIVING_META && NmsVersion.v1_13.isSupported()) { + LivingWatcher livingWatcher = (LivingWatcher) watcher; + + livingWatcher.setRightClicking(livingWatcher.isRightClicking() && displayExtraAnimations); + livingWatcher.setLeftClicking(livingWatcher.isLeftClicking() && displayExtraAnimations); + livingWatcher.setSpinning(livingWatcher.isSpinning() && displayExtraAnimations); + + if (!displayExtraAnimations) { + Arrays.fill(livingWatcher.getModifiedLivingAnimations(), false); + } } } diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java index cd0467d9..ce0481fa 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/FlagWatcher.java @@ -168,7 +168,11 @@ public class FlagWatcher { sendHeadPacket(); } - private byte addEntityAnimations(byte originalValue, byte entityValue) { + protected byte addEntityAnimations(MetaIndex index, byte originalValue, byte entityValue) { + if (index != MetaIndex.ENTITY_META) { + return originalValue; + } + for (int i = 0; i < 6; i++) { if ((entityValue & 1 << i) != 0 && !modifiedEntityAnimations[i]) { originalValue = (byte) (originalValue | 1 << i); @@ -294,10 +298,12 @@ public class FlagWatcher { } if (value != null) { - if (isEntityAnimationsAdded() && id == MetaIndex.ENTITY_META.getIndex()) { - value = addEntityAnimations((byte) value, (byte) watch.getRawValue()); + if (isEntityAnimationsAdded() && (index == MetaIndex.ENTITY_META || index == MetaIndex.LIVING_META)) { + value = addEntityAnimations(index, (byte) value, (byte) watch.getRawValue()); - doSneakCheck((Byte) value); + if (index == MetaIndex.ENTITY_META) { + doSneakCheck((Byte) value); + } } boolean isDirty = watch.getDirtyState(); @@ -835,8 +841,8 @@ public class FlagWatcher { Object value = entityValues.get(data.getIndex()); - if (isEntityAnimationsAdded() && DisguiseConfig.isMetaPacketsEnabled() && data == MetaIndex.ENTITY_META) { - value = addEntityAnimations((byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0)); + if (isEntityAnimationsAdded() && DisguiseConfig.isMetaPacketsEnabled() && (data == MetaIndex.ENTITY_META || data == MetaIndex.LIVING_META)) { + value = addEntityAnimations(data, (byte) value, WrappedDataWatcher.getEntityWatcher(disguise.getEntity()).getByte(0)); } WrappedWatchableObject watch = ReflectionManager.createWatchable(data, value); diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java index fd3256e8..50ed537c 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/MetaIndex.java @@ -387,7 +387,7 @@ public class MetaIndex { /** * The main hand of the living entity */ - public static MetaIndex LIVING_HAND = new MetaIndex<>(LivingWatcher.class, 0, (byte) 0); + public static MetaIndex LIVING_META = new MetaIndex<>(LivingWatcher.class, 0, (byte) 0); /** * How much health the living entity has, generally only visible on bosses due to their health bar 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 dd6a38c8..c46fd303 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/watchers/LivingWatcher.java @@ -6,6 +6,7 @@ import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.BlockPosition; import com.comphenix.protocol.wrappers.WrappedAttribute; import com.comphenix.protocol.wrappers.WrappedAttribute.Builder; +import lombok.Getter; import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.FlagWatcher; @@ -25,6 +26,8 @@ public class LivingWatcher extends FlagWatcher { private double maxHealth; private boolean maxHealthSet; private HashSet potionEffects = new HashSet<>(); + @Getter + private boolean[] modifiedLivingAnimations = new boolean[3]; public LivingWatcher(Disguise disguise) { super(disguise); @@ -36,6 +39,7 @@ public class LivingWatcher extends FlagWatcher { clone.potionEffects = (HashSet) potionEffects.clone(); clone.maxHealth = maxHealth; clone.maxHealthSet = maxHealthSet; + clone.modifiedLivingAnimations = Arrays.copyOf(modifiedLivingAnimations, modifiedLivingAnimations.length); return clone; } @@ -79,31 +83,58 @@ public class LivingWatcher extends FlagWatcher { }*/ private boolean getHandFlag(int byteValue) { - return (getData(MetaIndex.LIVING_HAND) & 1 << byteValue) != 0; + return (getData(MetaIndex.LIVING_META) & 1 << byteValue) != 0; } private void setHandFlag(int byteValue, boolean flag) { - byte b0 = getData(MetaIndex.LIVING_HAND); + byte b0 = getData(MetaIndex.LIVING_META); + modifiedLivingAnimations[byteValue] = true; if (flag) { - setData(MetaIndex.LIVING_HAND, (byte) (b0 | 1 << byteValue)); + setData(MetaIndex.LIVING_META, (byte) (b0 | 1 << byteValue)); } else { - setData(MetaIndex.LIVING_HAND, (byte) (b0 & ~(1 << byteValue))); + setData(MetaIndex.LIVING_META, (byte) (b0 & ~(1 << byteValue))); } - sendData(MetaIndex.LIVING_HAND); + sendData(MetaIndex.LIVING_META); + } + + private boolean isRightHandInUse() { + return getHandFlag(1); + } + + private void setHandInUse(boolean rightHand) { + if (isRightHandInUse() == rightHand) { + return; + } + + setHandFlag(1, rightHand); } @NmsAddedIn(NmsVersion.v1_13) public boolean isRightClicking() { - return getHandFlag(0); + return isRightHandInUse() && getHandFlag(0); } @NmsAddedIn(NmsVersion.v1_13) public void setRightClicking(boolean setRightClicking) { + setHandInUse(true); + setHandFlag(0, setRightClicking); } + @NmsAddedIn(NmsVersion.v1_13) + public boolean isLeftClicking() { + return !isRightHandInUse() && getHandFlag(0); + } + + @NmsAddedIn(NmsVersion.v1_13) + public void setLeftClicking(boolean setLeftClicking) { + setHandInUse(false); + + setHandFlag(0, setLeftClicking); + } + @NmsAddedIn(NmsVersion.v1_13) public boolean isSpinning() { return getHandFlag(2); @@ -262,4 +293,19 @@ public class LivingWatcher extends FlagWatcher { setData(MetaIndex.LIVING_ARROWS, Math.max(0, Math.min(127, arrowsNo))); sendData(MetaIndex.LIVING_ARROWS); } + + @Override + protected byte addEntityAnimations(MetaIndex index, byte originalValue, byte entityValue) { + if (index != MetaIndex.LIVING_META) { + return super.addEntityAnimations(index, originalValue, entityValue); + } + + for (int i = 0; i < 3; i++) { + if ((entityValue & 1 << i) != 0 && !modifiedLivingAnimations[i]) { + originalValue = (byte) (originalValue | 1 << i); + } + } + + return originalValue; + } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java b/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java index ea5660f1..5ce76917 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java @@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities.mineskin; import com.google.gson.Gson; import lombok.Getter; import lombok.Setter; +import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.SkinUtils; import me.libraryaddict.disguise.utilities.translations.LibsMsg; @@ -65,11 +66,11 @@ public class MineSkinAPI { } private void printDebug(String message) { - if (!isDebugging()) { + if (!isDebugging() || LibsDisguises.getInstance() == null) { return; } - System.out.println("[MineSkinAPI] " + message); + LibsDisguises.getInstance().getLogger().info("[MineSkinAPI] " + message); } private MineSkinResponse doPost(SkinUtils.SkinCallback callback, String path, String skinUrl, File file, SkinUtils.ModelType modelType) { 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 95fe70ee..fb5e5067 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 @@ -8,6 +8,7 @@ import com.mojang.datafixers.util.Pair; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.MetaIndex; +import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.utilities.packets.IPacketHandler; import me.libraryaddict.disguise.utilities.packets.LibsPackets; import me.libraryaddict.disguise.utilities.packets.PacketsHandler; @@ -80,14 +81,14 @@ public class PacketHandlerEquipment implements IPacketHandler { itemStack = ReflectionManager.getBukkitItem(pair.getSecond()); } - if (disguise.getWatcher().isRightClicking() && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { + if ((disguise.getWatcher().isRightClicking() || (disguise.getWatcher() instanceof LivingWatcher && ((LivingWatcher) disguise.getWatcher()).isLeftClicking())) && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { if (itemStack != null && itemStack.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); if (DisguiseConfig.isMetaPacketsEnabled()) { WrappedWatchableObject watch = ReflectionManager - .createWatchable(MetaIndex.LIVING_HAND, WrappedDataWatcher.getEntityWatcher(entity).getByte(MetaIndex.LIVING_HAND.getIndex())); + .createWatchable(MetaIndex.LIVING_META, WrappedDataWatcher.getEntityWatcher(entity).getByte(MetaIndex.LIVING_META.getIndex())); if (watch != null) { list.add(watch); @@ -96,7 +97,7 @@ public class PacketHandlerEquipment implements IPacketHandler { list = disguise.getWatcher().convert(observer, list); } else { for (WrappedWatchableObject obj : disguise.getWatcher().getWatchableObjects()) { - if (obj.getIndex() == MetaIndex.LIVING_HAND.getIndex()) { + if (obj.getIndex() == MetaIndex.LIVING_META.getIndex()) { list.add(obj); break; } @@ -151,7 +152,7 @@ public class PacketHandlerEquipment implements IPacketHandler { equipPacket.getModifier().write(2, ReflectionManager.getNmsItem(itemStack.getType() == Material.AIR ? null : itemStack)); } - if (disguise.getWatcher().isRightClicking() && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { + if ((disguise.getWatcher().isRightClicking() || (disguise.getWatcher() instanceof LivingWatcher && ((LivingWatcher) disguise.getWatcher()).isLeftClicking())) && (slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND)) { if (itemStack == null) { itemStack = packets.getPackets().get(0).getItemModifier().read(0); } @@ -159,7 +160,7 @@ public class PacketHandlerEquipment implements IPacketHandler { if (itemStack != null && itemStack.getType() != Material.AIR) { // Convert the datawatcher List list = new ArrayList<>(); - MetaIndex toUse = NmsVersion.v1_13.isSupported() ? MetaIndex.LIVING_HAND : MetaIndex.ENTITY_META; + MetaIndex toUse = NmsVersion.v1_13.isSupported() ? MetaIndex.LIVING_META : MetaIndex.ENTITY_META; if (DisguiseConfig.isMetaPacketsEnabled()) { WrappedWatchableObject watch =