001    package org.bukkit.event.entity;
002    
003    import org.bukkit.Location;
004    import org.bukkit.entity.CreatureType;
005    import org.bukkit.entity.Entity;
006    import org.bukkit.entity.LivingEntity;
007    import org.bukkit.event.Cancellable;
008    import org.bukkit.event.HandlerList;
009    
010    /**
011     * Called when a creature is spawned into a world.
012     * <p>
013     * If a Creature Spawn event is cancelled, the creature will not spawn.
014     */
015    public class CreatureSpawnEvent extends EntityEvent implements Cancellable {
016        private static final HandlerList handlers = new HandlerList();
017        private boolean canceled;
018        private final SpawnReason spawnReason;
019    
020        public CreatureSpawnEvent(final LivingEntity spawnee, final SpawnReason spawnReason) {
021            super(spawnee);
022            this.spawnReason = spawnReason;
023        }
024    
025        @Deprecated
026        public CreatureSpawnEvent(Entity spawnee, CreatureType type, Location loc, SpawnReason reason) {
027            super(spawnee);
028            spawnReason = reason;
029        }
030    
031        public boolean isCancelled() {
032            return canceled;
033        }
034    
035        public void setCancelled(boolean cancel) {
036            canceled = cancel;
037        }
038    
039        @Override
040        public LivingEntity getEntity() {
041            return (LivingEntity) entity;
042        }
043    
044        /**
045         * Gets the location at which the creature is spawning.
046         *
047         * @return The location at which the creature is spawning
048         */
049        public Location getLocation() {
050            return getEntity().getLocation();
051        }
052    
053        /**
054         * Gets the type of creature being spawned.
055         *
056         * @return A CreatureType value detailing the type of creature being
057         *     spawned
058         * @deprecated In favour of {@link #getEntityType()}.
059         */
060        @Deprecated
061        public CreatureType getCreatureType() {
062            return CreatureType.fromEntityType(getEntityType());
063        }
064    
065        /**
066         * Gets the reason for why the creature is being spawned.
067         *
068         * @return A SpawnReason value detailing the reason for the creature being
069         *     spawned
070         */
071        public SpawnReason getSpawnReason() {
072            return spawnReason;
073        }
074    
075        @Override
076        public HandlerList getHandlers() {
077            return handlers;
078        }
079    
080        public static HandlerList getHandlerList() {
081            return handlers;
082        }
083    
084        /**
085         * An enum to specify the type of spawning
086         */
087        public enum SpawnReason {
088    
089            /**
090             * When something spawns from natural means
091             */
092            NATURAL,
093            /**
094             * When an entity spawns as a jockey of another entity (mostly spider
095             * jockeys)
096             */
097            JOCKEY,
098            /**
099             * When a creature spawns due to chunk generation
100             */
101            CHUNK_GEN,
102            /**
103             * When a creature spawns from a spawner
104             */
105            SPAWNER,
106            /**
107             * When a creature spawns from an egg
108             */
109            EGG,
110            /**
111             * When a creature spawns from a Spawner Egg
112             */
113            SPAWNER_EGG,
114            /**
115             * When a creature spawns because of a lightning strike
116             */
117            LIGHTNING,
118            /**
119             * When a creature is spawned by a player that is sleeping
120             *
121             * @deprecated No longer used
122             */
123            @Deprecated
124            BED,
125            /**
126             * When a snowman is spawned by being built
127             */
128            BUILD_SNOWMAN,
129            /**
130             * When an iron golem is spawned by being built
131             */
132            BUILD_IRONGOLEM,
133            /**
134             * When a wither boss is spawned by being built
135             */
136            BUILD_WITHER,
137            /**
138             * When an iron golem is spawned to defend a village
139             */
140            VILLAGE_DEFENSE,
141            /**
142             * When a zombie is spawned to invade a village
143             */
144            VILLAGE_INVASION,
145            /**
146             * When an animal breeds to create a child
147             */
148            BREEDING,
149            /**
150             * When a slime splits
151             */
152            SLIME_SPLIT,
153            /**
154             * When an entity calls for reinforcements
155             */
156            REINFORCEMENTS,
157            /**
158             * When a creature is spawned by nether portal
159             */
160            NETHER_PORTAL,
161            /**
162             * When a creature is spawned by a dispenser dispensing an egg
163             */
164            DISPENSE_EGG,
165            /**
166             * When a zombie infects a villager
167             */
168            INFECTION,
169            /**
170             * When a villager is cured from infection
171             */
172            CURED,
173            /**
174             * When an ocelot has a baby spawned along with them
175             */
176            OCELOT_BABY,
177            /**
178             * When a silverfish spawns from a block
179             */
180            SILVERFISH_BLOCK,
181            /**
182             * When an entity spawns as a mount of another entity (mostly chicken
183             * jockeys)
184             */
185            MOUNT,
186            /**
187             * When a creature is spawned by plugins
188             */
189            CUSTOM,
190            /**
191             * When an entity is missing a SpawnReason
192             */
193            DEFAULT
194        }
195    }