Jump to content

[1.12] How to save a specific item.


WildHeart

Recommended Posts

Hello, i use event PlayerEvent.Clone for saving items with death, but if i use that code, my mc has been crashed.

@SubscribeEvent
    public void onClone(PlayerEvent.Clone e)
    {
        if (e.wasDeath)
        {
            ItemStack backStack = null;

            for (ItemStack stack : e.original.inventory.mainInventory)
            {
                if (stack != null && (stack.getItem() instanceof ModGun || stack.getItem() instanceof ModTool))
                {
                    backStack = stack;
                }
            }

            if (backStack != null)
            {
                e.entityPlayer.inventory.addItemStackToInventory(backStack);
            }
        }
    }

What's the problem?

Link to comment
Share on other sites

What's the crash?

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

20 minutes ago, jabelar said:

Just compare each item in the inventory with the one you want and if it is in the inventory copy it.

So, if (st.getItem() instanceof MyItem)

{

 e.entityPlayer.inventory.copyInv(e.original.inventory);

}

Edited by WildHeart
Link to comment
Share on other sites

3 hours ago, diesieben07 said:

No. Look at what copyInventory does and adapt it for your needs.

@SubscribeEvent
    public void onClone(PlayerEvent.Clone e)
    {
        if (e.wasDeath)
        {
            InventoryPlayer inv = e.entityPlayer.inventory; 
            
            for (int i = 0; i < inv.getSizeInventory(); ++i)
            {
                if (inv.getStackInSlot(i).getItem() instanceof CustomArmor || 
                        inv.getStackInSlot(i).getItem() instanceof CustomWeapon)
                {
                    inv.setInventorySlotContents(i, e.original.inventory.getStackInSlot(i));
                }
            }
        }
    }

?

Link to comment
Share on other sites

Why are you getting from and setting to the same inventory?

Capture.PNG

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:

Why are you getting from and setting to the same inventory?

Capture.PNG

Ups, my fail. Corrected.

@SubscribeEvent
    public void onClone(PlayerEvent.Clone e)
    {
        if (e.wasDeath)
        {
            InventoryPlayer orig = e.original.inventory;

            for (int i = 0; i < orig.getSizeInventory(); ++i)
            {
                if (orig.getStackInSlot(i).getItem() instanceof CustomArmor ||
                        orig.getStackInSlot(i).getItem() instanceof CustomWeapon)
                {
                    e.entityPlayer.inventory.setInventorySlotContents(i, orig.getStackInSlot(i));
                }
            }
        }
    }

 

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.