make hoe harvest faster

This commit is contained in:
Jörg Henke 2022-01-01 23:17:15 +01:00
parent e4401d2b3d
commit 7f46b71e2f
2 changed files with 34 additions and 8 deletions

View File

@ -9,7 +9,7 @@
loader_version=0.12.12
# Mod Properties
mod_version = 1.18.1.1
mod_version = 1.18.1.2
maven_group = de.jottyfan.minecraft
archives_base_name = quickiefabric

View File

@ -2,6 +2,9 @@ package de.jottyfan.minecraft.quickiefabric.tools;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.common.collect.Lists;
import de.jottyfan.minecraft.quickiefabric.init.RegistryManager;
@ -9,6 +12,7 @@ import net.fabricmc.fabric.api.tool.attribute.v1.ToolManager;
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.Item;
import net.minecraft.item.ItemStack;
@ -16,15 +20,20 @@ import net.minecraft.item.ItemUsageContext;
import net.minecraft.item.ToolMaterials;
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 class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
private static final Logger LOGGER = LogManager.getLogger(ToolSpeedpowderHoe.class);
public static final Integer DEFAULT_PLOW_RANGE = 4;
public HarvestRange range;
@ -36,14 +45,19 @@ public class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
@Override
public ActionResult useOnBlock(ItemUsageContext context) {
ActionResult res = super.useOnBlock(context);
if (!ActionResult.PASS.equals(res)) {
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++) {
BlockHitResult bhr = new BlockHitResult(context.getHitPos(), Direction.UP,
context.getBlockPos().add(new Vec3i(x, y, z)), isDamageable());
ItemUsageContext ctx = new ItemUsageContext(context.getPlayer(), context.getHand(), bhr);
super.useOnBlock(ctx);
if (!isCrop) {
BlockHitResult bhr = new BlockHitResult(context.getHitPos(), Direction.UP,
context.getBlockPos().add(new Vec3i(x, y, z)), isDamageable());
ItemUsageContext ctx = new ItemUsageContext(context.getPlayer(), context.getHand(), bhr);
super.useOnBlock(ctx);
} else {
harvestIfPossible(context.getBlockPos().add(x, y, z), context.getWorld());
}
}
}
}
@ -51,6 +65,18 @@ public class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
return res;
}
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
@ -59,7 +85,7 @@ public class ToolSpeedpowderHoe extends HoeItem implements ToolRangeable {
@Override
public boolean canBreakNeighbors(BlockState blockState) {
return ToolManager.handleIsEffectiveOn(blockState, new ItemStack(this), null)
return ToolManager.handleIsEffectiveOn(blockState, new ItemStack(this), null)
|| Blocks.GRASS.equals(blockState.getBlock());
}