Skip to content

Commit

Permalink
Merge branch '1.19.3' into 1.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerozgen committed Mar 13, 2023
2 parents 38b34c5 + 868c0cb commit 1a20768
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 196 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'fabric-loom' version '1.0-SNAPSHOT'
id 'io.github.juuxel.loom-quiltflower' version '1.7.4'
id 'fabric-loom' version '1.1.9'
id 'io.github.juuxel.loom-quiltflower' version '1.8.0'
id 'maven-publish'
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loader_version=0.14.12
fabric_version=0.72.0+1.19.2

# Mod Properties
mod_version=1.5.4+1.19.2
mod_version=1.5.5+1.19.2
maven_group=jerozgen
archives_base_name=language-reload

Expand Down
15 changes: 9 additions & 6 deletions src/main/java/jerozgen/languagereload/LanguageReload.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.advancement.AdvancementsScreen;
import net.minecraft.client.gui.screen.ingame.BookScreen;
import net.minecraft.client.resource.language.LanguageDefinition;
import net.minecraft.client.resource.language.LanguageManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -59,23 +59,26 @@ public static void reloadSearch() {
((MinecraftClientAccessor) client).getSearchManager().reload(client.getResourceManager());
}

public static void setLanguage(LanguageDefinition language, LinkedList<String> fallbacks) {
public static void setLanguage(String language, LinkedList<String> fallbacks) {
var client = MinecraftClient.getInstance();
var languageManager = client.getLanguageManager();
var config = Config.getInstance();

var languageIsSame = languageManager.getLanguage().equals(language);
var languageIsSame = languageManager.getLanguage().getCode().equals(language);
var fallbacksAreSame = config.fallbacks.equals(fallbacks);
if (languageIsSame && fallbacksAreSame) return;

if (languageManager.getLanguage(language) == null)
language = LanguageManager.DEFAULT_LANGUAGE_CODE;

config.previousLanguage = languageManager.getLanguage().getCode();
config.previousFallbacks = config.fallbacks;
config.language = language.getCode();
config.language = language;
config.fallbacks = fallbacks;
Config.save();

languageManager.setLanguage(language);
client.options.language = language.getCode();
languageManager.setLanguage(languageManager.getLanguage(language));
client.options.language = language;
client.options.write();

reloadLanguages();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package jerozgen.languagereload.access;

import jerozgen.languagereload.gui.LanguageEntry;

public interface ILanguageOptionsScreen {
void focusEntry(LanguageEntry entry);
}
101 changes: 59 additions & 42 deletions src/main/java/jerozgen/languagereload/gui/LanguageEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,111 @@
import jerozgen.languagereload.LanguageReload;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.screen.narration.NarrationPart;
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import net.minecraft.client.resource.language.LanguageDefinition;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public abstract class LanguageEntry extends ElementListWidget.Entry<LanguageEntry> {
public abstract class LanguageEntry extends AlwaysSelectedEntryListWidget.Entry<LanguageEntry> {
protected static final Identifier TEXTURE = new Identifier(LanguageReload.MOD_ID, "textures/gui/language_selection.png");
protected static final int TEXTURE_WIDTH = 64;
protected static final int TEXTURE_HEIGHT = 64;
protected static final int HOVERED_V_OFFSET = 24;

private final static int SCROLLBAR_WIDTH = 6;
private final static int LEFT_MARGIN = 2;
private final static int ENTRY_HEIGHT = 24;

protected final MinecraftClient client = MinecraftClient.getInstance();

protected final String code;
protected final LanguageDefinition language;
protected final LinkedList<LanguageDefinition> selectedLanguages;
protected final LinkedList<String> selectedLanguages;
protected final Runnable refreshListsAction;

private final List<ClickableWidget> buttons = new ArrayList<>();

public LanguageEntry(Runnable refreshListsAction, LanguageDefinition language, LinkedList<LanguageDefinition> selectedLanguages) {
protected LanguageListWidget parentList;

public LanguageEntry(Runnable refreshListsAction, String code, LanguageDefinition language, LinkedList<String> selectedLanguages) {
this.code = code;
this.language = language;
this.selectedLanguages = selectedLanguages;
this.refreshListsAction = refreshListsAction;
}

protected ButtonWidget addChild(ButtonWidget button) {
protected ButtonWidget addButton(int width, int height, int u, int v, ButtonWidget.PressAction action) {
return addButton(new TexturedButtonWidget(0, 0, width, height, u, v, HOVERED_V_OFFSET, TEXTURE, TEXTURE_WIDTH, TEXTURE_HEIGHT, action));
}

protected ButtonWidget addButton(TexturedButtonWidget button) {
button.visible = false;
buttons.add(button);
return button;
}

protected abstract void renderButtons(ButtonRenderer buttonRenderer, int top, int left);

public LanguageDefinition getLanguage() {
return language;
public boolean isFocused() {
return parentList.getSelectedOrNull() == this && parentList.isFocused();
}

public void toggle() {}

public void moveUp() {}

public void moveDown() {}

protected abstract void renderButtons(ButtonRenderer buttonRenderer, int x, int y);

@Override
public void render(MatrixStack matrices, int index, int top, int left, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
if (client.options.getTouchscreen().getValue() || hovered) {
if (hovered) {
var right = left + entryWidth - SCROLLBAR_WIDTH - LEFT_MARGIN;
DrawableHelper.fill(matrices, left, top, right, top + ENTRY_HEIGHT, 0xA0909090);
}
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
x -= 2;
y -= 2;
if (hovered || isFocused() || client.options.getTouchscreen().getValue()) {
DrawableHelper.fill(matrices,
x + 1, y + 1, x + entryWidth - 1, y + entryHeight + 3,
(hovered || isFocused()) ? 0xA0909090 : 0x50909090);
buttons.forEach(button -> button.visible = false);
ButtonRenderer buttonRenderer = (button, x, y) -> {
button.x = x;
button.y = y;
renderButtons((button, buttonX, buttonY) -> {
button.x = buttonX;
button.y = buttonY;
button.visible = true;
button.render(matrices, mouseX, mouseY, tickDelta);
};
renderButtons(buttonRenderer, top, left);
}, x, y);
}
client.textRenderer.drawWithShadow(matrices, language.getName(), left + 29, top + 3, 0xFFFFFF);
client.textRenderer.drawWithShadow(matrices, language.getRegion(), left + 29, top + 14, 0x808080);
client.textRenderer.drawWithShadow(matrices, language.getName(), x + 29, y + 3, 0xFFFFFF);
client.textRenderer.drawWithShadow(matrices, language.getRegion(), x + 29, y + 14, 0x808080);
}

public void appendNarrations(NarrationMessageBuilder builder) {
builder.put(NarrationPart.TITLE, Text.translatable("narrator.select", language));
@Override
public Text getNarration() {
return Text.translatable("narrator.select", language);
}

@Override
public boolean changeFocus(boolean lookForwards) {
public boolean mouseClicked(double mouseX, double mouseY, int button) {
for (var widget : buttons)
if (widget.mouseClicked(mouseX, mouseY, button)) {
return true;
}
return false;
}

@Override
public List<? extends Element> children() {
return buttons;
public void setParent(LanguageListWidget list) {
this.parentList = list;
}

@Override
public List<? extends Selectable> selectableChildren() {
return buttons;
public LanguageListWidget getParent() {
return parentList;
}

public String getCode() {
return code;
}

public LanguageDefinition getLanguage() {
return language;
}

@FunctionalInterface
Expand Down
92 changes: 78 additions & 14 deletions src/main/java/jerozgen/languagereload/gui/LanguageListWidget.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package jerozgen.languagereload.gui;


import jerozgen.languagereload.access.ILanguageOptionsScreen;
import jerozgen.languagereload.mixin.EntryListWidgetAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.client.gui.ParentElement;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.option.LanguageOptionsScreen;
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;

public class LanguageListWidget extends ElementListWidget<LanguageEntry> {
import static org.lwjgl.glfw.GLFW.*;

public class LanguageListWidget extends AlwaysSelectedEntryListWidget<LanguageEntry> {
private final Text title;
private final LanguageOptionsScreen screen;

public LanguageListWidget(MinecraftClient client, int width, int height, Text title) {
public LanguageListWidget(MinecraftClient client, LanguageOptionsScreen screen, int width, int height, Text title) {
super(client, width, height, 48, height - 55 + 4, 24);
this.title = title;
this.screen = screen;

setRenderHeader(true, (int) (9.0f * 1.5f));
setRenderHeader(true, (int) (9f * 1.5f));
centerListVertically = false;
}

Expand All @@ -29,8 +39,67 @@ protected void renderHeader(MatrixStack matrices, int x, int y, Tessellator tess
}

@Override
public int getRowTop(int index) {
return super.getRowTop(index);
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
var selectedEntry = this.getSelectedOrNull();
if (selectedEntry == null) return super.keyPressed(keyCode, scanCode, modifiers);

if (keyCode == GLFW_KEY_SPACE || keyCode == GLFW_KEY_ENTER) {
selectedEntry.toggle();
this.setFocused(null);
((ILanguageOptionsScreen) screen).focusEntry(selectedEntry);
return true;
}

if (Screen.hasShiftDown()) {
if (keyCode == GLFW_KEY_DOWN) {
selectedEntry.moveDown();
return true;
}
if (keyCode == GLFW_KEY_UP) {
selectedEntry.moveUp();
return true;
}
}

return super.keyPressed(keyCode, scanCode, modifiers);
}

// Remove focusing on entry click
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
this.updateScrollingState(mouseX, mouseY, button);
if (!this.isMouseOver(mouseX, mouseY)) return false;

var entry = this.getEntryAtPosition(mouseX, mouseY);
if (entry == null && button == 0) return true;

if (entry != null && entry.mouseClicked(mouseX, mouseY, button)) {
var focusedEntry = this.getFocused();
if (focusedEntry != entry && focusedEntry instanceof ParentElement parentElement)
parentElement.setFocused(null);
this.setDragging(true);
return true;
}

return ((EntryListWidgetAccessor) this).isScrolling();
}

@Override
@Nullable
protected LanguageEntry getEntryAtPosition(double x, double y) {
int halfRowWidth = this.getRowWidth() / 2;
int center = left + width / 2;
int minX = center - halfRowWidth;
int maxX = center + halfRowWidth;
var scrollbarPositionX = this.getScrollbarPositionX();
int m = MathHelper.floor(y - top) - headerHeight + (int) this.getScrollAmount() - 4 + 2;
int entryIndex = m / itemHeight;
return (x < scrollbarPositionX && x >= minX && x <= maxX && entryIndex >= 0 && m >= 0
&& entryIndex < this.getEntryCount() ? this.children().get(entryIndex) : null);
}

public LanguageOptionsScreen getScreen() {
return screen;
}

@Override
Expand All @@ -43,12 +112,7 @@ protected int getScrollbarPositionX() {
return right - 6;
}

@Override
public void appendNarrations(NarrationMessageBuilder builder) {
var hoveredEntry = getHoveredEntry();
if (hoveredEntry != null) {
appendNarrations(builder.nextMessage(), hoveredEntry);
hoveredEntry.appendNarrations(builder);
}
protected boolean isFocused() {
return screen.getFocused() == this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import java.util.function.Consumer;

public class LockedLanguageEntry extends LanguageEntry {
private final Text LOCK_BUTTON_TOOLTIP = Text.translatable("language.default.tooltip");
private static final Text LOCK_BUTTON_TOOLTIP = Text.translatable("language.default.tooltip");

private final ButtonWidget lockButton = addChild(new TexturedButtonWidget(0, 0, 16, 24, 43, 0, 0,
private final ButtonWidget lockButton = addButton(new TexturedButtonWidget(0, 0, 16, 24, 43, 0, 0,
TEXTURE, TEXTURE_WIDTH, TEXTURE_HEIGHT, __ -> {}, new ButtonWidget.TooltipSupplier() {
@Override
public void onTooltip(ButtonWidget button, MatrixStack matrices, int mouseX, int mouseY) {
Expand All @@ -28,13 +28,13 @@ public void supply(Consumer<Text> consumer) {
}
}, ScreenTexts.EMPTY));

public LockedLanguageEntry(Runnable refreshListsAction, LanguageDefinition language, LinkedList<LanguageDefinition> selectedLanguages) {
super(refreshListsAction, language, selectedLanguages);
public LockedLanguageEntry(Runnable refreshListsAction, String code, LanguageDefinition language, LinkedList<String> selectedLanguages) {
super(refreshListsAction, code, language, selectedLanguages);
lockButton.active = false;
}

@Override
protected void renderButtons(ButtonRenderer buttonRenderer, int top, int left) {
buttonRenderer.render(lockButton, left + 6, top);
protected void renderButtons(ButtonRenderer renderer, int x, int y) {
renderer.render(lockButton, x + 6, y);
}
}
Loading

0 comments on commit 1a20768

Please sign in to comment.