Jump to content

Recommended Posts

Posted (edited)

I'm creating a block that,when activated, will hurt the player if his inventory is empty, and if it isn't put a random item in the inventory into an itemstack list. It only sometimes does the intended behaviour, though, and I can't really tell when it does so.

 

Block.onBlockActivated code:

 

            List<ItemStack> lostItems = playerIn.getCapability(Capabilities.LOST_ITEMS_CAPABILITY, null).getLostItems();

            if(!lostItems.isEmpty())
            {
                Random random = new Random();

                if(playerIn.inventory.isEmpty())
                {
                    playerIn.attackEntityFrom(new HoleDamageSource(), 19.0f);
                }
                else
                {
                    int itemIndex = random.nextInt(playerIn.inventory.getSizeInventory());

                    while(playerIn.inventory.getStackInSlot(itemIndex).isEmpty())
                    {
                        itemIndex = random.nextInt(playerIn.inventory.getSizeInventory());
                    }

                    playerIn.inventory.removeStackFromSlot(itemIndex);
                    lostItems.add(playerIn.inventory.getStackInSlot(itemIndex));
                }
            }
            else
            {
                playerIn.sendMessage(new TextComponentString("You reach in but find nothing inside"));
            }
            return true;

 

EDIT: To clarify, sometimes it adds AIR items to the list when the inventory is empty (even though it shouldn't), but other times it hurts the player.

Edited by That_Martin_Guy

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.