@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer entityPlayer) {
for(int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if(entityPlayer.inventory.mainInventory[i].getItem().equals(this)) {
entityPlayer.inventory.setInventorySlotContents(i, null); // removing the first available bag of resources
entityPlayer.inventory.setInventorySlotContents(i, new ItemStack(RandomItems.giveItemForPlayer(RandomItems.LEGENDARY_ITEMS)));
break;
}
}
return super.onItemRightClick(itemStack, world, entityPlayer);
}
A huge disadvantage is that if the player actually keeps the bag at the end of the inventory (by index) and if on the way to the end of the inventory the loop encounters the same bag, it will replace the name of the first hit