Thread: Cycle Based Event Manager

Page 1 of 3 123 LastLast
Results 1 to 10 of 24
  1. #1 Cycle Based Event Manager 
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Code:
    package org.maggot.server.model.world.cycleEvents;
    
    /**
     *
     * @author Daniel
     */
    public abstract class CycleEvent {
    
        /**
         * Called after the alloted number or cycles have passed.
         */
        public abstract void execute(CycleEventContainer container);
    
        /**
         * Called on event stop.
         */
        public abstract void stop();
    }
    Code:
    package org.maggot.server.model.world.cycleEvents;
    
    /**
     *
     * @author Daniel
     */
    public class CycleEventContainer {
    
        /**
         * The container for the methods executed when the event is finished.
         */
        private final CycleEvent event;
    
        /**
         * The number of ticks that should happen before the event is executed.
         */
        private final int endCycleCount;
        
        /**
         * The amount of cycles that have passed since the event was first
         * "registered".
         */
        private int currentCycleCount;
    
        /**
         * Whether or not the event should be killed.
         */
        private boolean killEvent;
    
        public CycleEventContainer(CycleEvent event, int endCycleCount) {
            currentCycleCount = 0;
            this.event = event;
            this.endCycleCount = endCycleCount;
        }
        /**
         * Executes the event.
         */
        public void execute() {
            event.execute(this);
        }
    
        /**
         * Stops the event from recurring.
         */
        public void stop() {
            killEvent = true;
            event.stop();
        }
    
        /**
         * Increases the current tick count by 1 and checks to see if the CycleEvent is
         * to be executed.
         * @return Whether or not the event shoult be executed.
         */
        public boolean executeEvent() {
            if(++currentCycleCount >= endCycleCount) {
                currentCycleCount = 0;
                return true;
            }
            return false;
        }
    
        /**
         * Whether or not to remove the event from the list.
         * @return
         */
        public boolean removeEvent() {
            return killEvent;
        }
    }
    Code:
    package org.maggot.server.model.world.cycleEvents;
    
    import java.util.LinkedList;
    
    /**
     *
     * @author Daniel
     */
    public class CycleEventManager {
    
        /**
         * An ArrayList containing all information needed to run the events.
         */
        private LinkedList<CycleEventContainer> currentEvents;
    
        /**
         * An ArrayList containing all the containers to be cleaned up after the
         * cycle is finished.
         */
        private LinkedList<CycleEventContainer> cleanupQueue;
    
        /**
         * Initializes class.
         */
        public CycleEventManager() {
            currentEvents = new LinkedList<CycleEventContainer>();
            cleanupQueue = new LinkedList<CycleEventContainer>();
        }
    
        /**
         *
         * @param event The event to add to the event list.
         * @param cycles The number of cycles that have to pass until the event is
         * executed.
         */
        public void addEvent(CycleEvent event, int cycles) {
            currentEvents.add(new CycleEventContainer(event, cycles));
        }
    
        /**
         * Called every 600ms (unless otherwise specified) and loops through all
         * current events and decides whether or not to execute it.
         */
        public void process() {
            for(CycleEventContainer processedEvent : currentEvents) {
                if(processedEvent == null) {
                    System.out.println("Prevented damaged event crash!");
                    return;
                }
                if(processedEvent.executeEvent()) {
                    processedEvent.execute();
                }
                if(processedEvent.removeEvent()) {
                    queueContainerCleanup(processedEvent);
                }
            }
            cleanupFinishedEvents();
        }
    
        /**
         * Adds a container to the cleaning (delete) queue.
         * @param processedEvent The container to clean up.
         */
        private void queueContainerCleanup(CycleEventContainer processedEvent) {
            cleanupQueue.add(processedEvent);
        }
    
        /**
         * Removes finished events from the event list.
         */
        private void cleanupFinishedEvents() {
            for(CycleEventContainer currentDeletion : cleanupQueue)
                currentEvents.remove(currentDeletion);
            cleanupQueue.clear();
        }
    }
    Works 100%, no bugs!
    Supports unlimited loop.

    Example:
    Code:
            World.getWorld().getCycleEventManager().addEvent(new CycleEvent() {
                @Override
                public void execute(CycleEventContainer container) {
                    System.out.println("HI");
                    container.stop();
                }
    
                @Override
                public void stop() {
                    System.out.println("Event killed.");
                }
            }, 5);
    After 5 cycles (3 seconds), the event executes.
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Banned

    Join Date
    Apr 2009
    Posts
    1,257
    Thanks given
    16
    Thanks received
    147
    Rep Power
    0
    Hmm... interesting.

    Good work maggot.
    Reply With Quote  
     

  4. Thankful user:


  5. #3  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    ArrayLsit will slow it down, use a linked queue.
    Reply With Quote  
     

  6. #4  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Quote Originally Posted by Colby View Post
    ArrayLsit will slow it down, use a linked queue.
    Alright, just added that, but what's the difference?
    Reply With Quote  
     

  7. #5  
    Banned

    Join Date
    Jul 2008
    Age
    28
    Posts
    5,827
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by i r maggot View Post
    Alright, just added that, but what's the difference?
    Whenever you remove the head of an ArrayList it has to move every element down one index, and since you aren't using get()'s, you can use a linked one, and it only has to change a couple references.

    BTW: If you override stop(), then the original method won't get executed..
    Reply With Quote  
     

  8. #6  
    Registered Member
    Linux's Avatar
    Join Date
    Feb 2008
    Age
    29
    Posts
    597
    Thanks given
    104
    Thanks received
    103
    Rep Power
    1451
    I have seen better ways to do such thing.

    Nice work tho.
    Reply With Quote  
     

  9. #7  
    Registered Member
    Mister Maggot's Avatar
    Join Date
    Dec 2008
    Posts
    7,227
    Thanks given
    3,283
    Thanks received
    2,875
    Rep Power
    5000
    Quote Originally Posted by Colby View Post
    Whenever you remove the head of an ArrayList it has to move every element down one index, and since you aren't using get()'s, you can use a linked one, and it only has to change a couple references.

    BTW: If you override stop(), then the original method won't get executed..
    Oh, I think I get that, xD

    and it works, it's just I named a few methods "stop" without thinking.
    Reply With Quote  
     

  10. #8  
    Banned
    Join Date
    Dec 2009
    Posts
    86
    Thanks given
    6
    Thanks received
    2
    Rep Power
    0
    Nice.
    Reply With Quote  
     

  11. #9  
    Banned
    Join Date
    Oct 2009
    Age
    30
    Posts
    320
    Thanks given
    43
    Thanks received
    45
    Rep Power
    0
    what do i need in server.java
    Reply With Quote  
     

  12. #10  
    Banned
    Join Date
    Mar 2008
    Posts
    1,937
    Thanks given
    131
    Thanks received
    61
    Rep Power
    0
    can u tell me why i shud add this ? or what this does do exactly
    Reply With Quote  
     

Page 1 of 3 123 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •