Fix the sound pitches being completely wrong

This commit is contained in:
libraryaddict 2018-02-14 19:14:49 +13:00
parent 206207a374
commit e6c904be6a
2 changed files with 60 additions and 54 deletions

View File

@ -32,25 +32,33 @@ public class ReflectionManager {
public static final Field entityCountField; public static final Field entityCountField;
static { static {
for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { try {
try { Object entity = createEntityInstance("Cow");
if (method.getReturnType() == float.class && Modifier.isProtected(method.getModifiers()) &&
method.getParameterTypes().length == 0) {
Object entity = createEntityInstance("Cow");
method.setAccessible(true); for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) {
float value = (Float) method.invoke(entity); if (method.getReturnType() != float.class)
continue;
if (value == 0.4F) { if (!Modifier.isProtected(method.getModifiers()))
damageAndIdleSoundMethod = method; continue;
break;
} if (method.getParameterTypes().length != 0)
} continue;
}
catch (Exception ex) { method.setAccessible(true);
ex.printStackTrace();
float value = (Float) method.invoke(entity);
if ((float) method.invoke(entity) != 0.4f)
continue;
damageAndIdleSoundMethod = method;
break;
} }
} }
catch (Exception ex) {
ex.printStackTrace();
}
craftItemClass = getCraftClass("inventory.CraftItemStack"); craftItemClass = getCraftClass("inventory.CraftItemStack");
@ -559,8 +567,6 @@ public class ReflectionManager {
public static Float getSoundModifier(Object entity) { public static Float getSoundModifier(Object entity) {
try { try {
damageAndIdleSoundMethod.setAccessible(true);
return (Float) damageAndIdleSoundMethod.invoke(entity); return (Float) damageAndIdleSoundMethod.invoke(entity);
} }
catch (Exception ignored) { catch (Exception ignored) {

View File

@ -1,14 +1,5 @@
package me.libraryaddict.disguise.utilities.packetlisteners; package me.libraryaddict.disguise.utilities.packetlisteners;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Location;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Zombie;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
@ -16,7 +7,6 @@ import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
@ -26,6 +16,10 @@ import me.libraryaddict.disguise.utilities.DisguiseSound;
import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType; import me.libraryaddict.disguise.utilities.DisguiseSound.SoundType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.ReflectionManager; import me.libraryaddict.disguise.utilities.ReflectionManager;
import org.bukkit.Location;
import org.bukkit.entity.*;
import java.lang.reflect.InvocationTargetException;
public class PacketListenerSounds extends PacketAdapter { public class PacketListenerSounds extends PacketAdapter {
/** /**
@ -120,15 +114,14 @@ public class PacketListenerSounds extends PacketAdapter {
try { try {
if (entity instanceof LivingEntity) { if (entity instanceof LivingEntity) {
hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt( hasInvun = ReflectionManager.getNmsField("Entity", "noDamageTicks").getInt(nmsEntity) ==
nmsEntity) == ReflectionManager.getNmsField("EntityLiving", ReflectionManager.getNmsField("EntityLiving", "maxNoDamageTicks")
"maxNoDamageTicks").getInt(nmsEntity); .getInt(nmsEntity);
} else { } else {
Class clazz = ReflectionManager.getNmsClass("DamageSource"); Class clazz = ReflectionManager.getNmsClass("DamageSource");
hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", hasInvun = (Boolean) ReflectionManager.getNmsMethod("Entity", "isInvulnerable", clazz)
clazz).invoke(nmsEntity, .invoke(nmsEntity, ReflectionManager.getNmsField(clazz, "GENERIC").get(null));
ReflectionManager.getNmsField(clazz, "GENERIC").get(null));
} }
} }
catch (Exception ex) { catch (Exception ex) {
@ -146,7 +139,8 @@ public class PacketListenerSounds extends PacketAdapter {
} }
} }
if (disguise != null && disguise.isSoundsReplaced() && (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) { if (disguise != null && disguise.isSoundsReplaced() &&
(disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) {
String sound = null; String sound = null;
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name()); DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
@ -162,9 +156,8 @@ public class PacketListenerSounds extends PacketAdapter {
int typeId = observer.getWorld().getBlockTypeIdAt((int) Math.floor(soundCords[0] / 8D), int typeId = observer.getWorld().getBlockTypeIdAt((int) Math.floor(soundCords[0] / 8D),
(int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D)); (int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D));
Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", int.class)
int.class).invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId);
typeId);
if (block != null) { if (block != null) {
Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block); Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block);
@ -184,14 +177,16 @@ public class PacketListenerSounds extends PacketAdapter {
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 || soundType == SoundType.IDLE) { if (soundType == SoundType.HURT || soundType == SoundType.DEATH ||
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, dSound.getDamageAndIdleSoundVolume()); mods.write(5, dSound.getDamageAndIdleSoundVolume());
} }
// Here I assume its the default pitch as I can't calculate if its real. // 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()) { if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity &&
((MobDisguise) disguise).doesDisguiseAge()) {
boolean baby = false; boolean baby = false;
if (disguisedEntity instanceof Zombie) { if (disguisedEntity instanceof Zombie) {
@ -205,33 +200,35 @@ 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 > 97 || pitch < 111) if (pitch < 1.5 || pitch > 1.7)
return; return;
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F; pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F;
// Min = 1.5 // Min = 1.5
// Cap = 97.5 // Cap = 97.5
// Max = 1.7 // Max = 1.7
// Cap = 110.5 // Cap = 110.5
} else { } else {
// If the pitch is not the expected // If the pitch is not the expected
if (pitch >= 63 || pitch <= 76) if (pitch < 1 || pitch > 1.2)
return; return;
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F;
// Min = 1 // Min = 1
// Cap = 63 // Cap = 63
// Max = 1.2 // Max = 1.2
// Cap = 75.6 // Cap = 75.6
} }
pitch *= 63; /*pitch *= 63;
if (pitch < 0) if (pitch < 0)
pitch = 0; pitch = 0;
if (pitch > 255) if (pitch > 255)
pitch = 255; pitch = 255;*/
mods.write(6, pitch); mods.write(6, pitch);
} }
@ -250,7 +247,8 @@ public class PacketListenerSounds extends PacketAdapter {
Disguise disguise = DisguiseAPI.getDisguise(observer, entity); Disguise disguise = DisguiseAPI.getDisguise(observer, entity);
if (disguise != null && !disguise.getType().isPlayer() && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { if (disguise != null && !disguise.getType().isPlayer() &&
(disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) {
DisguiseSound disSound = DisguiseSound.getType(entity.getType().name()); DisguiseSound disSound = DisguiseSound.getType(entity.getType().name());
if (disSound == null) if (disSound == null)
@ -278,8 +276,8 @@ public class PacketListenerSounds extends PacketAdapter {
soundType = SoundType.HURT; soundType = SoundType.HURT;
} }
if (disSound.getSound( if (disSound.getSound(soundType) == null ||
soundType) == null || (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) { (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) {
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) { if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) {
cancelSound = !cancelSound; cancelSound = !cancelSound;
@ -311,22 +309,24 @@ public class PacketListenerSounds extends PacketAdapter {
float pitch; float pitch;
if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) {
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.5F; pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.5F;
} else } else
pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) *
0.2F + 1.0F;
if (disguise.getType() == DisguiseType.BAT) if (disguise.getType() == DisguiseType.BAT)
pitch *= 95F; pitch *= 0.95F;
pitch *= 63; /* pitch *= 63;
if (pitch < 0) if (pitch < 0)
pitch = 0; pitch = 0;
if (pitch > 255) if (pitch > 255)
pitch = 255; pitch = 255;*/
mods.write(6, (int) pitch); mods.write(6, pitch);
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);