Fix sounds, release 9.0.6

This commit is contained in:
libraryaddict 2016-05-16 03:51:16 +12:00
parent b17191a462
commit 06c0084e3a
2 changed files with 181 additions and 173 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>LibsDisguises</groupId> <groupId>LibsDisguises</groupId>
<artifactId>LibsDisguises</artifactId> <artifactId>LibsDisguises</artifactId>
<version>9.0.5-SNAPSHOT</version> <version>9.0.6</version>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>

View File

@ -16,7 +16,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;
@ -47,6 +46,7 @@ public class PacketListenerSounds extends PacketAdapter
{ {
return; return;
} }
if (event.isAsync()) if (event.isAsync())
{ {
return; return;
@ -63,19 +63,28 @@ public class PacketListenerSounds extends PacketAdapter
if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) if (event.getPacketType() == Server.NAMED_SOUND_EFFECT)
{ {
Object soundEffect = mods.read(0);
SoundType soundType = null; SoundType soundType = null;
Location soundLoc = new Location(observer.getWorld(), ((Integer) mods.read(2)) / 8D, ((Integer) mods.read(3)) / 8D, int[] soundCords = new int[]
((Integer) mods.read(4)) / 8D); {
(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; Entity disguisedEntity = null;
DisguiseSound entitySound = null; DisguiseSound entitySound = null;
Disguise disguise = 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) for (Entity entity : entities)
{ {
@ -85,198 +94,197 @@ public class PacketListenerSounds extends PacketAdapter
{ {
Location loc = entity.getLocation(); Location loc = entity.getLocation();
loc = new Location(observer.getWorld(), ((int) (loc.getX() * 8)) / 8D, ((int) (loc.getY() * 8)) / 8D, int[] entCords = new int[]
((int) (loc.getZ() * 8)) / 8D);
if (loc.equals(soundLoc))
{
entitySound = DisguiseSound.getType(entity.getType().name());
if (entitySound != null)
{ {
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 soundType = SoundType.DEATH;
{
// 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();
}
} }
else
if (obj == null)
{ {
boolean hasInvun = false; obj = null;
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;
} }
} }
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; try
DisguiseSound dSound = DisguiseSound.getType(disguise.getType().name());
if (dSound != null)
sound = dSound.getSound(soundType);
if (sound == null)
{ {
event.setCancelled(true); int typeId = observer.getWorld().getBlockTypeIdAt((int) Math.floor(soundCords[0] / 8D),
} (int) Math.floor(soundCords[1] / 8D), (int) Math.floor(soundCords[2] / 8D));
else
{ Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", int.class)
if (sound.equals("step.grass")) .invoke(ReflectionManager.getNmsField("Block", "REGISTRY").get(null), typeId);
if (block != null)
{ {
try Object step = ReflectionManager.getNmsField("Block", "stepSound").get(block);
{
int typeId = soundLoc.getWorld().getBlockTypeIdAt(soundLoc.getBlockX(),
soundLoc.getBlockY() - 1, soundLoc.getBlockZ());
Object block = ReflectionManager.getNmsMethod("RegistryMaterials", "getId", int.class) mods.write(0, ReflectionManager.getNmsMethod(step.getClass(), "d").invoke(step));
.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(1, ReflectionManager.getSoundCategory(disguise.getType())); 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 // 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 (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 baby = ((Zombie) disguisedEntity).isBaby();
if (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) }
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. pitch *= 63;
if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity
&& ((MobDisguise) disguise).doesDisguiseAge())
{
boolean baby = false;
if (disguisedEntity instanceof Zombie) if (pitch < 0)
{ pitch = 0;
baby = ((Zombie) disguisedEntity).isBaby();
}
else if (disguisedEntity instanceof Ageable)
{
baby = !((Ageable) disguisedEntity).isAdult();
}
if (((MobDisguise) disguise).isAdult() == baby) if (pitch > 255)
{ pitch = 255;
float pitch = (Integer) mods.read(6);
if (baby) mods.write(6, (int) pitch);
{
// 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);
}
}
} }
} }
} }