9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-04 15:41:38 +00:00

简化实现

This commit is contained in:
jhqwqmc
2025-09-24 16:35:43 +08:00
parent 154779cdf4
commit a3e9e74913

View File

@@ -7,44 +7,34 @@ import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.sound.SoundData;
import net.momirealms.craftengine.core.util.Pair;
import net.momirealms.craftengine.core.util.RandomUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
public class ChimeBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final List<Pair<SoundData, Float>> hitSounds;
private final SoundData hitSound;
public ChimeBlockBehavior(CustomBlock customBlock, List<Pair<SoundData, Float>> hitSounds) {
public ChimeBlockBehavior(CustomBlock customBlock, SoundData hitSound) {
super(customBlock);
this.hitSounds = hitSounds;
this.hitSound = hitSound;
}
@Override
public void onProjectileHit(Object thisBlock, Object[] args, Callable<Object> superMethod) {
if (hitSounds.isEmpty()) return;
Pair<SoundData, Float> hitSound = hitSounds.get(RandomUtils.generateRandomInt(0, hitSounds.size()));
Object blockPos = FastNMS.INSTANCE.field$BlockHitResult$blockPos(args[2]);
Object sound = FastNMS.INSTANCE.constructor$SoundEvent(KeyUtils.toResourceLocation(hitSound.left().id()), Optional.empty());
float pitch = hitSound.left().pitch().get() + RandomUtils.generateRandomInt(0, 1) * hitSound.right();
FastNMS.INSTANCE.method$LevelAccessor$playSound(args[0], null, blockPos, sound, CoreReflections.instance$SoundSource$BLOCKS, hitSound.left().volume().get(), pitch);
Object sound = FastNMS.INSTANCE.constructor$SoundEvent(KeyUtils.toResourceLocation(hitSound.id()), Optional.empty());
FastNMS.INSTANCE.method$LevelAccessor$playSound(args[0], null, blockPos, sound, CoreReflections.instance$SoundSource$BLOCKS, hitSound.volume().get(), hitSound.pitch().get());
}
public static class Factory implements BlockBehaviorFactory {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
List<Pair<SoundData, Float>> hitSounds = ResourceConfigUtils.parseConfigAsList(arguments.get("hit-sounds"), map -> {
SoundData hitSound = SoundData.create(ResourceConfigUtils.requireNonNullOrThrow(arguments.get("sound"), "warning.config.block.behavior.chime.missing_hit_sound"), SoundData.SoundValue.FIXED_1, SoundData.SoundValue.ranged(0.9f, 1f));
float randomMultiplier = ResourceConfigUtils.getAsFloat(arguments.get("random-pitch-multiplier"), "random-pitch-multiplier");
return Pair.of(hitSound, randomMultiplier);
});
return new ChimeBlockBehavior(block, hitSounds);
SoundData hitSound = SoundData.create(ResourceConfigUtils.requireNonNullOrThrow(arguments.get("hit-sound"), "warning.config.block.behavior.chime.missing_hit_sound"), SoundData.SoundValue.FIXED_1, SoundData.SoundValue.ranged(0.9f, 1f));
return new ChimeBlockBehavior(block, hitSound);
}
}
}