From 66750cd5701f1420b672e609a33bcc7a06f496ef Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 13 Sep 2015 13:13:04 +0200 Subject: [PATCH] Fixed Bugs & Added Hologram Format Configuration --- config.yml | 13 ++++++-- plugin.yml | 2 +- src/de/epiceric/shopchest/config/Config.java | 4 +++ .../shopchest/event/InteractShop.java | 16 ++++++--- .../event/ItemCustomNameListener.java | 2 +- .../shopchest/event/UpdateHolograms.java | 33 ++++++++++--------- src/de/epiceric/shopchest/shop/Shop.java | 7 ++-- src/de/epiceric/shopchest/sql/Database.java | 3 +- 8 files changed, 50 insertions(+), 30 deletions(-) diff --git a/config.yml b/config.yml index 2da06d9..35a2070 100644 --- a/config.yml +++ b/config.yml @@ -155,6 +155,9 @@ messages: # Set the message when the player doesn't have enough items to sell. not-enough-items: "&cNot enough items." + # Set the message when the vendor doesn't have enough money to buy something from the player + vendor-not-enough-money: "&cVendor has not enough money." + # Set the message when the shop is out of stock. out-of-stock: "&cShop out of stock." @@ -231,15 +234,19 @@ messages: hologram: - # Set the message in the second row of the shops' hologram when the player can buy and sell an item. + # Set the text in the first row of the shops' hologram + # Usable regex: %ITEMNAME%, %AMOUNT% + format: "%AMOUNT% * %ITEMNAME%" + + # Set the text in the second row of the shops' hologram when the player can buy and sell an item. # Usable regex: %BUY-PRICE%, %SELL-PRICE%, %CURRENCY-SYMBOL% buy-and-sell: "Buy %BUY-PRICE%%CURRENCY-SYMBOL% | %SELL-PRICE%%CURRENCY-SYMBOL% Sell" - # Set the message in the second row of the shops' hologram when the player can only buy an item. + # Set the text in the second row of the shops' hologram when the player can only buy an item. # Usable regex: %BUY-PRICE%, %CURRENCY-SYMBOL% only-buy: "Buy %BUY-PRICE%%CURRENCY-SYMBOL%" - # Set the message in the second row of the shops' hologram when the player can only sell an item. + # Set the text in the second row of the shops' hologram when the player can only sell an item. # Usable regex: %SELL-PRICE%, %CURRENCY-SYMBOL% only-sell: "Sell %SELL-PRICE%%CURRENCY-SYMBOL%" diff --git a/plugin.yml b/plugin.yml index e5d2945..fece8c4 100644 --- a/plugin.yml +++ b/plugin.yml @@ -2,7 +2,7 @@ name: ShopChest main: de.epiceric.shopchest.ShopChest -version: 1.5.6 +version: 1.5.7 author: EpicEric website: https://www.spigotmc.org/resources/shopchest.11431/ depend: [Vault] diff --git a/src/de/epiceric/shopchest/config/Config.java b/src/de/epiceric/shopchest/config/Config.java index 1c2f5b2..c30ba24 100644 --- a/src/de/epiceric/shopchest/config/Config.java +++ b/src/de/epiceric/shopchest/config/Config.java @@ -86,6 +86,10 @@ public class Config { return plugin.getConfig().getString("messages.update.update-available").replace(Regex.version, version); } + public static String hologram_format(int amount, String itemName) { + return plugin.getConfig().getString("messages.hologram.format").replace(Regex.amount, String.valueOf(amount)).replace(Regex.itemName, itemName).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); + } + public static String hologram_buy_sell(double buyPrice, double sellPrice) { return plugin.getConfig().getString("messages.hologram.buy-and-sell").replace(Regex.currencySymbol, currency_symbol()).replace(Regex.buyPrice, String.valueOf(buyPrice)).replace(Regex.sellPrice, String.valueOf(sellPrice)).replaceAll("(&([a-f0-9k-or]))", "\u00A7$2"); } diff --git a/src/de/epiceric/shopchest/event/InteractShop.java b/src/de/epiceric/shopchest/event/InteractShop.java index 47ed7da..a59e901 100644 --- a/src/de/epiceric/shopchest/event/InteractShop.java +++ b/src/de/epiceric/shopchest/event/InteractShop.java @@ -17,6 +17,7 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.yi.acru.bukkit.Lockette.Lockette; import com.griefcraft.model.Protection; @@ -123,7 +124,7 @@ public class InteractShop implements Listener{ Shop shop = ShopUtils.getShop(b.getLocation()); - if (shop.getVendor().equals(p) || perm.has(p, "shopchest.removeOther")) { + if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || perm.has(p, "shopchest.removeOther")) { remove(p, shop); } else { p.sendMessage(Config.noPermission_removeOthers()); @@ -144,7 +145,7 @@ public class InteractShop implements Listener{ e.setCancelled(true); Shop shop = ShopUtils.getShop(b.getLocation()); - if (p.equals(shop.getVendor())) { + if (p.getUniqueId().equals(shop.getVendor().getUniqueId())) { e.setCancelled(false); return; } else { @@ -195,7 +196,7 @@ public class InteractShop implements Listener{ if (ShopUtils.isShop(b.getLocation())) { Shop shop = ShopUtils.getShop(b.getLocation()); - if (!p.equals(shop.getVendor())) { + if (!p.getUniqueId().equals(shop.getVendor().getUniqueId())) { if (shop.getSellPrice() > 0) { if (perm.has(p, "shopchest.sell")) { if (Utils.getAmount(p.getInventory(), shop.getProduct().getType(), shop.getProduct().getDurability(), shop.getProduct().getItemMeta()) >= shop.getProduct().getAmount()) { @@ -262,8 +263,15 @@ public class InteractShop implements Listener{ String price = Config.shopInfo_price(shop.getBuyPrice(), shop.getSellPrice()); String infinite = (shop.isInfinite() ? Config.shopInfo_isInfinite() : Config.shopInfo_isNormal()); + Map enchantmentMap; + + if (shop.getProduct().getItemMeta() instanceof EnchantmentStorageMeta) { + EnchantmentStorageMeta esm = (EnchantmentStorageMeta) shop.getProduct().getItemMeta(); + enchantmentMap = esm.getStoredEnchants(); + } else { + enchantmentMap = shop.getProduct().getEnchantments(); + } - Map enchantmentMap = shop.getProduct().getItemMeta().getEnchants(); Enchantment[] enchantments = enchantmentMap.keySet().toArray(new Enchantment[enchantmentMap.size()]); for (int i = 0; i < enchantments.length; i++) { diff --git a/src/de/epiceric/shopchest/event/ItemCustomNameListener.java b/src/de/epiceric/shopchest/event/ItemCustomNameListener.java index 0a18432..31a5829 100644 --- a/src/de/epiceric/shopchest/event/ItemCustomNameListener.java +++ b/src/de/epiceric/shopchest/event/ItemCustomNameListener.java @@ -7,7 +7,7 @@ import org.bukkit.event.entity.ItemSpawnEvent; public class ItemCustomNameListener implements Listener { - @EventHandler(priority=EventPriority.HIGH) + @EventHandler(priority=EventPriority.MONITOR) public void onItemSpawn(ItemSpawnEvent e) { if (e.getEntity().hasMetadata("shopItem")) { e.getEntity().setCustomNameVisible(false); diff --git a/src/de/epiceric/shopchest/event/UpdateHolograms.java b/src/de/epiceric/shopchest/event/UpdateHolograms.java index 241c6ba..045639f 100644 --- a/src/de/epiceric/shopchest/event/UpdateHolograms.java +++ b/src/de/epiceric/shopchest/event/UpdateHolograms.java @@ -20,27 +20,30 @@ public class UpdateHolograms implements Listener{ Player p = e.getPlayer(); Location playerLocation = p.getLocation(); - for (Shop shop : ShopUtils.getShops()) { + for (Shop shop : ShopUtils.getShops()) { - Location shopLocation = shop.getLocation(); - - if (playerLocation.getWorld().equals(shopLocation.getWorld())) { + if (shop.getHologram() != null) { - if (playerLocation.distance(shop.getHologram().getLocation()) <= Config.maximal_distance()) { + Location shopLocation = shop.getLocation(); + + if (playerLocation.getWorld().equals(shopLocation.getWorld())) { - if (!shop.getHologram().isVisible(p)) { - shop.getHologram().showPlayer(p); - } - - } else { - - if (shop.getHologram().isVisible(p)) { - shop.getHologram().hidePlayer(p); + if (playerLocation.distance(shop.getHologram().getLocation()) <= Config.maximal_distance()) { + + if (!shop.getHologram().isVisible(p)) { + shop.getHologram().showPlayer(p); + } + + } else { + + if (shop.getHologram().isVisible(p)) { + shop.getHologram().hidePlayer(p); + } + } } - - } + } } diff --git a/src/de/epiceric/shopchest/shop/Shop.java b/src/de/epiceric/shopchest/shop/Shop.java index 93a8f04..276b5c5 100644 --- a/src/de/epiceric/shopchest/shop/Shop.java +++ b/src/de/epiceric/shopchest/shop/Shop.java @@ -64,9 +64,9 @@ public class Shop { itemStack.setItemMeta(itemMeta); item = location.getWorld().dropItem(itemLocation, itemStack); - item.getItemStack().getItemMeta().setDisplayName(UUID.randomUUID().toString()); item.setVelocity(new Vector(0, 0, 0)); item.setMetadata("shopItem", new FixedMetadataValue(plugin, true)); + item.setCustomNameVisible(false); this.item = item; } @@ -131,10 +131,9 @@ public class Shop { } - } else holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 0.5); + } else holoLocation = new Location(b.getWorld(), b.getX() + 0.5, b.getY() - 0.6, b.getZ() + 0.5); - - holoText[0] = String.valueOf(product.getAmount()) + " x " + ItemNames.lookup(product); + holoText[0] = Config.hologram_format(product.getAmount(), ItemNames.lookup(product)); if ((buyPrice <= 0) && (sellPrice > 0)) holoText[1] = Config.hologram_sell(sellPrice); else if ((buyPrice > 0) && (sellPrice <= 0)) holoText[1] = Config.hologram_buy(buyPrice); diff --git a/src/de/epiceric/shopchest/sql/Database.java b/src/de/epiceric/shopchest/sql/Database.java index 6cff066..8eb7e19 100644 --- a/src/de/epiceric/shopchest/sql/Database.java +++ b/src/de/epiceric/shopchest/sql/Database.java @@ -182,7 +182,6 @@ public abstract class Database { return null; } - @SuppressWarnings("deprecation") public OfflinePlayer getVendor(int id) { Connection conn = null; PreparedStatement ps = null; @@ -194,7 +193,7 @@ public abstract class Database { rs = ps.executeQuery(); while(rs.next()){ if(rs.getInt("id") == id){ - return (Utils.isUUID(rs.getString("vendor"))) ? Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor"))) : Bukkit.getOfflinePlayer(rs.getString("vendor")); + return Bukkit.getOfflinePlayer(UUID.fromString(rs.getString("vendor"))); } } } catch (SQLException ex) {