001    package org.bukkit.event.entity;
002    
003    import java.util.ArrayList;
004    import java.util.Collection;
005    import java.util.Map;
006    
007    import org.apache.commons.lang.Validate;
008    import org.bukkit.entity.LivingEntity;
009    import org.bukkit.entity.ThrownPotion;
010    import org.bukkit.event.Cancellable;
011    import org.bukkit.event.HandlerList;
012    
013    /**
014     * Called when a splash potion hits an area
015     */
016    public class PotionSplashEvent extends ProjectileHitEvent implements Cancellable {
017        private static final HandlerList handlers = new HandlerList();
018        private boolean cancelled;
019        private final Map<LivingEntity, Double> affectedEntities;
020    
021        public PotionSplashEvent(final ThrownPotion potion, final Map<LivingEntity, Double> affectedEntities) {
022            super(potion);
023    
024            this.affectedEntities = affectedEntities;
025        }
026    
027        @Override
028        public ThrownPotion getEntity() {
029            return (ThrownPotion) entity;
030        }
031    
032        /**
033         * Gets the potion which caused this event
034         *
035         * @return The thrown potion entity
036         */
037        public ThrownPotion getPotion() {
038            return (ThrownPotion) getEntity();
039        }
040    
041        /**
042         * Retrieves a list of all effected entities
043         *
044         * @return A fresh copy of the affected entity list
045         */
046        public Collection<LivingEntity> getAffectedEntities() {
047            return new ArrayList<LivingEntity>(affectedEntities.keySet());
048        }
049    
050        /**
051         * Gets the intensity of the potion's effects for given entity; This
052         * depends on the distance to the impact center
053         *
054         * @param entity Which entity to get intensity for
055         * @return intensity relative to maximum effect; 0.0: not affected; 1.0:
056         *     fully hit by potion effects
057         */
058        public double getIntensity(LivingEntity entity) {
059            Double intensity = affectedEntities.get(entity);
060            return intensity != null ? intensity : 0.0;
061        }
062    
063        /**
064         * Overwrites the intensity for a given entity
065         *
066         * @param entity For which entity to define a new intensity
067         * @param intensity relative to maximum effect
068         */
069        public void setIntensity(LivingEntity entity, double intensity) {
070            Validate.notNull(entity, "You must specify a valid entity.");
071            if (intensity <= 0.0) {
072                affectedEntities.remove(entity);
073            } else {
074                affectedEntities.put(entity, Math.min(intensity, 1.0));
075            }
076        }
077    
078        public boolean isCancelled() {
079            return cancelled;
080        }
081    
082        public void setCancelled(boolean cancel) {
083            cancelled = cancel;
084        }
085    
086        @Override
087        public HandlerList getHandlers() {
088            return handlers;
089        }
090    
091        public static HandlerList getHandlerList() {
092            return handlers;
093        }
094    }