Search in sources :

Example 1 with ISingleSlotItemHandler

use of com.almuradev.almura.shared.capability.ISingleSlotItemHandler in project Almura by AlmuraDev.

the class CacheBlock method onBlockClicked.

// Left Click
@Override
public void onBlockClicked(World world, BlockPos pos, EntityPlayer player) {
    if (world.isRemote) {
        return;
    }
    final TileEntity te = world.getTileEntity(pos);
    if (te == null || !(te instanceof SingleSlotTileEntity)) {
        return;
    }
    final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
    final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (itemHandler == null) {
        return;
    }
    final ItemStack cacheStack = itemHandler.getStackInSlot(0);
    if (cacheStack.isEmpty()) {
        return;
    }
    int amountToExtract = 0;
    if (player.isSneaking()) {
        amountToExtract = 1;
    } else {
        amountToExtract = Math.min(cacheStack.getItem().getItemStackLimit(), player.inventory.getInventoryStackLimit());
    }
    final ItemStack toExtract = itemHandler.extractItem(0, amountToExtract, true);
    if (toExtract.isEmpty()) {
        return;
    }
    final int preAddStackSize = toExtract.getCount();
    player.inventory.addItemStackToInventory(toExtract);
    final int postAddStackSize = toExtract.getCount();
    final int extractDiff = preAddStackSize - postAddStackSize;
    if (extractDiff <= 0) {
        ((Player) player).sendMessage(Text.of("Cannot withdrawal from cache as your inventory is full!"));
    } else {
        itemHandler.extractItem(0, extractDiff, false);
        ((Player) player).sendMessage(Text.of("Withdrew ", TextColors.AQUA, extractDiff, TextColors.WHITE, " ", cacheStack.getDisplayName(), " " + "from the cache."));
    }
}
Also used : SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) ItemStack(net.minecraft.item.ItemStack)

Example 2 with ISingleSlotItemHandler

use of com.almuradev.almura.shared.capability.ISingleSlotItemHandler in project Almura by AlmuraDev.

the class CacheBlock method createNewTileEntity.

@Nullable
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
    final SingleSlotTileEntity cte = new SingleSlotTileEntity();
    final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    ((SingleSlotItemHandler) itemHandler).setSlotLimit(this.slotLimit);
    return cte;
}
Also used : SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) SingleSlotItemHandler(com.almuradev.almura.shared.capability.impl.SingleSlotItemHandler) ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) Nullable(javax.annotation.Nullable)

Example 3 with ISingleSlotItemHandler

use of com.almuradev.almura.shared.capability.ISingleSlotItemHandler in project Almura by AlmuraDev.

the class CacheBlock method onBlockActivated.

// Right click
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (world.isRemote) {
        return true;
    }
    if (player.isSneaking()) {
        return false;
    }
    final TileEntity te = world.getTileEntity(pos);
    if (te == null || !(te instanceof SingleSlotTileEntity)) {
        return false;
    }
    final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
    final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (itemHandler == null) {
        return false;
    }
    boolean isDirty = false;
    /*
         * How cache interaction works:
         *
         * 1. If filled hand and cache does not exist, merge filled hand to become a new cache (up to cache limit).
         * 2. If empty hand and cache exists, withdrawal from the cache up to the inventory stack limit or the cache usage, whichever is lower.
         * 3. If cache exists, search for and merge appropriate stack from inventory (up to cache limit)
         */
    final ItemStack handStack = player.getHeldItem(hand);
    final ItemStack cacheStack = itemHandler.getStackInSlot(0);
    if (!handStack.isEmpty() && cacheStack.isEmpty()) {
        final ItemStack resultStack = itemHandler.insertItem(0, handStack, false);
        // Creative starts a new cache without removing items
        if (!player.isCreative()) {
            player.setHeldItem(hand, resultStack);
        }
        ((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(Math.abs(resultStack.getCount() - handStack.getCount())), TextColors.WHITE, " ", handStack.getDisplayName(), " into the cache."));
        isDirty = true;
    } else if (!cacheStack.isEmpty()) {
        if (!handStack.isEmpty() && !ItemStack.areItemsEqual(handStack, cacheStack)) {
            ((Player) player).sendMessage(Text.of("This cache does not support this item! Try emptying the cache first."));
            return true;
        }
        // Creative adds to cache indefinitely.
        if (player.isCreative() && !handStack.isEmpty()) {
            final ItemStack resultStack = itemHandler.insertItem(0, handStack, false);
            if (resultStack.getCount() == handStack.getCount()) {
                ((Player) player).sendMessage(Text.of("Cannot add more to the cache as it is full!"));
            } else {
                ((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(handStack.getCount() - resultStack.getCount()), TextColors.WHITE, " ", handStack.getDisplayName(), " into the cache."));
                isDirty = true;
            }
        } else {
            final boolean depositAll = handStack.isEmpty();
            // Search inventory for items that are similar to the cache (only non-matching aspect is the stack size)
            // TODO Please be kind PhaseTracker
            int deposited = 0;
            for (int i = 0; i < player.inventory.getSizeInventory(); i++) {
                final ItemStack slotStack = player.inventory.getStackInSlot(i);
                if (slotStack.isEmpty()) {
                    continue;
                }
                // Slot stack matches cache stack, merge it
                if (slotStack.isItemEqual(cacheStack)) {
                    final ItemStack remaining = itemHandler.insertItem(0, slotStack, false);
                    player.inventory.setInventorySlotContents(i, remaining);
                    deposited += slotStack.getCount() - remaining.getCount();
                    isDirty = true;
                    if (!remaining.isEmpty()) {
                        break;
                    }
                    if (!depositAll) {
                        break;
                    }
                }
            }
            if (deposited > 0) {
                ((Player) player).sendMessage(Text.of("Deposited ", TextColors.AQUA, CacheFeature.format.format(deposited), TextColors.WHITE, " ", cacheStack.getDisplayName(), " into the cache."));
            }
        }
    }
    if (isDirty) {
        cte.markDirty();
    }
    return true;
}
Also used : SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) ItemStack(net.minecraft.item.ItemStack)

