Skip to content

Commit

Permalink
Fix the shit that didnt work
Browse files Browse the repository at this point in the history
  • Loading branch information
AliahX committed Apr 28, 2023
1 parent 435edf5 commit da3112c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 46 deletions.
10 changes: 9 additions & 1 deletion src/main/java/com/aliahx/mixtape/Mixtape.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.util.math.BlockPos;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static net.minecraft.sound.SoundCategory.MUSIC;

@Environment(EnvType.CLIENT)
Expand All @@ -33,7 +39,9 @@ public class Mixtape implements ClientModInitializer {
public static int debugMaxTimeUntilNextSong = Integer.MAX_VALUE;
public static boolean discPlaying = false;
public static float volumeScale = 1.0f;

public static Map<BlockPos, Boolean> jukeBoxesPlaying = new ConcurrentHashMap<BlockPos, Boolean>() {};
public static Map<BlockPos, Boolean> lastJukeBoxes = new ConcurrentHashMap<BlockPos, Boolean>() {};
public static Map<BlockPos, Boolean> lastLastJukeBoxes = new ConcurrentHashMap<BlockPos, Boolean>() {};
public static boolean paused = false;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.aliahx.mixtape.mixin;

import com.aliahx.mixtape.Mixtape;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.JukeboxBlockEntity;
import net.minecraft.inventory.SingleStackInventory;
import net.minecraft.item.MusicDiscItem;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(JukeboxBlockEntity.class)
public abstract class JukeboxBlockEntityMixin implements SingleStackInventory {

@Shadow private long tickCount;

@Shadow private long recordStartTick;

@Inject(method = "tick(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/block/entity/JukeboxBlockEntity;)V", at = @At("HEAD"))
private static void tickMixin(World world, BlockPos pos, BlockState state, JukeboxBlockEntity blockEntity, CallbackInfo ci) {
if(Mixtape.lastJukeBoxes.containsKey(pos)) {
if(!Mixtape.lastLastJukeBoxes.get(pos) && !Mixtape.lastJukeBoxes.get(pos) && !blockEntity.isPlayingRecord()) {
Mixtape.jukeBoxesPlaying.put(pos, false);
} else {
Mixtape.jukeBoxesPlaying.put(pos, true);
}
} else {
Mixtape.jukeBoxesPlaying.put(pos, true);
}

Mixtape.lastLastJukeBoxes.put(pos, Mixtape.lastJukeBoxes.getOrDefault(pos, false));
Mixtape.lastJukeBoxes.put(pos, blockEntity.isPlayingRecord());
}

@Inject(method = "isSongFinished", at = @At("HEAD"))
private void isSongFinishedMixin(MusicDiscItem musicDisc, CallbackInfoReturnable<Boolean> cir) {
if(tickCount >= this.recordStartTick + (long)musicDisc.getSongLengthInTicks() + 20L) {
BlockPos pos = ((JukeboxBlockEntity)(Object)this).getPos();
Mixtape.jukeBoxesPlaying.remove(pos);
Mixtape.lastJukeBoxes.remove(pos);
Mixtape.lastLastJukeBoxes.remove(pos);
}
}
}
41 changes: 16 additions & 25 deletions src/main/java/com/aliahx/mixtape/mixin/MusicTrackerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import com.aliahx.mixtape.Mixtape;
import com.aliahx.mixtape.config.ModConfig;
import me.shedaniel.autoconfig.AutoConfig;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.MusicTracker;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.sound.MusicSound;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -19,7 +16,6 @@
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static com.aliahx.mixtape.Mixtape.debugCurrentMusicType;
import static com.aliahx.mixtape.Mixtape.paused;

@Mixin(MusicTracker.class)
Expand All @@ -32,14 +28,7 @@ public class MusicTrackerMixin {
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/sound/MusicSound;getMinDelay()I"))
private int getMinDelayMixin(MusicSound musicSound) {
ModConfig config = AutoConfig.getConfigHolder(ModConfig.class).getConfig();
if(!config.mainConfig.enabled) {
return musicSound.getMinDelay();
}
if(paused) {
return Integer.MAX_VALUE;
}

return config.mainConfig.noDelayBetweenSongs ? 0 : switch (musicSound.getSound().value().getId().toString()) {
return !config.mainConfig.enabled ? musicSound.getMinDelay() : paused ? Integer.MAX_VALUE : config.mainConfig.noDelayBetweenSongs ? 0 : switch (musicSound.getSound().value().getId().toString()) {
case "minecraft:music.menu" -> config.menuConfig.minSongDelay;
case "minecraft:music.creative" -> config.creativeConfig.minSongDelay;
case "minecraft:music.end" -> config.endConfig.minSongDelay;
Expand All @@ -52,13 +41,22 @@ private int getMinDelayMixin(MusicSound musicSound) {

@Inject(at = @At("RETURN"), method = "tick")
private void tick(CallbackInfo info) {
Mixtape.debugTimeUntilNextSong = this.timeUntilNextSong;
Mixtape.debugMaxTimeUntilNextSong = this.getMaxDelayMixin(this.client.getMusicType());
Mixtape.debugNextMusicType = this.client.getMusicType().getSound().value().getId().toString();

ModConfig config = AutoConfig.getConfigHolder(ModConfig.class).getConfig();

Mixtape.debugTimeUntilNextSong = timeUntilNextSong;
Mixtape.debugMaxTimeUntilNextSong = getMaxDelayMixin(client.getMusicType());
Mixtape.debugNextMusicType = client.getMusicType().getSound().value().getId().toString();
client.getSoundManager().updateSoundVolume(SoundCategory.MUSIC, config.jukeboxConfig.turnDownMusic ? Mixtape.volumeScale : 1.0f);


Mixtape.discPlaying = false;
Mixtape.jukeBoxesPlaying.forEach((blockPos, isPlaying) -> {
if(isPlaying && (this.client.world != null && this.client.world.getBlockEntity(blockPos) != null && this.client.world.getBlockEntity(blockPos).getType() == BlockEntityType.JUKEBOX && this.client.world.isChunkLoaded(blockPos)) || config.jukeboxConfig.mono) {
assert this.client.player != null;
if(Math.sqrt(this.client.player.squaredDistanceTo(blockPos.toCenterPos())) < 64 || config.jukeboxConfig.mono) Mixtape.discPlaying = true;
}
});

if(Mixtape.discPlaying && Mixtape.volumeScale > 0.1f) {
Mixtape.volumeScale -= 0.1f;
} else if (Mixtape.discPlaying && Mixtape.volumeScale <= 0.1f) {
Expand All @@ -73,14 +71,7 @@ private void tick(CallbackInfo info) {
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/sound/MusicSound;getMaxDelay()I"))
private int getMaxDelayMixin(MusicSound musicSound) {
ModConfig config = AutoConfig.getConfigHolder(ModConfig.class).getConfig();
if(!config.mainConfig.enabled) {
return musicSound.getMaxDelay();
}
if(paused) {
return Integer.MAX_VALUE;
}

return config.mainConfig.noDelayBetweenSongs ? 0 : switch (musicSound.getSound().value().getId().toString()) {
return !config.mainConfig.enabled ? musicSound.getMaxDelay() : paused ? Integer.MAX_VALUE : config.mainConfig.noDelayBetweenSongs ? 0 : switch (musicSound.getSound().value().getId().toString()) {
case "minecraft:music.menu" -> config.menuConfig.maxSongDelay;
case "minecraft:music.creative" -> config.creativeConfig.maxSongDelay;
case "minecraft:music.end" -> config.endConfig.maxSongDelay;
Expand Down
20 changes: 7 additions & 13 deletions src/main/java/com/aliahx/mixtape/mixin/SoundSystemMixin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.aliahx.mixtape.mixin;

import net.minecraft.client.sound.Channel;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.sound.SoundListener;
import net.minecraft.client.sound.SoundSystem;
import net.minecraft.client.sound.*;
import net.minecraft.sound.SoundCategory;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -12,6 +9,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Mixin(SoundSystem.class)
Expand All @@ -31,22 +30,17 @@ private void updateSoundVolume(SoundCategory category, float volume, CallbackInf
this.listener.setVolume(volume);
} else {
this.sources.forEach((source, sourceManager) -> {
float f = this.getAdjustedVolume(source);
if(source.getCategory() == category) {
f *= volume;
}
float finalF = f;
float f = this.getAdjustedVolume(source) * (source.getCategory() == category ? volume : 1.0F);
sourceManager.run((sourcex) -> {
if (finalF <= 0.0F) {
if (f <= 0.0F) {
sourcex.stop();
} else {
sourcex.setVolume(finalF);
sourcex.setVolume(f);
}

});
});
}
}
ci.cancel();
}
}
}
17 changes: 12 additions & 5 deletions src/main/java/com/aliahx/mixtape/mixin/WorldRendererMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ private void playSongMixin(WorldRenderer instance, SoundEvent song, BlockPos son
}
this.updateEntitiesForSong(this.world, songPosition, song != null);
}

}



@Inject(method = "updateEntitiesForSong", at = @At("HEAD"), cancellable = true)
@Inject(method = "updateEntitiesForSong", at = @At("HEAD"))
private void updateEntitiesForSongMixin(World world, BlockPos pos, boolean playing, CallbackInfo ci) {
Mixtape.discPlaying = playing;
if(client.player != null) {
if (playing) {
Mixtape.jukeBoxesPlaying.put(pos, true);
Mixtape.lastJukeBoxes.put(pos, true);
Mixtape.lastLastJukeBoxes.put(pos, true);
} else {
Mixtape.jukeBoxesPlaying.remove(pos);
Mixtape.lastJukeBoxes.remove(pos);
Mixtape.lastLastJukeBoxes.remove(pos);
}
}
}
}
5 changes: 3 additions & 2 deletions src/main/resources/Mixtape.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
"client": [
"DebugHudMixin",
"MinecraftClientMixin",
"MusicDiscItemMixin",
"MusicTrackerMixin",
"OptionsScreenMixin",
"PositionedSoundInstanceMixin",
"SoundSystemMixin",
"WorldRendererMixin",
"MusicDiscItemMixin",
"SoundSystemMixin"
"JukeboxBlockEntityMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit da3112c

Please sign in to comment.