Skip to content

Commit c353a44

Browse files
committed
improved PLAY_SOUND
1) you can now use custom resource pack sounds 2) the Did you mean message now contains the actual identifiers for the sounds 3) there's now a separate error for incorrect identifiers
1 parent 3285fc9 commit c353a44

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/main/java/io/github/techstreet/dfscript/script/action/ScriptActionType.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
4444
import net.minecraft.client.network.ClientPlayNetworkHandler;
4545
import net.minecraft.client.sound.PositionedSoundInstance;
46+
import net.minecraft.client.sound.SoundManager;
4647
import net.minecraft.enchantment.Enchantment;
4748
import net.minecraft.enchantment.Enchantments;
4849
import net.minecraft.item.Item;
@@ -1107,40 +1108,35 @@ public enum ScriptActionType {
11071108
pitch = ctx.value("Pitch").asNumber();
11081109
}
11091110

1110-
SoundEvent snd = null;
1111+
Identifier sndid = null;
1112+
SoundManager sndManager = io.github.techstreet.dfscript.DFScript.MC.getSoundManager();
11111113

11121114
try {
1113-
snd = Registry.SOUND_EVENT.get(new Identifier(sound));
1114-
} catch (Exception err) {
1115-
err.printStackTrace();
1115+
sndid = new Identifier(sound);
11161116
}
1117-
1118-
String jname = sound.toUpperCase().replaceAll("\\.", "_").replaceAll(" ", "_").toUpperCase();
1119-
if (snd == null) {
1120-
try {
1121-
Class<SoundEvents> clazz = SoundEvents.class;
1122-
Field field = clazz.getField(jname);
1123-
snd = (SoundEvent) field.get(null);
1124-
} catch (Exception err) {
1125-
err.printStackTrace();
1126-
}
1117+
catch(Exception err) {
1118+
err.printStackTrace();
1119+
ChatUtil.error("Incorrect identifier: " + sound);
1120+
return;
11271121
}
11281122

1129-
if (snd != null) {
1130-
io.github.techstreet.dfscript.DFScript.MC.getSoundManager().play(PositionedSoundInstance.master(snd, (float) volume, (float) pitch));
1123+
if (sndManager.getKeys().contains(sndid)) {
1124+
SoundEvent snd = new SoundEvent(sndid);
1125+
sndManager.play(PositionedSoundInstance.master(snd, (float) volume, (float) pitch));
11311126
} else {
11321127
ChatUtil.error("Unknown sound: " + sound);
11331128

11341129
try {
1135-
Class<SoundEvents> clazz = SoundEvents.class;
1130+
String jname = StringUtil.fromSoundIDToRegistryID(sound);
11361131

11371132
List<String> similiar = new ArrayList<>();
11381133

11391134
int counter = 0;
1140-
for (Field field : clazz.getFields()) {
1141-
String name = field.getName();
1135+
for (Identifier id : sndManager.getKeys()) {
1136+
String sid = id.toString();
1137+
String name = StringUtil.fromSoundIDToRegistryID(sid);
11421138
if (name.contains(jname)) {
1143-
similiar.add(StringUtil.toTitleCase(name.replaceAll("_", " ")));
1139+
similiar.add(sid);
11441140
counter++;
11451141
if (counter > 5) {
11461142
break;
@@ -1149,7 +1145,7 @@ public enum ScriptActionType {
11491145
}
11501146

11511147
if (similiar.size() > 0) {
1152-
ChatUtil.error("Did you mean: " + String.join(", ", similiar));
1148+
ChatUtil.error("Did you mean: \n" + String.join(", \n", similiar));
11531149
}
11541150
} catch (Exception err) {
11551151
err.printStackTrace();

src/main/java/io/github/techstreet/dfscript/util/StringUtil.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,9 @@ public static String toTitleCase(String input) {
149149
.collect(Collectors.joining(" "));
150150

151151
}
152+
153+
// converts 'minecraft:entity.axolotl.attack' to 'ENTITY_AXOLOTL_ATTACK'
154+
public static String fromSoundIDToRegistryID(String soundID) {
155+
return soundID.toUpperCase().replaceAll("^minecraft:", "").replaceAll("\\.", "_").replaceAll(" ", "_");
156+
}
152157
}

0 commit comments

Comments
 (0)