Skip to content

Commit 218de8a

Browse files
committed
fix: allow duplicates, use a hash set to contain sound events
build: v1.1.8
1 parent ca49907 commit 218de8a

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
99
loader_version=0.16.10
1010

1111
# Mod Properties
12-
mod_version=1.1.7
12+
mod_version=1.1.8
1313
maven_group=dev.amble.lib
1414
archives_base_name=amble
1515
mod_id=amblekit

src/main/java/dev/amble/lib/datagen/sound/AmbleSoundProvider.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public 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
}

src/main/java/dev/amble/lib/util/StringCursor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.amble.lib.util;
22

3+
34
public class StringCursor {
45

56
private final String str;
@@ -24,13 +25,24 @@ public char peekNext() {
2425
return this.str.charAt(cursor + step);
2526
}
2627

28+
/**
29+
* @return {null} if no change was made.
30+
*/
2731
public String substring() {
28-
if (step > 0)
32+
if (step > 0) {
33+
if (cursor == 0)
34+
return null;
35+
2936
return str.substring(this.cursor);
37+
}
38+
39+
if (step < 0) {
40+
if (cursor + 1 == str.length())
41+
return null;
3042

31-
if (step < 0)
3243
return str.substring(0, this.cursor + 1);
44+
}
3345

34-
return "";
46+
return null;
3547
}
3648
}

0 commit comments

Comments
 (0)