001    package org.bukkit.event.entity;
002    
003    import org.bukkit.entity.Entity;
004    import org.bukkit.event.Cancellable;
005    import org.bukkit.event.HandlerList;
006    import org.bukkit.util.NumberConversions;
007    
008    /**
009     * Stores data for health-regain events
010     */
011    public class EntityRegainHealthEvent extends EntityEvent implements Cancellable {
012        private static final HandlerList handlers = new HandlerList();
013        private boolean cancelled;
014        private double amount;
015        private final RegainReason regainReason;
016    
017        @Deprecated
018        public EntityRegainHealthEvent(final Entity entity, final int amount, final RegainReason regainReason) {
019            this(entity, (double) amount, regainReason);
020        }
021    
022        public EntityRegainHealthEvent(final Entity entity, final double amount, final RegainReason regainReason) {
023            super(entity);
024            this.amount = amount;
025            this.regainReason = regainReason;
026        }
027    
028        /**
029         * Gets the amount of regained health
030         *
031         * @return The amount of health regained
032         */
033        public double getAmount() {
034            return amount;
035        }
036    
037        /**
038         * This method exists for legacy reasons to provide backwards
039         * compatibility. It will not exist at runtime and should not be used
040         * under any circumstances.
041         */
042        @Deprecated
043        public int _INVALID_getAmount() {
044            return NumberConversions.ceil(getAmount());
045        }
046    
047        /**
048         * Sets the amount of regained health
049         *
050         * @param amount the amount of health the entity will regain
051         */
052        public void setAmount(double amount) {
053            this.amount = amount;
054        }
055    
056        /**
057         * This method exists for legacy reasons to provide backwards
058         * compatibility. It will not exist at runtime and should not be used
059         * under any circumstances.
060         */
061        @Deprecated
062        public void _INVALID_setAmount(int amount) {
063            setAmount(amount);
064        }
065    
066        public boolean isCancelled() {
067            return cancelled;
068        }
069    
070        public void setCancelled(boolean cancel) {
071            cancelled = cancel;
072        }
073    
074        /**
075         * Gets the reason for why the entity is regaining health
076         *
077         * @return A RegainReason detailing the reason for the entity regaining
078         *     health
079         */
080        public RegainReason getRegainReason() {
081            return regainReason;
082        }
083    
084        @Override
085        public HandlerList getHandlers() {
086            return handlers;
087        }
088    
089        public static HandlerList getHandlerList() {
090            return handlers;
091        }
092    
093        /**
094         * An enum to specify the type of health regaining that is occurring
095         */
096        public enum RegainReason {
097    
098            /**
099             * When a player regains health from regenerating due to Peaceful mode
100             * (difficulty=0)
101             */
102            REGEN,
103            /**
104             * When a player regains health from regenerating due to their hunger
105             * being satisfied
106             */
107            SATIATED,
108            /**
109             * When a player regains health from eating consumables
110             */
111            EATING,
112            /**
113             * When an ender dragon regains health from an ender crystal
114             */
115            ENDER_CRYSTAL,
116            /**
117             * When a player is healed by a potion or spell
118             */
119            MAGIC,
120            /**
121             * When a player is healed over time by a potion or spell
122             */
123            MAGIC_REGEN,
124            /**
125             * When a wither is filling its health during spawning
126             */
127            WITHER_SPAWN,
128            /**
129             * When an entity is damaged by the Wither potion effect
130             */
131            WITHER,
132            /**
133             * Any other reason not covered by the reasons above
134             */
135            CUSTOM
136        }
137    }