001    package net.minecraftforge.event;
002    
003    
004    /**
005     * Base Event class that all other events are derived from
006     */
007    public class Event
008    {
009        public enum Result
010        {
011            DENY,
012            DEFAULT,
013            ALLOW
014        }
015    
016        private boolean isCanceled = false;
017        private final boolean isCancelable;
018        private static ListenerList listeners = new ListenerList();
019        
020        public Event()
021        {
022            setup();
023            Class cls = this.getClass();
024            boolean found = false;
025            while (cls != Event.class)
026            {
027                if (cls.isAnnotationPresent(Cancelable.class))
028                {
029                    found = true;
030                    break;
031                }
032                cls = cls.getSuperclass();
033            }
034            isCancelable = found;
035        }
036        
037        /**
038         * Determine if this function is cancelable at all. 
039         * @return If access to setCanceled should be allowed
040         */
041        public boolean isCancelable()
042        {
043            return isCancelable;
044        }
045        
046        /**
047         * Determine if this event is canceled and should stop executing.
048         * @return The current canceled state
049         */
050        public boolean isCanceled()
051        {
052            return isCanceled;
053        }
054        
055        /**
056         * Sets the state of this event, not all events are cancelable, and any attempt to
057         * cancel a event that can't be will result in a IllegalArgumentException.
058         * 
059         * The functionality of setting the canceled state is defined on a per-event bases.
060         * 
061         * @param cancel The new canceled value
062         */
063        public void setCanceled(boolean cancel)
064        {
065            if (!isCancelable())
066            {
067                throw new IllegalArgumentException("Attempted to cancel a uncancelable event");
068            }
069            isCanceled = cancel;
070        }
071        
072        /**
073         * Called by the base constructor, this is used by ASM generated 
074         * event classes to setup various functionality such as the listener's list.
075         */
076        protected void setup()
077        {
078        }
079        
080        /**
081         * Returns a ListenerList object that contains all listeners
082         * that are registered to this event.
083         * 
084         * @return Listener List
085         */
086        public ListenerList getListenerList()
087        {
088            return listeners;
089        }
090    }