diff options
author | alemi <me@alemi.dev> | 2023-03-01 18:16:19 +0100 |
---|---|---|
committer | alemi <me@alemi.dev> | 2023-03-01 18:16:19 +0100 |
commit | 9b92d8fb7ff17e9b5b68224494de049034406963 (patch) | |
tree | 0f261a46df8ceff961caedc72843669e633fbf46 /src/main/java | |
parent | b8eba78e20219a076048d35b3a7acee4c2a3bd45 (diff) |
feat: added Highlighter
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/ftbsc/bscv/modules/hud/Highlighter.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java new file mode 100644 index 0000000..35da3c4 --- /dev/null +++ b/src/main/java/ftbsc/bscv/modules/hud/Highlighter.java @@ -0,0 +1,101 @@ +package ftbsc.bscv.modules.hud; + +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.systems.RenderSystem; + +import ftbsc.bscv.modules.AbstractModule; +import ftbsc.bscv.tools.Setting; +import net.minecraft.client.gui.screen.inventory.ContainerScreen; +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.ListNBT; +import net.minecraftforge.client.event.GuiContainerEvent; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.client.gui.GuiUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Pattern; + +import ftbsc.bscv.api.ILoadable; +import com.google.auto.service.AutoService; + +@AutoService(ILoadable.class) +public class Highlighter extends AbstractModule { + + public final ForgeConfigSpec.ConfigValue<String> query; + private Pattern pattern; + + public Highlighter() { + super(); + + this.pattern = Pattern.compile(""); + this.query = Setting.Str.builder() + .fallback("") + .name("query") + .comment("search query") + .callback(to -> this.pattern = Pattern.compile(to)) + .build(this); + } + + private List<String> 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<String> 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(); + ContainerScreen<?> screen = event.getGuiContainer(); + + RenderSystem.enableDepthTest(); + matrix.pushPose(); + matrix.translate(screen.getGuiLeft(), screen.getGuiTop(), 0); + + for (Slot slot : screen.getMenu().slots) { + ItemStack stack = slot.getItem(); + if (this.matches(stack)) { + GuiUtils.drawGradientRect( + matrix.last().pose(), 0, + slot.x, slot.y, slot.x + 16, slot.y + 16, + // TODO de-hardcode these colors by creating our util! + -925194976, // R218 G165 B 32 A200 + -927090837 // R189 G183 B107 A200 + ); + } + } + + matrix.popPose(); + } +} |