From 30a2b8aefcd7de1a6f11ce848a029ebe88076626 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 25 Mar 2020 14:50:21 +0100 Subject: [PATCH] Make ShopUtils#getShops() return a new copy ShopUtils#getShopsCopy() is now deprecated --- src/main/java/de/epiceric/shopchest/ShopChest.java | 2 +- .../shopchest/command/ShopCommandExecutor.java | 5 ++--- .../shopchest/listeners/AreaShopListener.java | 2 +- .../shopchest/listeners/BentoBoxListener.java | 2 +- .../java/de/epiceric/shopchest/utils/ShopUtils.java | 13 +++++++------ 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 88f3c43..50462bb 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -249,7 +249,7 @@ public class ShopChest extends JavaPlugin { shopCreationThreadPool.shutdown(); } - for (Shop shop : shopUtils.getShopsCopy()) { + for (Shop shop : shopUtils.getShops()) { shopUtils.removeShop(shop, false); debug("Removed shop (#" + shop.getID() + ")"); } diff --git a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java index 7087cbc..2ddfec0 100644 --- a/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java +++ b/src/main/java/de/epiceric/shopchest/command/ShopCommandExecutor.java @@ -202,9 +202,8 @@ class ShopCommandExecutor implements CommandExecutor { plugin.getUpdater().restart(); // Remove all shops - Iterator iter = shopUtils.getShops().iterator(); - while (iter.hasNext()) { - shopUtils.removeShop(iter.next(), false); + for (Shop shop : shopUtils.getShops()) { + shopUtils.removeShop(shop, false); } Chunk[] loadedChunks = Bukkit.getWorlds().stream().map(World::getLoadedChunks) diff --git a/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java b/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java index 3bc72d3..e431b15 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/AreaShopListener.java @@ -53,7 +53,7 @@ public class AreaShopListener implements Listener { private void removeShopsInRegion(GeneralRegion generalRegion) { if (!plugin.hasWorldGuard()) return; - for (Shop shop : plugin.getShopUtils().getShopsCopy()) { + for (Shop shop : plugin.getShopUtils().getShops()) { if (!shop.getLocation().getWorld().getName().equals(generalRegion.getWorldName())) continue; for (IWrappedRegion r : WorldGuardWrapper.getInstance().getRegions(shop.getLocation())) { diff --git a/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java b/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java index 8364662..45035de 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java @@ -70,7 +70,7 @@ public class BentoBoxListener implements Listener { if (!Config.enableBentoBoxIntegration) return; - Collection shops = plugin.getShopUtils().getShopsCopy(); + Collection shops = plugin.getShopUtils().getShops(); for (Shop shop : shops) { if (!shop.getLocation().getWorld().getName().equals(world.getName())) { continue; diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 71ee757..46937e0 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -65,23 +65,24 @@ public class ShopUtils { } /** - * Get all shops - * Do not use for removing while iteration! + * Get a collection of all loaded shops + *

+ * This collection is safe to use for looping over and removing shops. * - * @see #getShopsCopy() - * @return Read-only collection of all shops, may contain duplicates + * @return Read-only collection of all shops, may contain duplicates for double chests */ public Collection getShops() { - return shopLocationValues; + return Collections.unmodifiableCollection(new ArrayList<>(shopLocationValues)); } /** * Get all shops - * Same as {@link #getShops()} but this is safe to remove while iterating * * @see #getShops() * @return Copy of collection of all shops, may contain duplicates + * @deprecated Use {@link #getShops()} instead */ + @Deprecated public Collection getShopsCopy() { return new ArrayList<>(getShops()); }