-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Mod Menu translations on (Neo)Forge
Unlike Fabric's ModMenu, Forge doesn't natively support mod info translations. I worked around this using a couple of forge-specific mixins.
- Loading branch information
1 parent
6e67fbb
commit 754bae1
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
neoforge/src/main/java/net/xolt/freecam/forge/mixins/ModListEntryMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package net.xolt.freecam.forge.mixins; | ||
|
||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.MutableComponent; | ||
import net.neoforged.neoforge.client.gui.widget.ModListWidget; | ||
import net.neoforged.neoforgespi.language.IModInfo; | ||
import org.spongepowered.asm.mixin.Final; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Shadow; | ||
import org.spongepowered.asm.mixin.Unique; | ||
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.CallbackInfoReturnable; | ||
|
||
import static net.xolt.freecam.Freecam.MOD_ID; | ||
|
||
/** | ||
* Enable translations in forge's mod list | ||
*/ | ||
@Mixin(ModListWidget.ModEntry.class) | ||
public class ModListEntryMixin { | ||
|
||
@Shadow @Final private IModInfo modInfo; | ||
|
||
@Unique | ||
private static MutableComponent freecam$displayName() { | ||
return Component.translatable("freecam.name"); | ||
} | ||
|
||
@Redirect(method = "render", at = @At(value = "INVOKE", ordinal = 0, target = "Lnet/minecraft/network/chat/Component;literal(Ljava/lang/String;)Lnet/minecraft/network/chat/MutableComponent;")) | ||
MutableComponent setNameToRender(String arg) { | ||
if (MOD_ID.equals(modInfo.getModId())) { | ||
return freecam$displayName(); | ||
} | ||
return Component.literal(arg); | ||
} | ||
|
||
@Inject(method = "getNarration", at = @At(value = "HEAD"), cancellable = true) | ||
void getNarration(CallbackInfoReturnable<Component> cir) { | ||
if (MOD_ID.equals(modInfo.getModId())) { | ||
cir.setReturnValue(Component.translatable("narrator.select", freecam$displayName())); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
neoforge/src/main/java/net/xolt/freecam/forge/mixins/ModListScreenMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package net.xolt.freecam.forge.mixins; | ||
|
||
import net.minecraft.locale.Language; | ||
import net.neoforged.neoforge.client.gui.ModListScreen; | ||
import net.neoforged.neoforgespi.language.IModInfo; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Redirect; | ||
|
||
import static net.xolt.freecam.Freecam.MOD_ID; | ||
|
||
/** | ||
* Enable translations in forge's mod info panel | ||
*/ | ||
@Mixin(ModListScreen.class) | ||
public abstract class ModListScreenMixin { | ||
|
||
@Unique | ||
private String freecam$translate(String key, String fallback) { | ||
return Language.getInstance().getOrDefault(key, fallback); | ||
} | ||
|
||
@Redirect(method = "updateCache", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforgespi/language/IModInfo;getDisplayName()Ljava/lang/String;")) | ||
String getName(IModInfo instance) { | ||
if (MOD_ID.equals(instance.getModId())) { | ||
return freecam$translate("freecam.name", instance.getDisplayName()); | ||
} | ||
return instance.getDisplayName(); | ||
} | ||
|
||
@Redirect(method = "updateCache", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforgespi/language/IModInfo;getDescription()Ljava/lang/String;")) | ||
String getDescription(IModInfo instance) { | ||
if (MOD_ID.equals(instance.getModId())) { | ||
return freecam$translate("freecam.description", instance.getDescription()); | ||
} | ||
return instance.getDescription(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters