2525public class AmbleSoundProvider implements DataProvider {
2626
2727 protected final FabricDataOutput dataOutput ;
28- private final Map <String , List < SoundEvent >> sounds = new HashMap <>();
28+ private final Map <String , Set < SoundEventWrapper >> sounds = new HashMap <>();
2929 private final boolean extractVariants ;
3030
3131 public AmbleSoundProvider (FabricDataOutput dataOutput ) {
@@ -37,18 +37,9 @@ public AmbleSoundProvider(FabricDataOutput dataOutput, boolean extractVariants)
3737 this .extractVariants = extractVariants ;
3838 }
3939
40- private boolean checkDuplicate (String name ) {
41- if (sounds .containsKey (name )) {
42- AmbleKit .LOGGER .error ("Duplicate sound event: {} - Duplicate will be ignored!" , name );
43- return false ;
44- }
45-
46- return true ;
47- }
48-
4940 private boolean canAdd (String name , boolean check ) {
5041 // you can't have duplicates in registries, nor bad ids.
51- return check && !checkDuplicate ( name ) && ! checkName (name );
42+ return check && !checkName (name );
5243 }
5344
5445 public void addSound (String name , SoundEvent event ) {
@@ -59,7 +50,7 @@ public void addSound(String name, boolean check, SoundEvent event) {
5950 if (canAdd (name , check ))
6051 return ;
6152
62- sounds .computeIfAbsent (name , s -> new ArrayList <>()).add (event );
53+ sounds .computeIfAbsent (name , s -> new HashSet <>()).add (new SoundEventWrapper ( event ) );
6354 }
6455
6556 public void addSound (String name , SoundEvent ... events ) {
@@ -70,7 +61,11 @@ public void addSound(String name, boolean check, SoundEvent... events) {
7061 if (canAdd (name , check ))
7162 return ;
7263
73- Collections .addAll (sounds .computeIfAbsent (name , s -> new ArrayList <>()), events );
64+ Set <SoundEventWrapper > set = sounds .computeIfAbsent (name , s -> new HashSet <>());
65+
66+ for (SoundEvent event : events ) {
67+ set .add (new SoundEventWrapper (event ));
68+ }
7469 }
7570
7671 @ Override
@@ -81,8 +76,13 @@ public CompletableFuture<?> run(DataWriter writer) {
8176 addSound (path , false , sound );
8277
8378 if (extractVariants ) {
84- path = extractPath (path );
85- addSound (path , false , sound );
79+ String newPath = extractPath (path );
80+
81+ // event is not a variant!
82+ if (newPath == null )
83+ return ;
84+
85+ addSound (newPath , false , sound );
8686 }
8787 });
8888
@@ -103,12 +103,14 @@ public String getName() {
103103 return "Sound Definitions" ;
104104 }
105105
106- private static JsonObject serializeSounds (Iterable <SoundEvent > soundEvents ) {
106+ private static JsonObject serializeSounds (Iterable <SoundEventWrapper > wrappers ) {
107107 JsonObject obj = new JsonObject ();
108108 JsonArray sounds = new JsonArray ();
109109
110- for (SoundEvent soundEvent : soundEvents ) {
111- sounds .add (soundEvent .getId ().toString ());
110+ //tardis/moody/moody,tardis/moody/moody1,tardis/moody/moody2 =>
111+ //tardis/moody/moody:[tardis/moody/moody, tardis/moody/moody1, tardis/moody/moody2]
112+ for (SoundEventWrapper wrapper : wrappers ) {
113+ sounds .add (wrapper .event .getId ().toString ());
112114 }
113115
114116 obj .add ("sounds" , sounds );
@@ -145,4 +147,18 @@ private static boolean checkName(String name) {
145147 public static Stream <SoundEvent > getSoundsFromMod (String namespace ) {
146148 return Registries .SOUND_EVENT .stream ().filter (sound -> sound .getId ().getNamespace ().equals (namespace ));
147149 }
150+
151+ static class SoundEventWrapper {
152+
153+ private final SoundEvent event ;
154+
155+ public SoundEventWrapper (SoundEvent event ) {
156+ this .event = event ;
157+ }
158+
159+ @ Override
160+ public int hashCode () {
161+ return event .getId ().hashCode ();
162+ }
163+ }
148164}
0 commit comments