From 06c0084e3a963f387fd4817b31cd84ca4c6fe771 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Mon, 16 May 2016 03:51:16 +1200 Subject: [PATCH] Fix sounds, release 9.0.6 --- pom.xml | 2 +- .../packetlisteners/PacketListenerSounds.java | 352 +++++++++--------- 2 files changed, 181 insertions(+), 173 deletions(-) diff --git a/pom.xml b/pom.xml index 594b2d5b..9f3d5d1a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 LibsDisguises LibsDisguises - 9.0.5-SNAPSHOT + 9.0.6 src diff --git a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java index 5aaf7585..2ee30f41 100644 --- a/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java +++ b/src/me/libraryaddict/disguise/utilities/packetlisteners/PacketListenerSounds.java @@ -16,7 +16,6 @@ import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; - import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; @@ -47,6 +46,7 @@ public class PacketListenerSounds extends PacketAdapter { return; } + if (event.isAsync()) { return; @@ -63,19 +63,28 @@ public class PacketListenerSounds extends PacketAdapter if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) { - Object soundEffect = mods.read(0); - SoundType soundType = null; - Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(2)) / 8D, ((Integer) mods.read(3)) / 8D, - ((Integer) mods.read(4)) / 8D); + int[] soundCords = new int[] + { + (Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4) + }; + + int chunkX = (int) Math.floor((soundCords[0] / 8D) / 16D); + int chunkZ = (int) Math.floor((soundCords[2] / 8D) / 16D); + + if (!observer.getWorld().isChunkLoaded(chunkX, chunkZ)) + { + return; + } Entity disguisedEntity = null; DisguiseSound entitySound = null; Disguise disguise = null; - Entity[] entities = soundLoc.getChunk().getEntities(); + String soundEffect = ReflectionManager.convertSoundEffectToString(mods.read(0)); + Entity[] entities = observer.getWorld().getChunkAt(chunkX, chunkZ).getEntities(); for (Entity entity : entities) { @@ -85,198 +94,197 @@ public class PacketListenerSounds extends PacketAdapter { Location loc = entity.getLocation(); - loc = new Location(observer.getWorld(), ((int) (loc.getX() * 8)) / 8D, ((int) (loc.getY() * 8)) / 8D, - ((int) (loc.getZ() * 8)) / 8D); - - if (loc.equals(soundLoc)) - { - entitySound = DisguiseSound.getType(entity.getType().name()); - - if (entitySound != null) + int[] entCords = new int[] { - Object obj = null; + (int) (loc.getX() * 8), (int) (loc.getY() * 8), (int) (loc.getZ() * 8) + }; - if (entity instanceof LivingEntity) + if (soundCords[0] != entCords[0] || soundCords[1] != entCords[1] || soundCords[2] != entCords[2]) + { + continue; + } + + entitySound = DisguiseSound.getType(entity.getType().name()); + + if (entitySound == null) + { + continue; + } + + Object obj = null; + + if (entity instanceof LivingEntity) + { + try + { + // Use reflection so that this works for either int or double methods + obj = LivingEntity.class.getMethod("getHealth").invoke(entity); + + if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) { - try - { - // Use reflection so that this works for either int or double methods - obj = LivingEntity.class.getMethod("getHealth").invoke(entity); - - if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) - { - soundType = SoundType.DEATH; - } - else - { - obj = null; - } - } - catch (Exception e) - { - e.printStackTrace(); - } + soundType = SoundType.DEATH; } - - if (obj == null) + else { - boolean hasInvun = false; - - Object nmsEntity = ReflectionManager.getNmsEntity(entity); - - try - { - if (entity instanceof LivingEntity) - { - hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks") - .getInt(nmsEntity) == ReflectionManager - .getNmsField("EntityLiving", "maxNoDamageTicks").getInt(nmsEntity); - } - else - { - Class clazz = ReflectionManager.getNmsClass("DamageSource"); - - hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz) - .invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC")); - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - - soundType = entitySound.getType(ReflectionManager.convertSoundEffectToString(soundEffect), - !hasInvun); - } - - if (soundType != null) - { - disguise = entityDisguise; - disguisedEntity = entity; - break; + obj = null; } } + catch (Exception e) + { + e.printStackTrace(); + } + } + + if (obj == null) + { + boolean hasInvun = false; + + Object nmsEntity = ReflectionManager.getNmsEntity(entity); + + try + { + if (entity instanceof LivingEntity) + { + hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks") + .getInt(nmsEntity) == ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks") + .getInt(nmsEntity); + } + else + { + Class clazz = ReflectionManager.getNmsClass("DamageSource"); + + hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz) + .invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC")); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + soundType = entitySound.getType(soundEffect, !hasInvun); + } + + if (soundType != null) + { + disguise = entityDisguise; + disguisedEntity = entity; + break; } } } - if (disguise != null) + if (disguise != null && disguise.isSoundsReplaced() + && (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) { - if (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != event.getPlayer()) + String sound = null; + + DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); + + if (dSound != null) + sound = dSound.getSound(soundType); + + if (sound == null) { - if (disguise.isSoundsReplaced()) + event.setCancelled(true); + } + else + { + if (sound.equals("step.grass")) { - String sound = null; - - DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); - - if (dSound != null) - sound = dSound.getSound(soundType); - - if (sound == null) + try { - event.setCancelled(true); - } - else - { - if (sound.equals("step.grass")) + int typeId = observer.getWorld().getBlockTypeIdAt((int) Math.floor(soundCords[0] / 8D), + (int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D)); + + Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", int.class) + .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId); + + if (block != null) { - try - { - int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(), - soundLoc.getBlockY() - 1, soundLoc.getBlockZ()); + Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block); - Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", int.class) - .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId); - - if (block != null) - { - Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block); - - mods.write(0, - ReflectionManager.getNmsMethod(step.getClass(), "d").invoke(step)); - mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); - } - } - catch (Exception ex) - { - ex.printStackTrace(); - } - // There is no else statement. Because seriously. This should never be null. Unless - // someone is - // sending fake sounds. In which case. Why cancel it. - } - else - { - mods.write(0, ReflectionManager.getCraftSoundEffect(sound)); + mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "d").invoke(step)); mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + } + // There is no else statement. Because seriously. This should never be null. Unless + // someone is + // sending fake sounds. In which case. Why cancel it. + } + else + { + mods.write(0, ReflectionManager.getCraftSoundEffect(sound)); + mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); - // Time to change the pitch and volume - if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) + // Time to change the pitch and volume + if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) + { + // If the volume is the default + if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) + { + mods.write(5, dSound.getDamageAndIdleSoundVolume()); + } + + // Here I assume its the default pitch as I can't calculate if its real. + if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity + && ((MobDisguise) disguise).doesDisguiseAge()) + { + boolean baby = false; + + if (disguisedEntity instanceof Zombie) { - // If the volume is the default - if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) + baby = ((Zombie) disguisedEntity).isBaby(); + } + else if (disguisedEntity instanceof Ageable) + { + baby = !((Ageable) disguisedEntity).isAdult(); + } + + if (((MobDisguise) disguise).isAdult() == baby) + { + float pitch = (Integer) mods.read(6); + + if (baby) { - mods.write(5, dSound.getDamageAndIdleSoundVolume()); + // If the pitch is not the expected + if (pitch > 97 || pitch < 111) + return; + + pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) + * 0.2F + 1.5F; + // Min = 1.5 + // Cap = 97.5 + // Max = 1.7 + // Cap = 110.5 + } + else + { + // If the pitch is not the expected + if (pitch >= 63 || pitch <= 76) + return; + + pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) + * 0.2F + 1.0F; + // Min = 1 + // Cap = 63 + // Max = 1.2 + // Cap = 75.6 } - // Here I assume its the default pitch as I can't calculate if its real. - if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity - && ((MobDisguise) disguise).doesDisguiseAge()) - { - boolean baby = false; + pitch *= 63; - if (disguisedEntity instanceof Zombie) - { - baby = ((Zombie) disguisedEntity).isBaby(); - } - else if (disguisedEntity instanceof Ageable) - { - baby = !((Ageable) disguisedEntity).isAdult(); - } + if (pitch < 0) + pitch = 0; - if (((MobDisguise) disguise).isAdult() == baby) - { - float pitch = (Integer) mods.read(6); + if (pitch > 255) + pitch = 255; - if (baby) - { - // If the pitch is not the expected - if (pitch > 97 || pitch < 111) - return; - - pitch = (DisguiseUtilities.random.nextFloat() - - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F; - // Min = 1.5 - // Cap = 97.5 - // Max = 1.7 - // Cap = 110.5 - } - else - { - // If the pitch is not the expected - if (pitch >= 63 || pitch <= 76) - return; - - pitch = (DisguiseUtilities.random.nextFloat() - - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; - // Min = 1 - // Cap = 63 - // Max = 1.2 - // Cap = 75.6 - } - - pitch *= 63; - - if (pitch < 0) - pitch = 0; - - if (pitch > 255) - pitch = 255; - - mods.write(6, (int) pitch); - } - } + mods.write(6, (int) pitch); } } }