From 3638f7f9c15eec46cd9f8639ca9b386a9e03c9c3 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 13 Nov 2023 04:25:29 +0100 Subject: chore: move regex filters from Highlighter to utils --- .../java/ftbsc/bscv/modules/hud/Highlighter.java | 35 +--------------- src/main/java/ftbsc/bscv/tools/Inventory.java | 46 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java index a5fa0e5..f5cc542 100644 --- a/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java +++ b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java @@ -4,6 +4,7 @@ import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.systems.RenderSystem; import ftbsc.bscv.modules.AbstractModule; +import ftbsc.bscv.tools.Inventory; import ftbsc.bscv.tools.Setting; import net.minecraft.client.gui.screen.inventory.ContainerScreen; import net.minecraft.inventory.container.Slot; @@ -48,38 +49,6 @@ public class Highlighter extends AbstractModule { this.pattern = Pattern.compile(this.query.get()); } - private List enchantments(ItemStack stack) { - final ListNBT tags; - if (Items.ENCHANTED_BOOK.equals(stack.getItem())) { - tags = EnchantedBookItem.getEnchantments(stack); // special case to also search book enchants - } else { - tags = stack.getEnchantmentTags(); - } - - List out = new ArrayList<>(); - for (int i = 0; i < tags.size(); i++) { - CompoundNBT tag = tags.getCompound(i); - out.add(String.format("%s %s", tag.getString("id"), tag.getInt("lvl"))); - } - - return out; - } - - private boolean matches(ItemStack stack) { - if (stack.isEmpty()) return false; - - String displayName = stack.getDisplayName().getString(); - if (this.pattern.matcher(displayName).find()) return true; - - if (Items.ENCHANTED_BOOK.equals(stack.getItem()) || stack.isEnchanted()) { - for (String ench : this.enchantments(stack)) { - if (this.pattern.matcher(ench).find()) return true; - } - } - - return false; - } - @SubscribeEvent public void onGuiContainerDraw(GuiContainerEvent.DrawBackground event) { MatrixStack matrix = event.getMatrixStack(); @@ -91,7 +60,7 @@ public class Highlighter extends AbstractModule { for (Slot slot : screen.getMenu().slots) { ItemStack stack = slot.getItem(); - if (this.matches(stack)) { + if (Inventory.matchItem(this.pattern, stack)) { GuiUtils.drawGradientRect( matrix.last().pose(), 0, slot.x, slot.y, slot.x + 16, slot.y + 16, diff --git a/src/main/java/ftbsc/bscv/tools/Inventory.java b/src/main/java/ftbsc/bscv/tools/Inventory.java index 8f1e2f5..f044be8 100644 --- a/src/main/java/ftbsc/bscv/tools/Inventory.java +++ b/src/main/java/ftbsc/bscv/tools/Inventory.java @@ -7,13 +7,19 @@ import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.ai.attributes.AttributeModifier; import net.minecraft.entity.ai.attributes.Attributes; import net.minecraft.inventory.EquipmentSlotType; +import net.minecraft.inventory.container.ClickType; import net.minecraft.inventory.container.Slot; +import net.minecraft.item.EnchantedBookItem; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; +import net.minecraft.nbt.ListNBT; +import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.regex.Pattern; public class Inventory implements ICommons { @@ -68,4 +74,44 @@ public class Inventory implements ICommons { return damage / (1. + speed); } + + public static List itemEnchantments(ItemStack stack) { + final ListNBT tags; + if (Items.ENCHANTED_BOOK.equals(stack.getItem())) { + tags = EnchantedBookItem.getEnchantments(stack); // special case to also search book enchants + } else { + tags = stack.getEnchantmentTags(); + } + + List out = new ArrayList<>(); + for (int i = 0; i < tags.size(); i++) { + CompoundNBT tag = tags.getCompound(i); + out.add(String.format("%s %s", tag.getString("id"), tag.getInt("lvl"))); + } + + return out; + } + + public static void clickSLot(Slot slot, ClickType click) { clickSlot(0, slot, 0, click); } + public static void clickSLot(Slot slot, int button, ClickType click) { clickSlot(0, slot, button, click); } + public static void clickSLot(int container, Slot slot, ClickType click) { clickSlot(container, slot, 0, click); } + + public static void clickSlot(int container, Slot slot, int button, ClickType click) { + MC.gameMode.handleInventoryMouseClick(container, slot.index, button, click, MC.player); + } + + public static boolean matchItem(Pattern pattern, ItemStack stack) { + if (stack.isEmpty()) return false; + + String displayName = stack.getDisplayName().getString(); + if (pattern.matcher(displayName).find()) return true; + + if (Items.ENCHANTED_BOOK.equals(stack.getItem()) || stack.isEnchanted()) { + for (String ench : itemEnchantments(stack)) { + if (pattern.matcher(ench).find()) return true; + } + } + + return false; + } } -- cgit v1.2.3-56-ga3b1