Cleanup more reflection

This commit is contained in:
libraryaddict 2020-08-13 12:10:48 +12:00
parent 15b4a6e935
commit 93936c3628
2 changed files with 83 additions and 112 deletions

View File

@ -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 {
@ -142,35 +123,12 @@ public class PacketListenerSounds extends PacketAdapter {
if (sound == null) { if (sound == null) {
event.setCancelled(true); event.setCancelled(true);
} else {
if (sound.equals("step.grass")) {
try {
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) {
Object nmsBlock = ReflectionManager.getCraftMethod("block.CraftBlock", "getNMSBlock")
.invoke(block);
Object step = ReflectionManager.getNmsMethod("Block", "getStepSound").invoke(nmsBlock);
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 { } else {
mods.write(0, sound); mods.write(0, sound);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
// Time to change the pitch and volume // Time to change the pitch and volume
if (soundType == SoundType.HURT || soundType == SoundType.DEATH || if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) {
soundType == SoundType.IDLE) {
// If the volume is the default // If the volume is the default
if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) { if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) {
mods.write(5, disguiseSound.getDamageAndIdleSoundVolume()); mods.write(5, disguiseSound.getDamageAndIdleSoundVolume());
@ -192,8 +150,9 @@ public class PacketListenerSounds extends PacketAdapter {
if (baby) { if (baby) {
// If the pitch is not the expected // If the pitch is not the expected
if (pitch < 1.5 || pitch > 1.7) if (pitch < 1.5 || pitch > 1.7) {
return; return;
}
pitch = (DisguiseUtilities.random.nextFloat() - pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F; DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F;
@ -203,8 +162,9 @@ public class PacketListenerSounds extends PacketAdapter {
// Cap = 110.5 // Cap = 110.5
} else { } else {
// If the pitch is not the expected // If the pitch is not the expected
if (pitch < 1 || pitch > 1.2) if (pitch < 1 || pitch > 1.2) {
return; return;
}
pitch = (DisguiseUtilities.random.nextFloat() - pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F;
@ -225,7 +185,7 @@ public class PacketListenerSounds extends PacketAdapter {
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; SoundType soundType = SoundType.HURT;
Object obj = null; Object obj = null;
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) {
try {
obj = LivingEntity.class.getMethod("getHealth").invoke(entity);
if (obj instanceof Double ? (Double) obj == 0 : (Integer) obj == 0) {
soundType = SoundType.DEATH; soundType = SoundType.DEATH;
} else {
obj = null;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
if (obj == null) {
soundType = SoundType.HURT;
} }
if (disSound.getSound(soundType) == null || if (disSound.getSound(soundType) == null ||
@ -277,9 +223,10 @@ 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();
} }
} }

View File

@ -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);