Cleanup more reflection
This commit is contained in:
		| @@ -31,13 +31,9 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|      * "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard" |      * "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard" | ||||||
|      */ |      */ | ||||||
|     private static boolean cancelSound; |     private static boolean cancelSound; | ||||||
|     private Object stepSoundEffect; |  | ||||||
|     private Method getHealth, getSomething; |  | ||||||
|  |  | ||||||
|     public PacketListenerSounds(LibsDisguises plugin) { |     public PacketListenerSounds(LibsDisguises plugin) { | ||||||
|         super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS); |         super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS); | ||||||
|  |  | ||||||
|         stepSoundEffect = ReflectionManager.getCraftSound(Sound.BLOCK_GRASS_STEP); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -51,7 +47,9 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary |         if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary | ||||||
|  |         { | ||||||
|             return; |             return; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         event.setPacket(event.getPacket().deepClone()); |         event.setPacket(event.getPacket().deepClone()); | ||||||
|  |  | ||||||
| @@ -98,24 +96,7 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) { |                     if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) { | ||||||
|                         boolean hasInvun = false; |                         boolean hasInvun = ReflectionManager.hasInvul(entity); | ||||||
|  |  | ||||||
|                         Object nmsEntity = ReflectionManager.getNmsEntity(entity); |  | ||||||
|  |  | ||||||
|                         try { |  | ||||||
|                             if (entity instanceof LivingEntity) { |  | ||||||
|                                 hasInvun = |  | ||||||
|                                         ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) > 0; |  | ||||||
|                             } else { |  | ||||||
|                                 Class clazz = ReflectionManager.getNmsClass("DamageSource"); |  | ||||||
|  |  | ||||||
|                                 hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz) |  | ||||||
|                                         .invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC").get(null)); |  | ||||||
|                             } |  | ||||||
|                         } |  | ||||||
|                         catch (Exception ex) { |  | ||||||
|                             ex.printStackTrace(); |  | ||||||
|                         } |  | ||||||
|  |  | ||||||
|                         soundType = entitySound.getType(soundEffectObj, !hasInvun); |                         soundType = entitySound.getType(soundEffectObj, !hasInvun); | ||||||
|                     } else { |                     } else { | ||||||
| @@ -143,77 +124,56 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                 if (sound == null) { |                 if (sound == null) { | ||||||
|                     event.setCancelled(true); |                     event.setCancelled(true); | ||||||
|                 } else { |                 } else { | ||||||
|                     if (sound.equals("step.grass")) { |                     mods.write(0, sound); | ||||||
|                         try { |                     mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); | ||||||
|                             Block block = observer.getWorld().getBlockAt((int) Math.floor(soundCords[0] / 8D), |  | ||||||
|                                     (int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D)); |  | ||||||
|  |  | ||||||
|                             if (block != null) { |                     // Time to change the pitch and volume | ||||||
|                                 Object nmsBlock = ReflectionManager.getCraftMethod("block.CraftBlock", "getNMSBlock") |                     if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) { | ||||||
|                                         .invoke(block); |                         // If the volume is the default | ||||||
|  |                         if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) { | ||||||
|                                 Object step = ReflectionManager.getNmsMethod("Block", "getStepSound").invoke(nmsBlock); |                             mods.write(5, disguiseSound.getDamageAndIdleSoundVolume()); | ||||||
|  |  | ||||||
|                                 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, sound); |  | ||||||
|                         mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); |  | ||||||
|  |  | ||||||
|                         // Time to change the pitch and volume |                         // Here I assume its the default pitch as I can't calculate if its real. | ||||||
|                         if (soundType == SoundType.HURT || soundType == SoundType.DEATH || |                         if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity && | ||||||
|                                 soundType == SoundType.IDLE) { |                                 ((MobDisguise) disguise).doesDisguiseAge()) { | ||||||
|                             // If the volume is the default |                             boolean baby = false; | ||||||
|                             if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) { |  | ||||||
|                                 mods.write(5, disguiseSound.getDamageAndIdleSoundVolume()); |                             if (disguisedEntity instanceof Zombie) { | ||||||
|  |                                 baby = ((Zombie) disguisedEntity).isBaby(); | ||||||
|  |                             } else if (disguisedEntity instanceof Ageable) { | ||||||
|  |                                 baby = !((Ageable) disguisedEntity).isAdult(); | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|                             // Here I assume its the default pitch as I can't calculate if its real. |                             if (((MobDisguise) disguise).isAdult() == baby) { | ||||||
|                             if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity && |                                 float pitch = (Float) mods.read(6); | ||||||
|                                     ((MobDisguise) disguise).doesDisguiseAge()) { |  | ||||||
|                                 boolean baby = false; |  | ||||||
|  |  | ||||||
|                                 if (disguisedEntity instanceof Zombie) { |                                 if (baby) { | ||||||
|                                     baby = ((Zombie) disguisedEntity).isBaby(); |                                     // If the pitch is not the expected | ||||||
|                                 } else if (disguisedEntity instanceof Ageable) { |                                     if (pitch < 1.5 || pitch > 1.7) { | ||||||
|                                     baby = !((Ageable) disguisedEntity).isAdult(); |                                         return; | ||||||
|                                 } |  | ||||||
|  |  | ||||||
|                                 if (((MobDisguise) disguise).isAdult() == baby) { |  | ||||||
|                                     float pitch = (Float) mods.read(6); |  | ||||||
|  |  | ||||||
|                                     if (baby) { |  | ||||||
|                                         // If the pitch is not the expected |  | ||||||
|                                         if (pitch < 1.5 || pitch > 1.7) |  | ||||||
|                                             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 < 1 || pitch > 1.2) |  | ||||||
|                                             return; |  | ||||||
|  |  | ||||||
|                                         pitch = (DisguiseUtilities.random.nextFloat() - |  | ||||||
|                                                 DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; |  | ||||||
|                                         // Min = 1 |  | ||||||
|                                         // Cap = 63 |  | ||||||
|                                         // Max = 1.2 |  | ||||||
|                                         // Cap = 75.6 |  | ||||||
|                                     } |                                     } | ||||||
|  |  | ||||||
|  |                                     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 < 1 || pitch > 1.2) { | ||||||
|  |                                         return; | ||||||
|  |                                     } | ||||||
|  |  | ||||||
|  |                                     pitch = (DisguiseUtilities.random.nextFloat() - | ||||||
|  |                                             DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; | ||||||
|  |                                     // Min = 1 | ||||||
|  |                                     // Cap = 63 | ||||||
|  |                                     // Max = 1.2 | ||||||
|  |                                     // Cap = 75.6 | ||||||
|  |                                 } | ||||||
|  |  | ||||||
|                                     /*pitch *= 63; |                                     /*pitch *= 63; | ||||||
|  |  | ||||||
|                                     if (pitch < 0) |                                     if (pitch < 0) | ||||||
| @@ -222,10 +182,10 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                                     if (pitch > 255) |                                     if (pitch > 255) | ||||||
|                                         pitch = 255;*/ |                                         pitch = 255;*/ | ||||||
|  |  | ||||||
|                                     mods.write(6, pitch); |                                 mods.write(6, pitch); | ||||||
|                                 } |  | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
| @@ -247,29 +207,15 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                     (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { |                     (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { | ||||||
|                 SoundGroup disSound = SoundGroup.getGroup(entity.getType().name()); |                 SoundGroup disSound = SoundGroup.getGroup(entity.getType().name()); | ||||||
|  |  | ||||||
|                 if (disSound == null) |                 if (disSound == null) { | ||||||
|                     return; |                     return; | ||||||
|  |  | ||||||
|                 SoundType soundType = null; |  | ||||||
|                 Object obj = null; |  | ||||||
|  |  | ||||||
|                 if (entity instanceof LivingEntity) { |  | ||||||
|                     try { |  | ||||||
|                         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(); |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 if (obj == null) { |                 SoundType soundType = SoundType.HURT; | ||||||
|                     soundType = SoundType.HURT; |                 Object obj = null; | ||||||
|  |  | ||||||
|  |                 if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) { | ||||||
|  |                     soundType = SoundType.DEATH; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 if (disSound.getSound(soundType) == null || |                 if (disSound.getSound(soundType) == null || | ||||||
| @@ -277,8 +223,9 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                     if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) { |                     if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) { | ||||||
|                         cancelSound = !cancelSound; |                         cancelSound = !cancelSound; | ||||||
|  |  | ||||||
|                         if (cancelSound) |                         if (cancelSound) { | ||||||
|                             return; |                             return; | ||||||
|  |                         } | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     disSound = SoundGroup.getGroup(disguise); |                     disSound = SoundGroup.getGroup(disguise); | ||||||
| @@ -305,12 +252,14 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|                             if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { |                             if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { | ||||||
|                                 pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * |                                 pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * | ||||||
|                                         0.2F + 1.5F; |                                         0.2F + 1.5F; | ||||||
|                             } else |                             } else { | ||||||
|                                 pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * |                                 pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * | ||||||
|                                         0.2F + 1.0F; |                                         0.2F + 1.0F; | ||||||
|  |                             } | ||||||
|  |  | ||||||
|                             if (disguise.getType() == DisguiseType.BAT) |                             if (disguise.getType() == DisguiseType.BAT) { | ||||||
|                                 pitch *= 0.95F; |                                 pitch *= 0.95F; | ||||||
|  |                             } | ||||||
|  |  | ||||||
|                          /*   pitch *= 63; |                          /*   pitch *= 63; | ||||||
|  |  | ||||||
| @@ -324,8 +273,7 @@ public class PacketListenerSounds extends PacketAdapter { | |||||||
|  |  | ||||||
|                             try { |                             try { | ||||||
|                                 ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); |                                 ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); | ||||||
|                             } |                             } catch (InvocationTargetException e) { | ||||||
|                             catch (InvocationTargetException e) { |  | ||||||
|                                 e.printStackTrace(); |                                 e.printStackTrace(); | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|   | |||||||
| @@ -101,6 +101,9 @@ public class ReflectionManager { | |||||||
|     private static Constructor mobEffectConstructor; |     private static Constructor mobEffectConstructor; | ||||||
|     private static Method boundingBoxMethod; |     private static Method boundingBoxMethod; | ||||||
|     private static Method bukkitEntityMethod; |     private static Method bukkitEntityMethod; | ||||||
|  |     private static Field noDamageTicks; | ||||||
|  |     private static Method isInvul; | ||||||
|  |     private static Object genericDamage; | ||||||
|  |  | ||||||
|     public static void init() { |     public static void init() { | ||||||
|         // Sometimes it doesn't like me if I don't set this :\ |         // Sometimes it doesn't like me if I don't set this :\ | ||||||
| @@ -176,6 +179,10 @@ public class ReflectionManager { | |||||||
|             deserializedItemMeta = |             deserializedItemMeta = | ||||||
|                     getCraftMethod(getCraftClass("inventory.CraftMetaItem$SerializableMeta"), "deserialize", Map.class); |                     getCraftMethod(getCraftClass("inventory.CraftMetaItem$SerializableMeta"), "deserialize", Map.class); | ||||||
|  |  | ||||||
|  |             noDamageTicks = getNmsField("Entity", "noDamageTicks"); | ||||||
|  |             isInvul = getNmsMethod("Entity", "isInvulnerable", getNmsClass("DamageSource")); | ||||||
|  |             genericDamage = getNmsField("DamageSource", "GENERIC"); | ||||||
|  |  | ||||||
|             Method method = getNmsMethod("SoundCategory", "a"); |             Method method = getNmsMethod("SoundCategory", "a"); | ||||||
|  |  | ||||||
|             for (Enum anEnum : (Enum[]) getNmsClass("SoundCategory").getEnumConstants()) { |             for (Enum anEnum : (Enum[]) getNmsClass("SoundCategory").getEnumConstants()) { | ||||||
| @@ -236,6 +243,22 @@ public class ReflectionManager { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public static boolean hasInvul(Entity entity) { | ||||||
|  |         Object nmsEntity = ReflectionManager.getNmsEntity(entity); | ||||||
|  |  | ||||||
|  |         try { | ||||||
|  |             if (entity instanceof LivingEntity) { | ||||||
|  |                 return noDamageTicks.getInt(nmsEntity) > 0; | ||||||
|  |             } else { | ||||||
|  |                 return (boolean) isInvul.invoke(nmsEntity, genericDamage); | ||||||
|  |             } | ||||||
|  |         } catch (Exception ex) { | ||||||
|  |             ex.printStackTrace(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     public static boolean isSupported(AccessibleObject obj) { |     public static boolean isSupported(AccessibleObject obj) { | ||||||
|         if (obj.isAnnotationPresent(NmsAddedIn.class)) { |         if (obj.isAnnotationPresent(NmsAddedIn.class)) { | ||||||
|             NmsAddedIn added = obj.getAnnotation(NmsAddedIn.class); |             NmsAddedIn added = obj.getAnnotation(NmsAddedIn.class); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user