001    package org.bukkit.event.player;
002    
003    import org.bukkit.entity.Fish;
004    import org.bukkit.entity.Player;
005    import org.bukkit.event.Cancellable;
006    import org.bukkit.entity.Entity;
007    import org.bukkit.event.HandlerList;
008    
009    /**
010     * Thrown when a player is fishing
011     */
012    public class PlayerFishEvent extends PlayerEvent implements Cancellable {
013        private static final HandlerList handlers = new HandlerList();
014        private final Entity entity;
015        private boolean cancel = false;
016        private int exp;
017        private final State state;
018        private final Fish hookEntity;
019    
020        /**
021         * @deprecated replaced by {@link #PlayerFishEvent(Player, Entity, Fish,
022         *     State)} to include the {@link Fish} hook entity.
023         * @param player
024         * @param entity
025         * @param state
026         */
027        @Deprecated
028        public PlayerFishEvent(final Player player, final Entity entity, final State state) {
029            this(player, entity, null, state);
030        }
031    
032        public PlayerFishEvent(final Player player, final Entity entity, final Fish hookEntity, final State state) {
033            super(player);
034            this.entity = entity;
035            this.hookEntity = hookEntity;
036            this.state = state;
037        }
038    
039        /**
040         * Gets the entity caught by the player.
041         * <p>
042         * If player has fished successfully, the result may be cast to {@link
043         * Item}.
044         *
045         * @return Entity caught by the player, Entity if fishing, and null if
046         *     bobber has gotten stuck in the ground or nothing has been caught
047         */
048        public Entity getCaught() {
049            return entity;
050        }
051    
052        /**
053         * Gets the fishing hook.
054         *
055         * @return Fish the entity representing the fishing hook/bobber.
056         */
057        public Fish getHook() {
058            return hookEntity;
059        }
060    
061        public boolean isCancelled() {
062            return cancel;
063        }
064    
065        public void setCancelled(boolean cancel) {
066            this.cancel = cancel;
067        }
068    
069        /**
070         * Gets the amount of experience received when fishing.
071         * <p>
072         * Note: This value has no default effect unless the event state is {@link
073         * State#CAUGHT_FISH}.
074         *
075         * @return the amount of experience to drop
076         */
077        public int getExpToDrop() {
078            return exp;
079        }
080    
081        /**
082         * Sets the amount of experience received when fishing.
083         * <p>
084         * Note: This value has no default effect unless the event state is {@link
085         * State#CAUGHT_FISH}.
086         *
087         * @param amount the amount of experience to drop
088         */
089        public void setExpToDrop(int amount) {
090            exp = amount;
091        }
092    
093        /**
094         * Gets the state of the fishing
095         *
096         * @return A State detailing the state of the fishing
097         */
098        public State getState() {
099            return state;
100        }
101    
102        @Override
103        public HandlerList getHandlers() {
104            return handlers;
105        }
106    
107        public static HandlerList getHandlerList() {
108            return handlers;
109        }
110    
111        /**
112         * An enum to specify the state of the fishing
113         */
114        public enum State {
115    
116            /**
117             * When a player is fishing, ie casting the line out.
118             */
119            FISHING,
120            /**
121             * When a player has successfully caught a fish and is reeling it in.
122             */
123            CAUGHT_FISH,
124            /**
125             * When a player has successfully caught an entity
126             */
127            CAUGHT_ENTITY,
128            /**
129             * When a bobber is stuck in the ground
130             */
131            IN_GROUND,
132            /**
133             * When a player fails to catch anything while fishing usually due to
134             * poor aiming or timing
135             */
136            FAILED_ATTEMPT,
137        }
138    }