package net.minecraft.world.item; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.mojang.datafixers.util.Pair; import java.util.Map; import java.util.function.Consumer; import java.util.function.Predicate; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.ItemLike; 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.state.BlockState; import net.minecraft.world.level.gameevent.GameEvent; public class HoeItem extends Item { protected static final Map, Consumer>> TILLABLES = Maps., Consumer>>newHashMap( ImmutableMap.of( Blocks.GRASS_BLOCK, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())), Blocks.DIRT_PATH, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())), Blocks.DIRT, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.FARMLAND.defaultBlockState())), Blocks.COARSE_DIRT, Pair.of(HoeItem::onlyIfAirAbove, changeIntoState(Blocks.DIRT.defaultBlockState())), Blocks.ROOTED_DIRT, Pair.of(context -> true, changeIntoStateAndDropItem(Blocks.DIRT.defaultBlockState(), Items.HANGING_ROOTS)) ) ); public HoeItem(final ToolMaterial material, final float attackDamageBaseline, final float attackSpeedBaseline, final Item.Properties properties) { super(properties.hoe(material, attackDamageBaseline, attackSpeedBaseline)); } @Override public InteractionResult useOn(final UseOnContext context) { Level level = context.getLevel(); BlockPos pos = context.getClickedPos(); Pair, Consumer> logicPair = (Pair, Consumer>)TILLABLES.get( level.getBlockState(pos).getBlock() ); if (logicPair == null) { return InteractionResult.PASS; } else { Predicate predicate = logicPair.getFirst(); Consumer action = logicPair.getSecond(); if (predicate.test(context)) { Player player = context.getPlayer(); level.playSound(player, pos, SoundEvents.HOE_TILL, SoundSource.BLOCKS, 1.0F, 1.0F); if (!level.isClientSide()) { action.accept(context); if (player != null) { context.getItemInHand().hurtAndBreak(1, player, context.getHand().asEquipmentSlot()); } } return InteractionResult.SUCCESS; } else { return InteractionResult.PASS; } } } public static Consumer changeIntoState(final BlockState state) { return context -> { context.getLevel().setBlock(context.getClickedPos(), state, 11); context.getLevel().gameEvent(GameEvent.BLOCK_CHANGE, context.getClickedPos(), GameEvent.Context.of(context.getPlayer(), state)); }; } public static Consumer changeIntoStateAndDropItem(final BlockState state, final ItemLike item) { return context -> { context.getLevel().setBlock(context.getClickedPos(), state, 11); context.getLevel().gameEvent(GameEvent.BLOCK_CHANGE, context.getClickedPos(), GameEvent.Context.of(context.getPlayer(), state)); Block.popResourceFromFace(context.getLevel(), context.getClickedPos(), context.getClickedFace(), new ItemStack(item)); }; } public static boolean onlyIfAirAbove(final UseOnContext context) { return context.getClickedFace() != Direction.DOWN && context.getLevel().getBlockState(context.getClickedPos().above()).isAir(); } }