Double Chests shouldn't be counted twice

This commit is contained in:
Eric Biedert 2016-06-24 18:39:31 +02:00
parent 2b9a2c957f
commit 67016787df
2 changed files with 49 additions and 55 deletions

View File

@ -37,6 +37,7 @@ public class Shop {
private double buyPrice; private double buyPrice;
private double sellPrice; private double sellPrice;
private ShopType shopType; private ShopType shopType;
private Chest chest;
public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) { public Shop(int id, ShopChest plugin, OfflinePlayer vendor, ItemStack product, Location location, double buyPrice, double sellPrice, ShopType shopType) {
this.id = id; this.id = id;
@ -48,6 +49,18 @@ public class Shop {
this.sellPrice = sellPrice; this.sellPrice = sellPrice;
this.shopType = shopType; 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 (hologram == null || !hologram.exists()) createHologram();
if (item == null || item.isDead()) createItem(); if (item == null || item.isDead()) createItem();
} }
@ -92,41 +105,26 @@ public class Shop {
} }
private void createHologram() { private void createHologram() {
boolean doubleChest; boolean doubleChest;
Chest[] chests = new Chest[2]; Chest[] chests = new Chest[2];
Block b = location.getBlock(); 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(); Chest r = (Chest) dc.getRightSide();
InventoryHolder ih = c.getInventory().getHolder(); Chest l = (Chest) dc.getLeftSide();
if (ih instanceof DoubleChest) { chests[0] = r;
DoubleChest dc = (DoubleChest) ih; chests[1] = l;
Chest r = (Chest) dc.getRightSide(); doubleChest = true;
Chest l = (Chest) dc.getLeftSide();
chests[0] = r;
chests[1] = l;
doubleChest = true;
} else {
doubleChest = false;
chests[0] = c;
}
} else { } else {
try { doubleChest = false;
throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ()); chests[0] = chest;
} catch (Exception e) {
e.printStackTrace();
}
return;
} }
Location holoLocation; Location holoLocation;
@ -167,8 +165,7 @@ public class Shop {
else if ((buyPrice > 0) && (sellPrice > 0)) else if ((buyPrice > 0) && (sellPrice > 0))
holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)), 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))); new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice)));
else else holoText[1] = LanguageUtils.getMessage(LocalizedMessage.Message.HOLOGRAM_BUY_SELL, new LocalizedMessage.ReplacedRegex(Regex.BUY_PRICE, String.valueOf(buyPrice)),
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))); new LocalizedMessage.ReplacedRegex(Regex.SELL_PRICE, String.valueOf(sellPrice)));
switch (Utils.getServerVersion()) { switch (Utils.getServerVersion()) {
@ -226,6 +223,10 @@ public class Shop {
return hologram; return hologram;
} }
public Chest getChest() {
return chest;
}
public enum ShopType { public enum ShopType {
NORMAL, NORMAL,
ADMIN ADMIN

View File

@ -45,36 +45,26 @@ public class ShopUtils {
} }
public static void addShop(Shop shop, boolean addToDatabase) { public static void addShop(Shop shop, boolean addToDatabase) {
Location loc = shop.getLocation(); InventoryHolder ih = shop.getChest().getInventory().getHolder();
Block b = loc.getBlock();
if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { if (ih instanceof DoubleChest) {
Chest c = (Chest) b.getState(); DoubleChest dc = (DoubleChest) ih;
InventoryHolder ih = c.getInventory().getHolder(); Chest r = (Chest) dc.getRightSide();
Chest l = (Chest) dc.getLeftSide();
if (ih instanceof DoubleChest) { shopLocation.put(r.getLocation(), shop);
DoubleChest dc = (DoubleChest) ih; shopLocation.put(l.getLocation(), shop);
Chest r = (Chest) dc.getRightSide(); } else {
Chest l = (Chest) dc.getLeftSide(); shopLocation.put(shop.getLocation(), shop);
shopLocation.put(r.getLocation(), shop);
shopLocation.put(l.getLocation(), shop);
} else {
shopLocation.put(shop.getLocation(), shop);
}
if (addToDatabase)
plugin.getShopDatabase().addShop(shop);
} }
if (addToDatabase)
plugin.getShopDatabase().addShop(shop);
} }
public static void removeShop(Shop shop, boolean removeFromDatabase) { public static void removeShop(Shop shop, boolean removeFromDatabase) {
Location loc = shop.getLocation(); InventoryHolder ih = shop.getChest().getInventory().getHolder();
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();
if (ih instanceof DoubleChest) { if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih; DoubleChest dc = (DoubleChest) ih;
@ -92,7 +82,6 @@ public class ShopUtils {
if (removeFromDatabase) if (removeFromDatabase)
plugin.getShopDatabase().removeShop(shop); plugin.getShopDatabase().removeShop(shop);
}
} }
public static int getShopLimit(Player p) { public static int getShopLimit(Player p) {
@ -149,16 +138,20 @@ public class ShopUtils {
} }
public static int getShopAmount(OfflinePlayer p) { public static int getShopAmount(OfflinePlayer p) {
int shopCount = 0; float shopCount = 0;
for (Shop shop : ShopUtils.getShops()) { for (Shop shop : ShopUtils.getShops()) {
if (shop.getVendor().equals(p)) { 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++; shopCount++;
if (shop.getChest().getInventory().getHolder() instanceof DoubleChest)
shopCount -= 0.5;
}
} }
} }
return shopCount; return Math.round(shopCount);
} }
public static int reloadShops() { public static int reloadShops() {