Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring Back NBT Filter as Data Filter #283

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21 2024-07-24T22:27:48.0729142 Languages: en_us for mod: laserio
9858c55ab597f0b1e24ce2bbada665158cfc3e9d assets/laserio/lang/en_us.json
// 1.21 2024-08-04T13:44:14.7251544 Languages: en_us for mod: laserio
85b7b822654083ab1c8be08c584bb15e38a364a0 assets/laserio/lang/en_us.json
10 changes: 5 additions & 5 deletions src/generated/resources/assets/laserio/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"item.laserio.filter_basic": "Basic Filter",
"item.laserio.filter_count": "Counting Filter",
"item.laserio.filter_mod": "Mod Filter",
"item.laserio.filter_nbt": "NBT Filter",
"item.laserio.filter_nbt": "Data Filter",
"item.laserio.filter_tag": "Tag Filter",
"item.laserio.laser_wrench": "Laser Wrench",
"item.laserio.logic_chip": "Logic Chip",
Expand All @@ -38,7 +38,7 @@
"laserio.tooltip.item.card.sneaky.SOUTH": "South",
"laserio.tooltip.item.card.sneaky.UP": "Up",
"laserio.tooltip.item.card.sneaky.WEST": "West",
"laserio.tooltip.item.filter.nbt": "Match NBT: ",
"laserio.tooltip.item.filter.nbt": "Match Data: ",
"laserio.tooltip.item.filter.nbt.allow": "True",
"laserio.tooltip.item.filter.nbt.deny": "False",
"laserio.tooltip.item.filter.type": "Type: ",
Expand All @@ -52,7 +52,7 @@
"screen.laserio.apply": "Apply",
"screen.laserio.blue": "Blue",
"screen.laserio.channel": "Channel: ",
"screen.laserio.comparenbt": "NBT",
"screen.laserio.comparenbt": "Data",
"screen.laserio.default": "Default",
"screen.laserio.denylist": "Deny",
"screen.laserio.down": "Down",
Expand All @@ -71,8 +71,8 @@
"screen.laserio.insert": "Insert",
"screen.laserio.lasernode": "Laser Node",
"screen.laserio.low": "Low",
"screen.laserio.nbtfalse": "Ignore NBT",
"screen.laserio.nbttrue": "Match NBT",
"screen.laserio.nbtfalse": "Ignore Data",
"screen.laserio.nbttrue": "Match Data",
"screen.laserio.north": "North",
"screen.laserio.or": "Or",
"screen.laserio.output": "Output",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
import com.mojang.blaze3d.platform.InputConstants;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.Util;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.network.PacketDistributor;

import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.*;

