Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] ItemStack Capabilities reset when moved in creative inventory


MrMarnic
 Share

Recommended Posts

I have attached an inventory to an item with the slot size of one (At the beggining the slot is empty).

When you right click with this item a gold ingot will be set in the slot.

If you sneak and right click the Display name of the inserted item will be printed. (At first "Air" because not item is set)

 

The issue was already adressed here, but there was never as solution.

Old issue

public class InventoryTestItem extends Item {
    public InventoryTestItem(Properties properties) {
        super(properties);
        setRegistryName("inv_test");
    }

    @Override
    public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
        if(playerIn.isSneaking()) {
            playerIn.getHeldItem(handIn).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,null).ifPresent((h)-> {
                ItemStackHandler handler = (ItemStackHandler) h;
                handler.setStackInSlot(0,new ItemStack(Items.GOLD_INGOT));
            });
        }else {
            playerIn.getHeldItem(handIn).getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,null).ifPresent((h)-> {
                ItemStackHandler handler = (ItemStackHandler) h;
                System.out.println(handler.getStackInSlot(0).getDisplayName());
            });
        }
        return super.onItemRightClick(worldIn, playerIn, handIn);
    }

    @Nullable
    @Override
    public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundNBT nbt) {
        return new InventoryTestProvider();
    }

    public class InventoryTestProvider implements ICapabilityProvider, ICapabilitySerializable<CompoundNBT> {

        public ItemStackHandler handler;

        public InventoryTestProvider() {
            this.handler = new ItemStackHandler(1);
        }

        @Nonnull
        @Override
        public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction side) {
            if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
                return LazyOptional.of(()->(T)handler);
            }
            return LazyOptional.empty();
        }

        @Override
        public CompoundNBT serializeNBT() {
            CompoundNBT tag = new CompoundNBT();
            tag.put("itemHandler",handler.serializeNBT());
            return tag;
        }

        @Override
        public void deserializeNBT(CompoundNBT nbt) {
            handler.deserializeNBT(nbt.getCompound("itemHandler"));
        }
    }
}

 

The ItemStack will always keep the Gold Ingot (if set), even when moved in inventories (like chests).

But when I move the Item in the Creative Inventory (for example from slot 1 to 2) the capability is reset and the "Air" is printed as Displayname.

Edited by MrMarnic
Link to comment
Share on other sites

I'm not sure that this can be fixed. The client has authoratative control over their inventory when in creative mode (that's literally how it works, the client says "yeah, this slot is X now"). As such moving an item from one slot to another has the effect of destroying and recreating the stack (pickup: "this slot is now air", place: "this slot is now a backpack"). Unless the client knew about the capability (and its data), then the capability gets reset (and even then, the client would have to know to send that cap data to the server).

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

3 minutes ago, Draco18s said:

I'm not sure that this can be fixed. The client has authoratative control over their inventory when in creative mode (that's literally how it works, the client says "yeah, this slot is X now"). As such moving an item from one slot to another has the effect of destroying and recreating the stack (pickup: "this slot is now air", place: "this slot is now a backpack"). Unless the client knew about the capability (and its data), then the capability gets reset (and even then, the client would have to know to send that cap data to the server).

Ok I undestand that. But can I maybe send the capability trough getShareTag() ? Or is this not possible at all and I can not create a Backpack item with capabilites?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.