NotFaceguy's fix for sound issues that has apparently been plaguing him for decades

This commit is contained in:
libraryaddict 2021-08-06 07:37:02 +12:00
parent fc97a5952b
commit ffcf4fd282

View File

@ -23,9 +23,10 @@ import java.lang.reflect.InvocationTargetException;
import java.util.Set; import java.util.Set;
public class PacketListenerSounds extends PacketAdapter { public class PacketListenerSounds extends PacketAdapter {
/** /**
* This is a fix for the stupidity that is * This is a fix for the stupidity that is "I can't separate the sounds from the sounds the player
* "I can't separate the sounds from the sounds the player heard, and the sounds of the entity tracker heard" * heard, and the sounds of the entity tracker heard"
*/ */
private static boolean cancelSound; private static boolean cancelSound;
@ -43,18 +44,23 @@ public class PacketListenerSounds extends PacketAdapter {
return; return;
} }
if (event.getPlayer().getName().contains("UNKNOWN[")) // If the player is temporary if (event.getPlayer().getName().contains("UNKNOWN[")) {
{
return; return;
} }
event.setPacket(event.getPacket().deepClone()); event.setPacket(event.getPacket().deepClone());
StructureModifier<Object> mods = event.getPacket().getModifier(); if (event.getPacketType() == Server.ENTITY_STATUS) {
handleEntityStatus(event);
} else if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) {
handleNamedSoundEffect(event);
}
}
private void handleNamedSoundEffect(PacketEvent event) {
StructureModifier<Object> mods = event.getPacket().getModifier();
Player observer = event.getPlayer(); Player observer = event.getPlayer();
if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) {
SoundType soundType = null; SoundType soundType = null;
Entity disguisedEntity = null; Entity disguisedEntity = null;
@ -65,7 +71,6 @@ public class PacketListenerSounds extends PacketAdapter {
int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)}; int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)};
loop:
for (Set<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) { for (Set<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) {
for (TargetedDisguise entityDisguise : disguises) { for (TargetedDisguise entityDisguise : disguises) {
Entity entity = entityDisguise.getEntity(); Entity entity = entityDisguise.getEntity();
@ -86,31 +91,42 @@ public class PacketListenerSounds extends PacketAdapter {
continue; continue;
} }
disguise = entityDisguise;
disguisedEntity = entity;
entitySound = SoundGroup.getGroup(entity.getType().name()); entitySound = SoundGroup.getGroup(entity.getType().name());
if (entitySound == null) { if (entitySound == null) {
event.setCancelled(true);
continue; continue;
} }
if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) { if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) {
boolean hasInvun = ReflectionManager.hasInvul(entity); boolean hasInvun = ReflectionManager.hasInvul(entity);
soundType = entitySound.getType(soundEffectObj, !hasInvun); soundType = entitySound.getType(soundEffectObj, !hasInvun);
} else { } else {
soundType = SoundType.DEATH; soundType = SoundType.DEATH;
} }
if (soundType != null) {
disguise = entityDisguise;
disguisedEntity = entity;
break loop;
} }
if (disguise != null) {
break;
} }
} }
if (disguise != null && disguise.isSoundsReplaced() && (disguise.isSelfDisguiseSoundsReplaced() || disguisedEntity != observer)) { if (disguise == null || !disguise.isSoundsReplaced()) {
return;
}
// Blocks null and CANCEL, HURT and DEATH are 100% handled by entity status!
if (soundType != SoundType.STEP && soundType != SoundType.IDLE) {
event.setCancelled(true);
return;
}
if (disguisedEntity == observer && !disguise.isSelfDisguiseSoundsReplaced()) {
return;
}
Object sound = null; Object sound = null;
SoundGroup disguiseSound = SoundGroup.getGroup(disguise); SoundGroup disguiseSound = SoundGroup.getGroup(disguise);
if (disguiseSound != null) { if (disguiseSound != null) {
@ -119,13 +135,13 @@ public class PacketListenerSounds extends PacketAdapter {
if (sound == null) { if (sound == null) {
event.setCancelled(true); event.setCancelled(true);
} else { return;
}
Enum soundCat = ReflectionManager.getSoundCategory(disguise.getType()); Enum soundCat = ReflectionManager.getSoundCategory(disguise.getType());
float volume = (float) mods.read(5); float volume = (float) mods.read(5);
float pitch = (float) mods.read(6); float pitch = (float) mods.read(6);
// Time to change the pitch and volume
if (soundType == SoundType.HURT || soundType == SoundType.DEATH || soundType == SoundType.IDLE) {
// If the volume is the default // If the volume is the default
if (volume == entitySound.getDamageAndIdleSoundVolume()) { if (volume == entitySound.getDamageAndIdleSoundVolume()) {
volume = disguiseSound.getDamageAndIdleSoundVolume(); volume = disguiseSound.getDamageAndIdleSoundVolume();
@ -181,9 +197,11 @@ public class PacketListenerSounds extends PacketAdapter {
mods.write(6, pitch); mods.write(6, pitch);
} }
} }
}
} private void handleEntityStatus(PacketEvent event) {
} else if (event.getPacketType() == Server.ENTITY_STATUS) { StructureModifier<Object> mods = event.getPacket().getModifier();
Player observer = event.getPlayer();
if ((byte) mods.read(1) != 2) { if ((byte) mods.read(1) != 2) {
return; return;
} }
@ -197,29 +215,40 @@ public class PacketListenerSounds extends PacketAdapter {
Entity entity = disguise.getEntity(); Entity entity = disguise.getEntity();
if (!disguise.getType().isPlayer() && (disguise.isSelfDisguiseSoundsReplaced() || entity != event.getPlayer())) { if (disguise instanceof TargetedDisguise) {
Set<TargetedDisguise> discs = DisguiseUtilities.getDisguises().get(entity.getEntityId());
for (TargetedDisguise targetedDisguise : discs) {
if (targetedDisguise != disguise) {
continue;
}
if (!targetedDisguise.canSee(observer)) {
return;
}
}
}
SoundType soundType = SoundType.HURT;
if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) {
soundType = SoundType.DEATH;
}
if (entity == event.getPlayer() && !disguise.getType().isPlayer()) {
if (!disguise.isSelfDisguiseSoundsReplaced()) {
cancelSound = !cancelSound;
if (cancelSound) {
return;
}
}
}
SoundGroup disSound = SoundGroup.getGroup(entity.getType().name()); SoundGroup disSound = SoundGroup.getGroup(entity.getType().name());
if (disSound == null) { if (disSound == null) {
return; return;
} }
SoundType soundType = SoundType.HURT; if (disSound.getSound(soundType) != null) {
Object obj = null;
if (entity instanceof LivingEntity && ((LivingEntity) entity).getHealth() == 0) {
soundType = SoundType.DEATH;
}
if (disSound.getSound(soundType) == null || (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer())) {
if (disguise.isSelfDisguiseSoundsReplaced() && entity == event.getPlayer()) {
cancelSound = !cancelSound;
if (cancelSound) {
return;
}
}
disSound = SoundGroup.getGroup(disguise); disSound = SoundGroup.getGroup(disguise);
if (disSound != null) { if (disSound != null) {
@ -253,6 +282,7 @@ public class PacketListenerSounds extends PacketAdapter {
mods.write(6, pitch); mods.write(6, pitch);
event.setCancelled(true);
try { try {
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
@ -263,5 +293,4 @@ public class PacketListenerSounds extends PacketAdapter {
} }
} }
} }
}
}