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"
*/
private static boolean cancelSound;
private Object stepSoundEffect;
private Method getHealth, getSomething;
public PacketListenerSounds(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS);
stepSoundEffect = ReflectionManager.getCraftSound(Sound.BLOCK_GRASS_STEP);
}
@Override
@ -51,7 +47,9 @@ public class PacketListenerSounds extends PacketAdapter {
}
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary
{
return;
}
event.setPacket(event.getPacket().deepClone());
@ -98,24 +96,7 @@ public class PacketListenerSounds extends PacketAdapter {
}
if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) {
boolean hasInvun = false;
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();
}
boolean hasInvun = ReflectionManager.hasInvul(entity);
soundType = entitySound.getType(soundEffectObj, !hasInvun);
} else {
@ -143,77 +124,56 @@ public class PacketListenerSounds extends PacketAdapter {
if (sound == null) {
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));
mods.write(0, sound);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
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()));
}
// 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, disguiseSound.getDamageAndIdleSoundVolume());
}
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
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, disguiseSound.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) {
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 (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity &&
((MobDisguise) disguise).doesDisguiseAge()) {
boolean baby = false;
if (((MobDisguise) disguise).isAdult() == baby) {
float pitch = (Float) mods.read(6);
if (disguisedEntity instanceof Zombie) {
baby = ((Zombie) disguisedEntity).isBaby();
} else if (disguisedEntity instanceof Ageable) {
baby = !((Ageable) disguisedEntity).isAdult();
}
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
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 *= 63;
if (pitch < 0)
@ -222,10 +182,10 @@ public class PacketListenerSounds extends PacketAdapter {
if (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())) {
SoundGroup disSound = SoundGroup.getGroup(entity.getType().name());
if (disSound == null)
if (disSound == null) {
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.HURT;
SoundType soundType = SoundType.HURT;
Object obj = null;
if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) {
soundType = SoundType.DEATH;
}
if (disSound.getSound(soundType) == null ||
@ -277,8 +223,9 @@ public class PacketListenerSounds extends PacketAdapter {
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) {
cancelSound = !cancelSound;
if (cancelSound)
if (cancelSound) {
return;
}
}
disSound = SoundGroup.getGroup(disguise);
@ -305,12 +252,14 @@ public class PacketListenerSounds extends PacketAdapter {
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) {
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.5F;
} else
} else {
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.0F;
}
if (disguise.getType() == DisguiseType.BAT)
if (disguise.getType() == DisguiseType.BAT) {
pitch *= 0.95F;
}
/* pitch *= 63;
@ -324,8 +273,7 @@ public class PacketListenerSounds extends PacketAdapter {
try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
}
catch (InvocationTargetException e) {
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

View File

@ -101,6 +101,9 @@ public class ReflectionManager {
private static Constructor mobEffectConstructor;
private static Method boundingBoxMethod;
private static Method bukkitEntityMethod;
private static Field noDamageTicks;
private static Method isInvul;
private static Object genericDamage;
public static void init() {
// Sometimes it doesn't like me if I don't set this :\
@ -176,6 +179,10 @@ public class ReflectionManager {
deserializedItemMeta =
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");
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) {
if (obj.isAnnotationPresent(NmsAddedIn.class)) {
NmsAddedIn added = obj.getAnnotation(NmsAddedIn.class);