Fix thread safety issue on ongoing fee

Former-commit-id: 0e3a89fbed12566778718c4b9a200c9462f396ee
This commit is contained in:
Sotr committed 2020-01-24 03:05:07 +07:00
1 parent 4610903ae9
commit 7890f2fa39
5 files changed
+48 -52

No files matched your search

@@ -198,7 +198,7 @@ public class QuickShop extends JavaPlugin {
private OngoingFeeWatcher ongoingFeeWatcher;
private SignUpdateWatcher signUpdateWatcher;
private ShopContainerWatcher shopContainerWatcher;
private DisplayDupeRemoverWatcher displayDupeRemoverWatcher;
private @Deprecated DisplayDupeRemoverWatcher displayDupeRemoverWatcher;
private BukkitAPIWrapper bukkitAPIWrapper;
private boolean isUtilInited = false;
@@ -630,21 +630,10 @@ public class QuickShop extends JavaPlugin {
}
if (getConfig().getBoolean("shop.ongoing-fee.enable")) {
getLogger().info("Ongoing fee feature is enabled.");
// NOT SAFE FOR ASYNC
if (getConfig().getBoolean("shop.ongoing-fee.async", true)) {
ongoingFeeWatcher.runTaskTimerAsynchronously(
this,
getConfig().getInt("shop.ongoing-fee.ticks"),
getConfig().getInt("shop.ongoing-fee.ticks"));
} else {
getLogger()
.warning(
"You forcing execute ongoing-fee tasks on server thread, it may cause serious performance issue, turn off it if you got performance issue,");
ongoingFeeWatcher.runTaskTimer(
this,
getConfig().getInt("shop.ongoing-fee.ticks"),
getConfig().getInt("shop.ongoing-fee.ticks"));
}
ongoingFeeWatcher.runTaskTimerAsynchronously(
this,
0,
getConfig().getInt("shop.ongoing-fee.ticks"));
}
if (this.display) {
if (getConfig().getBoolean("shop.display-auto-despawn")) {
@@ -19,19 +19,23 @@
package org.maxgamer.quickshop.Shop;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
@@ -62,8 +66,8 @@ import org.maxgamer.quickshop.Util.Util;
/** Manage a lot of shops. */
public class ShopManager {
private final HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> shops =
new HashMap<>();
private final ConcurrentHashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> shops =
new ConcurrentHashMap<>();
private final Set<Shop> loadedShops = Sets.newConcurrentHashSet();
private HashMap<UUID, Info> actions = new HashMap<>();
private QuickShop plugin;
@@ -1189,13 +1193,10 @@ public class ShopManager {
*
* @return All shop in the database
*/
public Collection<Shop> getAllShops() {
//noinspection unchecked
HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> worldsMap =
(HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>>) getShops().clone();
Collection<Shop> shops = new ArrayList<>();
for (HashMap<ShopChunk, HashMap<Location, Shop>> shopMapData : worldsMap.values()) {
for (HashMap<Location, Shop> shopData : shopMapData.values()) {
public List<Shop> getAllShops() {
List<Shop> shops = new ArrayList<>();
for (HashMap<ShopChunk, HashMap<Location, Shop>> shopChunkMap : this.shops.values()) {
for (HashMap<Location, Shop> shopData : shopChunkMap.values()) {
shops.addAll(shopData.values());
}
}
@@ -1208,7 +1209,7 @@ public class ShopManager {
* @return a hashmap of World - Chunk - Shop
*/
@NotNull
public HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> getShops() {
public ConcurrentHashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> getShops() {
return this.shops;
}
@@ -1251,10 +1252,7 @@ public class ShopManager {
private Iterator<HashMap<ShopChunk, HashMap<Location, Shop>>> worlds;
public ShopIterator() {
//noinspection unchecked
HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>> worldsMap =
(HashMap<String, HashMap<ShopChunk, HashMap<Location, Shop>>>) getShops().clone();
worlds = worldsMap.values().iterator();
worlds = Collections.unmodifiableCollection(getShops().values()).iterator();
}
/** Returns true if there is still more shops to iterate over. */
@@ -209,6 +209,7 @@ class ItemMetaMatcher {
if (!this.displayname) {
return true;
}
Util.debugLog("Checking displayname");
if (!meta1.hasDisplayName()) {
return true;
} else {
@@ -19,8 +19,11 @@
package org.maxgamer.quickshop.Watcher;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
@@ -50,29 +53,37 @@ public class OngoingFeeWatcher extends BukkitRunnable {
int cost = plugin.getConfig().getInt("shop.ongoing-fee.cost-per-shop");
boolean allowLoan = plugin.getConfig().getBoolean("shop.allow-economy-loan");
boolean ignoreUnlimited = plugin.getConfig().getBoolean("shop.ongoing-fee.ignore-unlimited");
int perTaskFlow = 0;
int parallelTasks = 0;
for (Shop shop : plugin.getShopManager().getAllShops()) {
if (!shop.isUnlimited() || !ignoreUnlimited) {
UUID shopOwner = shop.getOwner();
if (!allowLoan) {
// Disallow loan
if (plugin.getEconomy().getBalance(shopOwner) < cost) {
this.removeShop(shop);
continue;
parallelTasks++;
if (parallelTasks > Bukkit.getTPS()[0] * 5) {
perTaskFlow++;
parallelTasks = 0;
}
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (!allowLoan) {
// Disallow loan
if (plugin.getEconomy().getBalance(shopOwner) < cost) {
this.removeShop(shop);
}
}
}
boolean success = plugin.getEconomy().withdraw(shop.getOwner(), cost);
if (!success) {
this.removeShop(shop);
continue;
}
try {
//noinspection ConstantConditions,deprecation
plugin
.getEconomy()
.deposit(
Bukkit.getOfflinePlayer(plugin.getConfig().getString("tax")).getUniqueId(), cost);
} catch (Exception ignored) {
}
boolean success = plugin.getEconomy().withdraw(shop.getOwner(), cost);
if (!success) {
this.removeShop(shop);
} else {
try {
//noinspection ConstantConditions,deprecation
plugin
.getEconomy()
.deposit(
Bukkit.getOfflinePlayer(plugin.getConfig().getString("tax")).getUniqueId(), cost);
} catch (Exception ignored) {
}
}
}, perTaskFlow);
} else {
Util.debugLog(
"Shop was ignored for ongoing fee cause it is unlimited and ignoreUnlimited = true : "
-3
View File
@@ -286,9 +286,6 @@ shop:
cost-per-shop: 2
#Should we ignore the unlimited shop?
ignore-unlimited: true
#Should we do check and transfer on off-thread?
#Only turn off it when your economy system can't handle calls on off-thread.
async: true
#Allow QuickShop use hack util to force load shops from higher Minecraft.
#WARNING: It may destory your server data and QuickShop shops, backup before enable it!
force-load-downgrade-items: