Search in sources :

Example 16 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project ImmersiveEngineering by BluSunrize.

the class ApiUtils method insertStackIntoInventory.

public static ItemStack insertStackIntoInventory(TileEntity inventory, ItemStack stack, EnumFacing side) {
    if (stack != null && inventory != null && inventory.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side)) {
        IItemHandler handler = inventory.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
        ItemStack temp = ItemHandlerHelper.insertItem(handler, stack.copy(), true);
        if (temp == null || temp.stackSize < stack.stackSize)
            return ItemHandlerHelper.insertItem(handler, stack, false);
    }
    return stack;
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack)

Example 17 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project Overloaded by CJ-MC-Mods.

the class PlayerInteractionUtil method placeBlock.

public static boolean placeBlock(@Nonnull ItemStack searchStack, @Nonnull EntityPlayerMP player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull EnumFacing facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
    // Can we place a block at this Pos
    ItemBlock itemBlock = ((ItemBlock) searchStack.getItem());
    if (!worldIn.mayPlace(itemBlock.getBlock(), newPosition, false, facing, null)) {
        return false;
    }
    BlockEvent.PlaceEvent event = ForgeEventFactory.onPlayerBlockPlace(player, new BlockSnapshot(worldIn, newPosition, worldIn.getBlockState(newPosition)), facing, EnumHand.MAIN_HAND);
    if (event.isCanceled())
        return false;
    long distance = Math.round(player.getPosition().getDistance(newPosition.getX(), newPosition.getY(), newPosition.getZ()));
    long cost = OverloadedConfig.multiToolConfig.placeBaseCost + OverloadedConfig.multiToolConfig.costPerMeterAway * distance;
    if (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost)
        return false;
    IItemHandler inventory = player.getCapability(ITEM_HANDLER_CAPABILITY, EnumFacing.UP);
    int foundStackSlot = findItemStack(searchStack, inventory);
    if (foundStackSlot == -1) {
        System.out.println("Stack not found");
        return false;
    }
    System.out.println("Stack found at index: " + foundStackSlot);
    ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.capabilities.isCreativeMode);
    int i = itemBlock.getMetadata(foundStack.getMetadata());
    IBlockState iblockstate1 = itemBlock.block.getStateForPlacement(worldIn, newPosition, facing, hitX, hitY, hitZ, i, player, EnumHand.MAIN_HAND);
    if (itemBlock.placeBlockAt(foundStack, player, worldIn, newPosition, facing, hitX, hitY, hitZ, iblockstate1)) {
        SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
        worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
        energy.extractEnergy((int) cost, false);
        System.out.println("Place Block Successs");
        return true;
    }
    System.out.println("Place Block Failed");
    inventory.insertItem(foundStackSlot, foundStack, player.capabilities.isCreativeMode);
    return false;
}
Also used : SoundType(net.minecraft.block.SoundType) IBlockState(net.minecraft.block.state.IBlockState) IItemHandler(net.minecraftforge.items.IItemHandler) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) ItemBlock(net.minecraft.item.ItemBlock) ItemStack(net.minecraft.item.ItemStack) BlockEvent(net.minecraftforge.event.world.BlockEvent)

Example 18 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project ImmersiveEngineering by BluSunrize.

the class BlockIETileProvider method breakBlock.

@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
    TileEntity tile = world.getTileEntity(pos);
    if (tile != null && (!(tile instanceof ITileDrop) || !((ITileDrop) tile).preventInventoryDrop())) {
        if (tile instanceof IIEInventory && ((IIEInventory) tile).getDroppedItems() != null) {
            for (ItemStack s : ((IIEInventory) tile).getDroppedItems()) if (s != null)
                spawnAsEntity(world, pos, s);
        } else if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
            IItemHandler h = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
            if (h instanceof IEInventoryHandler)
                for (int i = 0; i < h.getSlots(); i++) if (h.getStackInSlot(i) != null) {
                    spawnAsEntity(world, pos, h.getStackInSlot(i));
                    ((IEInventoryHandler) h).setStackInSlot(i, null);
                }
        }
    }
    if (tile instanceof IHasDummyBlocks) {
        ((IHasDummyBlocks) tile).breakDummies(pos, state);
    }
    if (tile instanceof IImmersiveConnectable)
        if (!world.isRemote || !Minecraft.getMinecraft().isSingleplayer())
            ImmersiveNetHandler.INSTANCE.clearAllConnectionsFor(Utils.toCC(tile), world, !world.isRemote && world.getGameRules().getBoolean("doTileDrops"));
    super.breakBlock(world, pos, state);
    world.removeTileEntity(pos);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IEInventoryHandler(blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler) IItemHandler(net.minecraftforge.items.IItemHandler) IImmersiveConnectable(blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable) IIEInventory(blusunrize.immersiveengineering.common.util.inventory.IIEInventory) ItemStack(net.minecraft.item.ItemStack)

