Simple Backpack [FABRIC]

Simple Backpack [FABRIC]

2M Downloads

[1.18.1] (Suggestion) Contents tooltip

dranorter opened this issue ยท 2 comments

commented

Something I feel is missing with this mod is a shulker-box style tooltip listing the first few backpack contents.

commented

I'll take a look at what is involved.

commented

A backpack mod I've been maintaining has the following in its BackpackItem.java. The way that mod interacts with Fabric is quite different, but I think this would still apply.

    @Override
    @Environment(EnvType.CLIENT)
    public void appendTooltip(ItemStack stack, World world, List<Text> tooltip, TooltipContext context) {
        super.appendTooltip(stack, world, tooltip, context);
        NbtCompound tag = stack.getNbt();
        if (tag != null) {
            if (tag.contains("Items", 9)) {
                DefaultedList<ItemStack> stacks = DefaultedList.ofSize(27, ItemStack.EMPTY);
                Inventories.readNbt(tag, stacks);
                //Inventories.fromTag(tag, stacks);
                int listedStacks = 0;
                int totalStacks = 0;

                for (ItemStack heldStack : stacks) {
                    if (!heldStack.isEmpty()) {
                        ++totalStacks;
                        if (listedStacks <= 4) {
                            ++listedStacks;
                            Text stackName = heldStack.getName().copy();
                            String stackString = stackName.getString();
                            stackString = stackString + " x" + (String.valueOf(heldStack.getCount()));
                            tooltip.add(Text.of(stackString));
                        }
                    }
                }

                if (totalStacks - listedStacks > 0) {
                    tooltip.add((Text.translatable("container.shulkerBox.more", new Object[]{totalStacks - listedStacks})).formatted(Formatting.ITALIC));
                }
            }
        }
    }