mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-12-23 01:21:47 +00:00
Use queue and move event for shop updater
This commit is contained in:
parent
c80129b56f
commit
7032209ef7
@ -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();
|
||||
|
@ -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;
|
||||
|
@ -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<Runnable> beforeNext = new ConcurrentLinkedQueue<>();
|
||||
private final BlockingQueue<Runnable> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user