aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/isxander/yacl3/gui/utils
diff options
context:
space:
mode:
authorisxander <xander@isxander.dev>2024-04-11 18:43:06 +0100
committerisxander <xander@isxander.dev>2024-04-11 18:43:06 +0100
commit04fe933f4c24817100f3101f088accf55a621f8a (patch)
treefeff94ca3ab4484160e69a24f4ee38522381950e /src/main/java/dev/isxander/yacl3/gui/utils
parent831b894fdb7fe3e173d81387c8f6a2402b8ccfa9 (diff)
downloadYetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.gz
YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.bz2
YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.zip
Extremely fragile and broken multiversion build with stonecutter
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/gui/utils')
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/utils/ButtonTextureRenderer.java34
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java32
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java116
-rw-r--r--src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java42
4 files changed, 224 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/ButtonTextureRenderer.java b/src/main/java/dev/isxander/yacl3/gui/utils/ButtonTextureRenderer.java
new file mode 100644
index 0000000..aa52a3f
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/gui/utils/ButtonTextureRenderer.java
@@ -0,0 +1,34 @@
+package dev.isxander.yacl3.gui.utils;
+
+import com.mojang.blaze3d.systems.RenderSystem;
+import net.minecraft.client.gui.GuiGraphics;
+import net.minecraft.resources.ResourceLocation;
+
+public class ButtonTextureRenderer {
+ /*? if >1.20.1 {*/
+ private static final net.minecraft.client.gui.components.WidgetSprites SPRITES = new net.minecraft.client.gui.components.WidgetSprites(
+ new ResourceLocation("widget/button"), // normal
+ new ResourceLocation("widget/button_disabled"), // disabled & !focused
+ new ResourceLocation("widget/button_highlighted"), // !disabled & focused
+ new ResourceLocation("widget/slider_highlighted") // disabled & focused
+ );
+ /*?} else {*//*
+ private static final ResourceLocation SLIDER_LOCATION = new ResourceLocation("textures/gui/slider.png");
+ *//*?}*/
+
+ public static void render(GuiGraphics graphics, int x, int y, int width, int height, boolean enabled, boolean focused) {
+ /*? if >1.20.1 {*/
+ graphics.blitSprite(SPRITES.get(enabled, focused), x, y, width, height);
+ /*?} else {*//*
+ int textureV;
+ if (enabled) {
+ textureV = focused ? 60 : 40;
+ } else {
+ textureV = focused ? 20 : 0;
+ }
+
+ RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
+ graphics.blitNineSliced(SLIDER_LOCATION, x, y, width, height, 20, 4, 200, 20, 0, textureV);
+ *//*?}*/
+ }
+}
diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java b/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java
new file mode 100644
index 0000000..2910d0f
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/gui/utils/GuiUtils.java
@@ -0,0 +1,32 @@
+package dev.isxander.yacl3.gui.utils;
+
+import net.minecraft.client.gui.Font;
+import net.minecraft.locale.Language;
+import net.minecraft.network.chat.Component;
+import net.minecraft.network.chat.MutableComponent;
+
+public class GuiUtils {
+ public static MutableComponent translatableFallback(String key, Component fallback) {
+ if (Language.getInstance().has(key))
+ return Component.translatable(key);
+ return fallback.copy();
+ }
+
+ public static String shortenString(String string, Font font, int maxWidth, String suffix) {
+ if (string.isEmpty())
+ return string;
+
+ boolean firstIter = true;
+ while (font.width(string) > maxWidth) {
+ string = string.substring(0, Math.max(string.length() - 1 - (firstIter ? 1 : suffix.length() + 1), 0)).trim();
+ string += suffix;
+
+ if (string.equals(suffix))
+ break;
+
+ firstIter = false;
+ }
+
+ return string;
+ }
+}
diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java b/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java
new file mode 100644
index 0000000..3c4f03a
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/gui/utils/ItemRegistryHelper.java
@@ -0,0 +1,116 @@
+package dev.isxander.yacl3.gui.utils;
+
+
+import net.minecraft.ResourceLocationException;
+import net.minecraft.core.registries.BuiltInRegistries;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.world.item.Item;
+import net.minecraft.world.item.Items;
+import org.apache.commons.lang3.StringUtils;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.function.Predicate;
+import java.util.stream.Stream;
+
+public final class ItemRegistryHelper {
+
+ /**
+ * Checks whether the given string is an identifier referring to a known item
+ *
+ * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included,
+ * the default vanilla namespace "minecraft" is used.
+ * @return true if the identifier refers to a registered item, false otherwise
+ */
+ public static boolean isRegisteredItem(String identifier) {
+ try {
+ ResourceLocation itemIdentifier = new ResourceLocation(identifier.toLowerCase());
+ return BuiltInRegistries.ITEM.containsKey(itemIdentifier);
+ } catch (ResourceLocationException e) {
+ return false;
+ }
+ }
+
+ /**
+ * Looks up the item of the given identifier string.
+ *
+ * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included,
+ * the default vanilla namespace "minecraft" is used.
+ * @param defaultItem Fallback item that gets returned if the identifier does not name a registered item.
+ * @return The item identified by the given string, or the fallback if the identifier is not known.
+ */
+ public static Item getItemFromName(String identifier, Item defaultItem) {
+ try {
+ ResourceLocation itemIdentifier = new ResourceLocation(identifier.toLowerCase());
+ if (BuiltInRegistries.ITEM.containsKey(itemIdentifier)) {
+ return BuiltInRegistries.ITEM.get(itemIdentifier);
+ }
+ } catch (ResourceLocationException ignored) {
+ }
+ return defaultItem;
+ }
+
+ /**
+ * Looks up the item of the given identifier string.
+ *
+ * @param identifier Item identifier, either of the format "namespace:path" or "path". If no namespace is included,
+ * the default vanilla namespace "minecraft" is used.
+ * @return The item identified by the given string, or `Items.AIR` if the identifier is not known.
+ */
+ public static Item getItemFromName(String identifier) {
+ return getItemFromName(identifier, Items.AIR);
+ }
+
+ /**
+ * Returns a list of item identifiers matching the given string. The value matches an identifier if:
+ * <li>No namespace is provided in the value and the value is a substring of the path segment of any identifier,
+ * regardless of namespace.</li>
+ * <li>A namespace is provided, equals the identifier's namespace, and the value is the begin of the identifier's
+ * path segment.</li>
+ *
+ * @param value (partial) identifier, either of the format "namespace:path" or "path".
+ * @return list of matching item identifiers; empty if the given string does not correspond to any known identifiers
+ */
+ public static Stream<ResourceLocation> getMatchingItemIdentifiers(String value) {
+ int sep = value.indexOf(ResourceLocation.NAMESPACE_SEPARATOR);
+ Predicate<ResourceLocation> filterPredicate;
+ if (sep == -1) {
+ filterPredicate = identifier ->
+ identifier.getPath().contains(value)
+ || BuiltInRegistries.ITEM.get(identifier).getDescription().getString().toLowerCase().contains(value.toLowerCase());
+ } else {
+ String namespace = value.substring(0, sep);
+ String path = value.substring(sep + 1);
+ filterPredicate = identifier -> identifier.getNamespace().equals(namespace) && identifier.getPath().startsWith(path);
+ }
+ return BuiltInRegistries.ITEM.holders()
+ .map(holder -> holder.key().location())
+ .filter(filterPredicate)
+ /*
+ Sort items as follows based on the given "value" string's path:
+ - if both items' paths begin with the entered string, sort the identifiers (including namespace)
+ - otherwise, if either of the items' path begins with the entered string, sort it to the left
+ - else neither path matches: sort by identifiers again
+
+ This allows the user to enter "diamond_ore" and match "minecraft:diamond_ore" before
+ "minecraft:deepslate_diamond_ore", even though the second is lexicographically smaller
+ */
+ .sorted((id1, id2) -> {
+ String path = (sep == -1 ? value : value.substring(sep + 1)).toLowerCase();
+ boolean id1StartsWith = id1.getPath().toLowerCase().startsWith(path);
+ boolean id2StartsWith = id2.getPath().toLowerCase().startsWith(path);
+ if (id1StartsWith) {
+ if (id2StartsWith) {
+ return id1.compareTo(id2);
+ }
+ return -1;
+ }
+ if (id2StartsWith) {
+ return 1;
+ }
+ return id1.compareTo(id2);
+ });
+ }
+}
diff --git a/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java b/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java
new file mode 100644
index 0000000..3328c16
--- /dev/null
+++ b/src/main/java/dev/isxander/yacl3/gui/utils/UndoRedoHelper.java
@@ -0,0 +1,42 @@
+package dev.isxander.yacl3.gui.utils;
+
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class UndoRedoHelper {
+ private final List<FieldState> history = new ArrayList<>();
+ private int index = 0;
+
+ public UndoRedoHelper(String text, int cursorPos, int selectionLength) {
+ history.add(new FieldState(text, cursorPos, selectionLength));
+ }
+
+ public void save(String text, int cursorPos, int selectionLength) {
+ int max = history.size();
+ history.subList(index, max).clear();
+ history.add(new FieldState(text, cursorPos, selectionLength));
+ index++;
+ }
+
+ public @Nullable FieldState undo() {
+ index--;
+ index = Math.max(index, 0);
+
+ if (history.isEmpty())
+ return null;
+ return history.get(index);
+ }
+
+ public @Nullable FieldState redo() {
+ if (index < history.size() - 1) {
+ index++;
+ return history.get(index);
+ } else {
+ return null;
+ }
+ }
+
+ public record FieldState(String text, int cursorPos, int selectionLength) {}
+}