Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Entry: disableNarratorThreadWhenUnused #28

Open
wants to merge 1 commit into
base: 1.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ repositories {
maven { url "https://maven.terraformersmc.com/releases/" }
}

loom {
accessWidenerPath = file("src/main/resources/blanket-client-tweaks.accesswidener")
}

dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:${project.minecraft_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ private static void loadMixinsSelectively() {


private static final String[] mixinTargets = {
"com/mojang/authlib/yggdrasil/YggdrasilUserApiService.class"
"com/mojang/authlib/yggdrasil/YggdrasilUserApiService.class",
"com/mojang/text2speech/NarratorWindows.class"
};

private static URL getUrl(ClassLoader loader, String file) {
Expand Down
35 changes: 34 additions & 1 deletion src/main/java/io/github/blanketmc/blanket/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@


import io.github.blanketmc.blanket.config.ConfigEntry;
import io.github.blanketmc.blanket.config.EntryListener;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.NarratorMode;
import net.minecraft.client.util.NarratorManager;

import java.util.concurrent.atomic.AtomicBoolean;

import static io.github.blanketmc.blanket.config.ConfigEntry.Category.*;

public final class Config {

public static AtomicBoolean isNarratorOptionChange = new AtomicBoolean(false);

//by KosmX
@ConfigEntry(
description = "The only internal config. Toggle how much log should blanket create",
Expand Down Expand Up @@ -81,7 +89,7 @@ public final class Config {
@ConfigEntry(
description = "Fix end crystals attempting to heal the ender dragon",
issues = "MC-187100",
categories = BUGFIX
categories = {BUGFIX, RENDER}
)
public static boolean crystalsTargetDeadDragonFix = true;

Expand All @@ -101,9 +109,34 @@ public final class Config {
)
public static boolean fixSwappedAmethystSound = true;

//by FX - PR0CESS
@ConfigEntry(
description = "Fixes the narrator thread always being loaded even thought it's not being used",
categories = {PERFORMANCE, RECOMMENDED},
listeners = onDisableNarratorThreadChange.class
)
public static boolean disableNarratorThreadWhenUnused = true;

/*

Entry Listeners

*/

private static class onDisableNarratorThreadChange extends EntryListener<Boolean> {
@Override
public Boolean onEntryChange(Boolean currentValue, Boolean newValue) {
if (currentValue != newValue) {
MinecraftClient MC = MinecraftClient.getInstance();
if (newValue && MC.options.narrator == NarratorMode.OFF) {
Config.isNarratorOptionChange.set(true);
NarratorManager.INSTANCE.destroy();
} else if (!newValue && MC.options.narrator != NarratorMode.OFF) {
disableNarratorThreadWhenUnused = false;
NarratorManager.INSTANCE.addToast(MC.options.narrator);
}
}
return newValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.github.blanketmc.blanket.mixin.narrator;

import com.mojang.text2speech.Narrator;
import io.github.blanketmc.blanket.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.NarratorMode;
import net.minecraft.client.toast.SystemToast;
import net.minecraft.client.toast.ToastManager;
import net.minecraft.client.util.NarratorManager;
import net.minecraft.text.TranslatableText;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
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.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(NarratorManager.class)
public class NarratorManager_toggleMixin {

@Mutable
@Shadow
@Final
private Narrator narrator;


@Redirect(
method = "<init>",
at = @At(
value = "INVOKE",
target = "Lcom/mojang/text2speech/Narrator;getNarrator()Lcom/mojang/text2speech/Narrator;"
)
)
private Narrator onGetNarrator() {
return Config.disableNarratorThreadWhenUnused &&
MinecraftClient.getInstance().options.narrator == NarratorMode.OFF ? null : Narrator.getNarrator();
}


@Inject(
method = "addToast(Lnet/minecraft/client/option/NarratorMode;)V",
at = @At("HEAD"),
cancellable = true
)
public void onNarratorOptionChange(NarratorMode option, CallbackInfo ci) {
if (option != NarratorMode.OFF && narrator == null) {
narrator = Narrator.getNarrator();
}
if (narrator == null) {
ToastManager toastManager = MinecraftClient.getInstance().getToastManager();
SystemToast.show(
toastManager,
SystemToast.Type.NARRATOR_TOGGLE,
new TranslatableText("narrator.toast.disabled"),
new TranslatableText("options.narrator.notavailable")
);
ci.cancel();
}
}


@Inject(
method = "isActive()Z",
at = @At("HEAD"),
cancellable = true
)
public void isActive(CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(this.narrator == null || this.narrator.active());
}


@Inject(
method = "destroy()V",
at = @At("HEAD"),
cancellable = true
)
public void onDestroy(CallbackInfo ci) {
if (this.narrator != null) {
this.narrator.destroy();
this.narrator = null;
}
ci.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.blanketmc.blanket.mixin.narrator;

import com.mojang.text2speech.NarratorWindows;
import io.github.blanketmc.blanket.Config;
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.CallbackInfo;

@Mixin(value = NarratorWindows.class, remap = false)
public class NarratorWindows_destroyMixin {


@Inject(
method = "destroy()V",
at = @At(
value = "INVOKE",
target = "Lcom/mojang/text2speech/NarratorWindows$SAPIWrapperSolutionDLL;uninit(J)V",
shift = At.Shift.BEFORE
),
cancellable = true
)
private void beforeUninit(CallbackInfo ci) {
if (Config.isNarratorOptionChange.get()) {
Config.isNarratorOptionChange.set(false);
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.blanketmc.blanket.mixin.narrator;

import io.github.blanketmc.blanket.Config;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.option.GameOptions;
import net.minecraft.client.option.NarratorMode;
import net.minecraft.client.option.Option;
import net.minecraft.client.util.NarratorManager;
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.CallbackInfo;

@Mixin(Option.class)
public class Option_toggleMixin {


@Inject(
method = "method_32553(Lnet/minecraft/client/option/GameOptions;Lnet/minecraft/client/option/Option;" +
"Lnet/minecraft/client/option/NarratorMode;)V",
at = @At("HEAD"),
cancellable = true
)
private static void onNarratorSettingChange(GameOptions options, Option option,
NarratorMode mode, CallbackInfo ci) {
if (Config.disableNarratorThreadWhenUnused) {
MinecraftClient MC = MinecraftClient.getInstance();
if (MC.options.narrator != mode) {
MC.options.narrator = mode;
if (mode == NarratorMode.OFF) {
Config.isNarratorOptionChange.set(true);
NarratorManager.INSTANCE.destroy();
ci.cancel();
} else {
NarratorManager.INSTANCE.addToast(mode);
ci.cancel();
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/blanket-client-tweaks.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
accessWidener v1 named
accessible class com/mojang/text2speech/NarratorWindows$NarratorThread;
11 changes: 8 additions & 3 deletions src/main/resources/blanket-client-tweaks.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"client": [
"clientCommands.ClientPlayerEntity_clientCommandsMixin",
"clientCommands.ClientPlayNetworkHandler_clientCommandsMixin",
"fixes.AmethystBlock_AmethystSoundFix",
"fixes.EnderDragonEntityRenderer_crystalMixin",
"fixes.Entity_clientDamageMixin",
"fixes.Entity_lavaDamageMixin",
Expand All @@ -14,10 +15,14 @@
"fixes.HandledScreen_mouseCloseMixin",
"fixes.WolfEntity_tailSpinMixin",
"fixes.YggdrasilUserApiService_syncBlockListMixin",
"performance.BiomeAccess_predictionMixin",
"fixes.AmethystBlock_AmethystSoundFix"
"narrator.NarratorManager_toggleMixin",
"performance.BiomeAccess_predictionMixin"
],
"injectors": {
"defaultRequire": 1
}
},
"mixins": [
"narrator.NarratorWindows_destroyMixin",
"narrator.Option_toggleMixin"
]
}
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"blanket-client-tweaks.mixins.json"
],

"accessWidener" : "blanket-client-tweaks.accesswidener",
"depends": {
"fabricloader": ">=0.12.12",
"fabric": "*",
Expand Down