Timer System

Well what actually kinda annoys me in JME2 is no real timer system.



Like I have a gun fires it, it set readyy to false, and starts a timer that sets ready to true again after a few seconds



A simple and nonperfect Idea is this:


package de.empirephoenix.util;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;

public class TimedInvocation {
   public static Vector<Timedmethodinvocation> timedobjects = new Vector<Timedmethodinvocation>();
   private static Vector<Timedmethodinvocation> addcache = new Vector<Timedmethodinvocation>();
   
   public static void Update(){
      long curtime = System.currentTimeMillis();
      Iterator<Timedmethodinvocation> additer = addcache.listIterator();
      while(additer.hasNext()){
         Timedmethodinvocation cur = additer.next();
         timedobjects.add(cur);
         additer.remove();
      }
      Iterator<Timedmethodinvocation> iter = timedobjects.listIterator();
      while(iter.hasNext()){
         Timedmethodinvocation current = iter.next();
         current.Update(curtime);
         if(current.isFinished){iter.remove();}
      }
   }
   
   public static void Add(Object toexecute,String methodname,int milisdelay){
      Add(toexecute, methodname, milisdelay,null,null);
   }
   
   public static void Add(Object toexecute,String methodname,int milisdelay,Object argument, Class<Object> objectclass){
      try {
         addcache.add(new Timedmethodinvocation(toexecute,methodname,milisdelay,argument,objectclass));
      } catch (SecurityException e) {
         e.printStackTrace();
      } catch (NoSuchMethodException e) {
         e.printStackTrace();
      }
   }
   
}

class Timedmethodinvocation{
   public boolean isFinished = false;
   private final Object objecttouse;
   private final Method toinvoke;
   private final long timetoexecute;
   private final Object argument;
   
   Timedmethodinvocation(Object toexecute, String methodname, int milisdelay,Object argument,Class objectclass) throws SecurityException, NoSuchMethodException{
      objecttouse = toexecute;
      if(argument != null){
         toinvoke = toexecute.getClass().getMethod(methodname,objectclass);
      }else{
         toinvoke = toexecute.getClass().getMethod(methodname);
      }

      timetoexecute = milisdelay + System.currentTimeMillis();
      this.argument = argument;
   }
   
   void Update(long curtime){
      if(curtime >= timetoexecute){
         try {
            if(argument != null){
               toinvoke.invoke(objecttouse, argument);
            }else{
               toinvoke.invoke(objecttouse);
            }
         } catch (Exception e) {e.printStackTrace();}
         isFinished = true;
      }
   }
}



Useage, for a singleshoot weapon: (Pseudocode)


public void Fire(){
if(ready){
firebullet();
ready = false;
TimedInvocation.Add(this,"Reloaded")
}
}

public void Reloaded(){
ready = true;
}
}


Either there is no simple system for that currently in JME or i never found it.