aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
diff options
context:
space:
mode:
authorKevin <92656833+kevinthegreat1@users.noreply.github.com>2024-07-14 21:35:52 +0800
committerGitHub <noreply@github.com>2024-07-14 21:35:52 +0800
commitf34b2aae6245325f293a56f190cda82895aa85f2 (patch)
tree7dffa560205d6d526c38d67aee75fec60f4dad77 /src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
parent1c25c8dcabc2684ebc829a7cfaa50fd7f9e4c184 (diff)
parentbc46261cf69d7db0e8eae50485c6c9fed2c64045 (diff)
downloadSkyblocker-f34b2aae6245325f293a56f190cda82895aa85f2.tar.gz
Skyblocker-f34b2aae6245325f293a56f190cda82895aa85f2.tar.bz2
Skyblocker-f34b2aae6245325f293a56f190cda82895aa85f2.zip
Merge pull request #728 from Emirlol/bazaar-highlight
Bazaar Helper
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils/ItemUtils.java')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/ItemUtils.java47
1 files changed, 36 insertions, 11 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
index e43ce783..65807886 100644
--- a/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/ItemUtils.java
@@ -43,7 +43,7 @@ import java.util.regex.Pattern;
import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal;
-public class ItemUtils {
+public final class ItemUtils {
public static final String ID = "id";
public static final String UUID = "uuid";
public static final Pattern NOT_DURABILITY = Pattern.compile("[^0-9 /]");
@@ -55,6 +55,8 @@ public class ItemUtils {
ComponentChanges.CODEC.optionalFieldOf("components", ComponentChanges.EMPTY).forGetter(ItemStack::getComponentChanges)
).apply(instance, ItemStack::new)));
+ private ItemUtils() {}
+
public static LiteralArgumentBuilder<FabricClientCommandSource> 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())));
@@ -198,9 +200,13 @@ 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<String> predicate) {
- for (Text line : getLore(item)) {
+ public static String getLoreLineIf(ItemStack stack, Predicate<String> predicate) {
+ for (Text line : getLore(stack)) {
String string = line.getString();
if (predicate.test(string)) {
return string;
@@ -210,21 +216,40 @@ 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) {
- for (Text line : getLore(item)) {
- String string = line.getString();
- Matcher matcher = pattern.matcher(string);
- if (matcher.matches()) {
+ public static Matcher getLoreLineIfMatch(ItemStack stack, Pattern pattern) {
+ Matcher matcher = pattern.matcher("");
+ for (Text line : getLore(stack)) {
+ if (matcher.reset(line.getString()).matches()) {
return matcher;
}
}
-
return null;
}
- public static @NotNull List<Text> getLore(ItemStack item) {
- return item.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines();
+ /**
+ * Gets the first line of the lore that matches the specified pattern, using {@link Matcher#find()}.
+ * @param pattern the pattern to search for
+ * @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 stack, Pattern pattern) {
+ Matcher matcher = pattern.matcher("");
+ for (Text line : getLore(stack)) {
+ if (matcher.reset(line.getString()).find()) {
+ return matcher;
+ }
+ }
+ return null;
+ }
+
+ public static @NotNull List<Text> getLore(ItemStack stack) {
+ return stack.getOrDefault(DataComponentTypes.LORE, LoreComponent.DEFAULT).styledLines();
}
public static @NotNull PropertyMap propertyMapWithTexture(String textureValue) {