Search in sources :

Example 6 with IHarvestResult

use of crazypants.enderio.api.farm.IHarvestResult in project EnderIO by SleepyTrousers.

the class ChorusFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull BlockPos bc, @Nonnull Block block, @Nonnull IBlockState state) {
    if (!farm.hasTool(FarmingTool.AXE)) {
        farm.setNotification(FarmNotification.NO_AXE);
        return null;
    }
    ChorusWalker walker = new ChorusWalker(farm, bc);
    if (walker.flowersToHarvest.isEmpty() && walker.stemsToHarvest.isEmpty()) {
        return null;
    }
    World world = farm.getWorld();
    final int fortune = farm.getLootingValue(FarmingTool.AXE);
    HarvestResult result = new HarvestResult();
    while (!walker.flowersToHarvest.isEmpty() && farm.checkAction(FarmingAction.HARVEST, FarmingTool.AXE)) {
        BlockPos remove = walker.flowersToHarvest.remove(0);
        doHarvest(farm, world, farm.getBlockState(remove), remove, fortune, result);
    }
    while (!walker.stemsToHarvest.isEmpty() && farm.checkAction(FarmingAction.HARVEST, FarmingTool.AXE)) {
        // Note: While stems can be broken by any tool, I decided to use the axe. This way a chorus farm can be used without hoe and the various auto-smelt
        // enchantments on axes can be used for the fruits.
        BlockPos remove = walker.stemsToHarvest.remove(walker.stemsToHarvest.size() - 1);
        doHarvest(farm, world, farm.getBlockState(remove), remove, fortune, result);
    }
    return result.getDrops().isEmpty() && result.getHarvestedBlocks().isEmpty() ? null : result;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World)

Example 7 with IHarvestResult

use of crazypants.enderio.api.farm.IHarvestResult in project EnderIO by SleepyTrousers.

the class FlowerPicker method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull final IFarmer farm, @Nonnull final BlockPos pos, @Nonnull Block block, @Nonnull IBlockState meta) {
    final World world = farm.getWorld();
    NNList<ItemStack> drops = new NNList<>();
    if (block instanceof IShearable) {
        if (!farm.hasTool(FarmingTool.SHEARS)) {
            farm.setNotification(FarmNotification.NO_SHEARS);
            return null;
        }
        ItemStack shears = farm.getTool(FarmingTool.SHEARS);
        if (!((IShearable) block).isShearable(shears, world, pos)) {
            return null;
        }
        drops.addAll(((IShearable) block).onSheared(shears, world, pos, farm.getLootingValue(FarmingTool.SHEARS)));
        farm.registerAction(FarmingAction.HARVEST, FarmingTool.SHEARS, meta, pos);
    } else {
        if (!farm.hasTool(FarmingTool.HOE)) {
            farm.setNotification(FarmNotification.NO_HOE);
            return null;
        }
        block.getDrops(drops, world, pos, meta, farm.getLootingValue(FarmingTool.HOE));
        farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, meta, pos);
    }
    final NNList<EntityItem> result = new NNList<EntityItem>();
    NNList.wrap(drops).apply(new Callback<ItemStack>() {

        @Override
        public void apply(@Nonnull ItemStack drop) {
            result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, drop.copy()));
        }
    });
    world.setBlockToAir(pos);
    return new HarvestResult(result, pos);
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) NNList(com.enderio.core.common.util.NNList) IShearable(net.minecraftforge.common.IShearable) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 8 with IHarvestResult

use of crazypants.enderio.api.farm.IHarvestResult in project EnderIO by SleepyTrousers.

the class PlantableFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull final BlockPos pos, @Nonnull Block block, @Nonnull IBlockState meta) {
    if (!canHarvest(farm, pos, block, meta)) {
        return null;
    }
    if (!farm.hasTool(FarmingTool.HOE)) {
        farm.setNotification(FarmNotification.NO_HOE);
        return null;
    }
    final World world = farm.getWorld();
    final NNList<EntityItem> result = new NNList<EntityItem>();
    final EntityPlayerMP fakePlayer = farm.startUsingItem(FarmingTool.HOE);
    final int fortune = farm.getLootingValue(FarmingTool.HOE);
    ItemStack removedPlantable = Prep.getEmpty();
    NNList<ItemStack> drops = new NNList<>();
    block.getDrops(drops, world, pos, meta, fortune);
    float chance = ForgeEventFactory.fireBlockHarvesting(drops, world, pos, meta, fortune, 1.0F, false, fakePlayer);
    farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, meta, pos);
    for (ItemStack stack : drops) {
        if (stack != null && Prep.isValid(stack) && world.rand.nextFloat() <= chance) {
            if (Prep.isInvalid(removedPlantable) && isPlantableForBlock(stack, block)) {
                removedPlantable = stack.copy();
                removedPlantable.setCount(1);
                stack.shrink(1);
            }
            if (Prep.isValid(stack)) {
                result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, stack.copy()));
            }
        }
    }
    NNList.wrap(farm.endUsingItem(FarmingTool.HOE)).apply(new Callback<ItemStack>() {

        @Override
        public void apply(@Nonnull ItemStack drop) {
            result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, drop.copy()));
        }
    });
    if (Prep.isValid(removedPlantable)) {
        if (!plant(farm, world, pos, (IPlantable) removedPlantable.getItem())) {
            result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, removedPlantable.copy()));
            world.setBlockState(pos, Blocks.AIR.getDefaultState(), 1 | 2);
        }
    } else {
        world.setBlockState(pos, Blocks.AIR.getDefaultState(), 1 | 2);
    }
    return new HarvestResult(result, pos);
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) IPlantable(net.minecraftforge.common.IPlantable) World(net.minecraft.world.World) NNList(com.enderio.core.common.util.NNList) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 9 with IHarvestResult

use of crazypants.enderio.api.farm.IHarvestResult in project EnderIO by SleepyTrousers.

the class RubberTreeFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull final IFarmer farm, @Nonnull BlockPos pos, @Nonnull Block block, @Nonnull IBlockState meta) {
    final HarvestResult res = new HarvestResult();
    final World world = farm.getWorld();
    setupHarvesting(farm, pos);
    while (pos.getY() <= 255) {
        IBlockState state = world.getBlockState(pos);
        if (isWood(state.getBlock())) {
            if (canHarvest(world, pos)) {
                if (farm.hasTool(FarmingTool.TREETAP)) {
                    harvest(res, world, pos);
                    farm.registerAction(FarmingAction.HARVEST, FarmingTool.TREETAP, state, pos);
                } else {
                    farm.setNotification(FarmNotification.NO_TREETAP);
                }
            }
            harvestLeavesAround(farm, world, res, pos);
        } else if (IHarvestingTarget.isDefaultLeaves(state)) {
            harvestLeavesBlock(farm, res, world, pos);
        } else {
            return res;
        }
        pos = pos.up();
    }
    return res;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) IBlockState(net.minecraft.block.state.IBlockState) World(net.minecraft.world.World)

Example 10 with IHarvestResult

use of crazypants.enderio.api.farm.IHarvestResult in project EnderIO by SleepyTrousers.

the class StemFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull final IFarmer farm, @Nonnull final BlockPos bc, @Nonnull Block block, @Nonnull IBlockState meta) {
    boolean hasHoe = farm.hasTool(FarmingTool.HOE);
    if (!hasHoe) {
        return new HarvestResult();
    }
    final World world = farm.getWorld();
    final EntityPlayerMP joe = farm.startUsingItem(FarmingTool.HOE);
    final int fortune = farm.getLootingValue(FarmingTool.HOE);
    final HarvestResult result = new HarvestResult();
    BlockPos harvestCoord = bc;
    boolean done = false;
    do {
        harvestCoord = harvestCoord.offset(EnumFacing.UP);
        final IBlockState harvestState = farm.getBlockState(harvestCoord);
        if (hasHoe && plantedBlock == harvestState.getBlock()) {
            result.getHarvestedBlocks().add(harvestCoord);
            NNList<ItemStack> drops = new NNList<>();
            plantedBlock.getDrops(drops, world, harvestCoord, meta, fortune);
            float chance = ForgeEventFactory.fireBlockHarvesting(drops, joe.world, harvestCoord, meta, fortune, 1.0F, false, joe);
            BlockPos farmPos = farm.getLocation();
            for (ItemStack drop : drops) {
                if (world.rand.nextFloat() <= chance) {
                    result.getDrops().add(new EntityItem(world, farmPos.getX() + 0.5, farmPos.getY() + 0.5, farmPos.getZ() + 0.5, drop.copy()));
                }
            }
            farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, harvestState, harvestCoord);
            hasHoe = farm.hasTool(FarmingTool.HOE);
        } else {
            if (!hasHoe) {
                farm.setNotification(FarmNotification.NO_HOE);
            }
            done = true;
        }
    } while (!done);
    NNList.wrap(farm.endUsingItem(FarmingTool.HOE)).apply(new Callback<ItemStack>() {

        @Override
        public void apply(@Nonnull ItemStack drop) {
            result.getDrops().add(new EntityItem(world, bc.getX() + 0.5, bc.getY() + 0.5, bc.getZ() + 0.5, drop.copy()));
        }
    });
    NNList<BlockPos> toClear = new NNList<BlockPos>(result.getHarvestedBlocks());
    Collections.sort(toClear, COMP);
    toClear.apply(new Callback<BlockPos>() {

        @Override
        public void apply(@Nonnull BlockPos coord) {
            farm.getWorld().setBlockToAir(coord);
        }
    });
    return result;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) IBlockState(net.minecraft.block.state.IBlockState) World(net.minecraft.world.World) NNList(com.enderio.core.common.util.NNList) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Aggregations

IHarvestResult (crazypants.enderio.api.farm.IHarvestResult)10 World (net.minecraft.world.World)8 EntityItem (net.minecraft.entity.item.EntityItem)6 ItemStack (net.minecraft.item.ItemStack)6 NNList (com.enderio.core.common.util.NNList)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 BlockPos (net.minecraft.util.math.BlockPos)4 IBlockState (net.minecraft.block.state.IBlockState)2 HarvestResult (crazypants.enderio.base.farming.farmers.HarvestResult)1 FarmHarvestingTarget (crazypants.enderio.base.farming.harvesters.FarmHarvestingTarget)1 IHarvestingTarget (crazypants.enderio.base.farming.harvesters.IHarvestingTarget)1 ArrayList (java.util.ArrayList)1 IPlantable (net.minecraftforge.common.IPlantable)1 IShearable (net.minecraftforge.common.IShearable)1