From 26af94886e964b5f83a26bf4a6866fd1553d0d79 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 13 Jun 2024 20:58:28 +0300 Subject: Add reorder helper that copies the number of items to clipboard --- src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/main/java/de/hysky/skyblocker/utils/ItemUtils.java') diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index e43ce783..ff6daad2 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -223,6 +223,19 @@ public class ItemUtils { return null; } + @Nullable + public static Matcher getLoreLineIfContainsMatch(ItemStack item, Pattern pattern) { + for (Text line : getLore(item)) { + String string = line.getString(); + Matcher matcher = pattern.matcher(string); + if (matcher.find()) { + return matcher; + } + } + + return null; + } + public static @NotNull List getLore(ItemStack item) { return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); } -- cgit From 478451fdc0a24b1b3616ca1d43c19fc5b90cfe70 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Thu, 27 Jun 2024 16:14:40 +0300 Subject: Add some javadocs for ItemUtils and simplify getLoreLine methods --- .../java/de/hysky/skyblocker/utils/ItemUtils.java | 29 +++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/ItemUtils.java') diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index ff6daad2..b8e99633 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -43,7 +43,8 @@ import java.util.regex.Pattern; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; -public class ItemUtils { +public final class ItemUtils { + private ItemUtils() {} public static final String ID = "id"; public static final String UUID = "uuid"; public static final Pattern NOT_DURABILITY = Pattern.compile("[^0-9 /]"); @@ -198,6 +199,10 @@ public class ItemUtils { return null; } + /** + * Gets the first line of the lore that matches the specified predicate. + * @return The first line of the lore that matches the predicate, or {@code null} if no line matches. + */ @Nullable public static String getLoreLineIf(ItemStack item, Predicate predicate) { for (Text line : getLore(item)) { @@ -210,29 +215,35 @@ public class ItemUtils { return null; } + /** + * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#matches()}. + * @return A matcher that contains match results if the pattern was found in the lore, otherwise {@code null}. + */ @Nullable public static Matcher getLoreLineIfMatch(ItemStack item, Pattern pattern) { + Matcher matcher = pattern.matcher(""); for (Text line : getLore(item)) { - String string = line.getString(); - Matcher matcher = pattern.matcher(string); - if (matcher.matches()) { + if (matcher.reset(line.getString()).matches()) { return matcher; } } - return null; } + /** + * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#find()}. + * @param pattern the pattern to search for + * @param item the item to search the lore of + * @return A {@link Matcher matcher} that contains match results if the pattern was found in the lore, otherwise {@code null}. + */ @Nullable public static Matcher getLoreLineIfContainsMatch(ItemStack item, Pattern pattern) { + Matcher matcher = pattern.matcher(""); for (Text line : getLore(item)) { - String string = line.getString(); - Matcher matcher = pattern.matcher(string); - if (matcher.find()) { + if (matcher.reset(line.getString()).find()) { return matcher; } } - return null; } -- cgit From 30b73a0557437c86a20f89b65eb878b850e207f7 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Sun, 14 Jul 2024 05:56:33 +0300 Subject: Refactor ItemUtils constructor to be sandwiched between fields and methods --- src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/ItemUtils.java') diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index b8e99633..5dc51089 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -44,7 +44,6 @@ import java.util.regex.Pattern; import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; public final class ItemUtils { - private ItemUtils() {} public static final String ID = "id"; public static final String UUID = "uuid"; public static final Pattern NOT_DURABILITY = Pattern.compile("[^0-9 /]"); @@ -56,6 +55,8 @@ public final class ItemUtils { ComponentChanges.CODEC.optionalFieldOf("components", ComponentChanges.EMPTY).forGetter(ItemStack::getComponentChanges) ).apply(instance, ItemStack::new))); + private ItemUtils() {} + public static LiteralArgumentBuilder dumpHeldItemCommand() { return literal("dumpHeldItem").executes(context -> { context.getSource().sendFeedback(Text.literal("[Skyblocker Debug] Held Item: " + SkyblockerMod.GSON_COMPACT.toJson(ItemStack.CODEC.encodeStart(JsonOps.INSTANCE, context.getSource().getPlayer().getMainHandStack()).getOrThrow()))); -- cgit From 3f1b9d9842018c905b29a763e018916ee3dcabe3 Mon Sep 17 00:00:00 2001 From: Rime <81419447+Emirlol@users.noreply.github.com> Date: Sun, 14 Jul 2024 05:57:48 +0300 Subject: Rename all `ItemStack` parameters in `ItemUtils` from `item` into `stack` --- src/main/java/de/hysky/skyblocker/utils/ItemUtils.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/utils/ItemUtils.java') diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java index 5dc51089..65807886 100644 --- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java +++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java @@ -205,8 +205,8 @@ public final class ItemUtils { * @return The first line of the lore that matches the predicate, or {@code null} if no line matches. */ @Nullable - public static String getLoreLineIf(ItemStack item, Predicate predicate) { - for (Text line : getLore(item)) { + public static String getLoreLineIf(ItemStack stack, Predicate predicate) { + for (Text line : getLore(stack)) { String string = line.getString(); if (predicate.test(string)) { return string; @@ -221,9 +221,9 @@ public final class ItemUtils { * @return A matcher that contains match results if the pattern was found in the lore, otherwise {@code null}. */ @Nullable - public static Matcher getLoreLineIfMatch(ItemStack item, Pattern pattern) { + public static Matcher getLoreLineIfMatch(ItemStack stack, Pattern pattern) { Matcher matcher = pattern.matcher(""); - for (Text line : getLore(item)) { + for (Text line : getLore(stack)) { if (matcher.reset(line.getString()).matches()) { return matcher; } @@ -234,13 +234,13 @@ public final class ItemUtils { /** * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#find()}. * @param pattern the pattern to search for - * @param item the item to search the lore of + * @param stack the stack to search the lore of * @return A {@link Matcher matcher} that contains match results if the pattern was found in the lore, otherwise {@code null}. */ @Nullable - public static Matcher getLoreLineIfContainsMatch(ItemStack item, Pattern pattern) { + public static Matcher getLoreLineIfContainsMatch(ItemStack stack, Pattern pattern) { Matcher matcher = pattern.matcher(""); - for (Text line : getLore(item)) { + for (Text line : getLore(stack)) { if (matcher.reset(line.getString()).find()) { return matcher; } @@ -248,8 +248,8 @@ public final class ItemUtils { return null; } - public static @NotNull List getLore(ItemStack item) { - return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); + public static @NotNull List getLore(ItemStack stack) { + return stack.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines(); } public static @NotNull PropertyMap propertyMapWithTexture(String textureValue) { -- cgit