minecraft-src/net/minecraft/world/item/ShovelItem.java
2025-07-04 03:45:38 +03:00

79 lines
3 KiB
Java

package net.minecraft.world.item;
import com.google.common.collect.Maps;
import com.google.common.collect.ImmutableMap.Builder;
import java.util.Map;
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.LivingEntity;
import net.minecraft.world.entity.player.Player;
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.CampfireBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.level.gameevent.GameEvent.Context;
public class ShovelItem extends Item {
/**
* Map used to lookup shovel right click interactions
*/
protected static final Map<Block, BlockState> FLATTENABLES = Maps.<Block, BlockState>newHashMap(
new Builder()
.put(Blocks.GRASS_BLOCK, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.DIRT, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.PODZOL, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.COARSE_DIRT, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.MYCELIUM, Blocks.DIRT_PATH.defaultBlockState())
.put(Blocks.ROOTED_DIRT, Blocks.DIRT_PATH.defaultBlockState())
.build()
);
public ShovelItem(ToolMaterial material, float attackDamage, float attackSpeed, Item.Properties properties) {
super(properties.shovel(material, attackDamage, attackSpeed));
}
@Override
public InteractionResult useOn(UseOnContext context) {
Level level = context.getLevel();
BlockPos blockPos = context.getClickedPos();
BlockState blockState = level.getBlockState(blockPos);
if (context.getClickedFace() == Direction.DOWN) {
return InteractionResult.PASS;
} else {
Player player = context.getPlayer();
BlockState blockState2 = (BlockState)FLATTENABLES.get(blockState.getBlock());
BlockState blockState3 = null;
if (blockState2 != null && level.getBlockState(blockPos.above()).isAir()) {
level.playSound(player, blockPos, SoundEvents.SHOVEL_FLATTEN, SoundSource.BLOCKS, 1.0F, 1.0F);
blockState3 = blockState2;
} else if (blockState.getBlock() instanceof CampfireBlock && (Boolean)blockState.getValue(CampfireBlock.LIT)) {
if (!level.isClientSide()) {
level.levelEvent(null, 1009, blockPos, 0);
}
CampfireBlock.dowse(context.getPlayer(), level, blockPos, blockState);
blockState3 = blockState.setValue(CampfireBlock.LIT, false);
}
if (blockState3 != null) {
if (!level.isClientSide) {
level.setBlock(blockPos, blockState3, 11);
level.gameEvent(GameEvent.BLOCK_CHANGE, blockPos, Context.of(player, blockState3));
if (player != null) {
context.getItemInHand().hurtAndBreak(1, player, LivingEntity.getSlotForHand(context.getHand()));
}
}
return InteractionResult.SUCCESS;
} else {
return InteractionResult.PASS;
}
}
}
}