diff --git a/gradle.properties b/gradle.properties index 06925a2..eaa6a69 100644 --- a/gradle.properties +++ b/gradle.properties @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx2G # Project Properties -modVersion=0.1.0 +modVersion=0.1.1 mavenGroup=com.github.liachmodded archivesBaseName=kayak description=Adds some boat related stuff diff --git a/src/code/java/com/github/liachmodded/kayak/client/KayakClient.java b/src/code/java/com/github/liachmodded/kayak/client/KayakClient.java index b0f061d..490ed8b 100644 --- a/src/code/java/com/github/liachmodded/kayak/client/KayakClient.java +++ b/src/code/java/com/github/liachmodded/kayak/client/KayakClient.java @@ -8,6 +8,7 @@ import com.github.liachmodded.kayak.Kayak; import com.github.liachmodded.kayak.client.render.entity.BedBoatEntityRenderer; import com.github.liachmodded.kayak.client.render.entity.BlockCarrierBoatEntityRenderer; +import com.github.liachmodded.kayak.client.sound.EntitySpecificSoundManager; import com.github.liachmodded.kayak.client.ui.KayakScreenProviders; import com.github.liachmodded.kayak.entity.KayakEntities; import com.github.liachmodded.kayak.network.KayakNetworking; @@ -22,7 +23,21 @@ public final class KayakClient implements ClientModInitializer { + private static KayakClient INSTANCE; private KeyBinding sleepInBoatKey; + private EntitySpecificSoundManager jukeboxBoatSounds; + + public static KayakClient getInstance() { + return INSTANCE; + } + + public KayakClient() { + INSTANCE = this; + } + + public EntitySpecificSoundManager getJukeboxBoatSounds() { + return jukeboxBoatSounds; + } @Override public void onInitializeClient() { @@ -43,9 +58,11 @@ public void onInitializeClient() { sleepInBoatKey = FabricKeyBinding.Builder.create(Kayak.name("request_sleep_in_boat"), Type.KEYSYM, GLFW.GLFW_KEY_N, "general").build(); ClientTickCallback.EVENT.register(this::clientTick); + + jukeboxBoatSounds = new EntitySpecificSoundManager(); } - public void clientTick(MinecraftClient client) { + private void clientTick(MinecraftClient client) { if (sleepInBoatKey.isPressed()) { KayakNetworking.requestSleepInBoat(); } diff --git a/src/code/java/com/github/liachmodded/kayak/client/sound/EntitySpecificSoundManager.java b/src/code/java/com/github/liachmodded/kayak/client/sound/EntitySpecificSoundManager.java new file mode 100644 index 0000000..e1ba472 --- /dev/null +++ b/src/code/java/com/github/liachmodded/kayak/client/sound/EntitySpecificSoundManager.java @@ -0,0 +1,40 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ +package com.github.liachmodded.kayak.client.sound; + +import com.google.common.collect.MapMaker; +import java.util.Map; +import net.minecraft.client.MinecraftClient; +import net.minecraft.entity.Entity; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class EntitySpecificSoundManager { + + private final Map specificSounds; + + public EntitySpecificSoundManager() { + specificSounds = new MapMaker().weakKeys().weakValues().makeMap(); + } + + public void put(Entity entity, StoppableEntityTrackingSoundInstance sound) { + StoppableEntityTrackingSoundInstance previous = specificSounds.put(entity, sound); + if (previous != null) { + previous.stop(); + } + MinecraftClient.getInstance().getSoundManager().play(sound); + } + + public void remove(Entity entity) { + StoppableEntityTrackingSoundInstance previous = specificSounds.remove(entity); + if (previous != null) { + previous.stop(); + } + } + + public @Nullable StoppableEntityTrackingSoundInstance get(Entity entity) { + return specificSounds.get(entity); + } +} diff --git a/src/code/java/com/github/liachmodded/kayak/entity/JukeboxBoatEntity.java b/src/code/java/com/github/liachmodded/kayak/entity/JukeboxBoatEntity.java index 452b894..c742ad4 100644 --- a/src/code/java/com/github/liachmodded/kayak/entity/JukeboxBoatEntity.java +++ b/src/code/java/com/github/liachmodded/kayak/entity/JukeboxBoatEntity.java @@ -5,11 +5,12 @@ */ package com.github.liachmodded.kayak.entity; +import com.github.liachmodded.kayak.client.KayakClient; +import com.github.liachmodded.kayak.client.sound.EntitySpecificSoundManager; import com.github.liachmodded.kayak.client.sound.StoppableEntityTrackingSoundInstance; import com.github.liachmodded.kayak.item.KayakItems; import com.github.liachmodded.kayak.item.inventory.KayakInventoryTools; import com.github.liachmodded.kayak.stat.KayakStats; -import java.lang.ref.WeakReference; import net.fabricmc.fabric.api.util.NbtType; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; @@ -33,7 +34,6 @@ public class JukeboxBoatEntity extends CarrierBoatEntity implements Clearable { protected static final TrackedData MUSIC_DISC = DataTracker.registerData(JukeboxBoatEntity.class, TrackedDataHandlerRegistry.ITEM_STACK); - private WeakReference soundRef = new WeakReference<>(null); protected JukeboxBoatEntity(EntityType type, World world) { super(type, world); @@ -48,18 +48,17 @@ protected void initDataTracker() { @Override public void onTrackedDataSet(TrackedData data) { super.onTrackedDataSet(data); - if (!world.isClient || data != MUSIC_DISC) { - return; + if (world.isClient && data.equals(MUSIC_DISC)) { + updateMusic(); } + } - StoppableEntityTrackingSoundInstance oldSound = soundRef.get(); - if (oldSound != null) { - oldSound.stop(); - } + private void updateMusic() { + EntitySpecificSoundManager sounds = KayakClient.getInstance().getJukeboxBoatSounds(); ItemStack disc = getMusicDisc(); if (disc.isEmpty()) { - soundRef = new WeakReference<>(null); + sounds.remove(this); return; } @@ -67,8 +66,7 @@ public void onTrackedDataSet(TrackedData data) { StoppableEntityTrackingSoundInstance newSound = new StoppableEntityTrackingSoundInstance(musicDisc.getSound(), SoundCategory.RECORDS, this); MinecraftClient.getInstance().inGameHud.setRecordPlayingOverlay(musicDisc.getDescription().asFormattedString()); - MinecraftClient.getInstance().getSoundManager().play(newSound); - soundRef = new WeakReference<>(newSound); + sounds.put(this, newSound); } @Override diff --git a/src/data/java/com/github/liachmodded/kayak/data/KayakRecipesProvider.java b/src/data/java/com/github/liachmodded/kayak/data/KayakRecipesProvider.java index 3c47e47..00a19e9 100644 --- a/src/data/java/com/github/liachmodded/kayak/data/KayakRecipesProvider.java +++ b/src/data/java/com/github/liachmodded/kayak/data/KayakRecipesProvider.java @@ -21,6 +21,7 @@ import net.minecraft.advancement.criterion.EnterBlockCriterion; import net.minecraft.advancement.criterion.InventoryChangedCriterion; import net.minecraft.block.Block; +import net.minecraft.block.Blocks; import net.minecraft.data.DataCache; import net.minecraft.data.DataGenerator; import net.minecraft.data.DataProvider; @@ -66,6 +67,15 @@ private void generate(Consumer consumer) { .criterion("has_boat", has(vanillaBoat)) .offerTo(consumer); } + + for (Entry entry : KayakItems.JUKEBOX_BOAT_ITEMS.entrySet()) { + Item vanillaBoat = KayakItems.getVanillaBoat(entry.getKey()); + ShapelessRecipeJsonFactory.create(entry.getValue()) + .input(vanillaBoat) + .input(Blocks.JUKEBOX) + .criterion("has_boat", has(vanillaBoat)) + .offerTo(consumer); + } } @Override