Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Assign a timer to individual objects


    oh okay

    Recommended Posts

    Lets say that I have a class called item.java and in the constructor I am starting a timer, is it possible to create multiple item objects and watch the timers and get notified when they have finished? 

     

    I would love a snippet, thanks!

    Link to comment
    Share on other sites

    	public int onLoop() {
    		for(Item item : items) {
    			if(item.expired()) {
    				//Do whatever
    			}
    		}
    		return 0;
    	}
    	
    	class Item{
    		long expirationTime;
    		public Item(long timer) {
    			this.expirationTime = System.currentTimeMillis() + timer;
    		}
    		
    		public boolean expired() {
    			return System.currentTimeMillis() > expirationTime;
    		}
    	}

    So the simplest way is to just check in an update loop if the timer has expired and do whatever you want to at that point.

    An alternative, and probably the better option depending on what you're trying to do is a Timer + TimerTask.
    You set the amount of time to wait in the constructor.
    Then after that amount of time the run method in ItemTask will run on the given item.

     

    class Item{
    		
    		Timer timer;
    		
    		public Item(int timer) {
    			timer = new Timer("Item", true);
    			timer.schedule(new ItemTask(this),timer);
    		}
    	
    	}
    	class ItemTask extends TimerTask{
    		
    		Item item;
    		
    		public ItemTask(Item item) {
    			this.item = item;
    		}
    		
    		@Override
    		public void run() {
    			//Do whatever
    		}
    		
    	}

     

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.