Search in sources :

Example 1 with SchematicBlockContext

use of buildcraft.api.schematics.SchematicBlockContext in project BuildCraft by BuildCraft.

the class SchematicBlockDefault method setRequiredBlockOffsets.

@SuppressWarnings({ "unused", "WeakerAccess" })
protected void setRequiredBlockOffsets(SchematicBlockContext context, Set<JsonRule> rules) {
    requiredBlockOffsets.clear();
    rules.stream().map(rule -> rule.requiredBlockOffsets).filter(Objects::nonNull).flatMap(Collection::stream).forEach(requiredBlockOffsets::add);
    if (context.block instanceof BlockFalling) {
        requiredBlockOffsets.add(new BlockPos(0, -1, 0));
    }
}
Also used : Blocks(net.minecraft.init.Blocks) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ItemStack(net.minecraft.item.ItemStack) NBTUtilBC(buildcraft.lib.misc.NBTUtilBC) SchematicBlockContext(buildcraft.api.schematics.SchematicBlockContext) IProperty(net.minecraft.block.properties.IProperty) Pair(org.apache.commons.lang3.tuple.Pair) Block(net.minecraft.block.Block) Nonnull(javax.annotation.Nonnull) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockFalling(net.minecraft.block.BlockFalling) BlockUtil(buildcraft.lib.misc.BlockUtil) Predicate(java.util.function.Predicate) World(net.minecraft.world.World) Collection(java.util.Collection) NBTBase(net.minecraft.nbt.NBTBase) InvalidInputDataException(buildcraft.api.core.InvalidInputDataException) Set(java.util.Set) EnumFacing(net.minecraft.util.EnumFacing) BlockPos(net.minecraft.util.math.BlockPos) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IBlockState(net.minecraft.block.state.IBlockState) List(java.util.List) Stream(java.util.stream.Stream) Rotation(net.minecraft.util.Rotation) ResourceLocation(net.minecraft.util.ResourceLocation) ISchematicBlock(buildcraft.api.schematics.ISchematicBlock) TileEntity(net.minecraft.tileentity.TileEntity) FluidStack(net.minecraftforge.fluids.FluidStack) NBTUtil(net.minecraft.nbt.NBTUtil) Objects(java.util.Objects) BlockFalling(net.minecraft.block.BlockFalling) BlockPos(net.minecraft.util.math.BlockPos)

Example 2 with SchematicBlockContext

use of buildcraft.api.schematics.SchematicBlockContext 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

InvalidInputDataException (buildcraft.api.core.InvalidInputDataException)2 ISchematicBlock (buildcraft.api.schematics.ISchematicBlock)2 SchematicBlockContext (buildcraft.api.schematics.SchematicBlockContext)2 NBTUtilBC (buildcraft.lib.misc.NBTUtilBC)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Nonnull (javax.annotation.Nonnull)2 IBlockState (net.minecraft.block.state.IBlockState)2 ItemStack (net.minecraft.item.ItemStack)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 EnumFacing (net.minecraft.util.EnumFacing)2 BlockPos (net.minecraft.util.math.BlockPos)2 World (net.minecraft.world.World)2 FluidStack (net.minecraftforge.fluids.FluidStack)2 BCLog (buildcraft.api.core.BCLog)1 SchematicBlockManager (buildcraft.builders.snapshot.SchematicBlockManager)1 InventoryWrapper (buildcraft.lib.inventory.InventoryWrapper)1 ItemBC_Neptune (buildcraft.lib.item.ItemBC_Neptune)1 BlockUtil (buildcraft.lib.misc.BlockUtil)1 SoundUtil (buildcraft.lib.misc.SoundUtil)1