@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (!player.isSneaking())
return super.onItemRightClick(world, player, hand);
ItemStack held = player.getHeldItem(hand);
if (held.isEmpty())
return super.onItemRightClick(world, player, hand);
ICellInventoryHandler<IAEEssentiaStack> handler = AEApi.instance().registries().cell().getCellInventory(held, null, this.getChannel());
if (handler == null)
throw new NullPointerException("Couldn't get ICellInventoryHandler for Essentia Cell");
if (!handler.getAvailableItems(this.getChannel().createList()).isEmpty())
return super.onItemRightClick(world, player, hand);
Optional<ItemStack> cellComponent = this.getComponentOfCell(held);
Optional<ItemStack> emptyCasing = AEApi.instance().definitions().materials().emptyStorageCell().maybeStack(1);
if (!cellComponent.isPresent() || !emptyCasing.isPresent())
return super.onItemRightClick(world, player, hand);
InventoryPlayer inv = player.inventory;
if (hand == EnumHand.MAIN_HAND)
inv.setInventorySlotContents(inv.currentItem, ItemStack.EMPTY);
if (!inv.addItemStackToInventory(cellComponent.get()))
player.dropItem(cellComponent.get(), false);
if (!inv.addItemStackToInventory(emptyCasing.get()))
player.dropItem(emptyCasing.get(), false);
if (player.inventoryContainer != null)
player.inventoryContainer.detectAndSendChanges();
return ActionResult.newResult(EnumActionResult.SUCCESS, ItemStack.EMPTY);
}