diff --git a/paper-api/src/main/java/org/bukkit/entity/FishHook.java b/paper-api/src/main/java/org/bukkit/entity/FishHook.java index 470443e3e..1839195eb 100644 --- a/paper-api/src/main/java/org/bukkit/entity/FishHook.java +++ b/paper-api/src/main/java/org/bukkit/entity/FishHook.java @@ -1,5 +1,6 @@ package org.bukkit.entity; +import org.bukkit.inventory.EquipmentSlot; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -323,7 +324,6 @@ public interface FishHook extends Projectile { BOBBING; } - // Paper start - More FishHook API /** * Get the number of ticks the hook needs to wait for a fish to bite. * @@ -367,5 +367,18 @@ public interface FishHook extends Projectile { * enchantment. */ void resetFishingState(); - // Paper end + + /** + * Retrieve this fishhook back to the casting player. + *
+ * This method will trigger and respect API events, which may be subject to cancellation. + * Plugins listening to {@link org.bukkit.event.player.PlayerFishEvent} might for example cancel this action. + * + * @param slot Slot holding the fishing rod (must be HAND/OFF_HAND) + * @return The amount of damage which would be applied to the itemstack + * @throws IllegalStateException if the fish hook does not have a player casting it. + * @throws IllegalStateException if the player casting it is not holding a + * {@link org.bukkit.inventory.ItemType#FISHING_ROD} in the specified equipment slot. + */ + int retrieve(@NotNull EquipmentSlot slot); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftFishHook.java b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftFishHook.java index 94f07ebc2..5f7225f18 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftFishHook.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/entity/CraftFishHook.java @@ -2,10 +2,16 @@ package org.bukkit.craftbukkit.entity; import com.google.common.base.Preconditions; import net.minecraft.core.BlockPos; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.FishingHook; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import org.bukkit.craftbukkit.CraftEquipmentSlot; import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Entity; import org.bukkit.entity.FishHook; +import org.bukkit.inventory.EquipmentSlot; public class CraftFishHook extends CraftProjectile implements FishHook { private double biteChance = -1; @@ -233,4 +239,18 @@ public class CraftFishHook extends CraftProjectile implements FishHook { hook.resetTimeUntilLured(); hook.timeUntilHooked = 0; // Reset time until hooked, will be repopulated once lured time is ticked down. } + + @Override + public int retrieve(EquipmentSlot slot) { + Preconditions.checkArgument(slot == EquipmentSlot.HAND || slot == EquipmentSlot.OFF_HAND, "Equipment slot must be HAND or OFF_HAND"); + final FishingHook fishingHook = getHandle(); + final Player playerOwner = fishingHook.getPlayerOwner(); + Preconditions.checkState(playerOwner != null, "Player owner cannot be null"); + + final InteractionHand hand = CraftEquipmentSlot.getHand(slot); + final ItemStack itemInHand = playerOwner.getItemInHand(hand); + Preconditions.checkState(itemInHand.is(Items.FISHING_ROD), "Item in slot is not a FISHING_ROD"); + + return fishingHook.retrieve(itemInHand, hand); + } }