Skip to content

Commit

Permalink
Make 'Advertisements' in Language Selection Tab
Browse files Browse the repository at this point in the history
Actually just a message and buttons to make sure poeple know where to find the Localization Packs for Nomi-Labs and Nomi-CEu.
  • Loading branch information
IntegerLimit committed Apr 27, 2024
1 parent 700bf95 commit 26ae0ef
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/config/LabsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageListMixin.java
Original file line number Diff line number Diff line change
@@ -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<String> langCodeList;

@Shadow
@Final
private Map<String, Language> languageMap;

@ModifyArg(method = "<init>(Lnet/minecraft/client/gui/GuiLanguage;Lnet/minecraft/client/Minecraft;)V",
at = @At(value = "INVOKE",
target = "net/minecraft/client/gui/GuiSlot.<init>(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();
}
}
161 changes: 161 additions & 0 deletions src/main/java/com/nomiceu/nomilabs/mixin/GuiLanguageMixin.java
Original file line number Diff line number Diff line change
@@ -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 = "<init>", 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,31 +8,43 @@
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<TileShape> {

/**
* Default Ignored Constructor
*/
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<Float> 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<Float> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nomiceu.nomilabs.mixinhelper;

import net.minecraft.client.resources.LanguageManager;

public interface AccessibleGuiLanguage {

LanguageManager getLanguageManager();
}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
Loading

0 comments on commit 26ae0ef

Please sign in to comment.