Posted March 3, 20187 yr 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 March 3, 20187 yr by That_Martin_Guy
March 3, 20187 yr 29 minutes ago, That_Martin_Guy said: sometimes it adds AIR items that is is intended, use NonNullList instead of list
March 3, 20187 yr Author I rewrote the method three times without figuring out the answer was this simple... Thanks!
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.