Search in sources :

Example 1 with IHarvestResult

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

the class CustomSeedFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull final BlockPos pos, @Nonnull Block block, @Nonnull IBlockState state) {
    if (!canHarvest(farm, pos, block, state)) {
        return null;
    }
    if (!farm.hasTool(FarmingTool.HOE)) {
        farm.setNotification(FarmNotification.NO_HOE);
        return null;
    }
    final World world = farm.getWorld();
    final EntityPlayerMP joe = farm.startUsingItem(FarmingTool.HOE);
    final int fortune = farm.getLootingValue(FarmingTool.HOE);
    final NNList<EntityItem> result = new NNList<EntityItem>();
    NNList<ItemStack> drops = new NNList<>();
    block.getDrops(drops, world, pos, state, fortune);
    float chance = ForgeEventFactory.fireBlockHarvesting(drops, joe.world, pos, state, fortune, 1.0F, false, joe);
    farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, state, pos);
    boolean removed = false;
    for (ItemStack stack : drops) {
        if (world.rand.nextFloat() <= chance) {
            if (!removed && stack.isItemEqual(getSeeds())) {
                stack.shrink(1);
                removed = true;
                if (Prep.isValid(stack)) {
                    result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, stack.copy()));
                }
            } else {
                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 (removed) {
        if (!plant(farm, world, pos)) {
            result.add(new EntityItem(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, getSeeds().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) 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 2 with IHarvestResult

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

the class PickableFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull final BlockPos bc, @Nonnull Block block, @Nonnull IBlockState meta) {
    if (!canHarvest(farm, bc, block, meta)) {
        return null;
    }
    if (!farm.hasTool(FarmingTool.HOE)) {
        farm.setNotification(FarmNotification.NO_HOE);
        return null;
    }
    final HarvestResult result = new HarvestResult();
    final World world = farm.getWorld();
    EntityPlayerMP joe = farm.startUsingItem(FarmingTool.HOE);
    joe.interactionManager.processRightClickBlock(joe, joe.world, joe.getHeldItemMainhand(), EnumHand.MAIN_HAND, bc, EnumFacing.DOWN, 0, 0, 0);
    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()));
        }
    });
    farm.registerAction(FarmingAction.HARVEST, FarmingTool.HOE, meta, bc);
    result.getHarvestedBlocks().add(bc);
    return result;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem)

Example 3 with IHarvestResult

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

the class TreeFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull BlockPos bc, @Nonnull Block block, @Nonnull IBlockState meta) {
    setupHarvesting(farm, bc);
    if (!hasAxe) {
        farm.setNotification(FarmNotification.NO_AXE);
        return null;
    }
    final World world = farm.getWorld();
    final HarvestResult res = new HarvestResult();
    final IHarvestingTarget target = new FarmHarvestingTarget(this, farm);
    TreeHarvester.harvest(world, bc, res, target);
    Collections.sort(res.getHarvestedBlocks(), comp);
    List<BlockPos> actualHarvests = new ArrayList<BlockPos>();
    for (int i = 0; i < res.getHarvestedBlocks().size() && hasAxe; i++) {
        final BlockPos coord = res.getHarvestedBlocks().get(i);
        harvestSingleBlock(farm, world, res, coord);
        actualHarvests.add(coord);
    }
    res.getHarvestedBlocks().clear();
    res.getHarvestedBlocks().addAll(actualHarvests);
    tryReplanting(farm, world, bc, res);
    return res;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) IHarvestingTarget(crazypants.enderio.base.farming.harvesters.IHarvestingTarget) ArrayList(java.util.ArrayList) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) FarmHarvestingTarget(crazypants.enderio.base.farming.harvesters.FarmHarvestingTarget)

Example 4 with IHarvestResult

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

the class NaturaBerryFarmer method harvestBlock.

@Override
public IHarvestResult harvestBlock(@Nonnull IFarmer farm, @Nonnull BlockPos bc, @Nonnull Block block, @Nonnull IBlockState meta) {
    if (block != getPlantedBlock()) {
        return null;
    }
    if (!farm.hasTool(FarmingTool.HOE)) {
        farm.setNotification(FarmNotification.NO_HOE);
        return null;
    }
    IHarvestResult res = new HarvestResult();
    BlockPos checkBlock = bc;
    while (checkBlock != null && checkBlock.getY() <= 255 && farm.hasTool(FarmingTool.HOE)) {
        meta = farm.getBlockState(checkBlock);
        block = meta.getBlock();
        if (block != getPlantedBlock()) {
            checkBlock = null;
        } else {
            if (getFullyGrownBlockMeta() == block.getMetaFromState(meta)) {
                IHarvestResult blockRes = super.harvestBlock(farm, checkBlock, block, meta);
                if (blockRes != null) {
                    res.getHarvestedBlocks().add(checkBlock);
                    res.getDrops().addAll(blockRes.getDrops());
                }
            }
            checkBlock = checkBlock.up();
        }
    }
    if (res.getHarvestedBlocks().isEmpty()) {
        return null;
    }
    return res;
}
Also used : HarvestResult(crazypants.enderio.base.farming.farmers.HarvestResult) IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) BlockPos(net.minecraft.util.math.BlockPos) IHarvestResult(crazypants.enderio.api.farm.IHarvestResult)

Example 5 with IHarvestResult

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

the class TileFarmStation method executeHarvest.

private boolean executeHarvest(@Nonnull IFarmer farmer, @Nonnull BlockPos farmingPos, @Nonnull IBlockState bs, @Nonnull Block block) {
    IHarvestResult harvest = Commune.instance.harvestBlock(farmer, farmingPos, block, bs);
    if (harvest != null && (!harvest.getHarvestedBlocks().isEmpty() || !harvest.getDrops().isEmpty())) {
        if (!harvest.getHarvestedBlocks().isEmpty()) {
            PacketFarmAction pkt = new PacketFarmAction(harvest.getHarvestedBlocks());
            PacketHandler.sendToAllAround(pkt, this);
        }
        NNList.wrap(harvest.getDrops()).apply(new Callback<EntityItem>() {

            @Override
            public void apply(@Nonnull EntityItem ei) {
                if (!ei.isDead) {
                    ItemStack stack = ei.getItem();
                    if (Prep.isValid(stack)) {
                        getFarmer().handleExtraItem(stack, farmingPos);
                    }
                    ei.setDead();
                }
            }
        });
        return true;
    }
    return false;
}
Also used : IHarvestResult(crazypants.enderio.api.farm.IHarvestResult) 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