|
20 | 20 | package org.maxgamer.quickshop.shop; |
21 | 21 |
|
22 | 22 | import org.bukkit.OfflinePlayer; |
| 23 | +import org.bukkit.scheduler.BukkitRunnable; |
23 | 24 | import org.maxgamer.quickshop.QuickShop; |
24 | 25 | import org.maxgamer.quickshop.api.economy.EconomyTransaction; |
25 | 26 | import org.maxgamer.quickshop.api.shop.Shop; |
|
28 | 29 |
|
29 | 30 | import java.time.temporal.ChronoUnit; |
30 | 31 | import java.util.ArrayList; |
| 32 | +import java.util.Iterator; |
31 | 33 | import java.util.List; |
32 | 34 | import java.util.UUID; |
33 | 35 |
|
@@ -95,26 +97,37 @@ private void run() { |
95 | 97 | } |
96 | 98 | if (pendingRemovalShops.size() > 0) { |
97 | 99 | 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."); |
112 | 125 | } |
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; |
114 | 129 | } |
115 | | - plugin.getLogger().info("[Shop Purger] Task completed, " + pendingRemovalShops.size() + " shops was purged"); |
116 | | - executing = false; |
117 | | - }, 1L); |
| 130 | + }.runTaskTimer(plugin, 1L, 1L); |
118 | 131 | } else { |
119 | 132 | plugin.getLogger().info("[Shop Purger] Task completed, No shops need to purge."); |
120 | 133 | executing = false; |
|
0 commit comments