Thread: My Cycle Based Event System

Page 1 of 3 123 LastLast
Results 1 to 10 of 23
  1. #1 My Cycle Based Event System 
    Registered Member

    Join Date
    Aug 2008
    Posts
    2,729
    Thanks given
    340
    Thanks received
    317
    Rep Power
    843
    Sorry about no comments in the code but I blanked when trying to write them :/

    Code:
    /*
     * CycleController
     *
     * By Fritz (Kataang)
     *
     * Controls all of the cycle events
     */
    package server.cycle;
    
    import java.util.ArrayList;
    import server.Config;
    import server.util.Misc;
    
    public class CycleController implements Runnable {
    
    	public static void add(double cyclesBetweenRunning, double cyclesToRun,
    			Cycle cycle) {
    		cycles.add(new CycleContainer(cyclesBetweenRunning, cyclesToRun, cycle));
    	}
    
    	public static void add(double cyclesToRun, Cycle cycle) {
    		cycles.add(new CycleContainer(cyclesToRun, -1, cycle));
    	}
    
    	public void run() {
    		while (true) {
    			for (CycleContainer cycle : cycles) {
    				try {
    					if (cycle.lastRun + Config.SERVER_CYCLE
    							* cycle.cyclesBetweenRunning() <= System
    							.currentTimeMillis()
    							&& cycle.cyclesToRun() > cycle.cyclesRan) {
    						cycle.toDo();
    						cycle.cyclesRan++;
    					} else if (cycle.cyclesToRun() <= cycle.cyclesRan) {
    						toRemove.add(cycle);
    					}
    				} catch (Exception e) {
    					Misc.print("Error during cycle, stopping the cycle.");
    				}
    			}
    			for (CycleContainer cycle : toRemove) {
    				cycles.remove(cycle);
    			}
    			toRemove.clear();
    		}
    	}
    
    	private static ArrayList<CycleContainer> cycles = new ArrayList<CycleContainer>();
    	private ArrayList<CycleContainer> toRemove = new ArrayList<CycleContainer>();
    }

    Code:
    /*
     * CycleContainer
     *
     * By Fritz (Kataang)
     *
     * Contains a cycle
     */
    package server.cycle;
    
    import server.util.Misc;
    
    public class CycleContainer {
    
    	public CycleContainer(double cyclesBetweenRunning, double cyclesToRun,
    			Cycle cycle) {
    		this.cycle = cycle;
    		this.cyclesBetweenRunning = cyclesBetweenRunning;
    		this.cyclesToRun = cyclesToRun;
    		if (cyclesToRun > 0)
    			cyclesRan = 0;
    		lastRun = System.currentTimeMillis();
    	}
    
    	public void toDo() {
    		this.lastRun = System.currentTimeMillis();
    		cycle.toDo(this);
    	}
    
    	public void end(Cycle cycle) {
    		cyclesRan = cyclesToRun;
    	}
    
    	public double lastRun() {
    		return lastRun;
    	}
    
    	public double cyclesToRun() {
    		return cyclesToRun;
    	}
    
    	public double cyclesBetweenRunning() {
    		return cyclesBetweenRunning;
    	}
    
    	private Cycle cycle;
    	private double cyclesToRun, cyclesBetweenRunning;
    	public double cyclesRan = Double.MIN_VALUE, lastRun;
    }

    Code:
    /*
     * Cycle
     *
     * By Fritz (Kataang)
     *
     * Interface for a cycle
     */
    package server.cycle;
    
    public interface Cycle {
    	public void toDo(CycleContainer cycleContainer);
    }

    And in Server.java below
    Code:
    Connection.initialize();
    add
    Code:
    new Thread(new CycleController()).start();

    And in Config.java add
    Code:
    public static final double SERVER_CYCLE = 600;



    __________________________________________________ __________________________________________________ ____________________________________





    There are 2 ways you can use this, one, you can run a cycle based event a certain number of times with an interval defined number of cycles between each execution OR you can run a cycle based event with an interval of a defined number of cycles.


    Usage #1:
    Code:
    		CycleController.add(4, 10, new Cycle() {
    			public void toDo(CycleContainer con) {
    				sendMessage("Hey there!");
    			}
    		});
    The 4 is the number of cycles between each execution and the 10 defines how many times it will execute the code inside public void toDo()
    Code:
    		CycleController.add(4, 10, new Cycle() {
    Code:
    			public void toDo(CycleContainer con) {
    				sendMessage("Hey there!");
    			}

    Usage #2:
    Code:
    		CycleController.add(4, new Cycle() {
    			public void toDo(CycleContainer con) {
    				sendMessage("Hey there!");
    			}
    		});
    The 4 is the number of cycles between each execution of the code inside toDo().
    Code:
    		CycleController.add(4, new Cycle() {
    Code:
    			public void toDo(CycleContainer con) {
    				sendMessage("Hey there!");
    			}

    To end a cycle event, use this inside the toDo() void:
    Code:
    con.end();
    >Animes
    Reply With Quote  
     

  2. Thankful user:


  3. #2  
    Registered Member
    squxx's Avatar
    Join Date
    Aug 2010
    Posts
    678
    Thanks given
    378
    Thanks received
    139
    Rep Power
    78
    Wow thanks def using.
    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  4. #3  
    Registered Member Pakku's Avatar
    Join Date
    Mar 2010
    Posts
    1,234
    Thanks given
    129
    Thanks received
    112
    Rep Power
    47
    What is different from the current cycle event system?
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Registered Member

    Join Date
    Aug 2008
    Posts
    2,729
    Thanks given
    340
    Thanks received
    317
    Rep Power
    843
    Quote Originally Posted by stripies View Post
    What is different from the current cycle event system?
    You mean EventManager? That isn't run off cycles.
    >Animes
    Reply With Quote  
     

  7. #5  
    Registered Member Psychedelic's Avatar
    Join Date
    Dec 2010
    Age
    10
    Posts
    1,161
    Thanks given
    512
    Thanks received
    346
    Rep Power
    35
    Quote Originally Posted by stripies View Post
    What is different from the current cycle event system?
    What he said.

    Yours looks very nice though.

    EDIT: I'm so confused, there's eventmanager, cycle event manger, i'm using a cycleevent manger atm that thispixel made.

    [Only registered and activated users can see links. ]

    Hallucination Generation

    Reply With Quote  
     

  8. #6  
    Registered Member
    squxx's Avatar
    Join Date
    Aug 2010
    Posts
    678
    Thanks given
    378
    Thanks received
    139
    Rep Power
    78
    Quote Originally Posted by vanweele View Post
    What he said.

    Yours looks very nice though.

    EDIT: I'm so confused, there's eventmanager, cycle event manger, i'm using a cycleevent manger atm that thispixel made.

    [Only registered and activated users can see links. ]
    gratz on armour set!


    also your right it is very confusing....
    [Only registered and activated users can see links. ]

    Reply With Quote  
     

  9. Thankful users:


  10. #7  
    Registered Member

    Join Date
    Aug 2008
    Posts
    2,729
    Thanks given
    340
    Thanks received
    317
    Rep Power
    843
    Quote Originally Posted by vanweele View Post
    What he said.

    Yours looks very nice though.

    EDIT: I'm so confused, there's eventmanager, cycle event manger, i'm using a cycleevent manger atm that thispixel made.

    [Only registered and activated users can see links. ]
    His doesn't have support for running an event a defined number of times.
    >Animes
    Reply With Quote  
     

  11. #8  
    Registered Member Pakku's Avatar
    Join Date
    Mar 2010
    Posts
    1,234
    Thanks given
    129
    Thanks received
    112
    Rep Power
    47
    Quote Originally Posted by Kataang View Post
    You mean EventManager? That isn't run off cycles.
    I mean exactly what vanweele said.
    Reply With Quote  
     

  12. #9  
    Registered Member

    Join Date
    Aug 2008
    Posts
    2,729
    Thanks given
    340
    Thanks received
    317
    Rep Power
    843
    Quote Originally Posted by stripies View Post
    I mean exactly what vanweele said.
    Look above your post.
    >Animes
    Reply With Quote  
     

  13. #10  
    Registered Member Pakku's Avatar
    Join Date
    Mar 2010
    Posts
    1,234
    Thanks given
    129
    Thanks received
    112
    Rep Power
    47
    Quote Originally Posted by Kataang View Post
    His doesn't have support for running an event a defined number of times.
    You could easily make it run a set number of times. You don't need a whole new system.
    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)

Similar Threads

  1. Replies: 23
    Last Post: 07-12-2011, 12:17 AM
  2. Cycle Manager, Event
    By Vox' in forum Help
    Replies: 2
    Last Post: 03-20-2011, 12:12 AM
  3. Cycle Based Event Manager
    By Mister Maggot in forum Snippets
    Replies: 23
    Last Post: 11-29-2010, 07:25 PM
  4. Easy Cycle Event
    By jman1208 in forum Help
    Replies: 12
    Last Post: 11-18-2010, 07:19 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •