9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

improve sounds

This commit is contained in:
XiaoMoMi
2025-03-24 22:01:09 +08:00
parent ebeb87fcf7
commit 8e0adaa6e1
22 changed files with 246 additions and 38 deletions

View File

@@ -115,7 +115,7 @@ public final class CraftEngineBlocks {
if (success) {
Reflections.method$BlockStateBase$onPlace.invoke(blockState, worldServer, blockPos, oldBlockState, true);
if (playSound) {
location.getWorld().playSound(location, block.sounds().placeSound().toString(), SoundCategory.BLOCKS, 1, 0.8f);
location.getWorld().playSound(location, block.sounds().placeSound().toString(), SoundCategory.BLOCKS, block.sounds().placeSound().volume(), block.sounds().placeSound().pitch());
}
}
} catch (ReflectiveOperationException e) {
@@ -185,7 +185,7 @@ public final class CraftEngineBlocks {
}
}
if (playSound) {
world.playBlockSound(vec3d, state.sounds().breakSound(), 1, 0.8f);
world.playBlockSound(vec3d, state.sounds().breakSound());
}
if (sendParticles) {
// TODO Particles

View File

@@ -121,7 +121,7 @@ public class BlockEventListener implements Listener {
}
// play sound
Vec3d vec3d = new Vec3d(location.getBlockX() + 0.5, location.getBlockY() + 0.5, location.getBlockZ() + 0.5);
world.playBlockSound(vec3d, state.sounds().breakSound(), 1f, 0.8f);
world.playBlockSound(vec3d, state.sounds().breakSound());
if (player.getGameMode() == GameMode.CREATIVE) {
return;
}
@@ -226,7 +226,7 @@ public class BlockEventListener implements Listener {
int stateId = BlockStateUtils.blockStateToId(blockState);
if (!BlockStateUtils.isVanillaBlock(stateId)) {
ImmutableBlockState state = manager.getImmutableBlockStateUnsafe(stateId);
player.playSound(playerLocation, state.sounds().stepSound().toString(), SoundCategory.BLOCKS, 0.15f, 1f);
player.playSound(playerLocation, state.sounds().stepSound().id().toString(), SoundCategory.BLOCKS, state.sounds().stepSound().volume(), state.sounds().stepSound().pitch());
} else if (ConfigManager.enableSoundSystem()) {
Object ownerBlock = BlockStateUtils.getBlockOwner(blockState);
if (manager.isBlockSoundRemoved(ownerBlock)) {
@@ -268,7 +268,7 @@ public class BlockEventListener implements Listener {
for (Item<Object> item : blockState.getDrops(builder, world)) {
world.dropItemNaturally(vec3d, item);
}
world.playBlockSound(vec3d, blockState.sounds().breakSound(),1f, 0.8f);
world.playBlockSound(vec3d, blockState.sounds().breakSound());
}
}
}
@@ -300,7 +300,7 @@ public class BlockEventListener implements Listener {
for (Item<Object> item : state.getDrops(builder, world)) {
world.dropItemNaturally(vec3d, item);
}
world.playBlockSound(vec3d, state.sounds().breakSound(), 1f, 0.8f);
world.playBlockSound(vec3d, state.sounds().breakSound());
}
}
}

View File

@@ -2,6 +2,7 @@ package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.block.CustomBlock;
@@ -102,6 +103,23 @@ public class FallingBlockBehavior extends BlockBehavior {
}
}
@Override
public void onLand(Object thisBlock, Object[] args) throws Exception {
Object fallingBlock = args[4];
Object entityData = Reflections.field$Entity$entityData.get(fallingBlock);
boolean isSilent = (boolean) Reflections.method$SynchedEntityData$get.invoke(entityData, Reflections.instance$Entity$DATA_SILENT);
if (!isSilent) {
Object blockState = args[2];
int stateId = BlockStateUtils.blockStateToId(blockState);
ImmutableBlockState immutableBlockState = BukkitBlockManager.instance().getImmutableBlockState(stateId);
if (immutableBlockState == null || immutableBlockState.isEmpty()) return;
Object level = args[0];
Object pos = args[1];
net.momirealms.craftengine.core.world.World world = new BukkitWorld((World) Reflections.method$Level$getCraftWorld.invoke(level));
world.playBlockSound(Vec3d.atCenterOf(LocationUtils.fromBlockPos(pos)), immutableBlockState.sounds().landSound());
}
}
public static class Factory implements BlockBehaviorFactory {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {

View File

@@ -24,7 +24,11 @@ import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.world.BlockPos;
import org.bukkit.*;
import net.momirealms.craftengine.core.world.Vec3d;
import org.bukkit.Bukkit;
import org.bukkit.GameEvent;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
@@ -120,7 +124,7 @@ public class BlockItemBehavior extends ItemBehavior {
}
player.swingHand(placeContext.getHand());
world.playSound(new Location(world, pos.x(), pos.y(), pos.z()), blockStateToPlace.sounds().placeSound().toString(), SoundCategory.BLOCKS, 1f, 0.8f);
placeContext.getLevel().playBlockSound(new Vec3d(pos.x() + 0.5, pos.y() + 0.5, pos.z() + 0.5), blockStateToPlace.sounds().placeSound());
world.sendGameEvent(bukkitPlayer, GameEvent.BLOCK_PLACE, new Vector(pos.x(), pos.y(), pos.z()));
return InteractionResult.SUCCESS;
}

View File

@@ -707,6 +707,19 @@ public class Reflections {
)
);
public static final Class<?> clazz$SynchedEntityData = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("network.syncher.SynchedEntityData"),
BukkitReflectionUtils.assembleMCClass("network.syncher.DataWatcher")
)
);
public static final Method method$SynchedEntityData$get = requireNonNull(
ReflectionUtils.getMethod(
clazz$SynchedEntityData, Object.class, clazz$EntityDataAccessor
)
);
public static final Class<?> clazz$SynchedEntityData$DataValue = requireNonNull(
ReflectionUtils.getClazz(
BukkitReflectionUtils.assembleMCClass("network.syncher.SynchedEntityData$DataValue"),
@@ -5428,4 +5441,34 @@ public class Reflections {
clazz$LinearPalette, Object.class.arrayType(), 0
)
);
public static final Object instance$Entity$DATA_SILENT;
static {
int i = 0;
Field targetField = null;
for (Field field : clazz$Entity.getDeclaredFields()) {
Type fieldType = field.getGenericType();
if (field.getType() == clazz$EntityDataAccessor && fieldType instanceof ParameterizedType paramType) {
if (paramType.getActualTypeArguments()[0] == Boolean.class) {
i++;
if (i == 2) {
targetField = field;
break;
}
}
}
}
try {
instance$Entity$DATA_SILENT = ReflectionUtils.setAccessible(requireNonNull(targetField)).get(null);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public static final Field field$Entity$entityData = requireNonNull(
ReflectionUtils.getDeclaredField(
clazz$Entity, clazz$SynchedEntityData, 0
)
);
}

View File

@@ -10,11 +10,11 @@ public class SoundUtils {
public static Object toSoundType(BlockSounds sounds) throws ReflectiveOperationException {
return Reflections.constructor$SoundType.newInstance(
1f, 1f,
getOrRegisterSoundEvent(sounds.breakSound()),
getOrRegisterSoundEvent(sounds.stepSound()),
getOrRegisterSoundEvent(sounds.placeSound()),
getOrRegisterSoundEvent(sounds.hitSound()),
getOrRegisterSoundEvent(sounds.fallSound())
getOrRegisterSoundEvent(sounds.breakSound().id()),
getOrRegisterSoundEvent(sounds.stepSound().id()),
getOrRegisterSoundEvent(sounds.placeSound().id()),
getOrRegisterSoundEvent(sounds.hitSound().id()),
getOrRegisterSoundEvent(sounds.fallSound().id())
);
}