Skip to content

Commit

Permalink
ensure load song from area
Browse files Browse the repository at this point in the history
  • Loading branch information
HamaIndustries committed Nov 26, 2024
1 parent 1371974 commit 4c98fc1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 26 deletions.
38 changes: 38 additions & 0 deletions src/client/java/symbolics/division/meret/MeretClient.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,49 @@
package symbolics.division.meret;

import dev.doublekekse.area_lib.data.AreaClientData;
import dev.doublekekse.area_lib.data.AreaSavedData;
import net.fabricmc.api.ClientModInitializer;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.sounds.Music;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.JukeboxSong;
import org.jetbrains.annotations.Nullable;

public class MeretClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
// This entrypoint is suitable for setting up client-specific logic, such as rendering.
}

@Nullable
public static Music getOverride(LocalPlayer player) {
if (player == null) return null;

AreaSavedData.IdentifiableArea potentialArea = AreaClientData.getClientLevelData()
.find(player.level(), player.position());

if (potentialArea == null) return null;

System.out.println("found area " + potentialArea.id().toString());

Registry<JukeboxSong> songRegistry = player.level().registryAccess().registry(Registries.JUKEBOX_SONG).orElse(null);

if (songRegistry == null) return null;

TagKey<JukeboxSong> musicTag = TagKey.create(
Registries.JUKEBOX_SONG,
potentialArea.id()
);

HolderSet.Named<JukeboxSong> taggedHolders = songRegistry.getOrCreateTag(musicTag);
Holder<JukeboxSong> song = taggedHolders.getRandomElement(player.getRandom()).orElse(null);
if (song == null) return null;

// vanilla game music values
return new Music(song.value().soundEvent(), 300, 300, false);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package symbolics.division.meret.mixin.client;

import com.llamalad7.mixinextras.sugar.Local;
import dev.doublekekse.area_lib.data.AreaClientData;
import dev.doublekekse.area_lib.data.AreaSavedData;
import net.minecraft.client.Minecraft;
Expand All @@ -9,7 +8,6 @@
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.Music;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.JukeboxSong;
Expand All @@ -18,38 +16,21 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;
import symbolics.division.meret.MeretClient;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Shadow public LocalPlayer player;

@Inject(
method = "getSituationalMusic",
at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/Optionull;map(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;")
at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/Optionull;map(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;"),
cancellable = true
)
public void modifyGetSituationalMusic(CallbackInfoReturnable<Music> ci) {
if (this.player == null) return;

AreaSavedData.IdentifiableArea potentialArea = AreaClientData.getClientLevelData()
.find(this.player.level(), this.player.position());

if (potentialArea == null) return;

Registry<JukeboxSong> songRegistry = this.player.level().registryAccess().registry(Registries.JUKEBOX_SONG).orElse(null);

if (songRegistry == null) return;

TagKey<JukeboxSong> musicTag = TagKey.create(
Registries.JUKEBOX_SONG,
potentialArea.id()
);

HolderSet.Named<JukeboxSong> taggedHolders = songRegistry.getOrCreateTag(musicTag);
Holder<JukeboxSong> song = taggedHolders.getRandomElement(this.player.getRandom()).orElse(null);
if (song == null) return;


Music override = MeretClient.getOverride(this.player);
if (override == null) return;
ci.setReturnValue(override);
ci.cancel();
}
}

0 comments on commit 4c98fc1

Please sign in to comment.