From 07b3029820a99b41a15b784ffea77040e1464bd6 Mon Sep 17 00:00:00 2001 From: gnembon Date: Mon, 13 Apr 2020 02:04:00 -0300 Subject: [PATCH] added mixer option and defaulted sounds to master. Fixes #201 --- docs/scarpet/Full.md | 9 ++++++--- docs/scarpet/api/Auxiliary.md | 9 ++++++--- src/main/java/carpet/script/CarpetExpression.java | 12 +++++++++++- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/docs/scarpet/Full.md b/docs/scarpet/Full.md index baed6c5d..3668b28f 100644 --- a/docs/scarpet/Full.md +++ b/docs/scarpet/Full.md @@ -2932,10 +2932,13 @@ Collection of other methods that control smaller, yet still important aspects of ## Sounds -### `sound(name, pos, volume?, pitch?)` +### `sound(name, pos, volume?, pitch?, mixer?)` -Plays a specific sound `name`, at block or position `pos`, with optional `volume` and modified `pitch`. `pos` can be -either a block, triple of coords, or a list of thee numbers. Uses the same options as a corresponding `playsound` command. +Plays a specific sound `name`, at block or position `pos`, with optional `volume` and modified `pitch`, and under +optional `mixer`. Default values for `volume`, `pitch` and `mixer` are `1.0`, `1.0`, and `master`. +Valid mixer options are `master`, `music`, `record`, `weather`, `block`, `hostile`,`neutral`, `player`, `ambient` +and `voice`. `pos` can be either a block, triple of coords, or a list of thee numbers. Uses the same options as a + corresponding `playsound` command. ## Particles diff --git a/docs/scarpet/api/Auxiliary.md b/docs/scarpet/api/Auxiliary.md index 17b1d003..205656ae 100644 --- a/docs/scarpet/api/Auxiliary.md +++ b/docs/scarpet/api/Auxiliary.md @@ -4,10 +4,13 @@ Collection of other methods that control smaller, yet still important aspects of ## Sounds -### `sound(name, pos, volume?, pitch?)` +### `sound(name, pos, volume?, pitch?, mixer?)` -Plays a specific sound `name`, at block or position `pos`, with optional `volume` and modified `pitch`. `pos` can be -either a block, triple of coords, or a list of thee numbers. Uses the same options as a corresponding `playsound` command. +Plays a specific sound `name`, at block or position `pos`, with optional `volume` and modified `pitch`, and under +optional `mixer`. Default values for `volume`, `pitch` and `mixer` are `1.0`, `1.0`, and `master`. +Valid mixer options are `master`, `music`, `record`, `weather`, `block`, `hostile`,`neutral`, `player`, `ambient` +and `voice`. `pos` can be either a block, triple of coords, or a list of thee numbers. Uses the same options as a + corresponding `playsound` command. ## Particles diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index f246d4fb..d78e178a 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -132,6 +132,7 @@ import java.util.Optional; import java.util.UUID; import java.util.function.BiFunction; import java.util.function.Predicate; +import java.util.stream.Collector; import java.util.stream.Collectors; import static carpet.script.utils.WorldTools.canHasChunk; @@ -2378,6 +2379,8 @@ public class CarpetExpression }); } + private static Map mixerMap = Arrays.stream(SoundCategory.values()).collect(Collectors.toMap(SoundCategory::getName, k -> k)); + private void API_AuxiliaryAspects() { this.expr.addLazyFunction("sound", -1, (c, t, lv) -> { @@ -2388,12 +2391,19 @@ public class CarpetExpression throw new InternalExpressionException("No such sound: "+soundName.getPath()); float volume = 1.0F; float pitch = 1.0F; + SoundCategory mixer = SoundCategory.MASTER; if (lv.size() > 0+locator.offset) { volume = (float) NumericValue.asNumber(lv.get(0+locator.offset).evalValue(c)).getDouble(); if (lv.size() > 1+locator.offset) { pitch = (float) NumericValue.asNumber(lv.get(1+locator.offset).evalValue(c)).getDouble(); + if (lv.size() > 2+locator.offset) + { + String mixerName = lv.get(2+locator.offset).evalValue(c).getString(); + mixer = mixerMap.get(mixerName.toLowerCase(Locale.ROOT)); + if (mixer == null) throw new InternalExpressionException(mixerName +" is not a valid mixer name"); + } } } Vec3d vec = locator.vec; @@ -2402,7 +2412,7 @@ public class CarpetExpression for (ServerPlayerEntity player : cc.s.getWorld().getPlayers( (p) -> p.squaredDistanceTo(vec) < d0)) { count++; - player.networkHandler.sendPacket(new PlaySoundIdS2CPacket(soundName, SoundCategory.PLAYERS, vec, volume, pitch)); + player.networkHandler.sendPacket(new PlaySoundIdS2CPacket(soundName, mixer, vec, volume, pitch)); } int totalPlayed = count; return (_c, _t) -> new NumericValue(totalPlayed);