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