package de.jottyfan.quickiemod.item; import java.util.List; import com.google.common.collect.Lists; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.CropBlock; import net.minecraft.item.HoeItem; import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUsageContext; import net.minecraft.item.ToolMaterial; import net.minecraft.util.ActionResult; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; /** * * @author jotty * */ public abstract class ToolRangeableHoe extends HoeItem implements ToolRangeable { public HarvestRange range; public ToolRangeableHoe(ToolMaterial material, float attackDamage, float attackSpeed, Settings settings, HarvestRange range) { super(material, attackDamage, attackSpeed, settings); this.range = range; } @Override public ActionResult useOnBlock(ItemUsageContext context) { ActionResult res = super.useOnBlock(context); boolean isCrop = context.getWorld().getBlockState(context.getBlockPos()).getBlock() instanceof CropBlock; if (!ActionResult.PASS.equals(res) || isCrop) { for (int x = -this.range.getxRange(); x <= this.range.getxRange(); x++) { for (int y = -this.range.getyRange(); y <= this.range.getyRange(); y++) { for (int z = -this.range.getzRange(); z <= this.range.getzRange(); z++) { if (!isCrop) { removePossibleGrass(context.getWorld(), new BlockPos(x, y, z)); BlockHitResult bhr = new BlockHitResult(context.getHitPos(), Direction.UP, context.getBlockPos().add(new Vec3i(x, y, z)), false); ItemUsageContext ctx = new ItemUsageContext(context.getPlayer(), context.getHand(), bhr); super.useOnBlock(ctx); } else { harvestIfPossible(context.getBlockPos().add(x, y, z), context.getWorld()); } } } } } return res; } private void removePossibleGrass(World world, BlockPos pos) { Block block = world.getBlockState(pos).getBlock(); Boolean grassFound = Blocks.FERN.equals(block) || Blocks.LARGE_FERN.equals(block) || Blocks.SHORT_GRASS.equals(block) || Blocks.TALL_GRASS.equals(block); if (grassFound) { world.breakBlock(pos, true); } } private void harvestIfPossible(BlockPos pos, World world) { BlockState blockState = world.getBlockState(pos); Block block = blockState.getBlock(); if (block instanceof CropBlock) { CropBlock cBlock = (CropBlock) block; if (cBlock.isMature(blockState)) { Block.dropStacks(blockState, world, pos); world.setBlockState(pos, cBlock.withAge(0)); } } } @Override public HarvestRange getRange(ItemStack stack) { // TODO: get range from stack return range; } @Override public boolean canBreakNeighbors(BlockState blockState) { return new ItemStack(this).isSuitableFor(blockState) || Blocks.TALL_GRASS.equals(blockState.getBlock()) || Blocks.FERN.equals(blockState.getBlock()) || Blocks.LARGE_FERN.equals(blockState.getBlock()); } @Override public List getBlockList(Block block) { return Lists.newArrayList(block); } }