From 3fc4e78aaeed4de21de1c797193bd649b3218e1a Mon Sep 17 00:00:00 2001 From: Sotr Date: Mon, 20 Jan 2020 19:43:44 +0700 Subject: [PATCH] Fixup a few threadsafe issue I guess Former-commit-id: e93bed251047a33c828767ee8c41f84f944a416a --- .../org/maxgamer/quickshop/QuickShop.java | 25 ++++++------ .../quickshop/Shop/ArmorStandDisplayItem.java | 2 +- .../quickshop/Shop/ContainerShop.java | 7 ++-- .../maxgamer/quickshop/Shop/ShopManager.java | 8 ++-- .../Watcher/DisplayAutoDespawnWatcher.java | 40 ++++++++----------- .../Watcher/DisplayDupeRemoverWatcher.java | 1 + .../quickshop/Watcher/DisplayWatcher.java | 24 ++--------- .../Watcher/ShopContainerWatcher.java | 1 + .../quickshop/Watcher/ShopVaildWatcher.java | 1 + 9 files changed, 45 insertions(+), 64 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 68e760b3e..431579dfd 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -178,7 +178,7 @@ public class QuickShop extends JavaPlugin { private ShopManager shopManager; private ShopProtectionListener shopProtectListener; private SyncTaskWatcher syncTaskWatcher; - private ShopVaildWatcher shopVaildWatcher; + //private ShopVaildWatcher shopVaildWatcher; private DisplayAutoDespawnWatcher displayAutoDespawnWatcher; /** * Use SpoutPlugin to get item / block names @@ -196,8 +196,8 @@ public class QuickShop extends JavaPlugin { private static PermissionManager permissionManager; private OngoingFeeWatcher ongoingFeeWatcher; private SignUpdateWatcher signUpdateWatcher; - private ShopContainerWatcher shopContainerWatcher; - private DisplayDupeRemoverWatcher displayDupeRemoverWatcher; + //private ShopContainerWatcher shopContainerWatcher; + //private DisplayDupeRemoverWatcher displayDupeRemoverWatcher; private BukkitAPIWrapper bukkitAPIWrapper; private boolean isUtilInited = false; @@ -381,7 +381,7 @@ public class QuickShop extends JavaPlugin { } Util.debugLog("Unloading all shops..."); try { - Objects.requireNonNull(this.getShopManager().getLoadedShops()).forEach(Shop::onUnload); + this.getShopManager().getLoadedShops().forEach(Shop::onUnload); } catch (Throwable th) { //ignore, we didn't care that } @@ -549,13 +549,13 @@ public class QuickShop extends JavaPlugin { shopProtectListener = new ShopProtectionListener(this); displayWatcher = new DisplayWatcher(this); syncTaskWatcher = new SyncTaskWatcher(this); - shopVaildWatcher = new ShopVaildWatcher(this); + //shopVaildWatcher = new ShopVaildWatcher(this); ongoingFeeWatcher = new OngoingFeeWatcher(this); lockListener = new LockListener(this); internalListener = new InternalListener(this); signUpdateWatcher = new SignUpdateWatcher(this); - shopContainerWatcher = new ShopContainerWatcher(this); - displayDupeRemoverWatcher = new DisplayDupeRemoverWatcher(); + //shopContainerWatcher = new ShopContainerWatcher(this); + //displayDupeRemoverWatcher = new DisplayDupeRemoverWatcher(); Bukkit.getPluginManager().registerEvents(blockListener, this); Bukkit.getPluginManager().registerEvents(playerListener, this); @@ -592,22 +592,23 @@ public class QuickShop extends JavaPlugin { } }.runTaskLater(this, 1); Util.debugLog("Registering shop watcher..."); - shopVaildWatcher.runTaskTimer(this, 0, 20 * 60); + //shopVaildWatcher.runTaskTimer(this, 0, 20 * 60); // Nobody use it signUpdateWatcher.runTaskTimer(this, 0, 120); - shopContainerWatcher.runTaskTimer(this, 0, 5); - displayDupeRemoverWatcher.runTaskTimerAsynchronously(this,0,1); + //shopContainerWatcher.runTaskTimer(this, 0, 5); // Nobody use it + //displayDupeRemoverWatcher.runTaskTimerAsynchronously(this,0,1); // This one definitely cannot run under async if (logWatcher != null) { logWatcher.runTaskTimerAsynchronously(this, 10, 10); getLogger().info("Log actions is enabled, actions will log in the qs.log file!"); } if (getConfig().getBoolean("shop.ongoing-fee.enable")) { getLogger().info("Ongoine fee feature is enabled."); - ongoingFeeWatcher.runTaskTimerAsynchronously(this, getConfig().getInt("shop.ongoing-fee.ticks"), getConfig().getInt("shop.ongoing-fee.ticks")); + // NOT SAFE FOR ASYNC + ongoingFeeWatcher.runTaskTimer(this, getConfig().getInt("shop.ongoing-fee.ticks"), getConfig().getInt("shop.ongoing-fee.ticks")); } if (this.display) { if (getConfig().getBoolean("shop.display-auto-despawn")) { this.displayAutoDespawnWatcher = new DisplayAutoDespawnWatcher(this); - this.displayAutoDespawnWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.display-check-time")); + this.displayAutoDespawnWatcher.runTaskTimer(this, 0, getConfig().getInt("shop.display-check-time")); // not worth async } } registerIntegrations(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java index 67c9a33e4..07cb7e773 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java @@ -161,7 +161,7 @@ public class ArmorStandDisplayItem implements DisplayItem { } @Override - public Entity getDisplay() { + public @Nullable Entity getDisplay() { return this.armorStand; } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 842a6fc6e..cac0373aa 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -569,7 +569,8 @@ public class ContainerShop implements Shop { * * @return The display item associated with this shop. */ - public @Nullable DisplayItem getDisplayItem() { + @NotNull + public DisplayItem getDisplayItem() { return this.displayItem; } @@ -877,7 +878,7 @@ public class ContainerShop implements Shop { } this.isLoaded = true; - Objects.requireNonNull(plugin.getShopManager().getLoadedShops()).add(this); + plugin.getShopManager().getLoadedShops().add(this); checkContainer(); @@ -910,7 +911,7 @@ public class ContainerShop implements Shop { } update(); this.isLoaded = false; - Objects.requireNonNull(plugin.getShopManager().getLoadedShops()).remove(this); + plugin.getShopManager().getLoadedShops().remove(this); ShopUnloadEvent shopUnloadEvent = new ShopUnloadEvent(this); Bukkit.getPluginManager().callEvent(shopUnloadEvent); } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index e6d72d81f..fafb95799 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -39,6 +39,8 @@ import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; +import com.google.common.collect.Sets; + import java.sql.SQLException; import java.text.DecimalFormat; import java.util.*; @@ -50,7 +52,6 @@ import java.util.stream.Collectors; /** * Manage a lot of shops. */ -@SuppressWarnings("WeakerAccess") public class ShopManager { private HashMap actions = new HashMap<>(); @@ -59,7 +60,7 @@ public class ShopManager { private final HashMap>> shops = new HashMap<>(); - private Set loadedShops = new CopyOnWriteArraySet<>(); + private final Set loadedShops = Sets.newHashSet(); private boolean useFastShopSearchAlgorithm = false; @@ -1086,7 +1087,8 @@ public class ShopManager { * * @return All loaded shops. */ - public @Nullable Set getLoadedShops() { + @NotNull + public Set getLoadedShops() { return this.loadedShops; } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java index f79809c1f..e5e081853 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayAutoDespawnWatcher.java @@ -31,36 +31,28 @@ public class DisplayAutoDespawnWatcher extends BukkitRunnable { @Override public void run() { - if (plugin.getShopManager().getLoadedShops() == null) { - return; - } - - plugin.getShopManager().getLoadedShops().parallelStream().forEach(shop -> { + plugin.getShopManager().getLoadedShops().stream().forEach(shop -> { //Check the range has player? int range = plugin.getConfig().getInt("shop.display-despawn-range"); boolean anyPlayerInRegion = Bukkit.getOnlinePlayers() - .parallelStream() + .stream() .filter(player -> player.getWorld().equals(shop.getLocation().getWorld())) .anyMatch(player -> player.getLocation().distance(shop.getLocation()) < range); - new BukkitRunnable() { - @Override - public void run() { - if (shop.getDisplay() == null) { - return; - } - if (anyPlayerInRegion) { - if (!shop.getDisplay().isSpawned()) { - Util.debugLog("Respawning the shop " + shop + " the display, cause it was despawned and a player close it"); - shop.checkDisplay(); - } - } else { - if (shop.getDisplay().isSpawned()) { - Util.debugLog("Removing the shop " + shop + " the display, cause nobody can see it"); - shop.getDisplay().remove(); - } - } + + if (shop.getDisplay() == null) { + return; + } + if (anyPlayerInRegion) { + if (!shop.getDisplay().isSpawned()) { + Util.debugLog("Respawning the shop " + shop + " the display, cause it was despawned and a player close it"); + shop.checkDisplay(); } - }.runTask(QuickShop.instance); + } else { + if (shop.getDisplay().isSpawned()) { + Util.debugLog("Removing the shop " + shop + " the display, cause nobody can see it"); + shop.getDisplay().remove(); + } + } }); } } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayDupeRemoverWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayDupeRemoverWatcher.java index 96ce84616..f74f0209a 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayDupeRemoverWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayDupeRemoverWatcher.java @@ -26,6 +26,7 @@ import org.maxgamer.quickshop.Shop.DisplayItem; import java.util.LinkedList; import java.util.Queue; +// unused, pending to remove public class DisplayDupeRemoverWatcher extends BukkitRunnable { private Queue checkQueue = new LinkedList<>(); @Override diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java index c42b65b22..6f8d9c289 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/DisplayWatcher.java @@ -28,24 +28,9 @@ import org.maxgamer.quickshop.Shop.Shop; import java.util.ArrayList; import java.util.Objects; -class DisplayRunnable implements Runnable { - @Override - public void run() { - @SuppressWarnings("unchecked") ArrayList pendingCheckDisplayCopy = (ArrayList) QuickShop.instance - .getDisplayWatcher().getPendingCheckDisplay().clone(); - for (Shop shop : pendingCheckDisplayCopy) { - if (shop != null) { - shop.checkDisplay(); - } - } - pendingCheckDisplayCopy.clear(); - QuickShop.instance.getDisplayWatcher().getPendingCheckDisplay().clear(); - } -} - @Data public class DisplayWatcher { - private ArrayList pendingCheckDisplay = new ArrayList<>(); + //private ArrayList pendingCheckDisplay = new ArrayList<>(); private QuickShop plugin; public DisplayWatcher(QuickShop plugin) { @@ -75,12 +60,9 @@ public class DisplayWatcher { // if (!Util.isLoaded(shop.getLocation())) { // continue; // } - pendingCheckDisplay.addAll(Objects.requireNonNull(plugin.getShopManager().getLoadedShops())); - //} - Bukkit.getScheduler().runTask(plugin, new DisplayRunnable()); + plugin.getShopManager().getLoadedShops().forEach(Shop::checkDisplay); } - }.runTaskTimerAsynchronously(plugin, 1L, plugin.getDisplayItemCheckTicks()); + }.runTaskTimer(plugin, 1L, plugin.getDisplayItemCheckTicks()); } } - } diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/ShopContainerWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/ShopContainerWatcher.java index 48a968e25..d22d606f0 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/ShopContainerWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/ShopContainerWatcher.java @@ -31,6 +31,7 @@ import java.util.Queue; /** * Check the shops after server booted up, make sure shop can correct self-deleted when container lost. */ +// unused, pending to remove public class ShopContainerWatcher extends BukkitRunnable { private QuickShop plugin; private Queue checkQueue = new LinkedList<>(); diff --git a/src/main/java/org/maxgamer/quickshop/Watcher/ShopVaildWatcher.java b/src/main/java/org/maxgamer/quickshop/Watcher/ShopVaildWatcher.java index 707852f93..ce2a7ff30 100644 --- a/src/main/java/org/maxgamer/quickshop/Watcher/ShopVaildWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/Watcher/ShopVaildWatcher.java @@ -31,6 +31,7 @@ import java.util.Queue; /** * Check the shops after server booted up, make sure shop can correct self-deleted when container lost. */ +// unused, pending to remove public class ShopVaildWatcher extends BukkitRunnable { private QuickShop plugin; private Queue checkQueue = new LinkedList<>();