From d875f1de879ad0d2379da6d9edbe3886429b2c7c Mon Sep 17 00:00:00 2001 From: jhqwqmc <2110242767@qq.com> Date: Sun, 13 Jul 2025 15:47:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E6=92=AD=E6=94=BE=E5=A3=B0?= =?UTF-8?q?=E9=9F=B3=E5=87=BD=E6=95=B0=E6=94=AF=E6=8C=81=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/function/PlaySoundFunction.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/PlaySoundFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/PlaySoundFunction.java index ea5f4c547..00004bbb8 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/PlaySoundFunction.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/PlaySoundFunction.java @@ -1,10 +1,13 @@ package net.momirealms.craftengine.core.plugin.context.function; +import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.Context; import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; import net.momirealms.craftengine.core.plugin.context.number.NumberProviders; import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters; +import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelector; +import net.momirealms.craftengine.core.plugin.context.selector.PlayerSelectors; import net.momirealms.craftengine.core.sound.SoundSource; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.ResourceConfigUtils; @@ -25,8 +28,19 @@ public class PlaySoundFunction extends AbstractConditionalF private final NumberProvider volume; private final NumberProvider pitch; private final SoundSource source; + private final PlayerSelector selector; - public PlaySoundFunction(Key soundEvent, NumberProvider x, NumberProvider y, NumberProvider z, NumberProvider volume, NumberProvider pitch, SoundSource source, List> predicates) { + public PlaySoundFunction( + Key soundEvent, + NumberProvider x, + NumberProvider y, + NumberProvider z, + NumberProvider volume, + NumberProvider pitch, + SoundSource source, + PlayerSelector selector, + List> predicates + ) { super(predicates); this.soundEvent = soundEvent; this.x = x; @@ -35,15 +49,22 @@ public class PlaySoundFunction extends AbstractConditionalF this.volume = volume; this.pitch = pitch; this.source = source; + this.selector = selector; } @Override public void runInternal(CTX ctx) { - Optional optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION); - if (optionalWorldPosition.isPresent()) { - World world = optionalWorldPosition.get().world(); - world.playSound(new Vec3d(this.x.getDouble(ctx), this.y.getDouble(ctx), this.z.getDouble(ctx)), - this.soundEvent, this.volume.getFloat(ctx), this.pitch.getFloat(ctx), this.source); + if (this.selector == null) { + Optional optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION); + if (optionalWorldPosition.isPresent()) { + World world = optionalWorldPosition.get().world(); + world.playSound(new Vec3d(this.x.getDouble(ctx), this.y.getDouble(ctx), this.z.getDouble(ctx)), + this.soundEvent, this.volume.getFloat(ctx), this.pitch.getFloat(ctx), this.source); + } + } else { + for (Player player : selector.get(ctx)) { + player.playSound(this.soundEvent, this.source, this.volume.getFloat(ctx), this.pitch.getFloat(ctx)); + } } } @@ -67,7 +88,8 @@ public class PlaySoundFunction extends AbstractConditionalF NumberProvider volume = NumberProviders.fromObject(arguments.getOrDefault("volume", 1)); NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", 1)); SoundSource source = Optional.ofNullable(arguments.get("source")).map(String::valueOf).map(it -> SoundSource.valueOf(it.toUpperCase(Locale.ENGLISH))).orElse(SoundSource.MASTER); - return new PlaySoundFunction<>(soundEvent, x, y, z, volume, pitch, source, getPredicates(arguments)); + PlayerSelector selector = PlayerSelectors.fromObject(arguments.get("target"), conditionFactory()); + return new PlaySoundFunction<>(soundEvent, x, y, z, volume, pitch, source, selector, getPredicates(arguments)); } } }