Search in sources :

Example 1 with WhitelistMode

use of com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode in project Create by Creators-of-Create.

the class BlueprintOverlayRenderer method getItemsMatchingFilter.

private static ItemStack[] getItemsMatchingFilter(ItemStack filter) {
    return cachedRenderedFilters.computeIfAbsent(filter, itemStack -> {
        CompoundTag tag = itemStack.getOrCreateTag();
        if (AllItems.FILTER.isIn(itemStack) && !tag.getBoolean("Blacklist")) {
            ItemStackHandler filterItems = FilterItem.getFilterItems(itemStack);
            List<ItemStack> list = new ArrayList<>();
            for (int slot = 0; slot < filterItems.getSlots(); slot++) {
                ItemStack stackInSlot = filterItems.getStackInSlot(slot);
                if (!stackInSlot.isEmpty())
                    list.add(stackInSlot);
            }
            return list.toArray(new ItemStack[list.size()]);
        }
        if (AllItems.ATTRIBUTE_FILTER.isIn(itemStack)) {
            WhitelistMode whitelistMode = WhitelistMode.values()[tag.getInt("WhitelistMode")];
            ListTag attributes = tag.getList("MatchedAttributes", net.minecraft.nbt.Tag.TAG_COMPOUND);
            if (whitelistMode == WhitelistMode.WHITELIST_DISJ && attributes.size() == 1) {
                ItemAttribute fromNBT = ItemAttribute.fromNBT((CompoundTag) attributes.get(0));
                if (fromNBT instanceof ItemAttribute.InTag) {
                    ItemAttribute.InTag inTag = (ItemAttribute.InTag) fromNBT;
                    Tag<Item> itag = ItemTags.getAllTags().getTag(inTag.tagName);
                    if (itag != null)
                        return Ingredient.of(itag).getItems();
                }
            }
        }
        return new ItemStack[0];
    });
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) FilterItem(com.simibubi.create.content.logistics.item.filter.FilterItem) Item(net.minecraft.world.item.Item) WhitelistMode(com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode) ItemStack(net.minecraft.world.item.ItemStack) ItemAttribute(com.simibubi.create.content.logistics.item.filter.ItemAttribute) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with WhitelistMode

use of com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode in project Create by Creators-of-Create.

the class FilterItem method test.

