Skip to content

Commit

Permalink
Use MixinExtras injections for better mod compat
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Dec 28, 2023
1 parent 0a6aafc commit 6dd3ad9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ dependencies {
modImplementation libs.fabric.api
modImplementation libs.fabric.loader

// Why do I need this on 0.15.x ..?
modImplementation libs.mixinextras
annotationProcessor libs.mixinextras

modApi libs.modmenu
modApi libs.cloth.config
}
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[versions]
minecraft = "1.20"

mixinextras = "0.3.2"
fabric_loader = "0.15.3"

modmenu = "7.0.1"
Expand All @@ -9,6 +11,8 @@ cloth_config = "11.0.99"

[libraries]
minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" }

mixinextras = { module = "io.github.llamalad7:mixinextras-fabric", version.ref = "mixinextras" }
fabric_loader = { module = "net.fabricmc:fabric-loader", version.ref = "fabric_loader" }

modmenu = { module = "com.terraformersmc:modmenu", version.ref = "modmenu" }
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/dynamic_fps/impl/mixin/DebugScreenOverlayMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;

import dynamic_fps.impl.DynamicFPSMod;
import dynamic_fps.impl.PowerState;
import net.minecraft.client.gui.components.DebugScreenOverlay;

@Mixin(DebugScreenOverlay.class)
public class DebugScreenOverlayMixin {
/*
* Insert information about effective frame rate below misleading FPS counter.
/**
* Show the current power state and effective frame rate below the FPS counter, unless focused.
*
* As we only slow the client loop to a minimum of 15 TPS the vanilla frame rate counter is inaccurate and confusing.
*/
@Inject(method = "getGameInformation", at = @At("RETURN"))
private void onGetGameInformation(CallbackInfoReturnable<List<String>> callbackInfo) {
@ModifyReturnValue(method = "getGameInformation", at = @At("RETURN"))
private List<String> getGameInformation(List<String> result) {
var status = DynamicFPSMod.powerState();

if (status != PowerState.FOCUSED) {
var result = callbackInfo.getReturnValue();
int target = DynamicFPSMod.targetFrameRate();

result.add(2, String.format(Locale.ROOT, "§c[Dynamic FPS] FPS: %s P: %s§r", target, status.toString().toLowerCase()));
}

return result;
}
}
15 changes: 7 additions & 8 deletions src/main/java/dynamic_fps/impl/mixin/SoundEngineMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
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;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.blaze3d.audio.Listener;

import dynamic_fps.impl.DynamicFPSMod;
Expand Down Expand Up @@ -102,16 +103,14 @@ private void play(SoundInstance instance, CallbackInfo callbackInfo) {
/**
* Applies the user's requested volume multiplier to any newly played sounds.
*/
@Inject(method = "getVolume", at = @At("HEAD"), cancellable = true)
private void getVolume(@Nullable SoundSource source, CallbackInfoReturnable<Float> callbackInfo) {
float base = 1.0f;

@ModifyReturnValue(method = "getVolume", at = @At("RETURN"))
private float getVolume(float original, @Local @Nullable SoundSource source) {
// Note: The original doesn't consider the user's setting when the source is MASTER
// In vanilla this doesn't matter because it's never called, but we use it when setting the gain
if (source != null) {
base = this.options.getSoundSourceVolume(source);
if (SoundSource.MASTER.equals(source)) {
original = this.options.getSoundSourceVolume(source);
}

callbackInfo.setReturnValue(base * DynamicFPSMod.volumeMultiplier(source));
return original * DynamicFPSMod.volumeMultiplier(source);
}
}

0 comments on commit 6dd3ad9

Please sign in to comment.