Use queue and move event for shop updater

This commit is contained in:
Eric 2018-08-19 23:07:58 +02:00
parent c80129b56f
commit 7032209ef7
3 changed files with 62 additions and 26 deletions

View File

@ -8,6 +8,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldLoadEvent;
@ -58,9 +59,7 @@ public class ShopUpdateListener implements Listener {
new BukkitRunnable() { new BukkitRunnable() {
@Override @Override
public void run() { public void run() {
plugin.getUpdater().beforeNext(new Runnable() { plugin.getUpdater().queue(() -> {
@Override
public void run() {
if (p.isOnline()) { if (p.isOnline()) {
for (Shop shop : plugin.getShopUtils().getShops()) { for (Shop shop : plugin.getShopUtils().getShops()) {
if (shop.hasItem()) { if (shop.hasItem()) {
@ -73,13 +72,18 @@ public class ShopUpdateListener implements Listener {
// so next update will update correctly // so next update will update correctly
plugin.getShopUtils().resetPlayerLocation(p); plugin.getShopUtils().resetPlayerLocation(p);
} }
}
}); });
plugin.getUpdater().updateShops(p);
} }
}.runTaskLater(plugin, 15L); }.runTaskLater(plugin, 15L);
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) {
plugin.getUpdater().updateShops(e.getPlayer());
}
@EventHandler @EventHandler
public void onWorldLoad(WorldLoadEvent e) { public void onWorldLoad(WorldLoadEvent e) {
final String worldName = e.getWorld().getName(); final String worldName = e.getWorld().getName();

View File

@ -119,11 +119,12 @@ public class Shop {
if (item == null) createItem(); if (item == null) createItem();
// Update shops for players in the same world after creation has finished // Update shops for players in the same world after creation has finished
plugin.getUpdater().beforeNext(() -> { plugin.getUpdater().queue(() -> {
for (Player player : location.getWorld().getPlayers()) { for (Player player : location.getWorld().getPlayers()) {
plugin.getShopUtils().resetPlayerLocation(player); plugin.getShopUtils().resetPlayerLocation(player);
} }
}); });
plugin.getUpdater().updateShops(location.getWorld());
}); });
created = true; created = true;

View File

@ -2,15 +2,16 @@ package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.ShopChest;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Queue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.LinkedBlockingQueue;
public class ShopUpdater { public class ShopUpdater {
private final ShopChest plugin; private final ShopChest plugin;
private final Queue<Runnable> beforeNext = new ConcurrentLinkedQueue<>(); private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
private volatile Thread thread; private volatile Thread thread;
@ -25,13 +26,10 @@ public class ShopUpdater {
if (!isRunning()) { if (!isRunning()) {
thread = new Thread(() -> { thread = new Thread(() -> {
while (!Thread.interrupted()) { while (!Thread.interrupted()) {
for (Runnable runnable : beforeNext) { try {
runnable.run(); queue.take().run();
} } catch (InterruptedException e) {
beforeNext.clear(); break;
for (Player p : Bukkit.getOnlinePlayers()) {
plugin.getShopUtils().updateShops(p);
} }
} }
}, "Shop Updater"); }, "Shop Updater");
@ -64,12 +62,45 @@ public class ShopUpdater {
return thread != null; 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 * Register a task to run before next loop
* *
* @param runnable task to run * @param runnable task to run
*/ */
public void beforeNext(Runnable runnable) { public void queue(Runnable runnable) {
beforeNext.add(runnable); queue.add(runnable);
} }
} }