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;
}
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;
}
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);
}
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);
}
}
}
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);
}
Aggregations