aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Hilyard <anthony.hilyard@gmail.com>2021-11-01 12:55:57 -0700
committerAnthony Hilyard <anthony.hilyard@gmail.com>2021-11-01 12:55:57 -0700
commit1ac3356e7fb70f2d09144498c73ff67e79b43c96 (patch)
tree3bd554526167f86cf94f2f47343b05ebac8823fb
parent95f63bb1d6ba39a01a7124626c051130640d39c4 (diff)
downloadIceberg-1ac3356e7fb70f2d09144498c73ff67e79b43c96.tar.gz
Iceberg-1ac3356e7fb70f2d09144498c73ff67e79b43c96.tar.bz2
Iceberg-1ac3356e7fb70f2d09144498c73ff67e79b43c96.zip
Consolidated item selector logic.
-rw-r--r--gradle.properties2
-rw-r--r--src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java89
2 files changed, 90 insertions, 1 deletions
diff --git a/gradle.properties b/gradle.properties
index 119e665..739b432 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -6,7 +6,7 @@ org.gradle.daemon=false
name=${rootProject.name}
group=com.anthonyhilyard.${name.toLowerCase()}
author=anthonyhilyard
-version=1.0.21
+version=1.0.22
mcVersion=1.17.1
fabricVersion=0.39.2+1.17
diff --git a/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java
new file mode 100644
index 0000000..04645db
--- /dev/null
+++ b/src/main/java/com/anthonyhilyard/iceberg/util/Selectors.java
@@ -0,0 +1,89 @@
+package com.anthonyhilyard.iceberg.util;
+
+import net.minecraft.core.Registry;
+import net.minecraft.world.item.ItemStack;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.network.chat.Component;
+import net.minecraft.network.chat.TextColor;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.tags.ItemTags;
+import net.minecraft.world.item.Rarity;
+import net.minecraft.world.item.TooltipFlag;
+
+public class Selectors
+{
+ private static Map<String, Rarity> rarities = new HashMap<String, Rarity>() {{
+ put("common", Rarity.COMMON);
+ put("uncommon", Rarity.UNCOMMON);
+ put("rare", Rarity.RARE);
+ put("epic", Rarity.EPIC);
+ }};
+
+ public static boolean itemMatches(ItemStack item, String selector)
+ {
+ String itemResourceLocation = Registry.ITEM.getKey(item.getItem()).toString();
+ if (selector.equals(itemResourceLocation) || selector.equals(itemResourceLocation.replace("minecraft:", "")))
+ {
+ return true;
+ }
+ else if (selector.startsWith("#"))
+ {
+ TextColor entryColor = TextColor.parseColor(selector);
+ if (entryColor.equals(ItemColor.getColorForItem(item, TextColor.fromRgb(0xFFFFFF))))
+ {
+ return true;
+ }
+ }
+ else if (selector.startsWith("!"))
+ {
+ if (item.getRarity() == rarities.get(selector.substring(1)))
+ {
+ return true;
+ }
+ }
+ else if (selector.startsWith("@"))
+ {
+ if (itemResourceLocation.startsWith(selector.substring(1) + ":"))
+ {
+ return true;
+ }
+ }
+ else if (selector.startsWith("$"))
+ {
+ if (ItemTags.getAllTags().getTagOrEmpty(new ResourceLocation(selector.substring(1))).getValues().contains(item.getItem()))
+ {
+ return true;
+ }
+ }
+ else if (selector.startsWith("%"))
+ {
+ if (item.getDisplayName().getString().contains(selector.substring(1)))
+ {
+ return true;
+ }
+ }
+ else if (selector.startsWith("^"))
+ {
+ Minecraft mc = Minecraft.getInstance();
+ List<Component> lines = item.getTooltipLines(mc.player, TooltipFlag.Default.ADVANCED);
+ String tooltipText = "";
+
+ // Skip title line.
+ for (int n = 1; n < lines.size(); n++)
+ {
+ tooltipText += lines.get(n).getString() + '\n';
+ }
+ if (tooltipText.contains(selector.substring(1)))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}