Skip to content

Commit

Permalink
Fix jukebox boat
Browse files Browse the repository at this point in the history
Signed-off-by: liach <liach@users.noreply.github.com>
  • Loading branch information
liach committed Dec 1, 2019
1 parent f2dc2e0 commit 92618dc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Entity, StoppableEntityTrackingSoundInstance> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,6 @@
public class JukeboxBoatEntity extends CarrierBoatEntity implements Clearable {

protected static final TrackedData<ItemStack> MUSIC_DISC = DataTracker.registerData(JukeboxBoatEntity.class, TrackedDataHandlerRegistry.ITEM_STACK);
private WeakReference<StoppableEntityTrackingSoundInstance> soundRef = new WeakReference<>(null);

protected JukeboxBoatEntity(EntityType<? extends BoatEntity> type, World world) {
super(type, world);
Expand All @@ -48,27 +48,25 @@ 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;
}

MusicDiscItem musicDisc = (MusicDiscItem) disc.getItem();
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,6 +67,15 @@ private void generate(Consumer<RecipeJsonProvider> consumer) {
.criterion("has_boat", has(vanillaBoat))
.offerTo(consumer);
}

for (Entry<BoatEntity.Type, Item> 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
Expand Down

0 comments on commit 92618dc

Please sign in to comment.