diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java index dcee49d..e9f79b4 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/HologramUpdateListener.java @@ -2,8 +2,9 @@ package de.epiceric.shopchest.listeners; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.shop.Shop; -import de.epiceric.shopchest.utils.ShopUtils; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -19,32 +20,30 @@ public class HologramUpdateListener implements Listener { @EventHandler public void onPlayerMove(PlayerMoveEvent e) { - Player p = e.getPlayer(); Location playerLocation = p.getLocation(); for (Shop shop : plugin.getShopUtils().getShops()) { + Block b = shop.getLocation().getBlock(); - if (shop.getHologram() != null) { + if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) { + plugin.getShopUtils().removeShop(shop, plugin.getShopChestConfig().remove_shop_on_error); + return; + } - Location shopLocation = shop.getLocation(); + if (shop.getHologram() == null) return; - if (playerLocation.getWorld().equals(shopLocation.getWorld())) { - - if (playerLocation.distance(shop.getHologram().getLocation()) <= plugin.getShopChestConfig().maximal_distance) { - - if (!shop.getHologram().isVisible(p)) { - shop.getHologram().showPlayer(p); - } - - } else { - - if (shop.getHologram().isVisible(p)) { - shop.getHologram().hidePlayer(p); - } + Location shopLocation = shop.getLocation(); + if (playerLocation.getWorld().equals(shopLocation.getWorld())) { + if (playerLocation.distance(shop.getHologram().getLocation()) <= plugin.getShopChestConfig().maximal_distance) { + if (!shop.getHologram().isVisible(p)) { + shop.getHologram().showPlayer(p); + } + } else { + if (shop.getHologram().isVisible(p)) { + shop.getHologram().hidePlayer(p); } - } } diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java b/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java index 87b5e05..080624a 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -5,13 +5,13 @@ import de.epiceric.shopchest.config.Regex; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.nms.IHologram; -import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.Utils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.entity.Item; @@ -36,7 +36,6 @@ public class Shop { private double buyPrice; private double sellPrice; private ShopType shopType; - private Chest chest; public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { this.id = id; @@ -49,18 +48,22 @@ public class Shop { this.shopType = shopType; Block b = location.getBlock(); - if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) { - this.chest = (Chest) b.getState(); - } else { + if (b.getType() != Material.CHEST && b.getType() != Material.TRAPPED_CHEST) { try { - if (plugin.getShopChestConfig().remove_shop_on_error) - plugin.getShopUtils().removeShop(this, true); - + plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); } catch (Exception ex) { ex.printStackTrace(); return; } + } else if ((b.getRelative(BlockFace.UP).getType() != Material.AIR) && plugin.getShopChestConfig().show_shop_items) { + try { + plugin.getShopUtils().removeShop(this, plugin.getShopChestConfig().remove_shop_on_error); + throw new Exception("No space above chest at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + } catch (Exception ex) { + ex.printStackTrace(); + return; + } } if (hologram == null || !hologram.exists()) createHologram(); @@ -133,7 +136,9 @@ public class Shop { Chest[] chests = new Chest[2]; Block b = location.getBlock(); - InventoryHolder ih = chest.getInventory().getHolder(); + InventoryHolder ih = getInventoryHolder(); + + if (ih == null) return; if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; @@ -148,7 +153,7 @@ public class Shop { } else { doubleChest = false; - chests[0] = chest; + chests[0] = (Chest) ih; } Location holoLocation; @@ -272,10 +277,17 @@ public class Shop { } /** - * @return Chest of the shop. If double chest, only one chest is returned + * @return {@link InventoryHolder} of the shop or null if the shop has no chest. */ - public Chest getChest() { - return chest; + public InventoryHolder getInventoryHolder() { + Block b = getLocation().getBlock(); + + if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) { + Chest chest = (Chest) b.getState(); + return chest.getInventory().getHolder(); + } + + return null; } /** diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 30eefb2..2190d16 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -4,10 +4,7 @@ import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.sql.Database; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.OfflinePlayer; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.entity.Entity; @@ -71,7 +68,7 @@ public class ShopUtils { * @param addToDatabase Whether the shop should also be added to the database */ public void addShop(Shop shop, boolean addToDatabase) { - InventoryHolder ih = shop.getChest().getInventory().getHolder(); + InventoryHolder ih = shop.getInventoryHolder(); if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; @@ -95,24 +92,24 @@ public class ShopUtils { * @param removeFromDatabase Whether the shop should also be removed from the database */ public void removeShop(Shop shop, boolean removeFromDatabase) { - InventoryHolder ih = shop.getChest().getInventory().getHolder(); + InventoryHolder ih = shop.getInventoryHolder(); - if (ih instanceof DoubleChest) { - DoubleChest dc = (DoubleChest) ih; - Chest r = (Chest) dc.getRightSide(); - Chest l = (Chest) dc.getLeftSide(); + if (ih instanceof DoubleChest) { + DoubleChest dc = (DoubleChest) ih; + Chest r = (Chest) dc.getRightSide(); + Chest l = (Chest) dc.getLeftSide(); - shopLocation.remove(r.getLocation()); - shopLocation.remove(l.getLocation()); - } else { - shopLocation.remove(shop.getLocation()); - } + shopLocation.remove(r.getLocation()); + shopLocation.remove(l.getLocation()); + } else { + shopLocation.remove(shop.getLocation()); + } - shop.removeItem(); - shop.removeHologram(); + shop.removeItem(); + shop.removeHologram(); - if (removeFromDatabase) - plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts); + if (removeFromDatabase) + plugin.getShopDatabase().removeShop(shop, plugin.getShopChestConfig().database_reconnect_attempts); } /** @@ -186,7 +183,9 @@ public class ShopUtils { if (shop.getShopType() != Shop.ShopType.ADMIN || !plugin.getShopChestConfig().exclude_admin_shops) { shopCount++; - if (shop.getChest().getInventory().getHolder() instanceof DoubleChest) + InventoryHolder ih = shop.getInventoryHolder(); + + if (ih instanceof DoubleChest) shopCount -= 0.5; } } @@ -224,7 +223,7 @@ public class ShopUtils { try { Shop shop = (Shop) plugin.getShopDatabase().get(id, Database.ShopInfo.SHOP, plugin.getShopChestConfig().database_reconnect_attempts); addShop(shop, false); - } catch (NullPointerException e) { + } catch (Exception e) { continue; }