Fixup a few threadsafe issue I guess

Former-commit-id: e93bed251047a33c828767ee8c41f84f944a416a
This commit is contained in:
Sotr committed 2020-01-20 19:43:44 +07:00
1 parent f375b5b2c0
commit 3fc4e78aae
9 files changed
+45 -64

No files matched your search

@@ -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();
@@ -161,7 +161,7 @@ public class ArmorStandDisplayItem implements DisplayItem {
}
@Override
public Entity getDisplay() {
public @Nullable Entity getDisplay() {
return this.armorStand;
}
@@ -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);
}
@@ -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<UUID, Info> actions = new HashMap<>();
@@ -59,7 +60,7 @@ public class ShopManager {
private final HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> shops = new HashMap<>();
private Set<Shop> loadedShops = new CopyOnWriteArraySet<>();
private final Set<Shop> loadedShops = Sets.newHashSet();
private boolean useFastShopSearchAlgorithm = false;
@@ -1086,7 +1087,8 @@ public class ShopManager {
*
* @return All loaded shops.
*/
public @Nullable Set<Shop> getLoadedShops() {
@NotNull
public Set<Shop> getLoadedShops() {
return this.loadedShops;
}
@@ -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();
}
}
});
}
}
@@ -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<DisplayItem> checkQueue = new LinkedList<>();
@Override
@@ -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<Shop> pendingCheckDisplayCopy = (ArrayList<Shop>) 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<Shop> pendingCheckDisplay = new ArrayList<>();
//private ArrayList<Shop> 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());
}
}
}
@@ -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<Shop> checkQueue = new LinkedList<>();
@@ -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<Shop> checkQueue = new LinkedList<>();