mirror of
https://github.com/amalthea-mc/ShopChest.git
synced 2024-11-12 13:41:06 +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.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,28 +59,31 @@ 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
|
if (p.isOnline()) {
|
||||||
public void run() {
|
for (Shop shop : plugin.getShopUtils().getShops()) {
|
||||||
if (p.isOnline()) {
|
if (shop.hasItem()) {
|
||||||
for (Shop shop : plugin.getShopUtils().getShops()) {
|
shop.getItem().hidePlayer(p);
|
||||||
if (shop.hasItem()) {
|
}
|
||||||
shop.getItem().hidePlayer(p);
|
if (shop.hasHologram()) {
|
||||||
}
|
shop.getHologram().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);
|
}.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();
|
||||||
|
@ -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;
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user