From 26ae0ef0fe793143df6860ae7c2fef031811e3dd Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sat, 27 Apr 2024 22:47:28 +1000 Subject: [PATCH] Make 'Advertisements' in Language Selection Tab Actually just a message and buttons to make sure poeple know where to find the Localization Packs for Nomi-Labs and Nomi-CEu. --- .../nomiceu/nomilabs/config/LabsConfig.java | 12 ++ .../nomilabs/mixin/GuiLanguageListMixin.java | 54 ++++++ .../nomilabs/mixin/GuiLanguageMixin.java | 161 ++++++++++++++++++ .../architecturecraft/BlockShapeMixin.java | 22 ++- .../mixinhelper/AccessibleGuiLanguage.java | 8 + .../mixinhelper/GuiCustomConfirmOpenLink.java | 95 +++++++++++ .../resources/assets/nomilabs/lang/en_us.lang | 9 + src/main/resources/mixins.nomilabs.json | 2 + 8 files changed, 356 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageListMixin.java create mode 100644 src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageMixin.java create mode 100644 src/main/java/com/nomiceu/nomilabs/mixinhelper/AccessibleGuiLanguage.java create mode 100644 src/main/java/com/nomiceu/nomilabs/mixinhelper/GuiCustomConfirmOpenLink.java diff --git a/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java b/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java index 127f2976..23d99409 100644 --- a/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java +++ b/src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java @@ -423,6 +423,18 @@ public static class Advanced { @Config.RequiresMcRestart public String[] ignoreBiomes = new String[0]; + @Config.Comment({ "How to Modify the Language Tab in Minecraft Options.", + "LABS or NOMI adds buttons and text for lanugage pack download.", + "[default: LABS]" }) + @Config.LangKey("config.nomilabs.advanced.language_modify_option") + public LanguageModifyOption languageModifyOption = LanguageModifyOption.LABS; + + public enum LanguageModifyOption { + NONE, + LABS, + NOMI + } + public static class FluidRegistry { @Config.Comment({ "List of Regex Patterns to be Default Fluids.", diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageListMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageListMixin.java new file mode 100644 index 00000000..e3a86fbc --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageListMixin.java @@ -0,0 +1,54 @@ +package com.nomiceu.nomilabs.mixin; + +import java.util.List; +import java.util.Map; + +import net.minecraft.client.gui.GuiLanguage; +import net.minecraft.client.resources.Language; + +import org.spongepowered.asm.mixin.Final; +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.ModifyArg; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import com.nomiceu.nomilabs.config.LabsConfig; +import com.nomiceu.nomilabs.mixinhelper.AccessibleGuiLanguage; + +/** + * Contracts the List Section, and Stops Language Refresh on Element Clicked. + */ +@Mixin(targets = "net.minecraft.client.gui.GuiLanguage$List") +public abstract class GuiLanguageListMixin { + + @Shadow(aliases = "this$0") + @Final + GuiLanguage this$0; + + @Shadow + @Final + private List langCodeList; + + @Shadow + @Final + private Map languageMap; + + @ModifyArg(method = "(Lnet/minecraft/client/gui/GuiLanguage;Lnet/minecraft/client/Minecraft;)V", + at = @At(value = "INVOKE", + target = "net/minecraft/client/gui/GuiSlot.(Lnet/minecraft/client/Minecraft;IIIII)V"), + index = 3) + private static int getNewArgs(int top) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return top; + return top * 2; + } + + @Inject(method = "elementClicked(IZII)V", at = @At("HEAD"), cancellable = true) + protected void cancelElementClicked(int slotIndex, boolean isDoubleClick, int mouseX, int mouseY, CallbackInfo ci) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return; + Language language = languageMap.get(langCodeList.get(slotIndex)); + ((AccessibleGuiLanguage) this$0).getLanguageManager().setCurrentLanguage(language); + ci.cancel(); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageMixin.java new file mode 100644 index 00000000..826ea0d7 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageMixin.java @@ -0,0 +1,161 @@ +package com.nomiceu.nomilabs.mixin; + +import java.lang.reflect.InvocationTargetException; +import java.net.URI; +import java.net.URISyntaxException; + +import net.minecraft.client.gui.*; +import net.minecraft.client.resources.I18n; +import net.minecraft.client.resources.LanguageManager; +import net.minecraft.client.settings.GameSettings; +import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.client.resource.VanillaResourceType; +import net.minecraftforge.fml.client.FMLClientHandler; + +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.callback.CallbackInfo; + +import com.nomiceu.nomilabs.NomiLabs; +import com.nomiceu.nomilabs.config.LabsConfig; +import com.nomiceu.nomilabs.mixinhelper.AccessibleGuiLanguage; +import com.nomiceu.nomilabs.mixinhelper.GuiCustomConfirmOpenLink; + +/** + * Adds Nomi-CEu & Nomi-Labs Translation Pack 'Advertisements' to Language Selection Screen. + */ +@Mixin(GuiLanguage.class) +public abstract class GuiLanguageMixin extends GuiScreen implements AccessibleGuiLanguage { + + @Unique + private static final int DOWNLOAD_PACK_BTN_ID = 10; + + @Unique + private static final int SHOW_GH_BTN_ID = 11; + + @Unique + private static final String TRANSLATIONS_GH = "https://github.com/Nomi-CEu/Nomi-CEu-Translations"; + + @Unique + private static final String TRANSLATIONS_DOWNLOAD = "https://nightly.link/Nomi-CEu/Nomi-CEu-Translations/workflows/pushbuildpack/main?preview"; + + @Unique + private String downloadPackNote; + + @Unique + private String previousLangCode; + + @Shadow + private GuiOptionButton forceUnicodeFontBtn; + + @Shadow + private GuiOptionButton confirmSettingsBtn; + + @Shadow + @Final + private GameSettings game_settings_3; + + @Shadow + @Final + private LanguageManager languageManager; + + @Inject(method = "", at = @At("TAIL")) + public void savePreviousLang(GuiScreen screen, GameSettings gameSettingsObj, LanguageManager manager, + CallbackInfo ci) { + previousLangCode = manager.getCurrentLanguage().getLanguageCode(); + } + + @Inject(method = "initGui", at = @At("TAIL")) + public void moveAndAddElements(CallbackInfo ci) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return; + forceUnicodeFontBtn.y = height - 28; + confirmSettingsBtn.y = height - 28; + confirmSettingsBtn.displayString = I18n.format("nomilabs.gui.language.exit"); + addButton(new GuiOptionButton(DOWNLOAD_PACK_BTN_ID, width / 2 - 155 + 160, height - 56, + I18n.format("nomilabs.gui.language.download"))); + addButton(new GuiOptionButton(SHOW_GH_BTN_ID, width / 2 - 155, height - 56, + I18n.format("nomilabs.gui.language.gh"))); + downloadPackNote = LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.LABS ? + I18n.format("nomilabs.gui.language.download.note.labs") : + I18n.format("nomilabs.gui.language.download.note.nomi"); + } + + @Inject(method = "drawScreen", + at = @At(value = "INVOKE", + target = "Lnet/minecraft/client/gui/GuiLanguage$List;drawScreen(IIF)V", + shift = At.Shift.AFTER), + cancellable = true) + public void drawNewLanguageScreen(int mouseX, int mouseY, float partialTicks, CallbackInfo ci) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return; + drawCenteredString(fontRenderer, TextFormatting.BOLD + I18n.format("options.language"), width / 2, 10, + 0xffffff); + drawCenteredString(fontRenderer, + I18n.format(LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.LABS ? + "gui.nomilabs.language_pack_labs" : "gui.nomilabs.language_pack_nomi"), + width / 2, 10 + (int) (fontRenderer.FONT_HEIGHT * 2.5), 0xffffff); + drawCenteredString(fontRenderer, "(" + I18n.format("options.languageWarning") + ")", width / 2, + 10 + fontRenderer.FONT_HEIGHT * 4, 0x808080); + super.drawScreen(mouseX, mouseY, partialTicks); + ci.cancel(); + } + + @Inject(method = "actionPerformed", at = @At("HEAD"), cancellable = true) + public void handleCustomActions(GuiButton button, CallbackInfo ci) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return; + switch (button.id) { + case 6: // Done Button + var language = languageManager.getCurrentLanguage(); + var code = language.getLanguageCode(); + if (code.equals(previousLangCode)) break; + + game_settings_3.language = code; + FMLClientHandler.instance().refreshResources(VanillaResourceType.LANGUAGES); + fontRenderer + .setUnicodeFlag(languageManager.isCurrentLocaleUnicode() || game_settings_3.forceUnicodeFont); + fontRenderer.setBidiFlag(languageManager.isCurrentLanguageBidirectional()); + game_settings_3.saveOptions(); + break; + case DOWNLOAD_PACK_BTN_ID: + mc.displayGuiScreen(new GuiCustomConfirmOpenLink(this, TRANSLATIONS_DOWNLOAD, downloadPackNote, + DOWNLOAD_PACK_BTN_ID)); + ci.cancel(); + break; + case SHOW_GH_BTN_ID: + mc.displayGuiScreen(new GuiCustomConfirmOpenLink(this, TRANSLATIONS_GH, null, SHOW_GH_BTN_ID)); + ci.cancel(); + break; + } + } + + @Override + public LanguageManager getLanguageManager() { + return languageManager; + } + + @Override + public void confirmClicked(boolean result, int id) { + if (LabsConfig.advanced.languageModifyOption == LabsConfig.Advanced.LanguageModifyOption.NONE) return; + String url = switch (id) { + case DOWNLOAD_PACK_BTN_ID -> TRANSLATIONS_DOWNLOAD; + case SHOW_GH_BTN_ID -> TRANSLATIONS_GH; + default -> ""; + }; + + if (result) { + try { + Class desktopClass = Class.forName("java.awt.Desktop"); + Object desktopObj = desktopClass.getMethod("getDesktop").invoke((Object) null); + desktopClass.getMethod("browse", URI.class).invoke(desktopObj, new URI(url)); + } catch (URISyntaxException | ClassNotFoundException | InvocationTargetException | IllegalAccessException | + NoSuchMethodException e) { + NomiLabs.LOGGER.error("Failed to Open Link!"); + NomiLabs.LOGGER.throwing(e); + } + } + this.mc.displayGuiScreen(this); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/architecturecraft/BlockShapeMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/architecturecraft/BlockShapeMixin.java index bd2dea49..1464b5f9 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/architecturecraft/BlockShapeMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/architecturecraft/BlockShapeMixin.java @@ -1,9 +1,5 @@ package com.nomiceu.nomilabs.mixin.architecturecraft; -import com.elytradev.architecture.common.block.BlockArchitecture; -import com.elytradev.architecture.common.block.BlockShape; -import com.elytradev.architecture.common.tile.TileShape; -import com.llamalad7.mixinextras.sugar.Local; import net.minecraft.block.Block; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; @@ -12,17 +8,24 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; + import org.jetbrains.annotations.NotNull; 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.elytradev.architecture.common.block.BlockArchitecture; +import com.elytradev.architecture.common.block.BlockShape; +import com.elytradev.architecture.common.tile.TileShape; +import com.llamalad7.mixinextras.sugar.Local; + /** * Improves Determining Whether the Shape can be Harvested, and the Hardness of the Shape. */ @Mixin(value = BlockShape.class, remap = false) public class BlockShapeMixin extends BlockArchitecture { + /** * Default Ignored Constructor */ @@ -30,13 +33,18 @@ public BlockShapeMixin(Material material) { super(material); } - @Inject(method = "acBlockStrength", at = @At(value = "INVOKE", target = "Lcom/elytradev/architecture/common/block/BlockShape;acCanHarvestBlock(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/entity/player/EntityPlayer;)Z"), cancellable = true) - private static void baseBlockStrength(IBlockState state, EntityPlayer player, World world, BlockPos pos, CallbackInfoReturnable cir, @Local(ordinal = 1) float strength) { + @Inject(method = "acBlockStrength", + at = @At(value = "INVOKE", + target = "Lcom/elytradev/architecture/common/block/BlockShape;acCanHarvestBlock(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/entity/player/EntityPlayer;)Z"), + cancellable = true) + private static void baseBlockStrength(IBlockState state, EntityPlayer player, World world, BlockPos pos, + CallbackInfoReturnable cir, @Local(ordinal = 1) float strength) { cir.setReturnValue(state.getBlock().canHarvestBlock(world, pos, player) ? strength / 100.0F : strength / 30.0F); } /** - * Modified from {@link net.minecraftforge.common.ForgeHooks#canHarvestBlock(Block, EntityPlayer, IBlockAccess, BlockPos)} + * Modified from + * {@link net.minecraftforge.common.ForgeHooks#canHarvestBlock(Block, EntityPlayer, IBlockAccess, BlockPos)} */ @SuppressWarnings("DuplicatedCode") @Override diff --git a/src/main/java/com/nomiceu/nomilabs/mixinhelper/AccessibleGuiLanguage.java b/src/main/java/com/nomiceu/nomilabs/mixinhelper/AccessibleGuiLanguage.java new file mode 100644 index 00000000..2f612995 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixinhelper/AccessibleGuiLanguage.java @@ -0,0 +1,8 @@ +package com.nomiceu.nomilabs.mixinhelper; + +import net.minecraft.client.resources.LanguageManager; + +public interface AccessibleGuiLanguage { + + LanguageManager getLanguageManager(); +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixinhelper/GuiCustomConfirmOpenLink.java b/src/main/java/com/nomiceu/nomilabs/mixinhelper/GuiCustomConfirmOpenLink.java new file mode 100644 index 00000000..8c741ce5 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixinhelper/GuiCustomConfirmOpenLink.java @@ -0,0 +1,95 @@ +package com.nomiceu.nomilabs.mixinhelper; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.gui.GuiYesNoCallback; +import net.minecraft.client.resources.I18n; +import net.minecraft.util.text.TextFormatting; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +import org.jetbrains.annotations.Nullable; + +@SideOnly(Side.CLIENT) +public class GuiCustomConfirmOpenLink extends GuiScreen { + + private final String titleText; + + private final String confirmButtonText; + private final String cancelButtonText; + private final String copyLinkButtonText; + + private final String linkText; + + private final boolean hasNoteText; + private final String noteText; + + private final List listLines; + + private final GuiYesNoCallback parentScreen; + private final int parentButtonClickedId; + + private static final int CONFIRM_ID = 0; + private static final int COPY_ID = 2; + private static final int CANCEL_ID = 1; + + public GuiCustomConfirmOpenLink(GuiYesNoCallback parentScreen, String linkText, @Nullable String noteText, + int parentButtonClickedId) { + this.parentScreen = parentScreen; + this.parentButtonClickedId = parentButtonClickedId; + + this.titleText = TextFormatting.BOLD + I18n.format("chat.link.confirmTrusted"); + + this.confirmButtonText = I18n.format("chat.link.open"); + this.cancelButtonText = I18n.format("gui.cancel"); + this.copyLinkButtonText = I18n.format("chat.copy"); + + this.noteText = noteText; + this.hasNoteText = this.noteText != null; + + this.linkText = linkText; + this.listLines = new ArrayList<>(); + } + + public void initGui() { + super.initGui(); + buttonList.clear(); + makeButton(CONFIRM_ID, -105, confirmButtonText); + makeButton(COPY_ID, 0, copyLinkButtonText); + makeButton(CANCEL_ID, 105, cancelButtonText); + listLines.clear(); + listLines.addAll(fontRenderer.listFormattedStringToWidth(TextFormatting.AQUA + linkText + TextFormatting.RESET, + this.width - 50)); + } + + private void makeButton(int id, int xOffset, String displayText) { + buttonList.add(new GuiButton(id, width / 2 - 50 + xOffset, height / 6 + 96, 100, 20, displayText)); + } + + protected void actionPerformed(GuiButton button) throws IOException { + if (button.id == 2) + setClipboardString(linkText); + + parentScreen.confirmClicked(button.id == 0, parentButtonClickedId); + } + + public void drawScreen(int mouseX, int mouseY, float partialTicks) { + drawDefaultBackground(); + + drawCenteredString(fontRenderer, titleText, width / 2, 70 - (hasNoteText ? 20 : 0), 0xffffff); + if (hasNoteText) + drawCenteredString(fontRenderer, noteText, width / 2, 70, 0xffffff); + + int i = 90; + for (String s : listLines) { + drawCenteredString(fontRenderer, s, width / 2, i, 0xffffff); + i += fontRenderer.FONT_HEIGHT; + } + + super.drawScreen(mouseX, mouseY, partialTicks); + } +} diff --git a/src/main/resources/assets/nomilabs/lang/en_us.lang b/src/main/resources/assets/nomilabs/lang/en_us.lang index b756556b..5c59f360 100644 --- a/src/main/resources/assets/nomilabs/lang/en_us.lang +++ b/src/main/resources/assets/nomilabs/lang/en_us.lang @@ -76,6 +76,7 @@ config.nomilabs.advanced.ignore_items=ITEM Missing Registry Ignore List config.nomilabs.advanced.ignore_blocks=BLOCK Missing Registry Ignore List config.nomilabs.advanced.ignore_entities=ENTITY Missing Registry Ignore List config.nomilabs.advanced.ignore_biomes=BIOME Missing Registry Ignore List +config.nomilabs.advanced.language_modify_option=Language Tab Modifier Mode config.nomilabs.advanced.tiers=Quest Tier Detector Settings config.nomilabs.advanced.tiers.normal_qb_ids=Normal Mode Quest IDs @@ -101,6 +102,14 @@ config.nomilabs.advanced.controls_tooltips.show_class=Show Class Tooltip gui.nomilabs.recipes.tooltip.ct_recipe=CraftTweaker Recipe. gui.nomilabs.recipes.tooltip.gs_recipe=GroovyScript Recipe. +gui.nomilabs.language_pack_nomi=§bTranslation Pack for §dNomi-CEu§b Available! See Below!§r +gui.nomilabs.language_pack_labs=§bTranslation Pack for §dNomi-Labs§b Available! See Below!§r +nomilabs.gui.language.download=Download Translation Pack +nomilabs.gui.language.gh=Available Languages +nomilabs.gui.language.download.note.labs=§7Download the 'nomi-labs' Zip!§r +nomilabs.gui.language.download.note.nomi=§7Download the 'nomi-ceu' Zip!§r +nomilabs.gui.language.exit=Apply and Exit + # Custom Fluids fluid.uranium233=Uranium 233 fluid.plutonium2=Plutonium diff --git a/src/main/resources/mixins.nomilabs.json b/src/main/resources/mixins.nomilabs.json index 25d8a1fd..3ca719ea 100644 --- a/src/main/resources/mixins.nomilabs.json +++ b/src/main/resources/mixins.nomilabs.json @@ -15,6 +15,8 @@ "EntityPlayerSPMixin", "GuiKeyBindingListAccessor", "GuiKeyBindingListKeyEntryMixin", + "GuiLanguageListMixin", + "GuiLanguageMixin", "KeyBindingAccessor", "KeyBindingMixin" ],