Skip to content

Commit 6df9ba9

Browse files
revert categories fix, sound events work fine
1 parent 52aa21d commit 6df9ba9

File tree

6 files changed

+26
-125
lines changed

6 files changed

+26
-125
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ authors=hama, Tomate0613
1717
contributors=Sisby folk
1818
license=CC0-1.0
1919
# Mod Version
20-
baseVersion=0.1.2
20+
baseVersion=0.1.3
2121
# Branch Metadata
2222
branch=1.21
2323
tagBranch=1.21

src/main/java/symbolics/division/meret/AreaMusic.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,41 @@
11
package symbolics.division.meret;
22

3-
import com.mojang.datafixers.util.Either;
43
import com.mojang.datafixers.util.Pair;
5-
import com.mojang.serialization.Codec;
6-
import com.mojang.serialization.codecs.RecordCodecBuilder;
7-
import dev.doublekekse.area_lib.Area;
84
import dev.doublekekse.area_lib.component.AreaDataComponent;
95
import dev.doublekekse.area_lib.data.AreaSavedData;
106
import net.minecraft.core.Holder;
11-
import net.minecraft.core.HolderSet;
12-
import net.minecraft.core.RegistryAccess;
13-
import net.minecraft.core.registries.Registries;
147
import net.minecraft.nbt.CompoundTag;
158
import net.minecraft.nbt.NbtOps;
16-
import net.minecraft.resources.ResourceLocation;
179
import net.minecraft.sounds.Music;
1810
import net.minecraft.sounds.SoundEvents;
19-
import net.minecraft.tags.TagKey;
20-
import net.minecraft.util.RandomSource;
21-
import net.minecraft.world.item.JukeboxSong;
22-
import org.jetbrains.annotations.Nullable;
2311

24-
import java.util.Optional;
25-
26-
public class AreaMusicComponent implements AreaDataComponent, AreaMusic {
27-
private static final Music EMPTY = new Music(Holder.direct(SoundEvents.EMPTY),0, 0, false);
28-
public record MusicCategory(TagKey<JukeboxSong> tag, int minDelay, int maxDelay, boolean replace) {
29-
private static final Codec<TagKey<JukeboxSong>> TAG_CODEC = TagKey.codec(Registries.JUKEBOX_SONG);
30-
public static Codec<MusicCategory> CODEC = RecordCodecBuilder.create(
31-
instance -> instance.group(
32-
TAG_CODEC.fieldOf("category").forGetter(MusicCategory::tag),
33-
Codec.INT.fieldOf("minDelay").forGetter(MusicCategory::minDelay),
34-
Codec.INT.fieldOf("maxDelay").forGetter(MusicCategory::maxDelay),
35-
Codec.BOOL.fieldOf("replace").forGetter(MusicCategory::replace)
36-
).apply(instance, MusicCategory::new)
37-
);
38-
public static MusicCategory of(ResourceLocation tag, int minDelay, int maxDelay, boolean replace) {
39-
return new MusicCategory(TagKey.create(Registries.JUKEBOX_SONG, tag), minDelay, maxDelay, replace);
40-
}
41-
}
42-
43-
private final Codec<Either<Music, MusicCategory>> CODEC = Codec.either(Music.CODEC, MusicCategory.CODEC);
44-
45-
@Nullable private Either<Music, MusicCategory> music;
46-
47-
public AreaMusicComponent() {}
12+
public class AreaMusicComponent implements AreaDataComponent {
13+
private static final Music EMPTY = new Music(Holder.direct(SoundEvents.EMPTY), 0, 0, false);
14+
private Music music;
4815

4916
public AreaMusicComponent(Music music) {
50-
this.music = Either.left(music);
51-
}
52-
53-
public AreaMusicComponent(ResourceLocation category, int minDelay, int maxDelay, boolean replace) {
54-
this.music = Either.right(MusicCategory.of(category, minDelay, maxDelay, replace));
17+
this.music = music;
5518
}
5619

5720
@Override
5821
public void load(AreaSavedData areaSavedData, CompoundTag compoundTag) {
59-
this.music = CODEC.decode(NbtOps.INSTANCE, compoundTag.get("music")).mapOrElse(
22+
this.music = Music.CODEC.decode(NbtOps.INSTANCE, compoundTag.get("music")).mapOrElse(
6023
Pair::getFirst,
6124
error -> {
62-
Meret.LOGGER.error("Failed to parse area music component: {}", error);
63-
return null;
25+
Meret.LOGGER.error("failed to decode area music data: {}", error);
26+
return EMPTY;
6427
}
6528
);
6629
}
6730

6831
@Override
6932
public CompoundTag save() {
7033
CompoundTag tag = new CompoundTag();
71-
tag.put("music", CODEC.encode(this.music, NbtOps.INSTANCE, null).getOrThrow());
34+
tag.put("music", Music.CODEC.encode(this.music, NbtOps.INSTANCE, null).getOrThrow());
7235
return tag;
7336
}
7437

75-
@Override
76-
public Music getMusic(RegistryAccess registryAccess, RandomSource randomSource) {
77-
if (this.music == null) return EMPTY;
78-
return this.music.map(
79-
song -> song,
80-
category -> {
81-
Optional<HolderSet.Named<JukeboxSong>> taggedOptional = registryAccess.registry(Registries.JUKEBOX_SONG).orElseThrow().getTag(category.tag);
82-
if (taggedOptional.isEmpty()) {
83-
Meret.LOGGER.warn("Failed to load music for tag: {}", category.tag);
84-
return EMPTY;
85-
}
86-
return taggedOptional.flatMap(
87-
set -> set.getRandomElement(randomSource)
88-
.map(Holder::value)
89-
.map(song -> new Music(song.soundEvent(), category.minDelay, category.maxDelay, category.replace))
90-
).orElse(EMPTY);
91-
}
92-
);
38+
public Music getMusic() {
39+
return music == null ? EMPTY : music;
9340
}
9441
}

src/main/java/symbolics/division/meret/Meret.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Meret implements ModInitializer {
1515
public static final Logger LOGGER = LoggerFactory.getLogger(ID);
1616
public static final MeretConfig CONFIG = MeretConfig.createToml(FabricLoader.getInstance().getConfigDir(), "", ID, MeretConfig.class);
1717

18-
public static final AreaDataComponentType<AreaMusicComponent> AREA_MUSIC_DATA_COMPONENT = AreaDataComponentTypeRegistry.registerTracking(id("area_music"), AreaMusicComponent::new);
18+
public static final AreaDataComponentType<AreaMusicComponent> AREA_MUSIC_DATA_COMPONENT = AreaDataComponentTypeRegistry.registerTracking(id("area_music"), () -> new AreaMusicComponent(null));
1919

2020
public static ResourceLocation id(String id) {
2121
return ResourceLocation.fromNamespaceAndPath(ID, id);

src/main/java/symbolics/division/meret/MeretCommands.java

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
package symbolics.division.meret;
22

3-
import com.mojang.brigadier.Command;
43
import com.mojang.brigadier.CommandDispatcher;
54
import com.mojang.brigadier.arguments.BoolArgumentType;
65
import com.mojang.brigadier.arguments.IntegerArgumentType;
7-
import com.mojang.brigadier.builder.ArgumentBuilder;
8-
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
9-
import com.mojang.brigadier.suggestion.SuggestionProvider;
106
import dev.doublekekse.area_lib.Area;
117
import dev.doublekekse.area_lib.command.argument.AreaArgument;
128
import net.minecraft.ChatFormatting;
139
import net.minecraft.commands.CommandBuildContext;
1410
import net.minecraft.commands.CommandSourceStack;
1511
import net.minecraft.commands.Commands;
16-
import net.minecraft.commands.SharedSuggestionProvider;
1712
import net.minecraft.commands.arguments.ResourceLocationArgument;
18-
import net.minecraft.commands.arguments.ResourceOrTagKeyArgument;
1913
import net.minecraft.commands.synchronization.SuggestionProviders;
2014
import net.minecraft.core.Holder;
2115
import net.minecraft.network.chat.Component;
@@ -52,17 +46,6 @@ private static int setMusic(Consumer<Component> feedback, MinecraftServer server
5246
return 1;
5347
}
5448

55-
private static int setMusicCategory(Consumer<Component> feedback, MinecraftServer server, Area area, ResourceLocation tag, int minInterval, int maxInterval, boolean replaceCurrentMusic) {
56-
area.put(server, Meret.AREA_MUSIC_DATA_COMPONENT, new AreaMusicComponent(tag, minInterval, maxInterval, replaceCurrentMusic));
57-
feedback.accept(feedback(ChatFormatting.GREEN,
58-
Component.literal("music override for "),
59-
Component.literal(area.getId().toString()).withStyle(ChatFormatting.WHITE),
60-
Component.literal(" set to category "),
61-
Component.literal(tag.toString()).withStyle(ChatFormatting.WHITE)
62-
));
63-
return 1;
64-
}
65-
6649
private static int clearMusic(Consumer<Component> feedback, MinecraftServer server, Area area) {
6750
if (!area.has(Meret.AREA_MUSIC_DATA_COMPONENT)) {
6851
feedback.accept(feedback(ChatFormatting.YELLOW,
@@ -78,46 +61,26 @@ private static int clearMusic(Consumer<Component> feedback, MinecraftServer serv
7861
return 1;
7962
}
8063

81-
private static RequiredArgumentBuilder<CommandSourceStack, Integer> applyMusicArguments(Command<CommandSourceStack> executor) {
82-
return argument("minDelay", IntegerArgumentType.integer(0))
83-
.then(argument("maxDelay", IntegerArgumentType.integer(0))
84-
.then(argument("replaceCurrent", BoolArgumentType.bool())
85-
.executes(executor)
86-
)
87-
);
88-
}
89-
9064
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context, Commands.CommandSelection selection) {
9165
dispatcher.register(
9266
literal("meret")
9367
.then(literal("set")
9468
.then(argument("area", AreaArgument.area())
9569
.then(argument("sound", ResourceLocationArgument.id())
9670
.suggests(SuggestionProviders.AVAILABLE_SOUNDS)
97-
.then(
98-
applyMusicArguments(c -> setMusic(
99-
c.getSource()::sendSystemMessage,
100-
c.getSource().getServer(),
101-
AreaArgument.getArea(c, "area"),
102-
ResourceLocationArgument.getId(c, "sound"),
103-
IntegerArgumentType.getInteger(c, "minDelay"),
104-
IntegerArgumentType.getInteger(c, "maxDelay"),
105-
BoolArgumentType.getBool(c, "replaceCurrent")
106-
))
107-
)
108-
)
109-
.then(literal("category")
110-
.then(argument("category", ResourceLocationArgument.id())
111-
.then(
112-
applyMusicArguments(c -> setMusicCategory(
113-
c.getSource()::sendSystemMessage,
114-
c.getSource().getServer(),
115-
AreaArgument.getArea(c, "area"),
116-
ResourceLocationArgument.getId(c, "category"),
117-
IntegerArgumentType.getInteger(c, "minDelay"),
118-
IntegerArgumentType.getInteger(c, "maxDelay"),
119-
BoolArgumentType.getBool(c, "replaceCurrent")
120-
))
71+
.then(argument("minDelay", IntegerArgumentType.integer(0))
72+
.then(argument("maxDelay", IntegerArgumentType.integer(0))
73+
.then(argument("replaceCurrent", BoolArgumentType.bool())
74+
.executes(c -> setMusic(
75+
c.getSource()::sendSystemMessage,
76+
c.getSource().getServer(),
77+
AreaArgument.getArea(c, "area"),
78+
ResourceLocationArgument.getId(c, "sound"),
79+
IntegerArgumentType.getInteger(c, "minDelay"),
80+
IntegerArgumentType.getInteger(c, "maxDelay"),
81+
BoolArgumentType.getBool(c, "replaceCurrent")
82+
))
83+
)
12184
)
12285
)
12386
)

src/main/java/symbolics/division/meret/client/MeretClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public static Optional<Music> getOverride(LocalPlayer player) {
3131
return areaSavedData.findTrackedAreasContaining(player).stream()
3232
.filter(area -> area.has(Meret.AREA_MUSIC_DATA_COMPONENT))
3333
.max(Comparator.comparingInt(Area::getPriority))
34-
.map(area -> area.get(Meret.AREA_MUSIC_DATA_COMPONENT).getMusic(player.registryAccess(), player.getRandom()));
34+
.map(area -> area.get(Meret.AREA_MUSIC_DATA_COMPONENT).getMusic());
3535
}
3636
}

0 commit comments

Comments
 (0)