From d683970d40e3da4eb9d44ac0b32a2c1af91bfa41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Namiu/=E3=81=86=E3=81=AB=E3=81=9F=E3=82=8D=E3=81=86?= <53502121+NamiUni@users.noreply.github.com> Date: Fri, 16 May 2025 03:17:32 +0900 Subject: [PATCH] Add FishHookStateChangeEvent (#12165) --- .../entity/FishHookStateChangeEvent.java | 52 +++++++++++++++++++ .../main/java/org/bukkit/entity/FishHook.java | 2 + .../bukkit/event/player/PlayerFishEvent.java | 3 ++ .../entity/projectile/FishingHook.java.patch | 23 ++++++++ 4 files changed, 80 insertions(+) create mode 100644 paper-api/src/main/java/io/papermc/paper/event/entity/FishHookStateChangeEvent.java diff --git a/paper-api/src/main/java/io/papermc/paper/event/entity/FishHookStateChangeEvent.java b/paper-api/src/main/java/io/papermc/paper/event/entity/FishHookStateChangeEvent.java new file mode 100644 index 0000000000..dcca237051 --- /dev/null +++ b/paper-api/src/main/java/io/papermc/paper/event/entity/FishHookStateChangeEvent.java @@ -0,0 +1,52 @@ +package io.papermc.paper.event.entity; + +import org.bukkit.entity.FishHook; +import org.bukkit.event.HandlerList; +import org.bukkit.event.entity.EntityEvent; +import org.bukkit.event.player.PlayerFishEvent; +import org.jetbrains.annotations.ApiStatus; +import org.jspecify.annotations.NullMarked; + +/** + * Called just before a {@link FishHook}'s {@link FishHook.HookState} is changed. + * + *
If you want to monitor a player's fishing state transition, you can use {@link PlayerFishEvent}.
+ */ +@NullMarked +public final class FishHookStateChangeEvent extends EntityEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + private final FishHook.HookState newHookState; + + @ApiStatus.Internal + public FishHookStateChangeEvent(final FishHook entity, final FishHook.HookState newHookState) { + super(entity); + this.newHookState = newHookState; + } + + /** + * Get the new hook state of the {@link FishHook}. + * + *Refer to {@link FishHook#getState()} to get the current hook state.
+ * + * @return the new hook state + */ + public FishHook.HookState getNewHookState() { + return this.newHookState; + } + + @Override + public FishHook getEntity() { + return (FishHook) super.getEntity(); + } + + @Override + public HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static HandlerList getHandlerList() { + return HANDLER_LIST; + } +} 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 1839195eb9..fc280b419c 100644 --- a/paper-api/src/main/java/org/bukkit/entity/FishHook.java +++ b/paper-api/src/main/java/org/bukkit/entity/FishHook.java @@ -1,6 +1,7 @@ package org.bukkit.entity; import org.bukkit.inventory.EquipmentSlot; +import io.papermc.paper.event.entity.FishHookStateChangeEvent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -306,6 +307,7 @@ public interface FishHook extends Projectile { /** * Represents a state in which a fishing hook may be. + * State transitions can be listened for using {@link FishHookStateChangeEvent} */ public enum HookState { diff --git a/paper-api/src/main/java/org/bukkit/event/player/PlayerFishEvent.java b/paper-api/src/main/java/org/bukkit/event/player/PlayerFishEvent.java index 691037dec0..d13c354d77 100644 --- a/paper-api/src/main/java/org/bukkit/event/player/PlayerFishEvent.java +++ b/paper-api/src/main/java/org/bukkit/event/player/PlayerFishEvent.java @@ -1,5 +1,6 @@ package org.bukkit.event.player; +import io.papermc.paper.event.entity.FishHookStateChangeEvent; import org.bukkit.entity.Entity; import org.bukkit.entity.FishHook; import org.bukkit.entity.Player; @@ -12,6 +13,8 @@ import org.jetbrains.annotations.Nullable; /** * Thrown when a player is fishing + * + *If you want to monitor a fishhooks state transition, you can use {@link FishHookStateChangeEvent}.
*/ public class PlayerFishEvent extends PlayerEvent implements Cancellable { diff --git a/paper-server/patches/sources/net/minecraft/world/entity/projectile/FishingHook.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/projectile/FishingHook.java.patch index 96b211d39d..bcd4ec06fe 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/projectile/FishingHook.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/projectile/FishingHook.java.patch @@ -42,6 +42,29 @@ return; } } else { +@@ -166,12 +_,14 @@ + if (this.currentState == FishingHook.FishHookState.FLYING) { + if (this.hookedIn != null) { + this.setDeltaMovement(Vec3.ZERO); ++ new io.papermc.paper.event.entity.FishHookStateChangeEvent((org.bukkit.entity.FishHook) getBukkitEntity(), org.bukkit.entity.FishHook.HookState.HOOKED_ENTITY).callEvent(); // Paper - Add FishHookStateChangeEvent. #HOOKED_ENTITY + this.currentState = FishingHook.FishHookState.HOOKED_IN_ENTITY; + return; + } + + if (flag) { + this.setDeltaMovement(this.getDeltaMovement().multiply(0.3, 0.2, 0.3)); ++ new io.papermc.paper.event.entity.FishHookStateChangeEvent((org.bukkit.entity.FishHook) getBukkitEntity(), org.bukkit.entity.FishHook.HookState.BOBBING).callEvent(); // Paper - Add FishHookStateChangeEvent. #BOBBING + this.currentState = FishingHook.FishHookState.BOBBING; + return; + } +@@ -184,6 +_,7 @@ + this.setPos(this.hookedIn.getX(), this.hookedIn.getY(0.8), this.hookedIn.getZ()); + } else { + this.setHookedEntity(null); ++ new io.papermc.paper.event.entity.FishHookStateChangeEvent((org.bukkit.entity.FishHook) getBukkitEntity(), org.bukkit.entity.FishHook.HookState.UNHOOKED).callEvent(); // Paper - Add FishHookStateChangeEvent. #UNHOOKED + this.currentState = FishingHook.FishHookState.FLYING; + } + } @@ -247,14 +_,14 @@ if (!player.isRemoved() && player.isAlive() && (isFishingRod || isFishingRod1) && !(this.distanceToSqr(player) > 1024.0)) { return false;