From 6d57267492eab811e8519565b0a2a47ae1313d63 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 7 Aug 2019 14:54:16 +0200 Subject: [PATCH] Fix removing double chest shops without chests When AreaShop integration was enabled and a region with double chest shops was reset and sold/unrented, those shops woudn't be removed. A method to remove a shop by its ID was introduced as a fix. --- .../shopchest/listeners/AreaShopListener.java | 2 +- .../epiceric/shopchest/utils/ShopUtils.java | 49 ++++++++++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java b/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java index b850e47..f841f1e 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java @@ -57,7 +57,7 @@ public class AreaShopListener implements Listener { for (IWrappedRegion r : WorldGuardWrapper.getInstance().getRegions(shop.getLocation())) { if (generalRegion.getLowerCaseName().equals(r.getId())) { - plugin.getShopUtils().removeShop(shop, true); + plugin.getShopUtils().removeShopById(shop.getID(), true); break; } } diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 0dbb15a..449149f 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -15,6 +15,7 @@ import org.bukkit.util.Vector; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; public class ShopUtils { @@ -114,10 +115,11 @@ public class ShopUtils { addShop(shop, addToDatabase, null); } - /** Remove a shop + /** Remove a shop. May not work properly if double chest doesn't exist! * @param shop Shop to remove * @param removeFromDatabase Whether the shop should also be removed from the database * @param callback Callback that - if succeeded - returns null + * @see ShopUtils#removeShopById(int, boolean, Callback) */ public void removeShop(Shop shop, boolean removeFromDatabase, Callback callback) { plugin.debug("Removing shop (#" + shop.getID() + ")"); @@ -146,14 +148,57 @@ public class ShopUtils { } /** - * Remove a shop + * Remove a shop. May not work properly if double chest doesn't exist! * @param shop Shop to remove * @param removeFromDatabase Whether the shop should also be removed from the database + * @see ShopUtils#removeShopById(int, boolean) */ public void removeShop(Shop shop, boolean removeFromDatabase) { removeShop(shop, removeFromDatabase, null); } + /** + * Remove a shop by its ID + * @param shopId ID of the shop to remove + * @param removeFromDatabase Whether the shop should also be removed from the database + * @param callback Callback that - if succeeded - returns null + */ + public void removeShopById(int shopId, boolean removeFromDatabase, Callback callback) { + Map toRemove = shopLocation.entrySet().stream() + .filter(e -> e.getValue().getID() == shopId) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + + plugin.debug(String.format("Removing %d shop(s) with ID %d", toRemove.size(), shopId)); + + if (toRemove.isEmpty()) { + if (callback != null) callback.callSyncResult(null); + return; + } + + toRemove.forEach((loc, shop) -> { + shopLocation.remove(loc); + + shop.removeItem(); + shop.removeHologram(); + }); + + // Database#removeShop removes shop by ID so this only needs to be called once + if (removeFromDatabase) { + plugin.getShopDatabase().removeShop(toRemove.values().iterator().next(), callback); + } else { + if (callback != null) callback.callSyncResult(null); + } + } + + /** + * Remove a shop by its ID + * @param shopId ID of the shop to remove + * @param removeFromDatabase Whether the shop should also be removed from the database + */ + public void removeShopById(int shopId, boolean removeFromDatabase) { + removeShopById(shopId, removeFromDatabase, null); + } + /** * Get the shop limits of a player * @param p Player, whose shop limits should be returned