4343import net .fabricmc .fabric .api .client .command .v2 .FabricClientCommandSource ;
4444import net .minecraft .client .network .ClientPlayNetworkHandler ;
4545import net .minecraft .client .sound .PositionedSoundInstance ;
46+ import net .minecraft .client .sound .SoundManager ;
4647import net .minecraft .enchantment .Enchantment ;
4748import net .minecraft .enchantment .Enchantments ;
4849import 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 ();
0 commit comments