Skip to content

Commit

Permalink
Client side implementation of the jukebox overhaul
Browse files Browse the repository at this point in the history
Uses a mixin into the sound system to determine the sound played at the position of the jukebox, then tries to get the record item of the sound.
If it fails it will fall back to the server side implementation.
  • Loading branch information
Motschen committed Mar 28, 2021
1 parent f802e80 commit afac024
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.1

# Mod Properties
mod_version = 3.0.1
mod_version = 3.1.0
maven_group = eu.midnightdust
archives_base_name = visualoverhaul

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eu.midnightdust.visualoverhaul.VisualOverhaulClient;
import eu.midnightdust.visualoverhaul.config.VOConfig;
import eu.midnightdust.visualoverhaul.util.sound.SoundTest;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Blocks;
Expand All @@ -17,11 +18,15 @@
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.item.ItemStack;
import net.minecraft.state.property.Properties;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;

import java.util.Random;

@Environment(EnvType.CLIENT)
public class JukeboxBlockEntityRenderer extends BlockEntityRenderer<JukeboxBlockEntity> {
private ItemStack record;
private Identifier discItem;

public JukeboxBlockEntityRenderer(BlockEntityRenderDispatcher blockEntityRenderDispatcher) {
super(blockEntityRenderDispatcher);
Expand All @@ -32,7 +37,26 @@ public void render(JukeboxBlockEntity blockEntity, float tickDelta, MatrixStack

if (VOConfig.jukebox) {
int lightAbove = WorldRenderer.getLightmapCoordinates(blockEntity.getWorld(), blockEntity.getPos().up());
ItemStack record = blockEntity.getRecord();

// Gets the record sound played at the position of the jukebox //
if (SoundTest.getSound(blockEntity.getPos()) != null) {
// Converts the Sound Id to the item id of the approprieate disc (minecraft:music_disc.cat -> minecraft:music_disc_cat) //
discItem = new Identifier(String.valueOf(SoundTest.getSound(blockEntity.getPos())).replace(".", "_"));
}
// If the sound is stopped or no sound is playing, the stack is set to an empty stack //
if (SoundTest.getSound(blockEntity.getPos()) == null) {
discItem = null;
record = ItemStack.EMPTY;
}
// Tries to get the disc item from the registry //
else if (Registry.ITEM.getOrEmpty(discItem).isPresent()) {
record = new ItemStack(Registry.ITEM.get(discItem));
}
// Fallback to serverside implementation if the id doesn't match an item //
else {
record = blockEntity.getRecord();
}

record.setCount(2);

matrices.push();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package eu.midnightdust.visualoverhaul.mixin;

import eu.midnightdust.visualoverhaul.util.sound.SoundTest;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundSystem;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.math.BlockPos;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(SoundSystem.class)
public abstract class MixinSoundSystem {

@Shadow private boolean started;

private BlockPos jukeboxPos;

@Inject(at = @At("TAIL"),method = "play(Lnet/minecraft/client/sound/SoundInstance;)V")
public void onPlayRecordSound(SoundInstance soundInstance, CallbackInfo ci) {
if (soundInstance.getCategory().equals(SoundCategory.RECORDS) && this.started) {
jukeboxPos = new BlockPos(soundInstance.getX(),soundInstance.getY(),soundInstance.getZ());
SoundTest.soundPos.put(jukeboxPos, soundInstance.getId());
}
}

@Inject(at = @At("TAIL"),method = "stop(Lnet/minecraft/client/sound/SoundInstance;)V")
public void onStopRecordSound(SoundInstance soundInstance, CallbackInfo ci) {
if (soundInstance.getCategory().equals(SoundCategory.RECORDS) && SoundTest.soundPos.containsKey(new BlockPos(soundInstance.getX(),soundInstance.getY(),soundInstance.getZ()))) {
jukeboxPos = new BlockPos(soundInstance.getX(),soundInstance.getY(),soundInstance.getZ());
SoundTest.soundPos.remove(jukeboxPos,soundInstance.getId());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.midnightdust.visualoverhaul.util.sound;

import com.google.common.collect.Maps;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import java.util.Map;

public class SoundTest {

public static Map<BlockPos, Identifier> soundPos = Maps.newHashMap();

/**
* Returns the Sound provided in MixinSoundSystem
* {@link eu.midnightdust.visualoverhaul.mixin.MixinSoundSystem}
*/
public static Identifier getSound(BlockPos pos) {
if (soundPos.containsKey(pos)) {
return soundPos.get(pos);
}
return null;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/visualoverhaul.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"MixinAbstractFurnaceBlockEntity",
"MixinServerWorld"
],
"client": [
"MixinSoundSystem"
],
"injectors": {
"defaultRequire": 1
}
Expand Down

0 comments on commit afac024

Please sign in to comment.