From e135df1c62268b8ebdbf1635c3cd725adad25a68 Mon Sep 17 00:00:00 2001 From: cnaude Date: Thu, 4 Apr 2013 20:48:56 -0700 Subject: [PATCH] Replace Timers with Bukkit schedulers --- src/net/spoothie/chairs/Chairs.java | 2 +- src/net/spoothie/chairs/EventListener.java | 34 +++++++++------------- src/plugin.yml | 2 +- 3 files changed, 16 insertions(+), 22 deletions(-) diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index 09093fc..a6c25e8 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -226,7 +226,7 @@ public class Chairs extends JavaPlugin { } public void sendSit() { - for (String s : sit.keySet()) { + for (String s : sit.keySet()) { Player p = Bukkit.getPlayer(s); if (p != null) { sendSit(p); diff --git a/src/net/spoothie/chairs/EventListener.java b/src/net/spoothie/chairs/EventListener.java index b699077..39c6873 100644 --- a/src/net/spoothie/chairs/EventListener.java +++ b/src/net/spoothie/chairs/EventListener.java @@ -1,8 +1,6 @@ package net.spoothie.chairs; import java.util.ArrayList; -import java.util.Timer; -import java.util.TimerTask; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -54,22 +52,19 @@ public class EventListener implements Listener { } } } - - - class sendSitTask extends TimerTask { - - @Override - public void run() { - plugin.sendSit(); - } - } - + @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - Timer timer = new Timer(); - long delay = 1 * 2000; - timer.schedule(new sendSitTask(), delay); - //plugin.sendSit(); + public void onPlayerJoin(PlayerJoinEvent event) { + delayedSitTask(); + } + + private void delayedSitTask() { + plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { + @Override + public void run() { + plugin.sendSit(); + } + }, 20 ); } @EventHandler @@ -113,6 +108,7 @@ public class EventListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.getPlayer().getItemInHand().getType().isBlock() + && (event.getPlayer().getItemInHand().getTypeId() != 0) && plugin.ignoreIfBlockInHand) { return; } @@ -287,9 +283,7 @@ public class EventListener implements Listener { plugin.sit.put(player.getName(), block.getLocation()); event.setUseInteractedBlock(Result.DENY); - Timer timer = new Timer(); - long delay = 1 * 2000; - timer.schedule(new sendSitTask(), delay); + delayedSitTask(); } } } diff --git a/src/plugin.yml b/src/plugin.yml index dc524f7..089bdf7 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: Chairs -version: 2.0.1 +version: 2.0.2 description: Let players sit on blocks. authors: - spoothie