Skip to content

Commit 6024b7c

Browse files
[ShopPurger] Add timeout to prevent crash server
1 parent ee08d54 commit 6024b7c

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/main/java/org/maxgamer/quickshop/shop/ShopPurger.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.maxgamer.quickshop.shop;
2121

2222
import org.bukkit.OfflinePlayer;
23+
import org.bukkit.scheduler.BukkitRunnable;
2324
import org.maxgamer.quickshop.QuickShop;
2425
import org.maxgamer.quickshop.api.economy.EconomyTransaction;
2526
import org.maxgamer.quickshop.api.shop.Shop;
@@ -28,6 +29,7 @@
2829

2930
import java.time.temporal.ChronoUnit;
3031
import java.util.ArrayList;
32+
import java.util.Iterator;
3133
import java.util.List;
3234
import java.util.UUID;
3335

@@ -95,26 +97,37 @@ private void run() {
9597
}
9698
if (pendingRemovalShops.size() > 0) {
9799
plugin.getLogger().info("[Shop Purger] Found " + pendingRemovalShops.size() + " need to removed, will remove in the next tick.");
98-
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
99-
for (Shop shop : pendingRemovalShops) {
100-
shop.delete(false);
101-
if (returnCreationFee) {
102-
EconomyTransaction transaction =
103-
EconomyTransaction.builder()
104-
.amount(plugin.getConfig().getDouble("shop.cost"))
105-
.allowLoan(false)
106-
.core(QuickShop.getInstance().getEconomy())
107-
.currency(shop.getCurrency())
108-
.world(shop.getLocation().getWorld())
109-
.to(shop.getOwner())
110-
.build();
111-
transaction.failSafeCommit();
100+
new BukkitRunnable() {
101+
@Override
102+
public void run() {
103+
Iterator<Shop> shopIterator = pendingRemovalShops.iterator();
104+
long startTimeMs = System.currentTimeMillis();
105+
while (shopIterator.hasNext()) {
106+
if (startTimeMs - System.currentTimeMillis() > 130) {
107+
plugin.getLogger().info("[Shop Purger] it seems taking long time to purge shop (>130ms), do it in the next tick...");
108+
}
109+
Shop shop = shopIterator.next();
110+
shop.delete(false);
111+
if (returnCreationFee) {
112+
EconomyTransaction transaction =
113+
EconomyTransaction.builder()
114+
.amount(plugin.getConfig().getDouble("shop.cost"))
115+
.allowLoan(false)
116+
.core(QuickShop.getInstance().getEconomy())
117+
.currency(shop.getCurrency())
118+
.world(shop.getLocation().getWorld())
119+
.to(shop.getOwner())
120+
.build();
121+
transaction.failSafeCommit();
122+
}
123+
shopIterator.remove();
124+
plugin.getLogger().info("[Shop Purger] Shop " + shop + " has been purged.");
112125
}
113-
plugin.getLogger().info("[Shop Purger] Shop " + shop + " has been purged.");
126+
plugin.getLogger().info("[Shop Purger] Task completed, " + pendingRemovalShops.size() + " shops was purged");
127+
this.cancel();
128+
executing = false;
114129
}
115-
plugin.getLogger().info("[Shop Purger] Task completed, " + pendingRemovalShops.size() + " shops was purged");
116-
executing = false;
117-
}, 1L);
130+
}.runTaskTimer(plugin, 1L, 1L);
118131
} else {
119132
plugin.getLogger().info("[Shop Purger] Task completed, No shops need to purge.");
120133
executing = false;

0 commit comments

Comments
 (0)