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    
007    /**
008     * Called when a creature targets or untargets another entity
009     */
010    public class EntityTargetEvent extends EntityEvent implements Cancellable {
011        private static final HandlerList handlers = new HandlerList();
012        private boolean cancel = false;
013        private Entity target;
014        private final TargetReason reason;
015    
016        public EntityTargetEvent(final Entity entity, final Entity target, final TargetReason reason) {
017            super(entity);
018            this.target = target;
019            this.reason = reason;
020        }
021    
022        public boolean isCancelled() {
023            return cancel;
024        }
025    
026        public void setCancelled(boolean cancel) {
027            this.cancel = cancel;
028        }
029    
030        /**
031         * Returns the reason for the targeting
032         *
033         * @return The reason
034         */
035        public TargetReason getReason() {
036            return reason;
037        }
038    
039        /**
040         * Get the entity that this is targeting.
041         * <p>
042         * This will be null in the case that the event is called when the mob
043         * forgets its target.
044         *
045         * @return The entity
046         */
047        public Entity getTarget() {
048            return target;
049        }
050    
051        /**
052         * Set the entity that you want the mob to target instead.
053         * <p>
054         * It is possible to be null, null will cause the entity to be
055         * target-less.
056         * <p>
057         * This is different from cancelling the event. Cancelling the event will
058         * cause the entity to keep an original target, while setting to be null
059         * will cause the entity to be reset.
060         *
061         * @param target The entity to target
062         */
063        public void setTarget(Entity target) {
064            this.target = target;
065        }
066    
067        @Override
068        public HandlerList getHandlers() {
069            return handlers;
070        }
071    
072        public static HandlerList getHandlerList() {
073            return handlers;
074        }
075    
076        /**
077         * An enum to specify the reason for the targeting
078         */
079        public enum TargetReason {
080    
081            /**
082             * When the entity's target has died, and so it no longer targets it
083             */
084            TARGET_DIED,
085            /**
086             * When the entity doesn't have a target, so it attacks the nearest
087             * player
088             */
089            CLOSEST_PLAYER,
090            /**
091             * When the target attacks the entity, so entity targets it
092             */
093            TARGET_ATTACKED_ENTITY,
094            /**
095             * When the target attacks a fellow pig zombie, so the whole group
096             * will target him with this reason.
097             */
098            PIG_ZOMBIE_TARGET,
099            /**
100             * When the target is forgotten for whatever reason.
101             * <p>
102             * Currently only occurs in with spiders when there is a high
103             * brightness.
104             */
105            FORGOT_TARGET,
106            /**
107             * When the target attacks the owner of the entity, so the entity
108             * targets it.
109             */
110            TARGET_ATTACKED_OWNER,
111            /**
112             * When the owner of the entity attacks the target attacks, so the
113             * entity targets it.
114             */
115            OWNER_ATTACKED_TARGET,
116            /**
117             * When the entity has no target, so the entity randomly chooses one.
118             */
119            RANDOM_TARGET,
120            /**
121             * When an entity selects a target while defending a village.
122             */
123            DEFEND_VILLAGE,
124            /**
125             * When the target attacks a nearby entity of the same type, so the entity targets it
126             */
127            TARGET_ATTACKED_NEARBY_ENTITY,
128            /**
129             * When a zombie targeting an entity summons reinforcements, so the reinforcements target the same entity
130             */
131            REINFORCEMENT_TARGET,
132            /**
133             * When an entity targets another entity after colliding with it.
134             */
135            COLLISION,
136            /**
137             * For custom calls to the event.
138             */
139            CUSTOM
140        }
141    }