111 lines
3.9 KiB
Java
111 lines
3.9 KiB
Java
package de.jottyfan.minecraft.item;
|
|
|
|
import java.util.List;
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.world.Containers;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.item.Item;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.item.ToolMaterial;
|
|
import net.minecraft.world.item.context.UseOnContext;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.Blocks;
|
|
import net.minecraft.world.level.block.CropBlock;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.level.storage.loot.LootParams;
|
|
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.Vec3;
|
|
|
|
/**
|
|
*
|
|
* @author jotty
|
|
*
|
|
*/
|
|
public class QHoe extends Item implements ToolRangeable {
|
|
public QIP properties;
|
|
|
|
public QHoe(QIP properties) {
|
|
super(properties.hoe(ToolMaterial.DIAMOND, 0.0f, -3.0f));
|
|
this.properties = properties;
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult useOn(UseOnContext context) {
|
|
InteractionResult res = super.useOn(context);
|
|
Block block = context.getLevel().getBlockState(context.getClickedPos()).getBlock();
|
|
boolean isCrop = block instanceof CropBlock;
|
|
if (!InteractionResult.PASS.equals(res) || isCrop) {
|
|
HarvestRange range = properties.getHarvestRange();
|
|
for (int x = -range.getxRange(); x <= range.getxRange(); x++) {
|
|
for (int y = -range.getyRange(); y <= range.getyRange(); y++) {
|
|
for (int z = -range.getzRange(); z <= range.getzRange(); z++) {
|
|
if (!isCrop) {
|
|
removePossibleGrass(context.getLevel(), new BlockPos(x, y, z));
|
|
BlockHitResult bhr = new BlockHitResult(Vec3.atCenterOf(context.getClickedPos()), Direction.UP,
|
|
context.getClickedPos().offset(x, y, z), false);
|
|
UseOnContext ctx = new UseOnContext(context.getPlayer(), context.getHand(), bhr);
|
|
super.useOn(ctx);
|
|
} else {
|
|
harvestIfPossible(context.getClickedPos().offset(x, y, z), context.getLevel());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return InteractionResult.SUCCESS;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
private void removePossibleGrass(Level level, BlockPos pos) {
|
|
Block block = level.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) {
|
|
level.destroyBlock(pos, true);
|
|
}
|
|
}
|
|
|
|
private void harvestIfPossible(BlockPos pos, Level level) {
|
|
if (!level.isClientSide()) {
|
|
BlockState blockState = level.getBlockState(pos);
|
|
Block block = blockState.getBlock();
|
|
if (block instanceof CropBlock cBlock) {
|
|
if (cBlock.isMaxAge(blockState)) {
|
|
LootParams.Builder builder = new LootParams.Builder((ServerLevel) level)
|
|
.withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
|
|
.withParameter(LootContextParams.BLOCK_STATE, blockState)
|
|
.withOptionalParameter(LootContextParams.BLOCK_ENTITY, level.getBlockEntity(pos))
|
|
.withParameter(LootContextParams.TOOL, ItemStack.EMPTY);
|
|
for (ItemStack stack : blockState.getDrops(builder)) {
|
|
Containers.dropItemStack(level, pos.getX(), pos.getY(), pos.getZ(), stack);
|
|
}
|
|
level.setBlockAndUpdate(pos, cBlock.defaultBlockState());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean canBreakNeighbors(BlockState blockState) {
|
|
return new ItemStack(this).isCorrectToolForDrops(blockState) || Blocks.TALL_GRASS.equals(blockState.getBlock())
|
|
|| Blocks.FERN.equals(blockState.getBlock()) || Blocks.LARGE_FERN.equals(blockState.getBlock());
|
|
}
|
|
|
|
@Override
|
|
public List<Block> getBlockList(Block block) {
|
|
return Lists.newArrayList(block);
|
|
}
|
|
|
|
@Override
|
|
public HarvestRange getRange(ItemStack stack) {
|
|
return properties.getHarvestRange();
|
|
}
|
|
}
|