9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-22 16:39:28 +00:00
This commit is contained in:
jhqwqmc
2025-09-24 16:31:48 +08:00
parent 10dbf3d590
commit d522cf105d
4 changed files with 437 additions and 554 deletions

View File

@@ -7,50 +7,43 @@ 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 SoundData hitSound;
private final boolean randomPitch;
private final float randomMultiplier;
private final List<Pair<SoundData, Float>> hitSounds;
public ChimeBlockBehavior(CustomBlock customBlock, SoundData hitSound, boolean randomPitch, float randomMultiplier) {
public ChimeBlockBehavior(CustomBlock customBlock, List<Pair<SoundData, Float>> hitSounds) {
super(customBlock);
this.hitSound = hitSound;
this.randomPitch = randomPitch;
this.randomMultiplier = randomMultiplier;
this.hitSounds = hitSounds;
}
@Override
public void onProjectileHit(Object thisBlock, Object[] args, Callable<Object> superMethod) {
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.id()), Optional.empty());
float pitch = hitSound.pitch().get();
if (randomPitch) {
pitch = pitch + RandomUtils.generateRandomInt(0, 1) * this.randomMultiplier;
}
FastNMS.INSTANCE.method$LevelAccessor$playSound(args[0], null, blockPos, sound, CoreReflections.instance$SoundSource$BLOCKS, hitSound.volume().get(), pitch);
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);
}
public static class Factory implements BlockBehaviorFactory {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
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));
Map<String, Object> randomPitch = ResourceConfigUtils.getAsMapOrNull(arguments.get("random-pitch"), "random-pitch");
boolean enableRandomPitch = false;
float randomMultiplier = 1f;
if (randomPitch != null) {
enableRandomPitch = true;
randomMultiplier = ResourceConfigUtils.getAsFloat(arguments.getOrDefault("multiplier", 1f), "multiplier");
}
return new ChimeBlockBehavior(block, hitSound, enableRandomPitch, randomMultiplier);
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);
}
}
}

View File

@@ -54,10 +54,6 @@ i18n:
item.hami_melon_seeds: Hami Melon Seeds
item.palm_button: Palm Button
item.palm_fence: Palm Fence
item.infected_palm_log: Infected Palm Log
item.small_mushroom: Small Mushroom
item.medium_mushroom: Medium Mushroom
item.large_mushroom: Large Mushroom
category.default.name: Default Assets
category.default.lore: Contains the default configuration of CraftEngine
category.palm_tree: Palm Tree
@@ -121,10 +117,6 @@ i18n:
item.hami_melon_seeds: 哈密瓜种子
item.palm_button: 棕榈木按钮
item.palm_fence: 棕榈木栅栏
item.infected_palm_log: 菌蚀棕榈原木
item.small_mushroom: 小型蘑菇
item.medium_mushroom: 中型蘑菇
item.large_mushroom: 大型蘑菇
category.default.name: 默认资产
category.default.lore: 包含了CraftEngine的默认配置
category.palm_tree: 棕榈树
@@ -172,10 +164,6 @@ lang:
block_name:default:default:attached_hami_melon_stem: Hami Melon Stem
block_name:default:palm_button: Palm Button
block_name:default:palm_fence: Palm Fence
block_name:default:infected_palm_log: Infected Palm Log
block_name:default:small_mushroom: Small Mushroom
block_name:default:medium_mushroom: Medium Mushroom
block_name:default:large_mushroom: Large Mushroom
zh_cn:
block_name:default:chinese_lantern: 灯笼
block_name:default:netherite_anvil: 下界合金砧
@@ -211,7 +199,3 @@ lang:
block_name:default:default:attached_hami_melon_stem: 哈密瓜茎
block_name:default:palm_button: 棕榈木按钮
block_name:default:palm_fence: 棕榈木栅栏
block_name:default:infected_palm_log: 菌蚀棕榈原木
block_name:default:small_mushroom: 小型蘑菇
block_name:default:medium_mushroom: 中型蘑菇
block_name:default:large_mushroom: 大型蘑菇

View File

@@ -171,6 +171,6 @@ public abstract class Property<T extends Comparable<T>> {
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("name", this.name).add("clazz", this.clazz).add("values", this.possibleValues()).toString();
return this.getClass().getSimpleName() + "{clazz=" + this.clazz + ", name='" + this.name + "', values=" + this.possibleValues() + '}';
}
}