Sound Listener Improvements & Fix for 1.19.3+ (#61)

This commit is contained in:
MetallicGoat 2023-04-12 11:22:00 -04:00 committed by GitHub
parent 1957bfb8d2
commit 396d0482e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,6 @@
package de.gerrygames.viarewind.legacysupport.listener;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
import de.gerrygames.viarewind.legacysupport.BukkitPlugin;
import de.gerrygames.viarewind.legacysupport.injector.NMSReflection;
import de.gerrygames.viarewind.legacysupport.reflection.MethodSignature;
@ -22,18 +21,18 @@ import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import java.lang.reflect.Method;
import java.util.concurrent.ThreadLocalRandom;
public class SoundListener implements Listener {
private static boolean isSoundCategory = false;
static {
try {
Class.forName("org.bukkit.SoundCategory");
isSoundCategory = true;
} catch (ClassNotFoundException ignored) {}
} catch (ClassNotFoundException ignored) {
}
}
private static final Class<?> I_BLOCK_DATA = NMSReflection.getBlockDataClass();
public SoundListener() {
try {
@ -43,7 +42,8 @@ public class SoundListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onItemPickUp(EntityPickupItemEvent e) {
if (!(e.getEntity() instanceof Player)) return;
if (!(e.getEntity() instanceof Player))
return;
SoundListener.this.onItemPickUp((Player) e.getEntity());
}
@ -63,8 +63,15 @@ public class SoundListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent e) {
Player player = e.getPlayer();
if (Via.getAPI().getPlayerVersion(player)>47) return;
playBlockPlaceSound(player, e.getBlock());
if (Via.getAPI().getPlayerVersion(player) > 47)
return;
if(Via.getAPI().getServerVersion().lowestSupportedVersion() >= 755) {
player.playSound(e.getBlockPlaced().getLocation(), e.getBlock().getBlockData().getSoundGroup().getPlaceSound(), 1.0f, 0.8f);
} else {
playBlockPlaceSoundNMS(player, e.getBlock());
}
}
private void onItemPickUp(Player player) {
@ -95,41 +102,39 @@ public class SoundListener implements Listener {
});
}
private static void playBlockPlaceSound(Player player, Block block) {
// 1.8.8 -> 1.16.5
private static void playBlockPlaceSoundNMS(Player player, Block block) {
try {
World world = block.getWorld();
Object nmsWorld = world.getClass().getMethod("getHandle").invoke(world);
Class<?> blockPositionClass = NMSReflection.getBlockPositionClass();
Object blockPosition = null;
if (blockPositionClass != null) {
if (blockPositionClass != null)
blockPosition = blockPositionClass.getConstructor(int.class, int.class, int.class).newInstance(block.getX(), block.getY(), block.getZ());
}
Method getTypeMethod = ReflectionAPI.pickMethod(
nmsWorld.getClass(),
new MethodSignature("getType", blockPositionClass).withReturnType(I_BLOCK_DATA),
new MethodSignature("a_", blockPositionClass).withReturnType(I_BLOCK_DATA) // 1.18.2
);
Method getTypeMethod = nmsWorld.getClass().getMethod("getType", blockPositionClass);
getTypeMethod.setAccessible(true);
Object blockData = getTypeMethod.invoke(nmsWorld, blockPosition);
Method getBlock = ReflectionAPI.pickMethod(
blockData.getClass(),
new MethodSignature("getBlock"),
new MethodSignature("b") // 1.18.2
);
Method getBlock = blockData.getClass().getMethod("getBlock");
getBlock.setAccessible(true);
Object nmsBlock = getBlock.invoke(blockData);
Method getStepSound = ReflectionAPI.pickMethod(
nmsBlock.getClass(),
new MethodSignature("m", blockData.getClass()), // 1.18.2
new MethodSignature("w"), // 1.18?
new MethodSignature("getStepSound", blockData.getClass()), // 1.17
new MethodSignature("getStepSound") // pre 1.17
new MethodSignature("w"), // 1.9 -> 1.10
new MethodSignature("getStepSound", blockData.getClass()), // 1.14 -> 1.16
new MethodSignature("getStepSound") // 1.11 -> 1.12
);
getStepSound.setAccessible(true);
Object soundType;
if (getStepSound.getParameterCount() == 0) {
soundType = getStepSound.invoke(nmsBlock);
soundType = getStepSound.invoke(nmsBlock); // 1.9 -> 1.13
} else {
soundType = getStepSound.invoke(nmsBlock, blockData);
soundType = getStepSound.invoke(nmsBlock, blockData); // 1.14 -> 1.16.5
}
Method soundEffectMethod;
@ -137,10 +142,12 @@ public class SoundListener implements Listener {
Method pitchMethod;
try {
// 1.16.5
soundEffectMethod = soundType.getClass().getMethod("getPlaceSound");
volumeMethod = soundType.getClass().getMethod("getVolume");
pitchMethod = soundType.getClass().getMethod("getPitch");
} catch (NoSuchMethodException ex) {
// 1.9 -> 1.16.4
soundEffectMethod = soundType.getClass().getMethod("e");
volumeMethod = soundType.getClass().getMethod("a");
pitchMethod = soundType.getClass().getMethod("b");
@ -154,18 +161,16 @@ public class SoundListener implements Listener {
volume = (volume + 1.0f) / 2.0f;
pitch *= 0.8;
playSound(player, soundEffect, soundCategory, block.getX() + 0.5, block.getY() + 0.5, block.getZ() + 0.5,
volume, pitch, ThreadLocalRandom.current().nextLong());
playSound(player, soundEffect, soundCategory, block.getX() + 0.5, block.getY() + 0.5, block.getZ() + 0.5, volume, pitch);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void playSound(Player player, Object soundEffect, Object soundCategory, double x, double y, double z, float volume, float pitch, long seed) {
// 1.8.8 -> 1.16.5
private static void playSound(Player player, Object soundEffect, Object soundCategory, double x, double y, double z, float volume, float pitch) {
try {
Object packet;
if (Via.getAPI().getServerVersion().lowestSupportedVersion() <= ProtocolVersion.v1_18_2.getVersion()) {
packet = NMSReflection.getGamePacketClass("PacketPlayOutNamedSoundEffect").getConstructor(
Object packet = NMSReflection.getGamePacketClass("PacketPlayOutNamedSoundEffect").getConstructor(
soundEffect.getClass(), soundCategory.getClass(),
double.class, double.class, double.class,
float.class, float.class
@ -174,18 +179,9 @@ public class SoundListener implements Listener {
x, y, z,
volume, pitch
);
} else {
packet = NMSReflection.getGamePacketClass("PacketPlayOutNamedSoundEffect").getConstructor(
soundEffect.getClass(), soundCategory.getClass(),
double.class, double.class, double.class,
float.class, float.class, long.class
).newInstance(
soundEffect, soundCategory,
x, y, z,
volume, pitch, seed
);
}
// Volume = 1
// Pitch = .8
NMSReflection.sendPacket(player, packet);
} catch (Exception ex) {
ex.printStackTrace();