Example 19 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project VoodooCraft by Mod-DevCafeTeam.

the class DollTrackerObj method updateTileEntry.

/**
	 * Adds tile & UUID to the list if it should be, and removes them if nothing is in the pedestal.
	 * Called every time the TileDollPedestal inventory changes, and when it is loaded in.
	 */
public static void updateTileEntry(TileDollPedestal tile) {
    int dim = tile.getWorld().provider.getDimension();
    IItemHandler inv = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
    ItemStack stack = inv.getStackInSlot(0);
    if (stack != null && stack.getItem() instanceof ItemDoll && stack.getTagCompound() != null && stack.getTagCompound().hasKey(NBTHelper.KEY_OWNER)) {
        if (stack.getTagCompound().getBoolean(NBTHelper.KEY_IS_PLAYER) == true) {
            addPlayerEntry(NBTUtil.getUUIDFromTag(stack.getTagCompound()), dim, tile.getPos());
        } else {
            addEntityEntry(NBTUtil.getUUIDFromTag(stack.getTagCompound()), dim, tile.getPos());
        }
    } else {
        for (DollTrackerObj obj : playerList) {
            if (obj.Pos == tile.getPos() && obj.Dim == dim)
                playerList.remove(obj);
        }
        for (DollTrackerObj obj : entityList) {
            if (obj.Pos == tile.getPos() && obj.Dim == dim)
                entityList.remove(obj);
        }
    }
}
Also used : IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack) ItemDoll(mdc.voodoocraft.items.ItemDoll)

Example 20 with IItemHandler

use of net.minecraftforge.items.IItemHandler in project VoodooCraft by Mod-DevCafeTeam.

the class BlockDollPedestal method breakBlock.

/**
	 * Drops item in the pedestal
	 * @param worldIn
	 * @param pos
	 * @param state
	 */
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
    if (!worldIn.isRemote) {
        TileEntity te = worldIn.getTileEntity(pos);
        if (te != null) {
            TileDollPedestal tile = (TileDollPedestal) te;
            IItemHandler tileinv = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
            ItemStack stack = tileinv.getStackInSlot(0);
            if (stack != null) {
                EntityItem entItem = new EntityItem(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
                worldIn.spawnEntity(entItem);
            }
        }
    }
    super.breakBlock(worldIn, pos, state);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileDollPedestal(mdc.voodoocraft.tile.TileDollPedestal) IItemHandler(net.minecraftforge.items.IItemHandler) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

IItemHandler (net.minecraftforge.items.IItemHandler)22 ItemStack (net.minecraft.item.ItemStack)17 TileEntity (net.minecraft.tileentity.TileEntity)7 BlockPos (net.minecraft.util.math.BlockPos)4 IBlockState (net.minecraft.block.state.IBlockState)3 MultiSlotTileEntity (com.almuradev.almura.shared.tileentity.MultiSlotTileEntity)2 ISoulbound (convenientadditions.api.item.ISoulbound)2 Nullable (javax.annotation.Nullable)2 ItemDoll (mdc.voodoocraft.items.ItemDoll)2 TileDollPedestal (mdc.voodoocraft.tile.TileDollPedestal)2 TileEntityTeleporter (net.dyeo.teleporter.tileentity.TileEntityTeleporter)2 Block (net.minecraft.block.Block)2 EntityItem (net.minecraft.entity.item.EntityItem)2 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)2 ISpecialMeasuringBehavior (betterwithaddons.util.ISpecialMeasuringBehavior)1 TileEntityFilteredHopper (betterwithmods.common.blocks.mechanical.tile.TileEntityFilteredHopper)1 IImmersiveConnectable (blusunrize.immersiveengineering.api.energy.wires.IImmersiveConnectable)1 IEInventoryHandler (blusunrize.immersiveengineering.common.util.inventory.IEInventoryHandler)1 IIEInventory (blusunrize.immersiveengineering.common.util.inventory.IIEInventory)1 DualItemHandlerContainer (com.almuradev.almura.shared.inventory.DualItemHandlerContainer)1