From 1e5eccc25b11f09a2943e16f48c38d1d16acd9d7 Mon Sep 17 00:00:00 2001 From: KaiKikuchi Date: Sun, 26 Jul 2015 00:44:31 +0200 Subject: [PATCH] #1 bugfix - /qs clean throws a ClassCastException after a shop chest was forcefully removed from the world --- .../java/org/maxgamer/quickshop/Command/QS.java | 17 +++++++++++------ .../maxgamer/quickshop/Shop/ContainerShop.java | 11 ++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Command/QS.java b/src/main/java/org/maxgamer/quickshop/Command/QS.java index c750ba8..a206bf5 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/QS.java +++ b/src/main/java/org/maxgamer/quickshop/Command/QS.java @@ -396,12 +396,17 @@ public class QS implements CommandExecutor { int i = 0; while (shIt.hasNext()) { Shop shop = shIt.next(); - if (shop.getLocation().getWorld() != null && shop.isSelling() && shop.getRemainingStock() == 0 && shop instanceof ContainerShop) { - ContainerShop cs = (ContainerShop) shop; - if (cs.isDoubleShop()) - continue; - shIt.remove(); // Is selling, but has no stock, and is a chest shop, but is not a double shop. Can be deleted safely. - i++; + + try { + if (shop.getLocation().getWorld() != null && shop.isSelling() && shop.getRemainingStock() == 0 && shop instanceof ContainerShop) { + ContainerShop cs = (ContainerShop) shop; + if (cs.isDoubleShop()) + continue; + shIt.remove(); // Is selling, but has no stock, and is a chest shop, but is not a double shop. Can be deleted safely. + i++; + } + } catch (IllegalStateException e) { + shIt.remove(); // The shop is not there anymore, remove it } } MsgUtil.clean(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 0a6baa3..1138912 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -216,9 +216,14 @@ public class ContainerShop implements Shop { /** * @return The chest this shop is based on. */ - public Inventory getInventory() { - InventoryHolder container = (InventoryHolder) this.loc.getBlock().getState(); - return container.getInventory(); + public Inventory getInventory() throws IllegalStateException { + InventoryHolder container; + try { + container = (InventoryHolder) this.loc.getBlock().getState(); + return container.getInventory(); + } catch (Exception e) { + throw new IllegalStateException("Inventory doesn't exist anymore"); + } } /**