From 383c9ecaca6877919199e26d1c07efa4a1e5053c Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 23 Sep 2018 11:07:54 +1200 Subject: [PATCH] Fixed falling block disguise, fixed velocity when flying --- .../disguise/disguisetypes/Disguise.java | 56 +++---------------- .../disguise/disguisetypes/DisguiseType.java | 2 +- .../disguise/disguisetypes/MiscDisguise.java | 24 ++++---- .../disguise/utilities/PacketsManager.java | 18 +++++- .../disguise/utilities/ReflectionManager.java | 32 ++--------- 5 files changed, 43 insertions(+), 89 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java index 64a919c7..56d1ca1d 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/Disguise.java @@ -112,15 +112,7 @@ public abstract class Disguise { final boolean alwaysSendVelocity; switch (getType()) { - case EGG: - case ENDER_PEARL: - case BAT: case EXPERIENCE_ORB: - case FIREBALL: - case SMALL_FIREBALL: - case SNOWBALL: - case SPLASH_POTION: - case THROWN_EXP_BOTTLE: case WITHER_SKULL: case FIREWORK: alwaysSendVelocity = true; @@ -130,55 +122,21 @@ public abstract class Disguise { break; } - double velocitySpeed = 0.0005; + final Double vectorY; switch (getType()) { case FIREWORK: - velocitySpeed = -0.040; - break; case WITHER_SKULL: - velocitySpeed = 0.000001D; - break; - case ARROW: - case TIPPED_ARROW: - case SPECTRAL_ARROW: - case BOAT: - case ENDER_CRYSTAL: - case ENDER_DRAGON: - case GHAST: - case ITEM_FRAME: - case MINECART: - case MINECART_CHEST: - case MINECART_COMMAND: - case MINECART_FURNACE: - case MINECART_HOPPER: - case MINECART_MOB_SPAWNER: - case MINECART_TNT: - case PAINTING: - case PLAYER: - case SQUID: - velocitySpeed = 0; - break; - case DROPPED_ITEM: - case PRIMED_TNT: - case WITHER: - case FALLING_BLOCK: - velocitySpeed = 0.04; + vectorY = 0.000001D; break; case EXPERIENCE_ORB: - velocitySpeed = 0.0221; - break; - case SPIDER: - case BAT: - case CAVE_SPIDER: - velocitySpeed = 0.004; + vectorY = 0.0221; break; default: + vectorY = null; break; } - final double vectorY = velocitySpeed; - final TargetedDisguise disguise = (TargetedDisguise) this; // A scheduler to clean up any unused disguises. @@ -190,9 +148,9 @@ public abstract class Disguise { @Override public void run() { // If entity is no longer valid. Remove it. - if (getEntity() instanceof Player && !((Player) getEntity()).isOnline()) + if (getEntity() instanceof Player && !((Player) getEntity()).isOnline()) { removeDisguise(); - else if (!getEntity().isValid()) { + } else if (!getEntity().isValid()) { // If it has been dead for 30+ ticks // This is to ensure that this disguise isn't removed while clients think its the real entity // The delay is because if it sends the destroy entity packets straight away, then it means no @@ -251,7 +209,7 @@ public abstract class Disguise { // If the vectorY isn't 0. Cos if it is. Then it doesn't want to send any vectors. // If this disguise has velocity sending enabled and the entity is flying. - if (isVelocitySent() && vectorY != 0 && (alwaysSendVelocity || !getEntity().isOnGround())) { + if (isVelocitySent() && vectorY != null && (alwaysSendVelocity || !getEntity().isOnGround())) { Vector vector = getEntity().getVelocity(); // If the entity doesn't have velocity changes already - You know. I really can't wrap my diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java index e7698f3a..34af19eb 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/DisguiseType.java @@ -60,7 +60,7 @@ public enum DisguiseType { EXPERIENCE_ORB, - FALLING_BLOCK(70, 1), + FALLING_BLOCK(70), FIREBALL(63), diff --git a/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java b/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java index b96e5c39..b63ec5f7 100644 --- a/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java +++ b/src/main/java/me/libraryaddict/disguise/disguisetypes/MiscDisguise.java @@ -19,22 +19,26 @@ public class MiscDisguise extends TargetedDisguise { this(disguiseType, -1, disguiseType.getDefaultData()); } - public MiscDisguise(Material material, int data) { - super(DisguiseType.DROPPED_ITEM); - - apply(0, 0, new ItemStack(material, 1, (short) data)); + public MiscDisguise(DisguiseType disguiseType, Material material, int data) { + this(disguiseType, new ItemStack(material, 1, (short) data)); } - public MiscDisguise(ItemStack itemStack) { - super(DisguiseType.DROPPED_ITEM); + public MiscDisguise(DisguiseType disguiseType, ItemStack itemStack) { + super(disguiseType); - apply(0, 0, itemStack); -} + if (disguiseType != DisguiseType.FALLING_BLOCK && disguiseType != DisguiseType.DROPPED_ITEM) { + throw new IllegalArgumentException( + "This constructor requires a DROPPED_ITEM or FALLING_BLOCK disguise type!"); + } + + apply(0, itemStack); + } public MiscDisguise(DisguiseType disguiseType, int id) { this(disguiseType, id, disguiseType.getDefaultData()); } + @Deprecated public MiscDisguise(DisguiseType disguiseType, int id, int data) { super(disguiseType); @@ -45,10 +49,10 @@ public class MiscDisguise extends TargetedDisguise { " instead"); } - apply(id, data, new ItemStack(Material.STONE)); + apply(id, new ItemStack(Material.STONE)); } - private void apply(int id, int data, ItemStack itemStack) { + private void apply(int id, ItemStack itemStack) { createDisguise(); this.id = getType().getTypeId(); diff --git a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java index bf355b13..32033f6e 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/PacketsManager.java @@ -18,6 +18,7 @@ import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseConfig; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.*; +import me.libraryaddict.disguise.disguisetypes.watchers.FallingBlockWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.LivingWatcher; import me.libraryaddict.disguise.disguisetypes.watchers.PlayerWatcher; import me.libraryaddict.disguise.utilities.packetlisteners.*; @@ -432,7 +433,9 @@ public class PacketsManager { int data = ((MiscDisguise) disguise).getData(); if (disguise.getType() == DisguiseType.FALLING_BLOCK) { - data = ReflectionManager.getCombinedId(((MiscDisguise) disguise).getId(), data); + ItemStack block = ((FallingBlockWatcher) disguise.getWatcher()).getBlock(); + + data = ReflectionManager.getCombinedIdByItemStack(block); } else if (disguise.getType() == DisguiseType.FISHING_HOOK && data == -1) { // If the MiscDisguise data isn't set. Then no entity id was provided, so default to the owners // entity id @@ -448,6 +451,16 @@ public class PacketsManager { .createPacket(nmsEntity, objectId, data); packets.addPacket(spawnEntity); + // If it's not the same type, then highly likely they have different velocity settings which we'd want to + // cancel + if (DisguiseType.getType(disguisedEntity) != disguise.getType()) { + StructureModifier ints = spawnEntity.getIntegers(); + + ints.write(1, 0); + ints.write(2, 0); + ints.write(3, 0); + } + spawnEntity.getModifier().write(8, pitch); spawnEntity.getModifier().write(9, yaw); @@ -1089,7 +1102,8 @@ public class PacketsManager { // If the entity is sending velocity and its a falling block else if (sentPacket.getType() == Server.ENTITY_VELOCITY) { - if (disguise.getType() == DisguiseType.FALLING_BLOCK) { + // If the disguise is a misc and the disguised is not the same type + if (disguise.getType().isMisc() && DisguiseType.getType(entity) != disguise.getType()) { packets.clear(); } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java index a26b8f67..b50d03dc 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -833,33 +833,13 @@ public class ReflectionManager { return ((Double) value).floatValue(); } else if (value instanceof NbtWrapper) { return ((NbtWrapper) value).getHandle(); - } else if (value instanceof Particle) { - return getParticleType((Particle) value); + } else if (value instanceof WrappedParticle) { + return ((WrappedParticle) value).getHandle(); } return value; } - private static Object getParticleType(Particle particle) { - try { - Class craftParticle = getCraftClass("CraftParticle"); - Field mcKeyField = craftParticle.getDeclaredField("minecraftKey"); - mcKeyField.setAccessible(true); - Object mcKey = mcKeyField.get(Enum.valueOf(craftParticle, particle.name())); - - Class typesClass = getNmsClass("IRegistry"); - - Object registry = typesClass.getField("PARTICLE_TYPE").get(null); - - return registry.getClass().getMethod("get", mcKey.getClass()).invoke(registry, mcKey); - } - catch (Exception e) { - e.printStackTrace(); - } - - return null; - } - public static String getMinecraftVersion() { String version = Bukkit.getVersion(); version = version.substring(version.lastIndexOf(" ") + 1, version.length() - 1); @@ -979,10 +959,6 @@ public class ReflectionManager { return new WrappedWatchableObject(watcherItem); } - public static int getCombinedId(int id, int data) { - return id + (data << 12); - } - public static int getCombinedIdByItemStack(ItemStack itemStack) { try { Object nmsItem = getNmsItem(itemStack); @@ -991,7 +967,9 @@ public class ReflectionManager { Object nmsBlock = getNmsMethod(blockClass, "asBlock", getNmsClass("Item")).invoke(null, item); - return (int) getNmsMethod(blockClass, "getBlockData").invoke(nmsBlock); + Object iBlockData = getNmsMethod(blockClass, "getBlockData").invoke(nmsBlock); + + return (int) getNmsMethod("Block", "getCombinedId", getNmsClass("IBlockData")).invoke(null, iBlockData); } catch (Exception ex) { ex.printStackTrace();