Example 4 with ISingleSlotItemHandler

use of com.almuradev.almura.shared.capability.ISingleSlotItemHandler in project Almura by AlmuraDev.

the class CacheBlock method onBlockPlacedBy.

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
    super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
    if (!worldIn.isRemote) {
        final NBTTagCompound tagCompound = stack.getSubCompound("tag");
        if (tagCompound == null) {
            return;
        }
        if (!tagCompound.hasKey("Cache")) {
            return;
        }
        final NBTTagCompound cacheCompound = tagCompound.getCompoundTag("Cache");
        final TileEntity te = worldIn.getTileEntity(pos);
        if (te == null || !(te instanceof SingleSlotTileEntity)) {
            return;
        }
        final SingleSlotTileEntity cte = (SingleSlotTileEntity) te;
        final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
        if (itemHandler == null) {
            return;
        }
        SharedCapabilities.SINGLE_SLOT_ITEM_HANDLER_CAPABILITY.readNBT(itemHandler, null, cacheCompound.getCompoundTag(Almura.ID + ":single_slot"));
    }
}
Also used : SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) SingleSlotTileEntity(com.almuradev.almura.shared.tileentity.SingleSlotTileEntity) ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) NBTTagCompound(net.minecraft.nbt.NBTTagCompound)

Example 5 with ISingleSlotItemHandler

use of com.almuradev.almura.shared.capability.ISingleSlotItemHandler in project Almura by AlmuraDev.

the class CacheBlock method addCacheToStack.

private ItemStack addCacheToStack(ItemStack targetStack, SingleSlotTileEntity cte) {
    final ISingleSlotItemHandler itemHandler = (ISingleSlotItemHandler) cte.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    if (itemHandler != null) {
        final ItemStack cacheStack = itemHandler.getStackInSlot(0);
        if (!cacheStack.isEmpty()) {
            final NBTTagCompound compound = new NBTTagCompound();
            final NBTTagCompound tagCompound = compound.getCompoundTag("tag");
            final NBTTagCompound cacheCompound = tagCompound.getCompoundTag("Cache");
            cacheCompound.setTag(Almura.ID + ":single_slot", SharedCapabilities.SINGLE_SLOT_ITEM_HANDLER_CAPABILITY.writeNBT(itemHandler, null));
            tagCompound.setTag("Cache", cacheCompound);
            compound.setTag("tag", tagCompound);
            targetStack.setTagCompound(compound);
        }
    }
    return targetStack;
}
Also used : ISingleSlotItemHandler(com.almuradev.almura.shared.capability.ISingleSlotItemHandler) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ItemStack(net.minecraft.item.ItemStack)

Aggregations

ISingleSlotItemHandler (com.almuradev.almura.shared.capability.ISingleSlotItemHandler)7 SingleSlotTileEntity (com.almuradev.almura.shared.tileentity.SingleSlotTileEntity)4 ItemStack (net.minecraft.item.ItemStack)4 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)3 TileEntity (net.minecraft.tileentity.TileEntity)3 EntityPlayer (net.minecraft.entity.player.EntityPlayer)2 Player (org.spongepowered.api.entity.living.player.Player)2 CacheBlock (com.almuradev.almura.feature.cache.block.CacheBlock)1 SingleSlotItemHandler (com.almuradev.almura.shared.capability.impl.SingleSlotItemHandler)1 Nullable (javax.annotation.Nullable)1 IBlockState (net.minecraft.block.state.IBlockState)1 FontRenderer (net.minecraft.client.gui.FontRenderer)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1