private static boolean test(Level world, ItemStack stack, ItemStack filter, boolean matchNBT) {
    if (filter.isEmpty())
        return true;
    if (!(filter.getItem() instanceof FilterItem))
        return (matchNBT ? ItemHandlerHelper.canItemStacksStack(filter, stack) : ItemStack.isSame(filter, stack));
    if (AllItems.FILTER.get() == filter.getItem()) {
        ItemStackHandler filterItems = getFilterItems(filter);
        boolean respectNBT = filter.getOrCreateTag().getBoolean("RespectNBT");
        boolean blacklist = filter.getOrCreateTag().getBoolean("Blacklist");
        for (int slot = 0; slot < filterItems.getSlots(); slot++) {
            ItemStack stackInSlot = filterItems.getStackInSlot(slot);
            if (stackInSlot.isEmpty())
                continue;
            boolean matches = test(world, stack, stackInSlot, respectNBT);
            if (matches)
                return !blacklist;
        }
        return blacklist;
    }
    if (AllItems.ATTRIBUTE_FILTER.get() == filter.getItem()) {
        WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag().getInt("WhitelistMode")];
        ListTag attributes = filter.getOrCreateTag().getList("MatchedAttributes", Tag.TAG_COMPOUND);
        for (Tag inbt : attributes) {
            CompoundTag compound = (CompoundTag) inbt;
            ItemAttribute attribute = ItemAttribute.fromNBT(compound);
            if (attribute == null)
                continue;
            boolean matches = attribute.appliesTo(stack, world) != compound.getBoolean("Inverted");
            if (matches) {
                switch(whitelistMode) {
                    case BLACKLIST:
                        return false;
                    case WHITELIST_CONJ:
                        continue;
                    case WHITELIST_DISJ:
                        return true;
                }
            } else {
                switch(whitelistMode) {
                    case BLACKLIST:
                        continue;
                    case WHITELIST_CONJ:
                        return false;
                    case WHITELIST_DISJ:
                        continue;
                }
            }
        }
        switch(whitelistMode) {
            case BLACKLIST:
                return true;
            case WHITELIST_CONJ:
                return true;
            case WHITELIST_DISJ:
                return false;
        }
    }
    return false;
}
Also used : ItemStackHandler(net.minecraftforge.items.ItemStackHandler) WhitelistMode(com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 3 with WhitelistMode

use of com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode in project Create by Creators-of-Create.

the class FilterItem method makeSummary.

private List<Component> makeSummary(ItemStack filter) {
    List<Component> list = new ArrayList<>();
    if (type == FilterType.REGULAR) {
        ItemStackHandler filterItems = getFilterItems(filter);
        boolean blacklist = filter.getOrCreateTag().getBoolean("Blacklist");
        list.add((blacklist ? Lang.translate("gui.filter.deny_list") : Lang.translate("gui.filter.allow_list")).withStyle(ChatFormatting.GOLD));
        int count = 0;
        for (int i = 0; i < filterItems.getSlots(); i++) {
            if (count > 3) {
                list.add(new TextComponent("- ...").withStyle(ChatFormatting.DARK_GRAY));
                break;
            }
            ItemStack filterStack = filterItems.getStackInSlot(i);
            if (filterStack.isEmpty())
                continue;
            list.add(new TextComponent("- ").append(filterStack.getHoverName()).withStyle(ChatFormatting.GRAY));
            count++;
        }
        if (count == 0)
            return Collections.emptyList();
    }
    if (type == FilterType.ATTRIBUTE) {
        WhitelistMode whitelistMode = WhitelistMode.values()[filter.getOrCreateTag().getInt("WhitelistMode")];
        list.add((whitelistMode == WhitelistMode.WHITELIST_CONJ ? Lang.translate("gui.attribute_filter.allow_list_conjunctive") : whitelistMode == WhitelistMode.WHITELIST_DISJ ? Lang.translate("gui.attribute_filter.allow_list_disjunctive") : Lang.translate("gui.attribute_filter.deny_list")).withStyle(ChatFormatting.GOLD));
        int count = 0;
        ListTag attributes = filter.getOrCreateTag().getList("MatchedAttributes", Tag.TAG_COMPOUND);
        for (Tag inbt : attributes) {
            CompoundTag compound = (CompoundTag) inbt;
            ItemAttribute attribute = ItemAttribute.fromNBT(compound);
            boolean inverted = compound.getBoolean("Inverted");
            if (count > 3) {
                list.add(new TextComponent("- ...").withStyle(ChatFormatting.DARK_GRAY));
                break;
            }
            list.add(new TextComponent("- ").append(attribute.format(inverted)));
            count++;
        }
        if (count == 0)
            return Collections.emptyList();
    }
    return list;
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) ItemStackHandler(net.minecraftforge.items.ItemStackHandler) ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) WhitelistMode(com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode) Tag(net.minecraft.nbt.Tag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

WhitelistMode (com.simibubi.create.content.logistics.item.filter.AttributeFilterContainer.WhitelistMode)3 CompoundTag (net.minecraft.nbt.CompoundTag)3 ListTag (net.minecraft.nbt.ListTag)3 ItemStack (net.minecraft.world.item.ItemStack)3 ItemStackHandler (net.minecraftforge.items.ItemStackHandler)3 ArrayList (java.util.ArrayList)2 Tag (net.minecraft.nbt.Tag)2 FilterItem (com.simibubi.create.content.logistics.item.filter.FilterItem)1 ItemAttribute (com.simibubi.create.content.logistics.item.filter.ItemAttribute)1 Component (net.minecraft.network.chat.Component)1 TextComponent (net.minecraft.network.chat.TextComponent)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 Item (net.minecraft.world.item.Item)1