feat: added Highlighter

This commit is contained in:
əlemi 2023-03-01 18:16:19 +01:00
parent b8eba78e20
commit 9b92d8fb7f
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -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();
}
}