Results 1 to 7 of 7
  1. #1
    Shakugan no Shana's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    5,213
    Reputation
    176
    Thanks
    830
    My Mood
    Pensive

    [Tutorial] Cycle Based Event Manager

    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();

  2. The Following 2 Users Say Thank You to Shakugan no Shana For This Useful Post:

    iMikez (08-13-2011),Paul (08-10-2011)

  3. #2
    Terell.'s Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    JAMAICAA
    Posts
    6,923
    Reputation
    273
    Thanks
    1,163
    My Mood
    Angry
    You wrote this yourself ? I'm impressed

    Warrock Minion 8-13-2011 - N/A
    A.V.A Minion since 11-1-11 - 11-12-11

  4. #3
    Paul's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    6,296
    Reputation
    473
    Thanks
    1,061
    My Mood
    Sleepy
    good job

    thanked and repped


  5. #4
    iMexi's Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    Behind my PC
    Posts
    2,026
    Reputation
    32
    Thanks
    288
    i don't really get what this does .. Oo

  6. #5
    Terell.'s Avatar
    Join Date
    Oct 2009
    Gender
    male
    Location
    JAMAICAA
    Posts
    6,923
    Reputation
    273
    Thanks
    1,163
    My Mood
    Angry
    Quote Originally Posted by iMexi View Post
    i don't really get what this does .. Oo
    It's a manager.

    Warrock Minion 8-13-2011 - N/A
    A.V.A Minion since 11-1-11 - 11-12-11

  7. #6
    iMexi's Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    Behind my PC
    Posts
    2,026
    Reputation
    32
    Thanks
    288
    @Shunnai
    You mean like you use this to control something or what?

  8. #7
    Paul's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    6,296
    Reputation
    473
    Thanks
    1,061
    My Mood
    Sleepy
    on delta based servers the "sendMessage("hello world"); " would be "sM("hi"); "


Similar Threads

  1. [RELEASE] Halo Tutorial Manager V2
    By OBASC in forum Halo Hacks
    Replies: 7
    Last Post: 09-06-2010, 11:05 AM
  2. [RELEASE] Halo Tutorial Manager
    By OBASC in forum Halo Hacks
    Replies: 1
    Last Post: 09-01-2010, 03:35 PM
  3. pro cycling manager 2010
    By jansma in forum Game Serial Keys
    Replies: 0
    Last Post: 07-01-2010, 01:25 AM
  4. [Tutorial]Creating D3D8 Base | Wallhack
    By Voltage552 in forum C++/C Programming
    Replies: 21
    Last Post: 05-16-2010, 08:05 PM
  5. [Tutorial] Task Manager
    By NextGen1 in forum Visual Basic Programming
    Replies: 11
    Last Post: 01-24-2010, 02:30 PM