Fix inability to define own sounds

This commit is contained in:
libraryaddict 2020-11-20 14:14:42 +13:00
parent 8aef2bffc0
commit 2d14fb90b8
3 changed files with 35 additions and 29 deletions

View File

@ -1,5 +1,6 @@
package me.libraryaddict.disguise.utilities.packets.packetlisteners;
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.PacketType.Play.Server;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.ListenerPriority;
@ -121,14 +122,15 @@ public class PacketListenerSounds extends PacketAdapter {
if (sound == null) {
event.setCancelled(true);
} else {
mods.write(0, sound);
mods.write(1, ReflectionManager.getSoundCategory(disguise.getType()));
Enum soundCat = ReflectionManager.getSoundCategory(disguise.getType());
float volume = (float) mods.read(5);
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 (mods.read(5).equals(entitySound.getDamageAndIdleSoundVolume())) {
mods.write(5, disguiseSound.getDamageAndIdleSoundVolume());
if (volume == entitySound.getDamageAndIdleSoundVolume()) {
volume = disguiseSound.getDamageAndIdleSoundVolume();
}
// Here I assume its the default pitch as I can't calculate if its real.
@ -143,8 +145,6 @@ public class PacketListenerSounds extends PacketAdapter {
}
if (((MobDisguise) disguise).isAdult() == baby) {
float pitch = (Float) mods.read(6);
if (baby) {
// If the pitch is not the expected
if (pitch < 1.5 || pitch > 1.7) {
@ -153,10 +153,6 @@ public class PacketListenerSounds extends PacketAdapter {
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 < 1 || pitch > 1.2) {
@ -165,25 +161,31 @@ public class PacketListenerSounds extends PacketAdapter {
pitch = (DisguiseUtilities.random.nextFloat() -
DisguiseUtilities.random.nextFloat()) * 0.2F + 1.0F;
// Min = 1
// Cap = 63
// Max = 1.2
// Cap = 75.6
}
}
}
/*pitch *= 63;
if (sound.getClass().getSimpleName().equals("MinecraftKey")) {
PacketContainer newPacket = new PacketContainer(Server.CUSTOM_SOUND_EFFECT);
StructureModifier<Object> newModifs = newPacket.getModifier();
if (pitch < 0)
pitch = 0;
newModifs.write(0, sound);
newModifs.write(1, soundCat);
if (pitch > 255)
pitch = 255;*/
newModifs.write(2, mods.read(2));
newModifs.write(3, mods.read(3));
newModifs.write(4, mods.read(4));
newModifs.write(5, volume);
newModifs.write(6, pitch);
event.setPacket(newPacket);
} else {
mods.write(0, sound);
mods.write(1, soundCat);
mods.write(5, volume);
mods.write(6, pitch);
}
}
}
}
}
} else if (event.getPacketType() == Server.ENTITY_STATUS) {
@ -232,8 +234,9 @@ public class PacketListenerSounds extends PacketAdapter {
if (sound != null) {
Location loc = entity.getLocation();
PacketContainer packet = new PacketContainer(Server.NAMED_SOUND_EFFECT);
PacketContainer packet = new PacketContainer(
sound.getClass().getSimpleName().equals("MinecraftKey") ?
Server.CUSTOM_SOUND_EFFECT : Server.NAMED_SOUND_EFFECT);
mods = packet.getModifier();

View File

@ -1338,6 +1338,7 @@ public class ReflectionManager {
return null;
}
@Deprecated
public static Object createSoundEffect(String minecraftKey) {
try {
return getNmsConstructor("SoundEffect", getNmsClass("MinecraftKey"))

View File

@ -3,6 +3,7 @@ package me.libraryaddict.disguise.utilities.sounds;
import lombok.Getter;
import me.libraryaddict.disguise.disguisetypes.Disguise;
import me.libraryaddict.disguise.disguisetypes.DisguiseType;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.reflection.ReflectionManager;
import org.apache.commons.lang.math.RandomUtils;
import org.bukkit.Sound;
@ -36,17 +37,18 @@ public class SoundGroup {
try {
DisguiseType.valueOf(name);
}
catch (Exception ex) {
} catch (Exception ex) {
customSounds = true;
}
}
public void addSound(Object sound, SoundType type) {
Object origSound = sound;
if (sound instanceof Sound) {
sound = ReflectionManager.getCraftSound((Sound) sound);
} else if (sound instanceof String) {
sound = ReflectionManager.createSoundEffect((String) sound);
sound = ReflectionManager.createMinecraftKey((String) sound);
} else if (!sound.getClass().getSimpleName().equals("SoundEffect")) {
throw new IllegalArgumentException("Unexpected " + sound.getClass());
}