Search in sources :

Example 1 with TileEntityBasicChest

use of net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest in project Bookshelf by Darkhax-Minecraft.

the class ItemBlockChest method placeBlockAt.

@Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ, IBlockState newState) {
    int typeCnt = 0;
    final BlockPos posN = pos.north();
    final BlockPos posS = pos.south();
    final BlockPos posW = pos.west();
    final BlockPos posE = pos.east();
    final BlockBasicChest cChest = (BlockBasicChest) this.getBlock();
    final IChestType myType = cChest.getCustomType(stack);
    if (world.getBlockState(posN).getBlock() == this.block && cChest.getCustomType(world, posN) == myType) {
        typeCnt += cChest.isDoubleChest(world, posN, myType) ? 2 : 1;
    }
    if (world.getBlockState(posS).getBlock() == this.block && cChest.getCustomType(world, posS) == myType) {
        typeCnt += cChest.isDoubleChest(world, posS, myType) ? 2 : 1;
    }
    if (world.getBlockState(posW).getBlock() == this.block && cChest.getCustomType(world, posW) == myType) {
        typeCnt += cChest.isDoubleChest(world, posW, myType) ? 2 : 1;
    }
    if (world.getBlockState(posE).getBlock() == this.block && cChest.getCustomType(world, posE) == myType) {
        typeCnt += cChest.isDoubleChest(world, posE, myType) ? 2 : 1;
    }
    if (typeCnt <= 1 && super.placeBlockAt(stack, player, world, pos, side, hitX, hitY, hitZ, newState)) {
        final TileEntity te = world.getTileEntity(pos);
        if (te instanceof TileEntityBasicChest) {
            ((TileEntityBasicChest) te).setType(myType);
            return true;
        }
    }
    return false;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IChestType(net.darkhax.bookshelf.builder.ChestBuilder.IChestType) TileEntityBasicChest(net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest) BlockPos(net.minecraft.util.math.BlockPos) BlockBasicChest(net.darkhax.bookshelf.block.BlockBasicChest)

Example 2 with TileEntityBasicChest

use of net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest in project Bookshelf by Darkhax-Minecraft.

the class BlockBasicChest method getContainer.

@Override
public ILockableContainer getContainer(World world, BlockPos pos, boolean locked) {
    final TileEntity tile = world.getTileEntity(pos);
    if (!(tile instanceof TileEntityBasicChest)) {
        return null;
    } else {
        ILockableContainer myChest = (TileEntityBasicChest) tile;
        final IChestType myType = ((TileEntityBasicChest) tile).getType();
        if (!locked && this.isBlocked(world, pos)) {
            return null;
        } else {
            for (final EnumFacing facing : EnumFacing.Plane.HORIZONTAL) {
                final BlockPos adjPos = pos.offset(facing);
                final TileEntity adjTile = world.getTileEntity(adjPos);
                if (world.getBlockState(adjPos).getBlock() == this && adjTile instanceof TileEntityBasicChest && ((TileEntityBasicChest) adjTile).getType() == myType) {
                    if (this.isBlocked(world, adjPos)) {
                        return null;
                    }
                    if (facing != EnumFacing.WEST && facing != EnumFacing.NORTH) {
                        myChest = new InventoryLargeChest("container.chestDouble", myChest, (TileEntityBasicChest) adjTile);
                    } else {
                        myChest = new InventoryLargeChest("container.chestDouble", (TileEntityBasicChest) adjTile, myChest);
                    }
                }
            }
            return myChest;
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) ILockableContainer(net.minecraft.world.ILockableContainer) IChestType(net.darkhax.bookshelf.builder.ChestBuilder.IChestType) InventoryLargeChest(net.minecraft.inventory.InventoryLargeChest) EnumFacing(net.minecraft.util.EnumFacing) TileEntityBasicChest(net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest) BlockPos(net.minecraft.util.math.BlockPos)

Example 3 with TileEntityBasicChest

use of net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest in project Bookshelf by Darkhax-Minecraft.

the class BlockBasicChest method onBlockPlacedBy.

@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) {
    final EnumFacing facing = EnumFacing.getHorizontal(MathHelper.floor(placer.rotationYaw * 4.0F / 360.0F + 0.5D) & 3).getOpposite();
    state = state.withProperty(FACING, facing);
    final BlockPos northPos = pos.north();
    final BlockPos southPos = pos.south();
    final BlockPos westPos = pos.west();
    final BlockPos eastPos = pos.east();
    final IChestType myType = this.getCustomType(stack);
    final boolean northChest = myType == this.getCustomType(worldIn, northPos);
    final boolean southChest = myType == this.getCustomType(worldIn, southPos);
    final boolean westChest = myType == this.getCustomType(worldIn, westPos);
    final boolean eastChest = myType == this.getCustomType(worldIn, eastPos);
    if (!northChest && !southChest && !westChest && !eastChest) {
        worldIn.setBlockState(pos, state, 3);
    } else if (facing.getAxis() != EnumFacing.Axis.X || !northChest && !southChest) {
        if (facing.getAxis() == EnumFacing.Axis.Z && (westChest || eastChest)) {
            if (westChest) {
                this.setState(worldIn, westPos, state, 3);
            } else {
                this.setState(worldIn, eastPos, state, 3);
            }
            worldIn.setBlockState(pos, state, 3);
        } else {
            final EnumFacing corrected = facing.rotateY();
            this.setState(worldIn, pos, state.withProperty(FACING, corrected), 3);
            if (northChest) {
                this.setState(worldIn, northPos, state.withProperty(FACING, corrected), 3);
            } else if (southChest) {
                this.setState(worldIn, southPos, state.withProperty(FACING, corrected), 3);
            } else if (westChest) {
                this.setState(worldIn, westPos, state.withProperty(FACING, corrected), 3);
            } else if (eastChest) {
                this.setState(worldIn, eastPos, state.withProperty(FACING, corrected), 3);
            }
        }
    } else {
        if (northChest) {
            this.setState(worldIn, northPos, state, 3);
        } else {
            this.setState(worldIn, southPos, state, 3);
        }
        worldIn.setBlockState(pos, state, 3);
    }
    final TileEntity te = worldIn.getTileEntity(pos);
    if (te instanceof TileEntityBasicChest) {
        final TileEntityBasicChest chest = (TileEntityBasicChest) te;
        if (stack.hasDisplayName()) {
            chest.setCustomName(stack.getDisplayName());
        }
        chest.setType(myType);
    }
    this.onBlockAdded(worldIn, pos, state);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IChestType(net.darkhax.bookshelf.builder.ChestBuilder.IChestType) EnumFacing(net.minecraft.util.EnumFacing) TileEntityBasicChest(net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest) BlockPos(net.minecraft.util.math.BlockPos)

Example 4 with TileEntityBasicChest

use of net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest in project Bookshelf by Darkhax-Minecraft.

the class BlockBasicChest method setState.

/**
     * Sets the state of the block, and updates the chest connections.
     *
     * @param worldIn The world instance.
     * @param pos The position.
     * @param state The state.
     * @param flag The update flags.
     */
public void setState(World worldIn, BlockPos pos, IBlockState state, int flag) {
    final TileEntity te = worldIn.getTileEntity(pos);
    worldIn.setBlockState(pos, state, flag);
    if (te != null) {
        te.validate();
        worldIn.setTileEntity(pos, te);
        if (te instanceof TileEntityBasicChest) {
            ((TileEntityBasicChest) te).adjacentChestChecked = false;
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityBasicChest(net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest)

Aggregations

TileEntityBasicChest (net.darkhax.bookshelf.block.tileentity.TileEntityBasicChest)4 TileEntity (net.minecraft.tileentity.TileEntity)4 IChestType (net.darkhax.bookshelf.builder.ChestBuilder.IChestType)3 BlockPos (net.minecraft.util.math.BlockPos)3 EnumFacing (net.minecraft.util.EnumFacing)2 BlockBasicChest (net.darkhax.bookshelf.block.BlockBasicChest)1 InventoryLargeChest (net.minecraft.inventory.InventoryLargeChest)1 ILockableContainer (net.minecraft.world.ILockableContainer)1