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