From d06e57bd1dcec7aec1d326e9d7fee894b097c109 Mon Sep 17 00:00:00 2001 From: PseudoKnight Date: Mon, 11 Nov 2024 03:24:27 -0800 Subject: [PATCH] Add seed parameter to play_sound() --- .../com/laytonsmith/abstraction/MCPlayer.java | 18 +-- .../com/laytonsmith/abstraction/MCWorld.java | 12 +- .../abstraction/bukkit/BukkitMCWorld.java | 64 ++++++----- .../bukkit/entities/BukkitMCPlayer.java | 78 ++++++------- .../core/functions/Environment.java | 104 +++++++++--------- .../core/functions/PlayerManagement.java | 16 +-- 6 files changed, 135 insertions(+), 157 deletions(-) diff --git a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java index 4a33460a3..ebe8d0601 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/MCPlayer.java @@ -140,23 +140,13 @@ public interface MCPlayer extends MCCommandSender, MCHumanEntity, MCOfflinePlaye void playNote(MCLocation loc, MCInstrument instrument, MCNote note); - void playSound(MCLocation l, MCSound sound, float volume, float pitch); + void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCLocation l, String sound, float volume, float pitch); + void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch); + void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch); - - void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch); - - void playSound(MCEntity ent, String sound, float volume, float pitch); - - void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch); - - void stopSound(MCSound sound); - - void stopSound(String sound); + void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch, Long seed); void stopSound(MCSound sound, MCSoundCategory category); diff --git a/src/main/java/com/laytonsmith/abstraction/MCWorld.java b/src/main/java/com/laytonsmith/abstraction/MCWorld.java index d89b22044..a05ad7509 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCWorld.java +++ b/src/main/java/com/laytonsmith/abstraction/MCWorld.java @@ -89,17 +89,13 @@ public interface MCWorld extends MCMetadatable { void spawnParticle(MCLocation l, MCParticle pa, int count, double offsetX, double offsetY, double offsetZ, double velocity, Object data); - void playSound(MCLocation l, MCSound sound, float volume, float pitch); + void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCLocation l, String sound, float volume, float pitch); + void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch); + void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch, Long seed); - void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch); - - void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch); - - void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch); + void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch, Long seed); MCItem dropItemNaturally(MCLocation l, MCItemStack is); diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCWorld.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCWorld.java index 70330ebc2..0e5156f1d 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCWorld.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCWorld.java @@ -308,51 +308,55 @@ public void spawnParticle(MCLocation l, MCParticle pa, int count, double offsetX } @Override - public void playSound(MCLocation l, MCSound sound, float volume, float pitch) { - w.playSound(((BukkitMCLocation) l).asLocation(), - ((BukkitMCSound) sound).getConcrete(), volume, pitch); - } - - @Override - public void playSound(MCLocation l, String sound, float volume, float pitch) { - w.playSound((Location) l.getHandle(), sound, volume, pitch); - } - - @Override - public void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch) { - if(category == null) { - w.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), - SoundCategory.MASTER, volume, pitch); + public void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + w.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch); } else { - w.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + w.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch, seed); } } @Override - public void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch) { - if(category == null) { - w.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), - SoundCategory.MASTER, volume, pitch); + public void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + w.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch); } else { - w.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + w.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch, seed); } } @Override - public void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch) { - if(category == null) { - w.playSound((Entity) ent.getHandle(), sound, SoundCategory.MASTER, volume, pitch); + public void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + w.playSound((Entity) ent.getHandle(), sound, cat, volume, pitch); } else { - w.playSound((Entity) ent.getHandle(), sound, BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + w.playSound((Entity) ent.getHandle(), sound, cat, volume, pitch, seed); } } @Override - public void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch) { - w.playSound((Location) l.getHandle(), sound, - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + public void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + w.playSound((Location) l.getHandle(), sound, cat, volume, pitch); + } else { + w.playSound((Location) l.getHandle(), sound, cat, volume, pitch, seed); + } } @Override diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java index 8e3540ca2..0e9bb1551 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java @@ -600,63 +600,55 @@ public void playNote(MCLocation loc, MCInstrument instrument, MCNote note) { } @Override - public void playSound(MCLocation l, MCSound sound, float volume, float pitch) { - p.playSound(((BukkitMCLocation) l).asLocation(), - ((BukkitMCSound) sound).getConcrete(), volume, pitch); - } - - @Override - public void playSound(MCLocation l, String sound, float volume, float pitch) { - p.playSound(((BukkitMCLocation) l).asLocation(), sound, volume, pitch); - } - - @Override - public void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch) { - if(category == null) { - p.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), - SoundCategory.MASTER, volume, pitch); + public void playSound(MCLocation l, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + p.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch); } else { - p.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + p.playSound((Location) l.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch, seed); } } @Override - public void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch) { + public void playSound(MCEntity ent, MCSound sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } if(category == null) { - p.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), - SoundCategory.MASTER, volume, pitch); + p.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch); } else { - p.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); + p.playSound((Entity) ent.getHandle(), ((BukkitMCSound) sound).getConcrete(), cat, volume, pitch, seed); } } @Override - public void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch) { - p.playSound((Location) l.getHandle(), sound, - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); - } - - @Override - public void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch) { - p.playSound(((Entity) ent.getHandle()), sound, - BukkitMCSoundCategory.getConvertor().getConcreteEnum(category), volume, pitch); - } - - @Override - public void playSound(MCEntity ent, String sound, float volume, float pitch) { - p.playSound(((Entity) ent.getHandle()), sound, volume, pitch); - } - - @Override - public void stopSound(MCSound sound) { - p.stopSound(((BukkitMCSound) sound).getConcrete()); + public void playSound(MCLocation l, String sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + p.playSound((Location) l.getHandle(), sound, cat, volume, pitch); + } else { + p.playSound((Location) l.getHandle(), sound, cat, volume, pitch, seed); + } } @Override - public void stopSound(String sound) { - p.stopSound(sound); + public void playSound(MCEntity ent, String sound, MCSoundCategory category, float volume, float pitch, Long seed) { + SoundCategory cat = BukkitMCSoundCategory.getConvertor().getConcreteEnum(category); + if(cat == null) { + cat = SoundCategory.MASTER; + } + if(seed == null) { + p.playSound((Entity) ent.getHandle(), sound, cat, volume, pitch); + } else { + p.playSound((Entity) ent.getHandle(), sound, cat, volume, pitch, seed); + } } @Override diff --git a/src/main/java/com/laytonsmith/core/functions/Environment.java b/src/main/java/com/laytonsmith/core/functions/Environment.java index 203f92214..eca5057a3 100644 --- a/src/main/java/com/laytonsmith/core/functions/Environment.java +++ b/src/main/java/com/laytonsmith/core/functions/Environment.java @@ -1754,6 +1754,7 @@ public Mixed exec(Target t, MCSoundCategory category = null; float volume = 1; float pitch = 1; + Long seed = null; if(!(args[1].isInstanceOf(CArray.TYPE))) { throw new CREFormatException("An array was expected but received " + args[1], t); @@ -1785,30 +1786,33 @@ public Mixed exec(Target t, pitch = ArgumentValidation.getDouble32(sa.get("pitch", t), t); } + if(sa.containsKey("seed") && Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_2)) { + seed = ArgumentValidation.getInt(sa.get("seed", t), t); + } + if(args.length == 3) { - java.util.List players = new java.util.ArrayList<>(); if(args[2].isInstanceOf(CArray.TYPE)) { - for(String key : ((CArray) args[2]).stringKeySet()) { - players.add(Static.GetPlayer(((CArray) args[2]).get(key, t), t)); - } - } else { - players.add(Static.GetPlayer(args[2], t)); - } - - if(loc == null) { - for(MCPlayer p : players) { - p.playSound(ent, sound, category, volume, pitch); + CArray players = (CArray) args[2]; + for(String key : players.stringKeySet()) { + MCPlayer p = Static.GetPlayer(players.get(key, t), t); + if(ent != null) { + p.playSound(ent, sound, category, volume, pitch, seed); + } else { + p.playSound(loc, sound, category, volume, pitch, seed); + } } } else { - for(MCPlayer p : players) { - p.playSound(loc, sound, category, volume, pitch); + MCPlayer p = Static.GetPlayer(args[2], t); + if(ent != null) { + p.playSound(ent, sound, category, volume, pitch, seed); + } else { + p.playSound(loc, sound, category, volume, pitch, seed); } } - } else if(loc == null) { - ent.getWorld().playSound(ent, sound, category, volume, pitch); + ent.getWorld().playSound(ent, sound, category, volume, pitch, seed); } else { - loc.getWorld().playSound(loc, sound, category, volume, pitch); + loc.getWorld().playSound(loc, sound, category, volume, pitch, seed); } return CVoid.VOID; } @@ -1826,14 +1830,17 @@ public Integer[] numArgs() { @Override public String docs() { return "void {source, soundArray[, players]} Plays a sound at the given source." - + " Source can be a location array or entity UUID. SoundArray is in an associative array with" - + " keys 'sound', 'category', 'volume', 'pitch', where all are optional except sound." - + " Volume, if greater than 1.0 (default), is the distance in chunks players can hear the sound." - + " Pitch has a range of 0.5 - 2.0, where where 1.0 is the middle pitch and default. Players can" - + " be a single player or an array of players to play the sound to, if" - + " not given, all players can potentially hear it. ---- Possible categories: " + + " Source can be a location array (or entity UUID on MC 1.18.1+)." + + " The soundArray is in an associative array with the keys:" + + " 'sound' 'category', 'volume' (float), 'pitch' (float), 'seed' (int; MC 1.20.2+)" + + " -- all are optional except sound." + + " Volume, if greater than 1.0 (default), controls the distance players can hear the sound." + + " Pitch has a clamped range of 0.5 - 2.0, where where 1.0 is the middle pitch and default." + + " Seed is an integer that determines which sound variant is played." + + " Players can be a single player or an array of players to play the sound to, if not given," + + " all players can potentially hear it. ---- Possible categories: " + StringUtils.Join(MCSoundCategory.values(), ", ", ", or ", " or ") + "." - + " \n\nPossible sounds: " + StringUtils.Join(MCSound.values(), "
"); + + " \n\nPossible sounds:\n" + StringUtils.Join(MCSound.values(), "
"); } @Override @@ -1918,6 +1925,7 @@ public Mixed exec(Target t, MCSoundCategory category = null; float volume = 1; float pitch = 1; + Long seed = null; if(!(args[1].isInstanceOf(CArray.TYPE))) { throw new CREFormatException("An array was expected but received " + args[1], t); @@ -1943,6 +1951,10 @@ public Mixed exec(Target t, pitch = ArgumentValidation.getDouble32(sa.get("pitch", t), t); } + if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_20_2) && sa.containsKey("seed")) { + seed = ArgumentValidation.getInt(sa.get("seed", t), t); + } + if(args.length == 3) { java.util.List players = new java.util.ArrayList<>(); if(args[2].isInstanceOf(CArray.TYPE)) { @@ -1954,21 +1966,11 @@ public Mixed exec(Target t, } try { - if(category == null) { - for(MCPlayer p : players) { - if(loc == null) { - p.playSound(ent, path, volume, pitch); - } else { - p.playSound(loc, path, volume, pitch); - } - } - } else { - for(MCPlayer p : players) { - if(loc == null) { - p.playSound(ent, path, category, volume, pitch); - } else { - p.playSound(loc, path, category, volume, pitch); - } + for(MCPlayer p : players) { + if(loc == null) { + p.playSound(ent, path, category, volume, pitch, seed); + } else { + p.playSound(loc, path, category, volume, pitch, seed); } } } catch(Exception ex) { @@ -1977,11 +1979,9 @@ public Mixed exec(Target t, } else { try { if(loc == null) { - ent.getWorld().playSound(ent, path, category, volume, pitch); - } else if(category == null) { - loc.getWorld().playSound(loc, path, volume, pitch); + ent.getWorld().playSound(ent, path, category, volume, pitch, seed); } else { - loc.getWorld().playSound(loc, path, category, volume, pitch); + loc.getWorld().playSound(loc, path, category, volume, pitch, seed); } } catch(Exception ex) { throw new CREFormatException(ex.getMessage(), t); @@ -2002,15 +2002,17 @@ public Integer[] numArgs() { @Override public String docs() { - return "void {source, soundArray[, players]} Plays a sound at the" - + " given source. Source can be a location array or entity UUID." - + " SoundArray is in an associative array with" - + " keys 'sound', 'category', 'volume', 'pitch', where all are optional except sound." - + " Volume, if greater than 1.0 (default), is the distance in chunks players can hear the sound." - + " Pitch has a range of 0.5 - 2.0, where where 1.0 is the middle pitch and default. Players can" - + " be a single player or an array of players to play the sound to, if" - + " not given, all players can potentially hear it. Sound is" - + " a sound path, separated by periods. ---- Possible categories: " + return "void {source, soundArray[, players]} Plays a sound at the given source." + + " Source can be a location array (or entity UUID on MC 1.19.3+)." + + " The soundArray is in an associative array with the keys:" + + " 'sound', 'category', 'volume' (float), 'pitch' (float), 'seed' (int; MC 1.20.2+)" + + " -- all are optional except sound." + + " Volume, if greater than 1.0 (default), controls the distance players can hear the sound." + + " Pitch has a clamped range of 0.5 - 2.0, where where 1.0 is the middle pitch and default." + + " Seed is an integer that determines which sound variant is played." + + " Players can be a single player or an array of players to play the sound to, if not given," + + " all players can potentially hear it. Sound is a sound path, separated by periods." + + " ---- Possible categories: " + StringUtils.Join(MCSoundCategory.values(), ", ", ", or ", " or ") + "."; } diff --git a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java index 80e371994..ed78e48ab 100644 --- a/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java +++ b/src/main/java/com/laytonsmith/core/functions/PlayerManagement.java @@ -5596,17 +5596,15 @@ public Mixed exec(Target t, com.laytonsmith.core.environments.Environment enviro throw new CREFormatException("Sound name '" + soundName + "' is invalid.", t); } + MCSoundCategory category = null; if(categoryName != null) { - MCSoundCategory category; try { category = MCSoundCategory.valueOf(categoryName.toUpperCase()); } catch (IllegalArgumentException iae) { throw new CREFormatException("Sound category '" + categoryName + "' is invalid.", t); } - p.stopSound(sound, category); - } else { - p.stopSound(sound); } + p.stopSound(sound, category); return CVoid.VOID; } @@ -5623,7 +5621,7 @@ public Integer[] numArgs() { @Override public String docs() { - return "void {player, sound, [category]} Stops the specified sound for the given player."; + return "void {player, sound, [category]} Stops the specified sound for a player."; } @Override @@ -5679,7 +5677,7 @@ public Integer[] numArgs() { @Override public String docs() { - return "void {player, category} Stops all sounds in a category for the given player. (MC 1.19)"; + return "void {player, category} Stops all sounds in a category for a player. (MC 1.19+)"; } @Override @@ -5739,11 +5737,7 @@ public Mixed exec(Target t, com.laytonsmith.core.environments.Environment enviro } } try { - if(category != null) { - p.stopSound(soundName, category); - } else { - p.stopSound(soundName); - } + p.stopSound(soundName, category); } catch(Exception ex) { throw new CREFormatException(ex.getMessage(), t); }