| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.server.level.ServerLevel;
|
| 4 | import net.minecraft.sounds.SoundEvents;
|
| 5 | import net.minecraft.sounds.SoundSource;
|
| 6 | import net.minecraft.stats.Stats;
|
| 7 | import net.minecraft.world.InteractionHand;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.entity.player.Player;
|
| 10 | import net.minecraft.world.entity.projectile.FishingHook;
|
| 11 | import net.minecraft.world.entity.projectile.Projectile;
|
| 12 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 15 |
|
| 16 | public class FishingRodItem extends Item {
|
| 17 | public FishingRodItem(final Item.Properties properties) {
|
| 18 | super(properties);
|
| 19 | }
|
| 20 |
|
| 21 | @Override
|
| 22 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 23 | ItemStack itemStack = player.getItemInHand(hand);
|
| 24 | if (player.fishing != null) {
|
| 25 | if (!level.isClientSide()) {
|
| 26 | int dmg = player.fishing.retrieve(itemStack);
|
| 27 | itemStack.hurtAndBreak(dmg, player, hand.asEquipmentSlot());
|
| 28 | }
|
| 29 |
|
| 30 | level.playSound(
|
| 31 | null,
|
| 32 | player.getX(),
|
| 33 | player.getY(),
|
| 34 | player.getZ(),
|
| 35 | SoundEvents.FISHING_BOBBER_RETRIEVE,
|
| 36 | SoundSource.NEUTRAL,
|
| 37 | 1.0F,
|
| 38 | 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
|
| 39 | );
|
| 40 | itemStack.causeUseVibration(player, GameEvent.ITEM_INTERACT_FINISH);
|
| 41 | } else {
|
| 42 | level.playSound(
|
| 43 | null,
|
| 44 | player.getX(),
|
| 45 | player.getY(),
|
| 46 | player.getZ(),
|
| 47 | SoundEvents.FISHING_BOBBER_THROW,
|
| 48 | SoundSource.NEUTRAL,
|
| 49 | 0.5F,
|
| 50 | 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F)
|
| 51 | );
|
| 52 | if (level instanceof ServerLevel serverLevel) {
|
| 53 | int lureSpeed = (int)(EnchantmentHelper.getFishingTimeReduction(serverLevel, itemStack, player) * 20.0F);
|
| 54 | int luck = EnchantmentHelper.getFishingLuckBonus(serverLevel, itemStack, player);
|
| 55 | Projectile.spawnProjectile(new FishingHook(player, level, luck, lureSpeed), serverLevel, itemStack);
|
| 56 | }
|
| 57 |
|
| 58 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 59 | itemStack.causeUseVibration(player, GameEvent.ITEM_INTERACT_START);
|
| 60 | }
|
| 61 |
|
| 62 | return InteractionResult.SUCCESS;
|
| 63 | }
|
| 64 | }
|
| 65 |
|