diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 6348caf3f..55b48d611 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -20,11 +20,11 @@ import org.maxgamer.quickshop.Database.*; import org.maxgamer.quickshop.Database.Database.ConnectionException; import org.maxgamer.quickshop.Economy.*; import org.maxgamer.quickshop.Listeners.*; -import org.maxgamer.quickshop.Shop.Shop; import org.maxgamer.quickshop.Shop.ShopLoader; import org.maxgamer.quickshop.Shop.ShopManager; import org.maxgamer.quickshop.Util.Timer; import org.maxgamer.quickshop.Util.*; +import org.maxgamer.quickshop.Watcher.DisplayWatcher; import org.maxgamer.quickshop.Watcher.LogWatcher; import org.maxgamer.quickshop.Watcher.UpdateWatcher; @@ -109,6 +109,7 @@ public class QuickShop extends JavaPlugin { private SentryErrorReporter sentryErrorReporter; private CommandManager commandManager; private ShopProtectionListener shopProtectListener; + private DisplayWatcher displayWatcher; /** * Get the Player's Shop limit. @@ -276,6 +277,7 @@ public class QuickShop extends JavaPlugin { customInventoryListener = new CustomInventoryListener(this); displayBugFixListener = new DisplayBugFixListener(this); shopProtectListener = new ShopProtectionListener(this); + displayWatcher = new DisplayWatcher(this); Bukkit.getServer().getPluginManager().registerEvents(blockListener, this); Bukkit.getServer().getPluginManager().registerEvents(playerListener, this); Bukkit.getServer().getPluginManager().registerEvents(chatListener, this); @@ -285,26 +287,7 @@ public class QuickShop extends JavaPlugin { Bukkit.getServer().getPluginManager().registerEvents(customInventoryListener, this); Bukkit.getServer().getPluginManager().registerEvents(displayBugFixListener, this); Bukkit.getServer().getPluginManager().registerEvents(shopProtectListener, this); - getLogger().info("Registering DisplayCheck task...."); - if (display && displayItemCheckTicks > 0) { - new BukkitRunnable() { - @Override - public void run() { - if (getConfig().getInt("shop.display-items-check-ticks") < 3000) - getLogger() - .severe("Shop.display-items-check-ticks is too low! It may cause HUGE lag! Pick a number > 3000"); - Iterator it = getShopManager().getShopIterator(); - while (it.hasNext()) { - Shop shop = it.next(); - if (shop == null) - continue; - if (!shop.isLoaded()) - continue; - shop.checkDisplay(); - } - } - }.runTaskTimer(this, 1L, displayItemCheckTicks); - } + getLogger().info("Cleaning MsgUtils..."); MsgUtil.loadTransactionMessages(); MsgUtil.clean(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 234ba3e41..604c4e29d 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -72,12 +72,12 @@ public class ContainerShop implements Shop { /** * Adds a new shop. * - * @param loc The location of the chest block - * @param price The cost per item - * @param item The itemstack with the properties we want. This is .cloned, no - * need to worry about references + * @param loc The location of the chest block + * @param price The cost per item + * @param item The itemstack with the properties we want. This is .cloned, no + * need to worry about references * @param moderator The modertators - * @param type The shop type + * @param type The shop type * @param unlimited The unlimited */ public ContainerShop(@NotNull Location loc, double price, @NotNull ItemStack item, @NotNull ShopModerator moderator, boolean unlimited, @NotNull ShopType type) { @@ -111,7 +111,6 @@ public class ContainerShop implements Shop { Util.debugLog("The display was disabled."); } - } /** @@ -600,15 +599,14 @@ public class ContainerShop implements Shop { for (Block b : blocks) { if (b == null) { - plugin.getLogger().warning("Null signs in the queue"); + plugin.getLogger().warning("Null signs in the queue, skipping"); + continue; } Material mat = b.getType(); - if (!Util.isWallSign(mat)) { + if (!Util.isWallSign(mat)) continue; - } - if (!isAttached(b)) { + if (!isAttached(b)) continue; - } if (!(b.getState() instanceof Sign)) continue; org.bukkit.block.Sign sign = (org.bukkit.block.Sign) b.getState(); @@ -623,9 +621,8 @@ public class ContainerShop implements Shop { break; } } - if (!text) { + if (!text) signs.add(sign); - } } } return signs; @@ -723,17 +720,10 @@ public class ContainerShop implements Shop { Util.debugLog("Dupe load request, canceled."); return; } - ShopLoadEvent shopLoadEvent = new ShopLoadEvent(this); Bukkit.getPluginManager().callEvent(shopLoadEvent); - if (shopLoadEvent.isCancelled()) { + if (shopLoadEvent.isCancelled()) return; - } - - // if (!Util.isLoaded(this.getLocation())) { - // Util.debugLog("Shop " + this.getLocation().toString() + " skipped to load: Target location not loaded."); - // return; - // } if (!Util.canBeShop(this.getLocation().getBlock())) { this.onUnload(); @@ -763,19 +753,14 @@ public class ContainerShop implements Shop { } public String ownerName() { - if (this.isUnlimited()) { + if (this.isUnlimited()) return MsgUtil.getMessage("admin-shop"); - } - if (this.getOwner() == null) { + if (this.getOwner() == null) return MsgUtil.getMessage("unknown-owner"); - } - - final String name = Bukkit.getOfflinePlayer(this.getOwner()).getName(); - if (name == null) { + String name = Bukkit.getOfflinePlayer(this.getOwner()).getName(); + if (name == null || name.isEmpty()) return MsgUtil.getMessage("unknown-owner"); - } - return name; } @@ -829,10 +814,12 @@ public class ContainerShop implements Shop { this.displayItem.spawn(); } else { /* If not spawned, we didn't need check these, only check them when we need. */ - if (this.displayItem.checkDisplayNeedRegen()) + if (this.displayItem.checkDisplayNeedRegen()) { this.displayItem.fixDisplayNeedRegen(); - if (this.displayItem.checkDisplayIsMoved()) - this.displayItem.fixDisplayMoved(); + } else {/* If display was regened, we didn't need check it moved, performance! */ + if (this.displayItem.checkDisplayIsMoved()) + this.displayItem.fixDisplayMoved(); + } } /* Dupe is always need check, if enabled display */ if (plugin.isDisplay()) diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java new file mode 100644 index 000000000..e29bac817 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java @@ -0,0 +1,62 @@ +package org.maxgamer.quickshop.Watcher; + +import java.util.ArrayList; +import java.util.Iterator; + +import lombok.*; +import org.bukkit.Bukkit; +import org.bukkit.scheduler.BukkitRunnable; +import org.maxgamer.quickshop.QuickShop; +import org.maxgamer.quickshop.Shop.Shop; +import org.maxgamer.quickshop.Util.Util; + +@Data +public class DisplayWatcher { + private QuickShop plugin; + private ArrayList pendingCheckDisplay = new ArrayList<>(); + + public DisplayWatcher(QuickShop plugin) { + this.plugin = plugin; + registerTask(); + } + + private void registerTask() { + plugin.getLogger().info("Registering DisplayCheck task...."); + if (plugin.isDisplay() && plugin.getDisplayItemCheckTicks() > 0) { + new BukkitRunnable() { + @Override + public void run() { + if (plugin.getConfig().getInt("shop.display-items-check-ticks") < 3000) + plugin.getLogger() + .severe("Shop.display-items-check-ticks is too low! It may cause HUGE lag! Pick a number > 3000"); + Iterator it = plugin.getShopManager().getShopIterator(); + while (it.hasNext()) { + Shop shop = it.next(); + if (shop == null) + continue; + if (!shop.isLoaded()) + continue; + if (!Util.isLoaded(shop.getLocation())) + continue; + pendingCheckDisplay.add(shop); + } + Bukkit.getScheduler().runTask(plugin, new DisplayRunnable()); + } + }.runTaskTimerAsynchronously(plugin, 1L, plugin.getDisplayItemCheckTicks()); + } + } + +} + +class DisplayRunnable implements Runnable { + @Override + public void run() { + @SuppressWarnings("unchecked") ArrayList pendingCheckDisplayCopy = (ArrayList) QuickShop.instance + .getDisplayWatcher().getPendingCheckDisplay().clone(); + for (Shop shop : pendingCheckDisplayCopy) { + shop.checkDisplay(); + } + pendingCheckDisplayCopy.clear(); + QuickShop.instance.getDisplayWatcher().getPendingCheckDisplay().clear(); + } +}