Fix sounds and volume

This commit is contained in:
libraryaddict 2023-01-29 21:34:09 +13:00
parent 8395d19e17
commit ecfc2c0314
4 changed files with 84 additions and 40 deletions

View File

@ -330,7 +330,7 @@ public class ReflectionManager implements ReflectionManagerAbstract {
return 0.0f; return 0.0f;
} else { } else {
try { try {
Method method = net.minecraft.world.entity.LivingEntity.class.getDeclaredMethod("eC"); Method method = net.minecraft.world.entity.LivingEntity.class.getDeclaredMethod("eI");
method.setAccessible(true); method.setAccessible(true);
return (Float) method.invoke(entity); return (Float) method.invoke(entity);

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.packets.packetlisteners; package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server; import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
@ -11,6 +12,7 @@ import me.libraryaddict.disguise.disguisetypes.Disguise;
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;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.sounds.SoundGroup; import me.libraryaddict.disguise.utilities.sounds.SoundGroup;
import me.libraryaddict.disguise.utilities.sounds.SoundGroup.SoundType; import me.libraryaddict.disguise.utilities.sounds.SoundGroup.SoundType;
@ -22,9 +24,9 @@ import org.bukkit.entity.Player;
import java.util.Set; import java.util.Set;
public class PacketListenerSounds extends PacketAdapter { public class PacketListenerSounds extends PacketAdapter {
public PacketListenerSounds(LibsDisguises plugin) { public PacketListenerSounds(LibsDisguises plugin) {
super(plugin, ListenerPriority.NORMAL, Server.NAMED_SOUND_EFFECT); super(plugin, ListenerPriority.NORMAL,
NmsVersion.v1_19_R2.isSupported() ? new PacketType[]{Server.NAMED_SOUND_EFFECT, Server.ENTITY_SOUND} : new PacketType[]{Server.NAMED_SOUND_EFFECT});
} }
@Override @Override
@ -40,61 +42,72 @@ public class PacketListenerSounds extends PacketAdapter {
StructureModifier<Object> mods = event.getPacket().getModifier(); StructureModifier<Object> mods = event.getPacket().getModifier();
Player observer = event.getPlayer(); Player observer = event.getPlayer();
SoundType soundType = null; SoundType soundType;
Entity disguisedEntity = null;
SoundGroup soundGroup = null; SoundGroup soundGroup = null;
Object soundEffectObj = mods.read(0); Object soundEffectObj = mods.read(0);
int offset = 0;
Disguise disguise = null; Disguise disguise = null;
Entity entity = null;
int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)}; if (event.getPacketType() == Server.NAMED_SOUND_EFFECT) {
offset = 3;
for (Set<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) { int[] soundCords = new int[]{(Integer) mods.read(2), (Integer) mods.read(3), (Integer) mods.read(4)};
for (TargetedDisguise entityDisguise : disguises) {
Entity entity = entityDisguise.getEntity();
if (entity == null || entity.getWorld() != observer.getWorld()) { loop:
continue; for (Set<TargetedDisguise> disguises : DisguiseUtilities.getDisguises().values()) {
} for (TargetedDisguise entityDisguise : disguises) {
entity = entityDisguise.getEntity();
if (!entityDisguise.canSee(observer)) { if (entity == null || entity.getWorld() != observer.getWorld()) {
continue; continue;
} }
Location loc = entity.getLocation(); if (!entityDisguise.canSee(observer)) {
continue;
}
int[] entCords = new int[]{(int) (loc.getX() * 8), (int) (loc.getY() * 8), (int) (loc.getZ() * 8)}; Location loc = entity.getLocation();
if (soundCords[0] != entCords[0] || soundCords[1] != entCords[1] || soundCords[2] != entCords[2]) { int[] entCords = new int[]{(int) (loc.getX() * 8), (int) (loc.getY() * 8), (int) (loc.getZ() * 8)};
continue;
}
disguise = entityDisguise; if (soundCords[0] != entCords[0] || soundCords[1] != entCords[1] || soundCords[2] != entCords[2]) {
disguisedEntity = entity; continue;
soundGroup = SoundGroup.getGroup(entity.getType().name()); }
if (soundGroup == null || soundGroup.getSound(soundEffectObj) == null) { disguise = entityDisguise;
return; soundGroup = SoundGroup.getGroup(entity.getType().name());
}
if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) { break loop;
soundType = soundGroup.getType(soundEffectObj);
} else {
soundType = SoundType.DEATH;
} }
} }
} else {
disguise = DisguiseUtilities.getDisguise(observer, (int) mods.read(2));
if (disguise != null) { if (disguise == null) {
break; return;
} }
entity = disguise.getEntity();
soundGroup = SoundGroup.getGroup(entity.getType().name());
} }
if (disguise == null || !disguise.isSoundsReplaced()) { if (disguise == null || !disguise.isSoundsReplaced()) {
return; return;
} }
if (disguisedEntity == observer && !disguise.isSelfDisguiseSoundsReplaced()) { if (soundGroup == null || soundGroup.getSound(soundEffectObj) == null) {
return;
}
if ((!(entity instanceof LivingEntity)) || ((LivingEntity) entity).getHealth() > 0) {
soundType = soundGroup.getType(soundEffectObj);
} else {
soundType = SoundType.DEATH;
}
if (entity == observer && !disguise.isSelfDisguiseSoundsReplaced()) {
return; return;
} }
@ -113,15 +126,15 @@ public class PacketListenerSounds extends PacketAdapter {
} }
Enum soundCat = ReflectionManager.getSoundCategory(disguise.getType()); Enum soundCat = ReflectionManager.getSoundCategory(disguise.getType());
float volume = (float) mods.read(5); float volume = (float) mods.read(offset + 2);
float pitch = (float) mods.read(6); float pitch = (float) mods.read(offset + 3);
// If the volume is the default, set it to what the real disguise sound group expects // 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();
} }
if (disguise instanceof MobDisguise && disguisedEntity instanceof LivingEntity && ((MobDisguise) disguise).doesDisguiseAge()) { if (disguise instanceof MobDisguise && entity instanceof LivingEntity && ((MobDisguise) disguise).doesDisguiseAge()) {
if (((MobDisguise) disguise).isAdult()) { if (((MobDisguise) disguise).isAdult()) {
pitch = ((DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F) + 1.0F; pitch = ((DisguiseUtilities.random.nextFloat() - DisguiseUtilities.random.nextFloat()) * 0.2F) + 1.0F;
} else { } else {
@ -131,7 +144,7 @@ public class PacketListenerSounds extends PacketAdapter {
PacketContainer newPacket; PacketContainer newPacket;
if (sound.getClass().getSimpleName().equals("MinecraftKey")) { if (!NmsVersion.v1_19_R2.isSupported() && sound.getClass().getSimpleName().equals("MinecraftKey")) {
newPacket = new PacketContainer(Server.CUSTOM_SOUND_EFFECT); newPacket = new PacketContainer(Server.CUSTOM_SOUND_EFFECT);
StructureModifier<Object> newModifs = newPacket.getModifier(); StructureModifier<Object> newModifs = newPacket.getModifier();
@ -147,8 +160,8 @@ public class PacketListenerSounds extends PacketAdapter {
mods.write(0, sound); mods.write(0, sound);
mods.write(1, soundCat); mods.write(1, soundCat);
mods.write(5, volume); mods.write(offset + 2, volume);
mods.write(6, pitch); mods.write(offset + 3, pitch);
event.setPacket(newPacket); event.setPacket(newPacket);
} }

View File

@ -7,6 +7,7 @@ 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 com.comphenix.protocol.wrappers.EnumWrappers;
import com.comphenix.protocol.wrappers.WrappedDataValue; import com.comphenix.protocol.wrappers.WrappedDataValue;
import com.comphenix.protocol.wrappers.WrappedWatchableObject; import com.comphenix.protocol.wrappers.WrappedWatchableObject;
import me.libraryaddict.disguise.DisguiseAPI; import me.libraryaddict.disguise.DisguiseAPI;
@ -21,6 +22,9 @@ import me.libraryaddict.disguise.utilities.packets.PacketsManager;
import me.libraryaddict.disguise.utilities.reflection.NmsVersion; import me.libraryaddict.disguise.utilities.reflection.NmsVersion;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager; import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import me.libraryaddict.disguise.utilities.reflection.WatcherValue; import me.libraryaddict.disguise.utilities.reflection.WatcherValue;
import me.libraryaddict.disguise.utilities.sounds.SoundGroup;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@ -173,6 +177,28 @@ public class PacketListenerViewSelfDisguise extends PacketAdapter {
} else if (event.getPacketType() == Server.ENTITY_STATUS) { } else if (event.getPacketType() == Server.ENTITY_STATUS) {
if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() && packet.getBytes().read(0) == 2) { if (disguise.isSelfDisguiseSoundsReplaced() && !disguise.getType().isPlayer() && packet.getBytes().read(0) == 2) {
event.setCancelled(true); event.setCancelled(true);
// As of 1.19.3, no sound is sent but instead the client is expected to play a hurt sound on entity status effect
if (NmsVersion.v1_19_R2.isSupported()) {
SoundGroup group = SoundGroup.getGroup(disguise);
Object sound = group.getSound(SoundGroup.SoundType.HURT);
if (sound != null) {
PacketContainer newPacket = new PacketContainer(Server.ENTITY_SOUND);
StructureModifier mods = newPacket.getModifier();
mods.write(0, sound);
// Category
mods.write(2, DisguiseAPI.getSelfDisguiseId());
mods.write(3, 1f);
mods.write(4, 1f);
mods.write(5, (long) (Math.random() * 1000L));
newPacket.getSoundCategories().write(0, EnumWrappers.SoundCategory.MASTER);
ProtocolLibrary.getProtocolManager().sendServerPacket(observer, newPacket);
}
}
} }
} else if (event.getPacketType() == Server.ENTITY_VELOCITY && !DisguiseUtilities.isPlayerVelocity(observer)) { } else if (event.getPacketType() == Server.ENTITY_VELOCITY && !DisguiseUtilities.isPlayerVelocity(observer)) {
// The player only sees velocity changes when there is a velocity event. As the method claims there // The player only sees velocity changes when there is a velocity event. As the method claims there

View File

@ -2439,6 +2439,11 @@ public class ReflectionManager {
if (soundStrength != null) { if (soundStrength != null) {
sound.setDamageAndIdleSoundVolume(soundStrength); sound.setDamageAndIdleSoundVolume(soundStrength);
// This should only display on custom builds
if (disguiseType == DisguiseType.COW && soundStrength != 0.4F && !LibsDisguises.getInstance().isNumberedBuild()) {
DisguiseUtilities.getLogger().severe("The hurt sound volume may be wrong on the COW disguise! Bad nms update?");
}
} }
} }