| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.HolderGetter;
|
| 7 | import net.minecraft.core.HolderSet;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 10 | import net.minecraft.server.level.ServerPlayer;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.sounds.SoundSource;
|
| 13 | import net.minecraft.tags.BlockTags;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.entity.EquipmentSlot;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.item.component.Tool;
|
| 19 | import net.minecraft.world.item.context.UseOnContext;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.Blocks;
|
| 23 | import net.minecraft.world.level.block.GrowingPlantHeadBlock;
|
| 24 | import net.minecraft.world.level.block.state.BlockState;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 |
|
| 27 | public class ShearsItem extends Item {
|
| 28 | public ShearsItem(final Item.Properties properties) {
|
| 29 | super(properties);
|
| 30 | }
|
| 31 |
|
| 32 | public static Tool createToolProperties() {
|
| 33 | HolderGetter<Block> registrationLookup = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.BLOCK);
|
| 34 | return new Tool(
|
| 35 | List.of(
|
| 36 | Tool.Rule.minesAndDrops(HolderSet.direct(Blocks.COBWEB.builtInRegistryHolder()), 15.0F),
|
| 37 | Tool.Rule.overrideSpeed(registrationLookup.getOrThrow(BlockTags.SHEARS_EXTREME_BREAKING_SPEED), 15.0F),
|
| 38 | Tool.Rule.overrideSpeed(registrationLookup.getOrThrow(BlockTags.SHEARS_MAJOR_BREAKING_SPEED), 5.0F),
|
| 39 | Tool.Rule.overrideSpeed(registrationLookup.getOrThrow(BlockTags.SHEARS_MINOR_BREAKING_SPEED), 2.0F)
|
| 40 | ),
|
| 41 | 1.0F,
|
| 42 | 1,
|
| 43 | true
|
| 44 | );
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public boolean mineBlock(final ItemStack itemStack, final Level level, final BlockState state, final BlockPos pos, final LivingEntity miner) {
|
| 49 | Tool tool = itemStack.get(DataComponents.TOOL);
|
| 50 | if (tool == null) {
|
| 51 | return false;
|
| 52 | }
|
| 53 |
|
| 54 | if (!level.isClientSide() && !state.is(BlockTags.FIRE) && tool.damagePerBlock() > 0) {
|
| 55 | itemStack.hurtAndBreak(tool.damagePerBlock(), miner, EquipmentSlot.MAINHAND);
|
| 56 | }
|
| 57 |
|
| 58 | return true;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public InteractionResult useOn(final UseOnContext context) {
|
| 63 | Level level = context.getLevel();
|
| 64 | BlockPos pos = context.getClickedPos();
|
| 65 | BlockState state = level.getBlockState(pos);
|
| 66 | if (state.getBlock() instanceof GrowingPlantHeadBlock plantBlock && !plantBlock.isMaxAge(state)) {
|
| 67 | Player player = context.getPlayer();
|
| 68 | ItemStack itemInHand = context.getItemInHand();
|
| 69 | if (player instanceof ServerPlayer serverPlayer) {
|
| 70 | CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(serverPlayer, pos, itemInHand);
|
| 71 | }
|
| 72 |
|
| 73 | level.playSound(player, pos, SoundEvents.GROWING_PLANT_CROP, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 74 | BlockState newState = plantBlock.getMaxAgeState(state);
|
| 75 | level.setBlockAndUpdate(pos, newState);
|
| 76 | level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(context.getPlayer(), newState));
|
| 77 | if (player != null) {
|
| 78 | itemInHand.hurtAndBreak(1, player, context.getHand().asEquipmentSlot());
|
| 79 | }
|
| 80 |
|
| 81 | return InteractionResult.SUCCESS;
|
| 82 | } else {
|
| 83 | return super.useOn(context);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|