The AbstractDelayedEvent class, and the corresponding DelayedEventHandler class can be used to execute a task after a certain amount of time.
An example is turning the character into an NPC for 30 seconds.
Usually you would use some sort of timer, create a new variable for a timer (using up valuable memory) and add checks to a majorUpdate.
You could also use the AbstractDelayedEvent class.
An example is shown here:
Code:
// import classes we use
import net.varek.rs2d.model.Player;
import net.varek.rs2d.event.AbstractDelayedEvent;
// the class
public class MyEvent extends AbstractDelayedEvent {
// the constructor, call the constructor of the AbstractDelayedEvent class using super
public MyEvent(Player owner, int delay) {
super(owner,delay);
}
// this is the method that is called after however long you specify
public void run() {
// getOwner() returns the owner of the class (ie the player)
// getActionSender() returns the player's action sender
// sendMessage() sends a message
getOwner().getActionSender().sendMessage("This happened after 10 seconds after you clicked the object.");
// now terminate the event, so it will not happen again
terminate();
// note: you could also restart the event like so:
// setInterval(10000); - this would restart it in 10 seconds
}
}
To use this example, you could add some code to your object handling like so:
Code:
World.getWorld().getDelayedEventHandler().add(new MyEvent(player,10000));
World.getWorld() will return the singleton of the world class, getDelayedEventHandler() returns the class that handles the various events, add() will add your event.
The first argument in the constructor is the player, this can be set to null (I believe) if it is not related to any player. The second argument is the delay in milliseconds (1 millisecond = 1 second).
In this example it would print the message 10 seconds after the object was clicked.
NOTE: There is a bug in the AbstractDelayedEvent file that you have probably got.
The constructor will look like this:
Code:
public AbstractDelayedEvent(Player owner, int delay) {
this.owner = owner;
setInterval(delay);
this.running = true;
}
It should look like this:
Code:
public AbstractDelayedEvent(Player owner, int delay) {
this.owner = owner;
this.running = true;
setInterval(delay);
}
If you do not make this change, all of your events will run instantly - not after the delay.
If you wonder why it happens, it is because the setInterval checks if it is running. Running is set to false by default. If it is set to false, setInterval will always make the interval 0. Therefore, it will happen immediately.
I hope this tutorial has taught you something useful, and spared you from the various actionTimer's of Winterlove.
However, one thing to note, is that if you click the object or whatever a lot, it will queue up a lot of events. You will still need to do various checks for mass clicking/waiting for an existing event to complete yourself.