Bukkit has a built in scheduling system that you can read up on
Scheduler Programming
Use this instead of normal Java timers, trust me. It'll make your life easier in the long run.
To do what you're wanting to do, you'd need a BukkitRunnable
class to give to the scheduler.
This is a generic one I over-simplified for example purposes:
public class Callback extends BukkitRunnable{
private Object targetObject;
public Method targetMethod;
private Object[] perameters;
public Callback(Object targetObject, String methodName, Object[] argsOrNull){
try {
this.targetMethod = targetObject.getClass().getMethod(methodName, (Class<?>[]) argsOrNull);
} catch (Exception e){
e.printStackTrace();
}
this.targetObject = targetObject;
this.perameters = argsOrNull;
}
public void run(){
try {
this.targetMethod.invoke(this.targetObject,perameters);
} catch (Exception e){
e.printStackTrace();
}
}
}
Then you create an object of that runnable, providing the callback method/props as args, and give it to the scheduler to run in 60 seconds:
For the movement part, you just watch that while the item is dropped and nobody's moved yet.
public class DropWatcher implements Listener {
private Boolean hasAnythingMoved;
private Boolean dropped;
private Pwncraft plugin;
private Player player;
public DropWatcher(Pwncraft plugin, Player player){
this.player = player;
this.hasAnythingMoved = false;
this.dropped = false;
this.plugin = plugin;
this.plugin.pluginManager.registerEvents(this, plugin);
}
//Drop event listener: When the player drops an item, it sets dropped to true, and initiates the countdown.
@EventHandler
public void onDropItem (PlayerDropItemEvent e) {
if(e.getPlayer().equals(this.player) && !this.dropped){
this.dropped = true;
BukkitCallbackTask doInSixtySeconds = new BukkitCallbackTask(this, "timesUp" , null);
doInSixtySeconds.runTaskLater(plugin, 1200); // time is in ticks (20 ticks +/- = 1 sec), so 1200 ticks = 1 min.
}
}
//Watches for other-players' movement, and sets hasAnythingMoved to true if so.
@EventHandler
public void onMove (PlayerMoveEvent e){
if(!e.getPlayer().equals(this.player) && this.dropped && !this.hasAnythingMoved){
this.hasAnythingMoved = true;
}
}
/*
This is the method the runnable calls when the timer is up.
It checks for movement, and if so, sends a message and explodes the player
(Just because it can. You're welcome to veto the explosion.)
*/
public void timesUp(){
if(this.hasAnythingMoved){
this.player.sendMessage("Someone moved! Time to party!");
this.player.getWorld().createExplosion(this.player.getLocation(), 5F);
this.dropped = false;
this.hasAnythingMoved = false;
}
}
}