Thread: System Timers

Page 1 of 2 12 LastLast
Results 1 to 10 of 18
  1. #1 System Timers 
    Banned

    Join Date
    Nov 2013
    Posts
    1,015
    Thanks given
    240
    Thanks received
    265
    Rep Power
    0
    SYSTEM TIMERS
    My First ACTUAL Tutorial


    Step 1 (Uno): Create a class, and name it "Timer"

    Step 2 (Dos): Copy and paste this into it:

    Code:
    public class Timer {
    	private int timeToRun; // time to run (in seconds)
    	private long startTime; // the time the timer started at
    	private boolean activeTimer; // is the timer currently active?
    	
    	/**
    	 * constructor sets up the amount of time to run and converts input from seconds to ms, gets the starting time of the timer, and sets the timer as active
    	 * @param timeToRun The amout of time for the timer to run for (in seconds)
    	 **/
    	public Timer(int timeToRun)
    	{
    		this.timeToRun = timeToRun*1000;
    		startTime = getStartTime();
    		activeTimer = true;
    	}
    	
    	/**
    	 * default constructor which initializes the variables to 0 and false(for the boolean)
    	 **/
    	public Timer()
    	{
    		timeToRun = 0;
    		startTime = 0;
    		activeTimer = false;
    	}
    	
    	/**
    	 * determines the starting time of the timer
    	 * @return the starting time of the timer in general
    	 **/
    	private long getStartTime()
    	{
    		return System.currentTimeMillis();
    	}
    	
    	/**
    	 * calculates the amount of time that has passed since the start of the timer
    	 * @return the amout of time has passed since the timer started
    	 **/
    	private long timeSinceStart()
    	{
    		return System.currentTimeMillis() - startTime;
    	}
    	
    	/**
    	 * @return whether the timer is active or not
    	 **/
    	public boolean isActive()
    	{
    		return activeTimer;
    	}
    	
    	/**
    	 * set whether the timer is active or not
    	 * @param activeTimer is the timer active?
    	 **/
    	public void setActive(boolean activeTimer)
    	{
    		this.activeTimer = activeTimer;
    	}
    	
    	/**
    	 * set the amount of time for the timer to run and gets the starting time of the timer
    	 * @param timeToRun The amout of time for the timer to run for (in seconds)
    	 **/
    	public void setTimeToRun(int timeToRun)
    	{
    		this.timeToRun = timeToRun*1000;
    		startTime = getStartTime();
    	}
    	
    	/**
    	 * @return how much time is left for the timer overall(in seconds)
    	 **/
    	public int getTimeLeft()
    	{
    		return misc.round((double)((timeToRun - timeSinceStart())/1000));
    	}
    	
    	/**
    	 * determines whether or not the timer is finished running
    	 * @return if the timer is finished running properly 
    	 **/
    	public boolean stop()
    	{
    		if (timeSinceStart() >= timeToRun)
    			return true;
    		return false;
    	}
    }
    Step 3 (Tres): Declare these variables, and the method given:

    Code:
    	private String news[] =  // actual news messages
    		{
    			"Message 1",
    			"Message 2",
    			"Message 3",
    			"Message 4",
    			"You can keep on going if you'd like."
    		};
    	private int newsIndex = 0; // index of the news array to print
    	private Timer newsTimer = new Timer(20); // news timer to print every 20 seconds
    	
    	// sends the news message to player
    	public void news() {
    		sendMessage("[NEWS]"+news[newsIndex]);
    		newsIndex++;
    		if (newsIndex >= news.length)
    			newsIndex = 0;
    	}
    	// end news
    Step 4 (Cuatro): We're going to add the processing of the timer, so, in your process method; add this:

    Code:
    		// news processing
    		if (newsTimer.stop()) {
    			news();
    			newsTimer.setTimeToRun(20);
    		}
    		// ends the news process
    Now, this honestly lasts as long as you have set it to.

    Here's a quick example:

    Timer:

    Code:
    private Timer sayHey = new Timer(30); // a 30 second timer
    And then the process for running:

    Code:
    		// if the timer is done and if it is active
    		if (sayHey.stop() && sayHey.isActive())
    		{
    			System.out.println("Hey!");
    			sayHi.setActive(false); // set active to false
    		}
    This will keep the action from being done constantly when you don't want it to because technically stop() will return true until you set time again.

    And if you wanted it to run again, you could say..

    Code:
    	sayHey.setTimeToRun(30);
    	sayHey.setActive(true);
    Reply With Quote  
     

  2. Thankful users:


  3. #2  
    Banned

    Join Date
    Mar 2012
    Age
    27
    Posts
    1,518
    Thanks given
    473
    Thanks received
    324
    Rep Power
    0
    For a first release this is pretty good IMO, Id only really use this for simple messages and other easier tasks, Newer developers would find use in this. good job for a first release man!
    Reply With Quote  
     

  4. #3  
    Registered Member
    thim slug's Avatar
    Join Date
    Nov 2010
    Age
    28
    Posts
    4,132
    Thanks given
    1,077
    Thanks received
    1,137
    Rep Power
    5000
    This is pretty bad. And there's already a Timer class in the java API so you may want to change the name.
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Banned

    Join Date
    Nov 2013
    Posts
    1,015
    Thanks given
    240
    Thanks received
    265
    Rep Power
    0
    Quote Originally Posted by 215tciddAyxO View Post
    This is pretty bad. And there's already a Timer class in the java API so you may want to change the name.
    Alright, thanks.
    Reply With Quote  
     

  7. #5  
    Registered Member
    Join Date
    Oct 2012
    Posts
    1,279
    Thanks given
    331
    Thanks received
    257
    Rep Power
    69
    Very bad, could have been done in half of the lines used.
    Reply With Quote  
     

  8. #6  
    Extreme Donator

    Zumurt's Avatar
    Join Date
    Aug 2012
    Posts
    297
    Thanks given
    49
    Thanks received
    44
    Rep Power
    79
    Thank you, I'll try this.
    Reply With Quote  
     

  9. #7  
    Banned System Timers Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    1. the "Timer" class could have been written a lot more simply... for example see blake's implementation


    2. you should really avoid making your class names the same as the ones in the java API (like previously stated)


    3. you're supposed to be using this class for throttled actions not delayed ones.... this literally isn't any better at all than just using a raw integer as a timer and throwing it in the process method. ie:

    what you ARENT supposed to be using this class for: making an announcement send every 10 minutes, if you want to do something like that use a task. pi users like to call them "cycle events"

    Code:
    // no clue how cycle events in PI look but I imagine its something like this
    CycleEvents.submit(new Event(1000) {
     
         String[] messages = { ... }
     
        @Override
         public void execute() {
             PlayerHandler.yell(messages[(int) Math.random() * messages.length]);
         }
    });
    what you ARE supposed to be using this class for:

    Code:
    // Our timer for throttling how fast we can eat food.
    Timer timer = new Timer();
    
    // Eating food method.
    public void eatFood() {
    
        // Makes sure we can only eat food every 3 seconds.
        if(timer.elapsed() > 3000) {
            ...
    
            // We've eaten the food, reset the timer.
            timer.reset();
        }
    }
    Reply With Quote  
     

  10. Thankful users:


  11. #8  
    Banned

    Join Date
    Nov 2013
    Posts
    1,015
    Thanks given
    240
    Thanks received
    265
    Rep Power
    0
    Quote Originally Posted by lare96 View Post
    1. the "Timer" class could have been written a lot more simply... for example see blake's implementation


    2. you should really avoid making your class names the same as the ones in the java API (like previously stated)


    3. you're supposed to be using this class for throttled actions not delayed ones.... this literally isn't any better at all than just using a raw integer as a timer and throwing it in the process method. ie:

    what you ARENT supposed to be using this class for: making an announcement send every 10 minutes, if you want to do something like that use a task. pi users like to call them "cycle events"

    Code:
    // no clue how cycle events in PI look but I imagine its something like this
    CycleEvents.submit(new Event(1000) {
     
         String[] messages = { ... }
     
        @Override
         public void execute() {
             PlayerHandler.yell(messages[(int) Math.random() * messages.length]);
         }
    });
    what you ARE supposed to be using this class for:

    Code:
    // Our timer for throttling how fast we can eat food.
    Timer timer = new Timer();
    
    // Eating food method.
    public void eatFood() {
    
        // Makes sure we can only eat food every 3 seconds.
        if(timer.elapsed() > 3000) {
            ...
    
            // We've eaten the food, reset the timer.
            timer.reset();
        }
    }
    Alright, thanks.
    Reply With Quote  
     

  12. #9  
    Registered Member
    Cadillac's Avatar
    Join Date
    Jul 2014
    Age
    9
    Posts
    336
    Thanks given
    0
    Thanks received
    228
    Rep Power
    951
    tbh this looks like a butchered version of blackman8192's stopwatch utility.
    back at it.
    Reply With Quote  
     

  13. Thankful user:


  14. #10  
    Banned

    Join Date
    Nov 2013
    Posts
    1,015
    Thanks given
    240
    Thanks received
    265
    Rep Power
    0
    Quote Originally Posted by Lonely Donkey View Post
    tbh this looks like a butchered version of blackman8192's stopwatch utility.
    Okay, thanks.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Similar Threads

  1. Replies: 3
    Last Post: 04-29-2010, 04:18 PM
  2. System Timers
    By tucybro in forum Tutorials
    Replies: 24
    Last Post: 04-10-2010, 01:04 AM
  3. (System) Timers
    By tj007razor in forum Tutorials
    Replies: 55
    Last Post: 01-20-2010, 07:46 AM
  4. Using the System timer instead of 'processing' them.
    By Profesor Oak in forum Tutorials
    Replies: 26
    Last Post: 05-06-2009, 10:04 AM
  5. System Update timer
    By ~Josh in forum Help
    Replies: 13
    Last Post: 04-19-2009, 04:48 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
  •