From 7032209ef7a82b407289e9b40173872fe9814a44 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 19 Aug 2018 23:07:58 +0200 Subject: [PATCH] Use queue and move event for shop updater --- .../listeners/ShopUpdateListener.java | 30 +++++----- .../java/de/epiceric/shopchest/shop/Shop.java | 3 +- .../epiceric/shopchest/utils/ShopUpdater.java | 55 +++++++++++++++---- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java index dd72872..5242615 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopUpdateListener.java @@ -8,6 +8,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.world.WorldLoadEvent; @@ -58,28 +59,31 @@ public class ShopUpdateListener implements Listener { new BukkitRunnable() { @Override public void run() { - plugin.getUpdater().beforeNext(new Runnable() { - @Override - public void run() { - if (p.isOnline()) { - for (Shop shop : plugin.getShopUtils().getShops()) { - if (shop.hasItem()) { - shop.getItem().hidePlayer(p); - } - if (shop.hasHologram()) { - shop.getHologram().hidePlayer(p); - } + plugin.getUpdater().queue(() -> { + if (p.isOnline()) { + for (Shop shop : plugin.getShopUtils().getShops()) { + if (shop.hasItem()) { + shop.getItem().hidePlayer(p); + } + if (shop.hasHologram()) { + shop.getHologram().hidePlayer(p); } - // so next update will update correctly - plugin.getShopUtils().resetPlayerLocation(p); } + // so next update will update correctly + plugin.getShopUtils().resetPlayerLocation(p); } }); + plugin.getUpdater().updateShops(p); } }.runTaskLater(plugin, 15L); } } + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onPlayerMove(PlayerMoveEvent e) { + plugin.getUpdater().updateShops(e.getPlayer()); + } + @EventHandler public void onWorldLoad(WorldLoadEvent e) { final String worldName = e.getWorld().getName(); diff --git a/src/main/java/de/epiceric/shopchest/shop/Shop.java b/src/main/java/de/epiceric/shopchest/shop/Shop.java index 523ed44..b5decc4 100644 --- a/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -119,11 +119,12 @@ public class Shop { if (item == null) createItem(); // Update shops for players in the same world after creation has finished - plugin.getUpdater().beforeNext(() -> { + plugin.getUpdater().queue(() -> { for (Player player : location.getWorld().getPlayers()) { plugin.getShopUtils().resetPlayerLocation(player); } }); + plugin.getUpdater().updateShops(location.getWorld()); }); created = true; diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java index 42146f5..5cf931f 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUpdater.java @@ -2,15 +2,16 @@ package de.epiceric.shopchest.utils; import de.epiceric.shopchest.ShopChest; import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.entity.Player; -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; public class ShopUpdater { private final ShopChest plugin; - private final Queue beforeNext = new ConcurrentLinkedQueue<>(); + private final BlockingQueue queue = new LinkedBlockingQueue<>(); private volatile Thread thread; @@ -25,13 +26,10 @@ public class ShopUpdater { if (!isRunning()) { thread = new Thread(() -> { while (!Thread.interrupted()) { - for (Runnable runnable : beforeNext) { - runnable.run(); - } - beforeNext.clear(); - - for (Player p : Bukkit.getOnlinePlayers()) { - plugin.getShopUtils().updateShops(p); + try { + queue.take().run(); + } catch (InterruptedException e) { + break; } } }, "Shop Updater"); @@ -64,12 +62,45 @@ public class ShopUpdater { return thread != null; } + /** + * Queue a task to update shops for the given player + * + * @param player Player to show updates + */ + public void updateShops(Player player) { + queue(() -> plugin.getShopUtils().updateShops(player)); + } + + /** + * Queue a task to update shops for players in the given world + * + * @param world World in whose players to show updates + */ + public void updateShops(World world) { + queue(() -> { + for (Player player : world.getPlayers()) { + plugin.getShopUtils().updateShops(player); + } + }); + } + + /** + * Queue a task to update shops for all players + */ + public void updateShops() { + queue(() -> { + for (Player player : Bukkit.getOnlinePlayers()) { + plugin.getShopUtils().updateShops(player); + } + }); + } + /** * Register a task to run before next loop * * @param runnable task to run */ - public void beforeNext(Runnable runnable) { - beforeNext.add(runnable); + public void queue(Runnable runnable) { + queue.add(runnable); } }