Search in sources :

Example 1 with InventoryWrapper

use of buildcraft.lib.inventory.InventoryWrapper in project BuildCraft by BuildCraft.

the class ItemSchematicSingle method onItemUseFirst.

@Override
public EnumActionResult onItemUseFirst(EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, EnumHand hand) {
    if (world.isRemote) {
        return EnumActionResult.PASS;
    }
    ItemStack stack = player.getHeldItem(hand);
    if (player.isSneaking()) {
        NBTTagCompound itemData = NBTUtilBC.getItemData(StackUtil.asNonNull(stack));
        itemData.removeTag(NBT_KEY);
        if (itemData.hasNoTags()) {
            stack.setTagCompound(null);
        }
        stack.setItemDamage(DAMAGE_CLEAN);
        return EnumActionResult.SUCCESS;
    }
    int damage = stack.getItemDamage();
    if (damage != DAMAGE_USED) {
        IBlockState state = world.getBlockState(pos);
        ISchematicBlock schematicBlock = SchematicBlockManager.getSchematicBlock(new SchematicBlockContext(world, pos, pos, state, state.getBlock()));
        if (schematicBlock.isAir()) {
            return EnumActionResult.FAIL;
        }
        NBTUtilBC.getItemData(stack).setTag(NBT_KEY, SchematicBlockManager.writeToNBT(schematicBlock));
        stack.setItemDamage(DAMAGE_USED);
        return EnumActionResult.SUCCESS;
    } else {
        BlockPos placePos = pos;
        boolean replaceable = world.getBlockState(pos).getBlock().isReplaceable(world, pos);
        if (!replaceable) {
            placePos = placePos.offset(side);
        }
        if (!world.mayPlace(world.getBlockState(pos).getBlock(), placePos, false, side, null)) {
            return EnumActionResult.FAIL;
        }
        if (replaceable && !world.isAirBlock(placePos)) {
            world.setBlockToAir(placePos);
        }
        try {
            ISchematicBlock schematicBlock = getSchematic(stack);
            if (schematicBlock != null) {
                if (!schematicBlock.isBuilt(world, placePos) && schematicBlock.canBuild(world, placePos)) {
                    List<FluidStack> requiredFluids = schematicBlock.computeRequiredFluids();
                    List<ItemStack> requiredItems = schematicBlock.computeRequiredItems();
                    if (requiredFluids.isEmpty()) {
                        InventoryWrapper itemTransactor = new InventoryWrapper(player.inventory);
                        if (StackUtil.mergeSameItems(requiredItems).stream().noneMatch(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), true).isEmpty())) {
                            if (schematicBlock.build(world, placePos)) {
                                StackUtil.mergeSameItems(requiredItems).forEach(s -> itemTransactor.extract(extracted -> StackUtil.canMerge(s, extracted), s.getCount(), s.getCount(), false));
                                SoundUtil.playBlockPlace(world, placePos);
                                player.swingArm(hand);
                                return EnumActionResult.SUCCESS;
                            }
                        } else {
                            player.sendStatusMessage(new TextComponentString("Not enough items. Total needed: " + StackUtil.mergeSameItems(requiredItems).stream().map(s -> s.getTextComponent().getFormattedText() + " x " + s.getCount()).collect(Collectors.joining(", "))), true);
                        }
                    } else {
                        player.sendStatusMessage(new TextComponentString("Schematic requires fluids"), true);
                    }
                }
            }
        } catch (InvalidInputDataException e) {
            player.sendStatusMessage(new TextComponentString("Invalid schematic: " + e.getMessage()), true);
            e.printStackTrace();
        }
        return EnumActionResult.FAIL;
    }
}
Also used : TIntObjectHashMap(gnu.trove.map.hash.TIntObjectHashMap) EnumHand(net.minecraft.util.EnumHand) ItemBC_Neptune(buildcraft.lib.item.ItemBC_Neptune) ModelResourceLocation(net.minecraft.client.renderer.block.model.ModelResourceLocation) ItemStack(net.minecraft.item.ItemStack) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) SchematicBlockManager(buildcraft.builders.snapshot.SchematicBlockManager) Side(net.minecraftforge.fml.relauncher.Side) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) Nonnull(javax.annotation.Nonnull) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BCLog(buildcraft.api.core.BCLog) World(net.minecraft.world.World) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) ActionResult(net.minecraft.util.ActionResult) TextComponentString(net.minecraft.util.text.TextComponentString) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EnumActionResult(net.minecraft.util.EnumActionResult) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) StackUtil(buildcraft.lib.misc.StackUtil) FluidStack(net.minecraftforge.fluids.FluidStack) InventoryWrapper(buildcraft.lib.inventory.InventoryWrapper) SoundUtil(buildcraft.lib.misc.SoundUtil) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) IBlockState(net.minecraft.block.state.IBlockState) InventoryWrapper(buildcraft.lib.inventory.InventoryWrapper) FluidStack(net.minecraftforge.fluids.FluidStack) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) TextComponentString(net.minecraft.util.text.TextComponentString) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack)

Aggregations

BCLog (buildcraft.api.core.BCLog)1 InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)1 ISchematicBlock (buildcraft.api.schematics.ISchematicBlock)1 SchematicBlockContext (buildcraft.api.schematics.SchematicBlockContext)1 SchematicBlockManager (buildcraft.builders.snapshot.SchematicBlockManager)1 InventoryWrapper (buildcraft.lib.inventory.InventoryWrapper)1 ItemBC_Neptune (buildcraft.lib.item.ItemBC_Neptune)1 NBTUtilBC (buildcraft.lib.misc.NBTUtilBC)1 SoundUtil (buildcraft.lib.misc.SoundUtil)1 StackUtil (buildcraft.lib.misc.StackUtil)1 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 IBlockState (net.minecraft.block.state.IBlockState)1 ModelResourceLocation (net.minecraft.client.renderer.block.model.ModelResourceLocation)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ActionResult (net.minecraft.util.ActionResult)1