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 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,17 +105,11 @@ public class Shop {
}
private void createHologram() {
boolean doubleChest;
Chest[] chests = new Chest[2];
Block b = location.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 = chest.getInventory().getHolder();
if (ih instanceof DoubleChest) {
DoubleChest dc = (DoubleChest) ih;
@ -117,16 +124,7 @@ public class Shop {
} else {
doubleChest = false;
chests[0] = c;
}
} else {
try {
throw new Exception("No Chest found at specified Location: " + b.getX() + "; " + b.getY() + "; " + b.getZ());
} catch (Exception e) {
e.printStackTrace();
}
return;
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

View File

@ -45,12 +45,7 @@ public class ShopUtils {
}
public static void addShop(Shop shop, boolean addToDatabase) {
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;
@ -65,16 +60,11 @@ public class ShopUtils {
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;
@ -93,7 +83,6 @@ public class ShopUtils {
if (removeFromDatabase)
plugin.getShopDatabase().removeShop(shop);
}
}
public static int getShopLimit(Player p) {
int limit = Config.default_limit;
@ -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() {