From dd538b6c4c2df7137b0a1f9afe2cb81e442288c3 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 27 Feb 2022 00:45:32 +1300 Subject: [PATCH] Fix sound groups --- .../packetlisteners/PacketListenerSounds.java | 122 ++---------------- 1 file changed, 8 insertions(+), 114 deletions(-) diff --git a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java index bf8c0134..45244747 100644 --- a/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java +++ b/plugin/src/main/java/me/libraryaddict/disguise/utilities/packets/packetlisteners/PacketListenerSounds.java @@ -1,7 +1,6 @@ package me.libraryaddict.disguise.utilities.packets.packetlisteners; import com.comphenix.protocol.PacketType.Play.Server; -import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketContainer; @@ -9,7 +8,6 @@ import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.reflect.StructureModifier; import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.disguisetypes.Disguise; -import me.libraryaddict.disguise.disguisetypes.DisguiseType; import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.utilities.DisguiseUtilities; @@ -21,7 +19,6 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import java.lang.reflect.InvocationTargetException; import java.util.Set; public class PacketListenerSounds extends PacketAdapter { @@ -33,7 +30,7 @@ public class PacketListenerSounds extends PacketAdapter { private static boolean cancelSound; public PacketListenerSounds(LibsDisguises plugin) { - super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS); + super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT); } @Override @@ -42,11 +39,7 @@ public class PacketListenerSounds extends PacketAdapter { return; } - if (event.getPacketType() == Server.ENTITY_STATUS) { - handleEntityStatus(event); - } else if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) { - handleNamedSoundEffect(event); - } + handleNamedSoundEffect(event); } private void handleNamedSoundEffect(PacketEvent event) { @@ -118,13 +111,15 @@ public class PacketListenerSounds extends PacketAdapter { return; } - Object sound = null; SoundGroup disguiseSound = SoundGroup.getGroup(disguise); - if (disguiseSound != null) { - sound = disguiseSound.getSound(soundType); + if (disguiseSound == null) { + event.setCancelled(true); + return; } + Object sound = disguiseSound.getSound(soundType); + if (sound == null) { event.setCancelled(true); return; @@ -134,7 +129,7 @@ public class PacketListenerSounds extends PacketAdapter { float volume = (float) mods.read(5); float pitch = (float) mods.read(6); - // If the volume is the default + // If the volume is the default, set it to what the real disguise sound group expects if (volume == soundGroup.getDamageAndIdleSoundVolume()) { volume = disguiseSound.getDamageAndIdleSoundVolume(); } @@ -170,105 +165,4 @@ public class PacketListenerSounds extends PacketAdapter { event.setPacket(newPacket); } - - private void handleEntityStatus(PacketEvent event) { - StructureModifier mods = event.getPacket().getModifier(); - Player observer = event.getPlayer(); - - if ((byte) mods.read(1) != 2) { - return; - } - - // It made a damage animation - Disguise disguise = DisguiseUtilities.getDisguise(observer, event.getPacket().getIntegers().read(0)); - - if (disguise == null) { - return; - } - - Entity entity = disguise.getEntity(); - - if (disguise instanceof TargetedDisguise) { - Set 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 entitySoundGroup = SoundGroup.getGroup(entity.getType().name()); - - if (entitySoundGroup == null) { - return; - } - - Object sound = entitySoundGroup.getSound(soundType); - - if (sound == null) { - return; - } - - SoundGroup disSound = SoundGroup.getGroup(disguise); - SoundGroup expectedGroup = SoundGroup.getGroup(disguise.getType().name()); - - if (disSound == null || disSound == expectedGroup) { - return; - } - - Location loc = entity.getLocation(); - PacketContainer packet = - new PacketContainer(sound.getClass().getSimpleName().equals("MinecraftKey") ? Server.CUSTOM_SOUND_EFFECT : Server.NAMED_SOUND_EFFECT); - - mods = packet.getModifier(); - - mods.write(0, sound); - mods.write(1, ReflectionManager.getSoundCategory(disguise.getType())); // Meh - mods.write(2, (int) (loc.getX() * 8D)); - mods.write(3, (int) (loc.getY() * 8D)); - mods.write(4, (int) (loc.getZ() * 8D)); - mods.write(5, disSound.getDamageAndIdleSoundVolume()); - - float pitch; - - if (disguise instanceof MobDisguise && !((MobDisguise) disguise).isAdult()) { - pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.4F; - } else { - pitch = (DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F; - } - - if (disguise.getType() == DisguiseType.BAT) { - pitch *= 0.95F; - } - - mods.write(6, pitch); - - event.setCancelled(true); - try { - ProtocolLibrary.getProtocolManager().sendServerPacket(observer, packet, false); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } }