Search in sources :

Example 1 with ITerraformableBlock

use of micdoodle8.mods.galacticraft.api.block.ITerraformableBlock in project Galacticraft by micdoodle8.

the class TileEntityTerraformer method update.

@Override
public void update() {
    super.update();
    if (!this.worldObj.isRemote) {
        final FluidStack liquid = FluidUtil.getFluidContained(this.containingItems[0]);
        if (FluidUtil.isFluidStrict(liquid, FluidRegistry.WATER.getName())) {
            FluidUtil.loadFromContainer(waterTank, FluidRegistry.WATER, containingItems, 0, liquid.amount);
        }
        this.active = this.bubbleSize == this.MAX_SIZE && this.hasEnoughEnergyToRun && this.getFirstBonemealStack() != null && this.waterTank.getFluid() != null && this.waterTank.getFluid().amount > 0;
    }
    if (!this.worldObj.isRemote && (this.active != this.lastActive || this.ticks % 60 == 0)) {
        this.terraformableBlocksList.clear();
        this.grassBlockList.clear();
        if (this.active) {
            int bubbleSize = (int) Math.ceil(this.bubbleSize);
            double bubbleSizeSq = this.bubbleSize;
            bubbleSizeSq *= bubbleSizeSq;
            boolean doGrass = !this.grassDisabled && this.getFirstSeedStack() != null;
            boolean doTrees = !this.treesDisabled && this.getFirstSaplingStack() != null;
            for (int x = this.getPos().getX() - bubbleSize; x < this.getPos().getX() + bubbleSize; x++) {
                for (int y = this.getPos().getY() - bubbleSize; y < this.getPos().getY() + bubbleSize; y++) {
                    for (int z = this.getPos().getZ() - bubbleSize; z < this.getPos().getZ() + bubbleSize; z++) {
                        BlockPos pos = new BlockPos(x, y, z);
                        Block blockID = this.worldObj.getBlockState(pos).getBlock();
                        if (blockID == null) {
                            continue;
                        }
                        if (!(blockID.isAir(this.worldObj, pos)) && this.getDistanceFromServer(x, y, z) < bubbleSizeSq) {
                            if (doGrass && blockID instanceof ITerraformableBlock && ((ITerraformableBlock) blockID).isTerraformable(this.worldObj, pos)) {
                                this.terraformableBlocksList.add(new BlockPos(x, y, z));
                            } else if (doTrees) {
                                Block blockIDAbove = this.worldObj.getBlockState(pos.up()).getBlock();
                                if (blockID == Blocks.grass && blockIDAbove.isAir(this.worldObj, pos.up())) {
                                    this.grassBlockList.add(new BlockPos(x, y, z));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!this.worldObj.isRemote && this.terraformableBlocksList.size() > 0 && this.ticks % 15 == 0) {
        ArrayList<BlockPos> terraformableBlocks2 = new ArrayList<BlockPos>(this.terraformableBlocksList);
        int randomIndex = this.worldObj.rand.nextInt(this.terraformableBlocksList.size());
        BlockPos vec = terraformableBlocks2.get(randomIndex);
        if (this.worldObj.getBlockState(vec).getBlock() instanceof ITerraformableBlock) {
            Block id;
            switch(this.worldObj.rand.nextInt(40)) {
                case 0:
                    if (this.worldObj.isBlockFullCube(new BlockPos(vec.getX() - 1, vec.getY(), vec.getZ())) && this.worldObj.isBlockFullCube(new BlockPos(vec.getX() + 1, vec.getY(), vec.getZ())) && this.worldObj.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() - 1)) && this.worldObj.isBlockFullCube(new BlockPos(vec.getX(), vec.getY(), vec.getZ() + 1))) {
                        id = Blocks.flowing_water;
                    } else {
                        id = Blocks.grass;
                    }
                    break;
                default:
                    id = Blocks.grass;
                    break;
            }
            this.worldObj.setBlockState(vec, id.getDefaultState());
            if (id == Blocks.grass) {
                this.useCount[0]++;
                this.waterTank.drain(1, true);
                this.checkUsage(1);
            } else if (id == Blocks.flowing_water) {
                this.checkUsage(2);
            }
        }
        this.terraformableBlocksList.remove(randomIndex);
    }
    if (!this.worldObj.isRemote && !this.treesDisabled && this.grassBlockList.size() > 0 && this.ticks % 50 == 0) {
        int randomIndex = this.worldObj.rand.nextInt(this.grassBlockList.size());
        BlockPos vecGrass = grassBlockList.get(randomIndex);
        if (this.worldObj.getBlockState(vecGrass).getBlock() == Blocks.grass) {
            BlockPos vecSapling = vecGrass.add(0, 1, 0);
            ItemStack sapling = this.getFirstSaplingStack();
            boolean flag = false;
            // Attempt to prevent placement too close to other trees
            for (BlockPos testVec : this.grownTreesList) {
                if (testVec.distanceSq(vecSapling) < 9) {
                    flag = true;
                    break;
                }
            }
            if (!flag && sapling != null) {
                Block b = Block.getBlockFromItem(sapling.getItem());
                this.worldObj.setBlockState(vecSapling, b.getStateFromMeta(sapling.getItemDamage()), 3);
                if (b instanceof BlockSapling) {
                    if (this.worldObj.getLightFromNeighbors(vecSapling) >= 9) {
                        ((BlockSapling) b).grow(this.worldObj, vecSapling, this.worldObj.getBlockState(vecSapling), this.worldObj.rand);
                        this.grownTreesList.add(new BlockPos(vecSapling.getX(), vecSapling.getY(), vecSapling.getZ()));
                    }
                } else if (b instanceof BlockBush) {
                    if (this.worldObj.getLightFromNeighbors(vecSapling) >= 5) // Hammer the update tick a few times to try to get it to grow - it won't always
                    {
                        for (int j = 0; j < 12; j++) {
                            if (this.worldObj.getBlockState(vecSapling).getBlock() == b) {
                                ((BlockBush) b).updateTick(this.worldObj, vecSapling, this.worldObj.getBlockState(vecSapling), this.worldObj.rand);
                            } else {
                                this.grownTreesList.add(new BlockPos(vecSapling.getX(), vecSapling.getY(), vecSapling.getZ()));
                                break;
                            }
                        }
                    }
                }
                this.useCount[1]++;
                this.waterTank.drain(50, true);
                this.checkUsage(0);
            }
        }
        this.grassBlockList.remove(randomIndex);
    }
    if (!this.worldObj.isRemote) {
        this.terraformableBlocksListSize = this.terraformableBlocksList.size();
        this.grassBlocksListSize = this.grassBlockList.size();
    }
    if (this.hasEnoughEnergyToRun && (!this.grassDisabled || !this.treesDisabled)) {
        this.bubbleSize = (float) Math.min(Math.max(0, this.bubbleSize + 0.1F), this.MAX_SIZE);
    } else {
        this.bubbleSize = (float) Math.min(Math.max(0, this.bubbleSize - 0.1F), this.MAX_SIZE);
    }
    this.lastActive = this.active;
}
Also used : BlockSapling(net.minecraft.block.BlockSapling) ArrayList(java.util.ArrayList) Block(net.minecraft.block.Block) ITerraformableBlock(micdoodle8.mods.galacticraft.api.block.ITerraformableBlock) BlockPos(net.minecraft.util.BlockPos) ITerraformableBlock(micdoodle8.mods.galacticraft.api.block.ITerraformableBlock) ItemStack(net.minecraft.item.ItemStack) BlockBush(net.minecraft.block.BlockBush)

Aggregations

ArrayList (java.util.ArrayList)1 ITerraformableBlock (micdoodle8.mods.galacticraft.api.block.ITerraformableBlock)1 Block (net.minecraft.block.Block)1 BlockBush (net.minecraft.block.BlockBush)1 BlockSapling (net.minecraft.block.BlockSapling)1 ItemStack (net.minecraft.item.ItemStack)1 BlockPos (net.minecraft.util.BlockPos)1