Fix sound groups

This commit is contained in:
libraryaddict 2022-02-27 00:45:32 +13:00
parent 8d75a07f52
commit dd538b6c4c

View File

@ -1,7 +1,6 @@
package me.libraryaddict.disguise.utilities.packets.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
@ -9,7 +8,6 @@ import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import me.libraryaddict.disguise.LibsDisguises; import me.libraryaddict.disguise.LibsDisguises;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.disguisetypes.MobDisguise; import me.libraryaddict.disguise.disguisetypes.MobDisguise;
import me.libraryaddict.disguise.disguisetypes.TargetedDisguise; import me.libraryaddict.disguise.disguisetypes.TargetedDisguise;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
@ -21,7 +19,6 @@ import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException;
import java.util.Set; import java.util.Set;
public class PacketListenerSounds extends PacketAdapter { public class PacketListenerSounds extends PacketAdapter {
@ -33,7 +30,7 @@ public class PacketListenerSounds extends PacketAdapter {
private static boolean cancelSound; private static boolean cancelSound;
public PacketListenerSounds(LibsDisguises plugin) { public PacketListenerSounds(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT, Server.ENTITY_STATUS); super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT);
} }
@Override @Override
@ -42,12 +39,8 @@ public class PacketListenerSounds extends PacketAdapter {
return; 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) { private void handleNamedSoundEffect(PacketEvent event) {
StructureModifier<Object> mods = event.getPacket().getModifier(); StructureModifier<Object> mods = event.getPacket().getModifier();
@ -118,13 +111,15 @@ public class PacketListenerSounds extends PacketAdapter {
return; return;
} }
Object sound = null;
SoundGroup disguiseSound = SoundGroup.getGroup(disguise); SoundGroup disguiseSound = SoundGroup.getGroup(disguise);
if (disguiseSound != null) { if (disguiseSound == null) {
sound = disguiseSound.getSound(soundType); event.setCancelled(true);
return;
} }
Object sound = disguiseSound.getSound(soundType);
if (sound == null) { if (sound == null) {
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -134,7 +129,7 @@ public class PacketListenerSounds extends PacketAdapter {
float volume = (float) mods.read(5); float volume = (float) mods.read(5);
float pitch = (float) mods.read(6); 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()) { if (volume == soundGroup.getDamageAndIdleSoundVolume()) {
volume = disguiseSound.getDamageAndIdleSoundVolume(); volume = disguiseSound.getDamageAndIdleSoundVolume();
} }
@ -170,105 +165,4 @@ public class PacketListenerSounds extends PacketAdapter {
event.setPacket(newPacket); event.setPacket(newPacket);
} }
private void handleEntityStatus(PacketEvent event) {
StructureModifier<Object> 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<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 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();
}
}
} }