public class FilterNBTScreen extends AbstractContainerScreen<FilterNBTContainer> {
private final ResourceLocation GUI = ResourceLocation.fromNamespaceAndPath(LaserIO.MODID, "textures/gui/filtertag.png");
Expand Down Expand Up @@ -61,7 +61,6 @@ public FilterNBTScreen(FilterNBTContainer container, Inventory inv, Component na

@Override
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
//this.renderBackground(guiGraphics);
super.render(guiGraphics, mouseX, mouseY, partialTicks);
this.renderTooltip(guiGraphics, mouseX, mouseY);
if (MiscTools.inBounds(getGuiLeft() + 5, getGuiTop() + 10, 16, 16, mouseX, mouseY)) {
Expand Down Expand Up @@ -112,64 +111,82 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partia
overSlot = -1;

for (String tag : displayTags) {
matrixStack.pushPose();
matrixStack.scale(0.75f, 0.75f, 0.75f);
int fontColor = stackInSlotTags.contains(tag) ? Color.BLUE.getRGB() : Color.DARK_GRAY.getRGB();
guiGraphics.drawString(font, tag, availableItemsstartX / 0.75f + 16, tagStartY / 0.75f, fontColor, false);
matrixStack.popPose();
renderScrollingString(guiGraphics, font, Component.literal(tag), availableItemsstartX, tagStartY, availableItemsstartX + 152, fontColor);

if (MiscTools.inBounds(availableItemsstartX, tagStartY - 2, 160, 8, mouseX, mouseY)) {
overSlot = slot;
color = -2130706433;// : 0xFF5B5B5B;

matrixStack.pushPose();
RenderSystem.disableDepthTest();
RenderSystem.colorMask(true, true, true, false);
guiGraphics.fillGradient(availableItemsstartX - 1, tagStartY - 2, availableItemsstartX + 160, tagStartY + 8, color, color);
//Tag tempTag = stackInSlot.getOrCreateTag().get(displayTags.get(overSlot));
Tag tempTag = new CompoundTag();
if (tempTag != null) { //Todo Revisit
//String tooltip = Objects.requireNonNull(stackInSlot.getOrCreateTag().get(displayTags.get(overSlot))).toString();
String tooltip = "";
if (tooltip.length() > 60) tooltip = tooltip.substring(0, 60) + "...";
guiGraphics.renderTooltip(font, Component.literal(tooltip), mouseX, mouseY);
String tagValue = getTagValueFor(displayTags.get(overSlot));
if (!tagValue.isEmpty()) {
if (tagValue.length() > 60) tagValue = tagValue.substring(0, 60) + "...";
guiGraphics.renderTooltip(font, Component.literal(tagValue), mouseX, mouseY);
guiGraphics.flush(); //Not sure why this is necessary, but it is!
}
RenderSystem.colorMask(true, true, true, true);
matrixStack.popPose();
}

if (slot == selectedSlot) {
color = 0xFFFF0000;

matrixStack.pushPose();
RenderSystem.disableDepthTest();
RenderSystem.colorMask(true, true, true, false);

int x1 = availableItemsstartX + 160;
int y1 = tagStartY + 10;
guiGraphics.hLine(availableItemsstartX - 2, x1 - 0, tagStartY - 2, color);
guiGraphics.hLine(availableItemsstartX - 2, x1 - 0, y1 - 3, color);
guiGraphics.vLine(availableItemsstartX - 2, tagStartY - 2, y1 - 2, color);
guiGraphics.vLine(x1 - 0, tagStartY - 2, y1 - 2, color);

RenderSystem.colorMask(true, true, true, true);
matrixStack.popPose();
}

tagStartY += 10;
slot++;
}
}

protected static void renderScrollingString(GuiGraphics graphics, Font fontRenderer, Component text, int xStart, int yStart, int xEnd, int textColor) {
int textWidth = fontRenderer.width(text);
int yEnd = yStart + fontRenderer.lineHeight;
int maxRenderWidth = xEnd - xStart;

if (textWidth > maxRenderWidth) {
int textOverflow = textWidth - maxRenderWidth;
double currentTime = (double) Util.getMillis() / 1000.0D;
double scrollDuration = Math.max((double) textOverflow * 0.5D, 3.0D);
double oscillation = Math.sin((Math.PI / 2D) * Math.cos((Math.PI * 2D) * currentTime / scrollDuration)) / 2.0D + 0.5D;
double scrollOffset = Mth.lerp(oscillation, 0.0D, (double) textOverflow);

graphics.enableScissor(xStart, yStart, xEnd, yEnd);
graphics.drawString(fontRenderer, text, xStart - (int) scrollOffset, yStart, textColor, false);
graphics.disableScissor();
} else {
graphics.drawString(fontRenderer, text, xStart, yStart, textColor, false);
}
}

protected void populateStackInSlotTags() {
stackInSlotTags = new ArrayList<>();
ItemStack stackInSlot = container.handler.getStackInSlot(0);
/*if (!stackInSlot.isEmpty()) {//Todo Revisit
stackInSlot.getOrCreateTag().getAllKeys().forEach(t -> {
if (!stackInSlotTags.contains(t) && !tags.contains(t))
stackInSlotTags.add(t);
if (!stackInSlot.isEmpty()) {
stackInSlot.getComponentsPatch().entrySet().forEach(t -> {
if (!stackInSlotTags.contains(t.getKey().toString()) && !tags.contains(t.getKey().toString()))
stackInSlotTags.add(t.getKey().toString());
});
}*/
}
}

protected String getTagValueFor(String name) {
ItemStack stackInSlot = container.handler.getStackInSlot(0);
if (!stackInSlot.isEmpty()) {
for (Map.Entry<DataComponentType<?>, Optional<?>> entry : stackInSlot.getComponentsPatch().entrySet()) {
if (entry.getKey().toString().equals(name)) {
// Check if the value is present and return its string representation
Optional<?> value = entry.getValue();
if (value.isPresent()) {
return value.get().toString();
}
}
}
}
return "";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.direwolf20.laserio.common.containers;

import com.direwolf20.laserio.common.blockentities.LaserNodeBE;
import com.direwolf20.laserio.common.containers.customhandler.CardItemHandler;
import com.direwolf20.laserio.common.containers.customhandler.FilterBasicHandler;
import com.direwolf20.laserio.common.containers.customslot.FilterBasicSlot;
import com.direwolf20.laserio.common.items.cards.BaseCard;
import com.direwolf20.laserio.setup.Registration;
import net.minecraft.core.BlockPos;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -29,15 +31,14 @@ public class FilterNBTContainer extends AbstractContainerMenu {
public BlockPos sourceContainer = BlockPos.ZERO;

public FilterNBTContainer(int windowId, Inventory playerInventory, Player player, RegistryFriendlyByteBuf extraData) {
this(windowId, playerInventory, player, new FilterBasicHandler(SLOTS, ItemStack.EMPTY), ItemStack.EMPTY);
filterItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(extraData);
this(windowId, playerInventory, player, ItemStack.OPTIONAL_STREAM_CODEC.decode(extraData));
this.sourceCard = ItemStack.OPTIONAL_STREAM_CODEC.decode(extraData);
}

public FilterNBTContainer(int windowId, Inventory playerInventory, Player player, FilterBasicHandler handler, ItemStack filterItem) {
public FilterNBTContainer(int windowId, Inventory playerInventory, Player player, ItemStack filterItem) {
super(Registration.FilterNBT_Container.get(), windowId);
playerEntity = player;
this.handler = handler;
this.handler = new FilterBasicHandler(SLOTS, filterItem);
this.playerInventory = new InvWrapper(playerInventory);
this.filterItem = filterItem;
if (handler != null)
Expand All @@ -46,8 +47,8 @@ public FilterNBTContainer(int windowId, Inventory playerInventory, Player player
layoutPlayerInventorySlots(8, 172);
}

public FilterNBTContainer(int windowId, Inventory playerInventory, Player player, FilterBasicHandler handler, BlockPos sourcePos, ItemStack filterItem, ItemStack sourceCard) {
this(windowId, playerInventory, player, handler, filterItem);
public FilterNBTContainer(int windowId, Inventory playerInventory, Player player, BlockPos sourcePos, ItemStack filterItem, ItemStack sourceCard) {
this(windowId, playerInventory, player, filterItem);
this.sourceContainer = sourcePos;
this.sourceCard = sourceCard;
}
Expand Down Expand Up @@ -121,6 +122,10 @@ public void removed(Player playerIn) {
Level world = playerIn.level();
if (!world.isClientSide) {
handler.setStackInSlot(0, ItemStack.EMPTY); //Clear the current slot
if (sourceCard != null && !sourceCard.isEmpty()) {
CardItemHandler cardItemHandler = BaseCard.getInventory(sourceCard);
cardItemHandler.setStackInSlot(0, filterItem);
}
if (!sourceContainer.equals(BlockPos.ZERO)) {
BlockEntity blockEntity = world.getBlockEntity(sourceContainer);
if (blockEntity instanceof LaserNodeBE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public static boolean setAllowList(ItemStack stack, boolean allowList) {
}

public static boolean getCompareNBT(ItemStack stack) {
if (stack.getItem() instanceof FilterNBT)
return stack.getOrDefault(LaserIODataComponents.FILTER_COMPARE, true);
return stack.getOrDefault(LaserIODataComponents.FILTER_COMPARE, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.direwolf20.laserio.common.containers.FilterBasicContainer;
import com.direwolf20.laserio.common.containers.FilterNBTContainer;
import com.direwolf20.laserio.common.containers.customhandler.FilterBasicHandler;
import com.direwolf20.laserio.setup.LaserIODataComponents;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand All @@ -12,6 +13,9 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

import java.util.ArrayList;
import java.util.List;

public class FilterNBT extends BaseFilter {
public FilterNBT() {
super();
Expand All @@ -22,11 +26,9 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera
ItemStack itemstack = player.getItemInHand(hand);
if (level.isClientSide()) return new InteractionResultHolder<>(InteractionResult.PASS, itemstack);

FilterBasicHandler handler = getInventory(itemstack);
player.openMenu(new SimpleMenuProvider(
(windowId, playerInventory, playerEntity) -> new FilterNBTContainer(windowId, playerInventory, player, handler, itemstack), Component.translatable("")), (buf -> {
(windowId, playerInventory, playerEntity) -> new FilterNBTContainer(windowId, playerInventory, player, itemstack), Component.translatable("")), (buf -> {
ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, itemstack);
ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, ItemStack.EMPTY);
}));

return new InteractionResultHolder<>(InteractionResult.PASS, itemstack);
Expand All @@ -37,55 +39,11 @@ public static FilterBasicHandler getInventory(ItemStack stack) {
return handler;
}

//TODO Re-Implement
/*public static FilterBasicHandler setInventory(ItemStack stack, FilterBasicHandler handler) {
stack.getOrCreateTag().put("inv", handler.serializeNBT());
return handler;
}*/

/*public static void addTag(ItemStack card, String tag) {
List<String> tags = getTags(card);
if (!tags.contains(tag)) {
tags.add(tag);
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}
}

public static void removeTag(ItemStack card, String tag) {
List<String> tags = getTags(card);
if (tags.contains(tag)) {
tags.remove(tag);
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}
}

public static void clearTags(ItemStack card) {
List<String> tags = new ArrayList();
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}

public static void setTags(ItemStack card, List<String> tagsIn) {
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tagsIn));
}

public static void setTags(ItemStack card, CompoundTag tagsTag) {
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", tagsTag.getList("tags", Tag.TAG_COMPOUND));
card.set(LaserIODataComponents.FILTER_TAG_TAGS, tagsIn);
}

public static List<String> getTags(ItemStack card) {
List<String> tags = new ArrayList();
CompoundTag compound = card.getOrCreateTag();
if (compound.contains("tags")) {
ListTag listNBT = compound.getList("tags", Tag.TAG_COMPOUND);
tags = new ArrayList<>(MiscTools.NBTToStringList(listNBT));
} else {
compound.put("tags", MiscTools.stringListToNBT(tags));
}
return tags;
}*/
return card.getOrDefault(LaserIODataComponents.FILTER_TAG_TAGS, new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,6 @@ public static FilterBasicHandler getInventory(ItemStack stack) {
return handler;
}

/*public static FilterBasicHandler setInventory(ItemStack stack, FilterBasicHandler handler) {
stack.getOrCreateTag().put("inv", handler.serializeNBT());
return handler;
}*/

/*public static void addTag(ItemStack card, String tag) {
List<String> tags = getTags(card);
if (!tags.contains(tag)) {
tags.add(tag);
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}
}

public static void removeTag(ItemStack card, String tag) {
List<String> tags = getTags(card);
if (tags.contains(tag)) {
tags.remove(tag);
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}
}

public static void clearTags(ItemStack card) {
List<String> tags = new ArrayList();
CompoundTag compound = card.getOrCreateTag();
compound.put("tags", MiscTools.stringListToNBT(tags));
}*/



public static void setTags(ItemStack card, List<String> tagsIn) {
card.set(LaserIODataComponents.FILTER_TAG_TAGS, tagsIn);
}
Expand Down
Loading
Loading