From 67016787df5a24bd910860ac64fcbdf6188b641a Mon Sep 17 00:00:00 2001 From: Eric Biedert Date: Fri, 24 Jun 2016 18:39:31 +0200 Subject: [PATCH] Double Chests shouldn't be counted twice --- src/de/epiceric/shopchest/shop/Shop.java | 55 ++++++++++--------- .../epiceric/shopchest/utils/ShopUtils.java | 49 +++++++---------- 2 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/de/epiceric/shopchest/shop/Shop.java b/src/de/epiceric/shopchest/shop/Shop.java index 109963c..507f750 100644 --- a/src/de/epiceric/shopchest/shop/Shop.java +++ b/src/de/epiceric/shopchest/shop/Shop.java @@ -37,6 +37,7 @@ 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; @@ -48,6 +49,18 @@ public class Shop { this.sellPrice = sellPrice; this.shopType = shopType; + Block b = location.getBlock(); + if (b.getType() == Material.CHEST || b.getType() == Material.TRAPPED_CHEST) { + this.chest = (Chest) b.getState(); + } else { + try { + throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); + } catch (Exception ex) { + ex.printStackTrace(); + return; + } + } + if (hologram == null || !hologram.exists()) createHologram(); if (item == null || item.isDead()) createItem(); } @@ -92,41 +105,26 @@ public class Shop { } private void createHologram() { - boolean doubleChest; Chest[] chests = new Chest[2]; - Block b = location.getBlock(); + InventoryHolder ih = chest.getInventory().getHolder(); - if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { + if (ih instanceof DoubleChest) { + DoubleChest dc = (DoubleChest) ih; - Chest c = (Chest) b.getState(); - InventoryHolder ih = c.getInventory().getHolder(); + Chest r = (Chest) dc.getRightSide(); + Chest l = (Chest) dc.getLeftSide(); - if (ih instanceof DoubleChest) { - DoubleChest dc = (DoubleChest) ih; + chests[0] = r; + chests[1] = l; - Chest r = (Chest) dc.getRightSide(); - Chest l = (Chest) dc.getLeftSide(); - - chests[0] = r; - chests[1] = l; - - doubleChest = true; - - } else { - doubleChest = false; - chests[0] = c; - } + doubleChest = true; } else { - try { - throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); - } catch (Exception e) { - e.printStackTrace(); - } - return; + doubleChest = false; + chests[0] = chest; } Location holoLocation; @@ -167,8 +165,7 @@ public class Shop { else if ((buyPrice > 0) && (sellPrice > 0)) holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); - else - holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), + else holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice))); switch (Utils.getServerVersion()) { @@ -226,6 +223,10 @@ public class Shop { return hologram; } + public Chest getChest() { + return chest; + } + public enum ShopType { NORMAL, ADMIN diff --git a/src/de/epiceric/shopchest/utils/ShopUtils.java b/src/de/epiceric/shopchest/utils/ShopUtils.java index 4d28615..8ce3a25 100644 --- a/src/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/de/epiceric/shopchest/utils/ShopUtils.java @@ -45,36 +45,26 @@ public class ShopUtils { } public static void addShop(Shop shop, boolean addToDatabase) { - Location loc = shop.getLocation(); - Block b = loc.getBlock(); + InventoryHolder ih = shop.getChest().getInventory().getHolder(); - if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { - Chest c = (Chest) b.getState(); - InventoryHolder ih = c.getInventory().getHolder(); + 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.put(r.getLocation(), shop); - shopLocation.put(l.getLocation(), shop); - } else { - shopLocation.put(shop.getLocation(), shop); - } - - if (addToDatabase) - plugin.getShopDatabase().addShop(shop); + shopLocation.put(r.getLocation(), shop); + shopLocation.put(l.getLocation(), shop); + } else { + shopLocation.put(shop.getLocation(), shop); } + + if (addToDatabase) + plugin.getShopDatabase().addShop(shop); + } public static void removeShop(Shop shop, boolean removeFromDatabase) { - Location loc = shop.getLocation(); - Block b = loc.getBlock(); - - if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { - Chest c = (Chest) b.getState(); - InventoryHolder ih = c.getInventory().getHolder(); + InventoryHolder ih = shop.getChest().getInventory().getHolder(); if (ih instanceof DoubleChest) { DoubleChest dc = (DoubleChest) ih; @@ -92,7 +82,6 @@ public class ShopUtils { if (removeFromDatabase) plugin.getShopDatabase().removeShop(shop); - } } public static int getShopLimit(Player p) { @@ -149,16 +138,20 @@ public class ShopUtils { } public static int getShopAmount(OfflinePlayer p) { - int shopCount = 0; + float shopCount = 0; for (Shop shop : ShopUtils.getShops()) { if (shop.getVendor().equals(p)) { - if (shop.getShopType() != Shop.ShopType.ADMIN || !Config.exclude_admin_shops) + if (shop.getShopType() != Shop.ShopType.ADMIN || !Config.exclude_admin_shops) { shopCount++; + + if (shop.getChest().getInventory().getHolder() instanceof DoubleChest) + shopCount -= 0.5; + } } } - return shopCount; + return Math.round(shopCount); } public static int reloadShops() {