From 6916b9f79ff6a81b8d29ec0349c33caae6d749b7 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Fri, 19 Oct 2018 20:38:02 +0800 Subject: [PATCH] NMS Advanced item preview and new more event. Former-commit-id: 7c47ee6f1ba3972b65eca2b88b1e3a48ddd39e94 --- lib/spigot-1.13.1-SNAPSHOT.jar.REMOVED.git-id | 1 + pom.xml | 9 ++- .../org/maxgamer/quickshop/Command/QS.java | 4 -- .../quickshop/Economy/Economy_Vault.java | 12 ---- .../org/maxgamer/quickshop/Nms/v1_13_R1.java | 18 ++++++ .../org/maxgamer/quickshop/Nms/v1_13_R2.java | 19 +++++++ .../org/maxgamer/quickshop/QuickShop.java | 25 ++------- .../maxgamer/quickshop/Shop/DisplayItem.java | 13 +++-- .../Shop/ShopDisplayItemSpawnEvent.java | 48 ++++++++++++++++ .../quickshop/Shop/ShopLoadEvent.java | 37 +++++++++++++ .../maxgamer/quickshop/Shop/ShopManager.java | 45 +++++++++------ .../quickshop/Shop/ShopUnloadEvent.java | 23 ++++++++ .../org/maxgamer/quickshop/Util/ItemNMS.java | 45 +++++++++++++++ .../org/maxgamer/quickshop/Util/MsgUtil.java | 10 ++-- .../org/maxgamer/quickshop/Util/Util.java | 55 ++++++++----------- src/main/resources/messages.yml | 3 +- 16 files changed, 268 insertions(+), 99 deletions(-) create mode 100644 lib/spigot-1.13.1-SNAPSHOT.jar.REMOVED.git-id create mode 100644 src/main/java/org/maxgamer/quickshop/Nms/v1_13_R1.java create mode 100644 src/main/java/org/maxgamer/quickshop/Nms/v1_13_R2.java create mode 100644 src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java create mode 100644 src/main/java/org/maxgamer/quickshop/Shop/ShopLoadEvent.java create mode 100644 src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java create mode 100644 src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java diff --git a/lib/spigot-1.13.1-SNAPSHOT.jar.REMOVED.git-id b/lib/spigot-1.13.1-SNAPSHOT.jar.REMOVED.git-id new file mode 100644 index 000000000..8552e32c6 --- /dev/null +++ b/lib/spigot-1.13.1-SNAPSHOT.jar.REMOVED.git-id @@ -0,0 +1 @@ +378e18de67ae3cbcc3c1c249c56e9db636f4bebe \ No newline at end of file diff --git a/pom.xml b/pom.xml index 72cf7018f..e47d75f2c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.maxgamer QuickShop - Reremake 1.2.5.2 + Reremake 1.2.6 @@ -94,6 +94,13 @@ system ${basedir}/lib/spigot-1.13-SNAPSHOT.jar + + org.bukkit.craftbukkit.v1_13_R2 + spigot + v1_13_R2 + system + ${basedir}/lib/spigot-1.13.1-SNAPSHOT.jar + com.onarandombox.MultiverseCore Multiverse-Core diff --git a/src/main/java/org/maxgamer/quickshop/Command/QS.java b/src/main/java/org/maxgamer/quickshop/Command/QS.java index 8c7848b98..2cfd60f54 100644 --- a/src/main/java/org/maxgamer/quickshop/Command/QS.java +++ b/src/main/java/org/maxgamer/quickshop/Command/QS.java @@ -12,16 +12,13 @@ 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.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.event.block.Action; import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.inventory.ItemStack; import org.bukkit.util.BlockIterator; @@ -280,7 +277,6 @@ public class QS implements CommandExecutor { } } - @SuppressWarnings("deprecation") private void create(CommandSender sender, String[] args) { if (sender instanceof Player) { Player p = (Player) sender; diff --git a/src/main/java/org/maxgamer/quickshop/Economy/Economy_Vault.java b/src/main/java/org/maxgamer/quickshop/Economy/Economy_Vault.java index 3a312e447..e4ca2f646 100644 --- a/src/main/java/org/maxgamer/quickshop/Economy/Economy_Vault.java +++ b/src/main/java/org/maxgamer/quickshop/Economy/Economy_Vault.java @@ -33,14 +33,12 @@ public class Economy_Vault implements EconomyCore { @Deprecated public boolean deposit(String name, double amount) { boolean result = this.vault.depositPlayer(name, amount).transactionSuccess(); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug: Vault return Deprecated deposit result:"+result);} return result; } @Deprecated public boolean withdraw(String name, double amount) { boolean result = this.vault.withdrawPlayer(name, amount).transactionSuccess(); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug: Vault return Deprecated withdraw result:"+result);} return result; } @@ -50,16 +48,12 @@ public class Economy_Vault implements EconomyCore { if (this.vault.withdrawPlayer(from, amount).transactionSuccess()) { if (!this.vault.depositPlayer(to, amount).transactionSuccess()) { this.vault.depositPlayer(from, amount); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer success");} return true; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } @@ -85,7 +79,6 @@ public class Economy_Vault implements EconomyCore { public boolean deposit(UUID name, double amount) { OfflinePlayer p = Bukkit.getOfflinePlayer(name); boolean result = this.vault.depositPlayer(p, amount).transactionSuccess(); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Vault report deposit "+result);} return result; } @@ -93,7 +86,6 @@ public class Economy_Vault implements EconomyCore { public boolean withdraw(UUID name, double amount) { OfflinePlayer p = Bukkit.getOfflinePlayer(name); boolean result = this.vault.withdrawPlayer(p, amount).transactionSuccess(); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Vault report withdraw "+result);} return result; } @@ -105,16 +97,12 @@ public class Economy_Vault implements EconomyCore { if (this.vault.withdrawPlayer(pFrom, amount).transactionSuccess()) { if (!this.vault.depositPlayer(pTo, amount).transactionSuccess()) { this.vault.depositPlayer(pFrom, amount); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer success");} return true; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Deprecated transfer failed");} return false; } diff --git a/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R1.java b/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R1.java new file mode 100644 index 000000000..a11323a87 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R1.java @@ -0,0 +1,18 @@ +package org.maxgamer.quickshop.Nms; + +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_13_R1.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; +import net.minecraft.server.v1_13_R1.NBTTagCompound; + +public class v1_13_R1 { + public String getNBTJson(ItemStack iStack) { + if(iStack.getType().equals(Material.AIR)) { + return null; + } + net.minecraft.server.v1_13_R1.ItemStack mcitem = CraftItemStack.asNMSCopy(iStack); + NBTTagCompound nbt = new NBTTagCompound(); + mcitem.save(nbt); + return nbt.toString(); + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R2.java b/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R2.java new file mode 100644 index 000000000..fda2095e6 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Nms/v1_13_R2.java @@ -0,0 +1,19 @@ +package org.maxgamer.quickshop.Nms; + +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_13_R2.inventory.CraftItemStack; +import org.bukkit.inventory.ItemStack; + +import net.minecraft.server.v1_13_R2.NBTTagCompound; + +public class v1_13_R2 { + public String getNBTJson(ItemStack iStack) { + if(iStack.getType().equals(Material.AIR)) { + return null; + } + net.minecraft.server.v1_13_R2.ItemStack mcitem = CraftItemStack.asNMSCopy(iStack); + NBTTagCompound nbt = new NBTTagCompound(); + mcitem.save(nbt); + return nbt.toString(); + } +} diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 8138591f4..aaf72e19c 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -37,7 +37,6 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; -import org.json.simple.JSONObject; import org.maxgamer.quickshop.Command.QS; import org.maxgamer.quickshop.Database.Database; import org.maxgamer.quickshop.Database.Database.ConnectionException; @@ -71,6 +70,7 @@ import com.onarandombox.MultiverseCore.MultiverseCore; + @SuppressWarnings("deprecation") public class QuickShop extends JavaPlugin { /** The active instance of QuickShop */ @@ -119,8 +119,6 @@ public class QuickShop extends JavaPlugin { /** Use SpoutPlugin to get item / block names */ public boolean useSpout = false; // private Metrics metrics; - /** Whether debug info should be shown in the console */ - public static boolean debug = false; MultiverseCore mPlugin = null; @@ -151,8 +149,6 @@ public class QuickShop extends JavaPlugin { reloadConfig(); // Reloads messages.yml too, aswell as config.yml and // others. getConfig().options().copyDefaults(true); // Load defaults. - if (getConfig().contains("debug")) - debug = true; if (loadEcon() == false) return; @@ -526,9 +522,9 @@ public class QuickShop extends JavaPlugin { // Command handlers QS commandExecutor = new QS(this); getCommand("qs").setExecutor(commandExecutor); -// if (getConfig().getInt("shop.find-distance") > 100) { -// getLogger().severe("Shop.find-distance is too high! Pick a number under 100!"); -// } + if (getConfig().getInt("shop.find-distance") > 100) { + getLogger().severe("Shop.find-distance is too high! It may cause lag! Pick a number under 100!"); + } if (display && displayItemCheckTicks > 0) { new BukkitRunnable() { @@ -820,19 +816,6 @@ public class QuickShop extends JavaPlugin { public Database getDB() { return this.database; } - - /** - * Prints debug information if QuickShop is configured to do so. - * - * @param s - * The string to print. - */ - public void debug(String s) { - if (!debug) - return; - this.getLogger().info(ChatColor.YELLOW + "[Debug] " + s); - } - /** * Returns the ShopManager. This is used for fetching, adding and removing * shops. diff --git a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java index 3f38d1336..729885e74 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/DisplayItem.java @@ -3,6 +3,7 @@ package org.maxgamer.quickshop.Shop; import java.util.ArrayList; import java.util.logging.Level; +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; @@ -160,11 +161,14 @@ public class DisplayItem { if (!showFloatItem) { return; } + //Call Event for QSAPI + ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, iStack); + Bukkit.getPluginManager().callEvent(shopDisplayItemSpawnEvent); + if(shopDisplayItemSpawnEvent.isCancelled()) { + return; + } this.item = shop.getLocation().getWorld().dropItem(dispLoc, this.iStack); this.item.setVelocity(new Vector(0, 0.1, 0)); - if (QuickShop.debug) { - plugin.getLogger().log(Level.WARNING, "Spawned item. Safeguarding."); - } try { this.safeGuard(this.item); // NMS.safeGuard @@ -216,9 +220,6 @@ public class DisplayItem { if (this.shop.matches(near)) { e.remove(); removed = true; - if (QuickShop.debug) { - plugin.getLogger().log(Level.WARNING, "Removed rogue item: " + near.getType()); - } } } } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java new file mode 100644 index 000000000..beb5ee96a --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java @@ -0,0 +1,48 @@ +package org.maxgamer.quickshop.Shop; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +public class ShopDisplayItemSpawnEvent extends Event implements Cancellable{ + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private Shop shop; + private ItemStack iStack; + /** + * This event is called before the shop display item created + */ + + public ShopDisplayItemSpawnEvent(Shop shop, ItemStack iStack) { + this.shop = shop; + this.iStack = iStack; + } + + public Shop getShop() { + return shop; + } + public ItemStack getItemStack() { + return iStack; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopLoadEvent.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoadEvent.java new file mode 100644 index 000000000..0177c6c49 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopLoadEvent.java @@ -0,0 +1,37 @@ +package org.maxgamer.quickshop.Shop; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ShopLoadEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private Shop shop; + /**Getting loading shops**/ + + public ShopLoadEvent(Shop shop) { + this.shop = shop; + } + public Shop getShop() { + return shop; + } + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index 27a568e9c..0f920e79e 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -29,7 +29,6 @@ import org.maxgamer.quickshop.Database.Database; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Permissions; import org.maxgamer.quickshop.Util.Util; - public class ShopManager { QuickShop plugin = QuickShop.instance; private HashMap actions = new HashMap(); @@ -53,6 +52,8 @@ public class ShopManager { } public void createShop(Shop shop) { + ShopCreateEvent ssShopCreateEvent = new ShopCreateEvent(shop, Bukkit.getPlayer(shop.getOwner())); + Bukkit.getPluginManager().callEvent(ssShopCreateEvent); Location loc = shop.getLocation(); ItemStack item = shop.getItem(); try { @@ -154,6 +155,12 @@ public class ShopManager { * The shop to add */ private void addShop(String world, Shop shop) { + ShopLoadEvent shopLoadEvent = new ShopLoadEvent(shop); + Bukkit.getPluginManager().callEvent(shopLoadEvent); + if(shopLoadEvent.isCancelled()) { + return; + } + HashMap> inWorld = this.getShops().get(world); // There's no world storage yet. We need to create that hashmap. if (inWorld == null) { @@ -186,6 +193,8 @@ public class ShopManager { * The shop to remove */ public void removeShop(Shop shop) { + ShopUnloadEvent shopUnloadEvent = new ShopUnloadEvent(shop); + Bukkit.getPluginManager().callEvent(shopUnloadEvent); Location loc = shop.getLocation(); String world = loc.getWorld().getName(); HashMap> inWorld = this.getShops().get(world); @@ -250,7 +259,8 @@ public class ShopManager { } int max = plugin.getShopLimit(p); if (owned + 1 > max) { - p.sendMessage(ChatColor.RED + "You have already created a maximum of " + owned + "/" + max + " shops!"); + //p.sendMessage(ChatColor.RED + "You have already created a maximum of " + owned + "/" + max + " shops!"); + MsgUtil.getMessage("reached-maximum-can-create", String.valueOf(owned),String.valueOf(max)); return false; } } @@ -335,7 +345,8 @@ public class ShopManager { Entry priceRestriction = Util.getPriceRestriction(info.getItem().getType()); if (priceRestriction!=null) { if (pricepriceRestriction.getValue()) { - p.sendMessage(ChatColor.RED+"Restricted prices for "+info.getItem().getType()+": min "+priceRestriction.getKey()+", max "+priceRestriction.getValue()); + //p.sendMessage(ChatColor.RED+"Restricted prices for "+info.getItem().getType()+": min "+priceRestriction.getKey()+", max "+priceRestriction.getValue()); + p.sendMessage(MsgUtil.getMessage("restricted-prices",MsgUtil.getDisplayName(info.getItem()),String.valueOf(priceRestriction.getKey()),String.valueOf(priceRestriction.getValue()))); } } @@ -378,7 +389,7 @@ public class ShopManager { /* The shop has hereforth been successfully created */ createShop(shop); Location loc = shop.getLocation(); - plugin.log(p.getName() + " created a " + MsgUtil.getDisplayName(shop.getItem(), shop.getDataName()) + " shop at (" + loc.getWorld().getName() + " - " + loc.getX() + "," + loc.getY() + "," + loc.getZ() + ")"); + plugin.log(p.getName() + " created a " + MsgUtil.getDisplayName(shop.getItem()) + " shop at (" + loc.getWorld().getName() + " - " + loc.getX() + "," + loc.getY() + "," + loc.getZ() + ")"); if (!plugin.getConfig().getBoolean("shop.lock")) { // Warn them if they haven't been warned since // reboot @@ -448,7 +459,6 @@ public class ShopManager { /* Purchase Handling */ else if (info.getAction() == ShopAction.BUY) { - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Starting buy");} int amount = 0; try { amount = Integer.parseInt(message); @@ -470,7 +480,7 @@ public class ShopManager { if (shop.isSelling()) { int stock = shop.getRemainingStock(); if (stock < amount) { - p.sendMessage(MsgUtil.getMessage("shop-stock-too-low", String.valueOf(shop.getRemainingStock()), MsgUtil.getDisplayName(shop.getItem(), shop.getDataName()))); + p.sendMessage(MsgUtil.getMessage("shop-stock-too-low", String.valueOf(shop.getRemainingStock()), MsgUtil.getDisplayName(shop.getItem()))); return; } if (amount == 0) { @@ -509,14 +519,15 @@ public class ShopManager { } double total = amount * shop.getPrice(); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug: trade total: "+total);} if (!plugin.getEcon().withdraw(p.getUniqueId(), total)) { p.sendMessage(MsgUtil.getMessage("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getUniqueId())))); return; } if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) { boolean depositresult = plugin.getEcon().deposit(shop.getOwner(), total * (1 - tax)); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:deposit in shop manager result:"+depositresult);} + if(!depositresult) { + QuickShop.instance.getLogger().info("Can't deposit the money! Economy plugin issues?"); + } if (tax != 0) { try { for(OfflinePlayer player : Bukkit.getOfflinePlayers()) { @@ -532,12 +543,12 @@ public class ShopManager { } // Notify the shop owner if (plugin.getConfig().getBoolean("show-tax")) { - String msg = MsgUtil.getMessage("player-bought-from-your-store-tax", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem(), shop.getDataName()), Util.format((tax * total))); + String msg = MsgUtil.getMessage("player-bought-from-your-store-tax", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem()), Util.format((tax * total))); if (stock == amount) - msg += "\n" + MsgUtil.getMessage("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), MsgUtil.getDisplayName(shop.getItem(), shop.getDataName())); + msg += "\n" + MsgUtil.getMessage("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), MsgUtil.getDisplayName(shop.getItem())); MsgUtil.send(shop.getOwner(), msg); } else { - String msg = MsgUtil.getMessage("player-bought-from-your-store", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem(), shop.getDataName())); + String msg = MsgUtil.getMessage("player-bought-from-your-store", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem())); if (stock == amount) msg += "\n" + MsgUtil.getMessage("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), shop.getDataName()); MsgUtil.send(shop.getOwner(), msg); @@ -546,18 +557,17 @@ public class ShopManager { // Transfers the item from A to B shop.sell(p, amount); MsgUtil.sendPurchaseSuccess(p, shop, amount); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Trade purchase success completed.");} plugin.log(p.getName() + " bought " + amount + " for " + (shop.getPrice() * amount) + " from " + shop.toString()); } else if (shop.isBuying()) { int space = shop.getRemainingSpace(); if (space < amount) { - p.sendMessage(MsgUtil.getMessage("shop-has-no-space", "" + space, MsgUtil.getDisplayName(shop.getItem(), shop.getDataName()))); + p.sendMessage(MsgUtil.getMessage("shop-has-no-space", "" + space, MsgUtil.getDisplayName(shop.getItem()))); return; } int count = Util.countItems(p.getInventory(), shop.getItem()); // Not enough items if (amount > count) { - p.sendMessage(MsgUtil.getMessage("you-dont-have-that-many-items", "" + count, MsgUtil.getDisplayName(shop.getItem(), shop.getDataName()))); + p.sendMessage(MsgUtil.getMessage("you-dont-have-that-many-items", "" + count, MsgUtil.getDisplayName(shop.getItem()))); return; } if (amount == 0) { @@ -610,9 +620,11 @@ public class ShopManager { } // Give them the money after we know we succeeded boolean depositresult = plugin.getEcon().deposit(p.getUniqueId(), total * (1 - tax)); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:deposit2 in shopmanager result:"+depositresult);} + if(!depositresult) { + QuickShop.instance.getLogger().info("Can't deposit the money! Economy plugin issues?"); + } // Notify the owner of the purchase. - String msg = MsgUtil.getMessage("player-sold-to-your-store", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem(), shop.getDataName())); + String msg = MsgUtil.getMessage("player-sold-to-your-store", p.getName(), "" + amount, MsgUtil.getDisplayName(shop.getItem())); if (space == amount) msg += "\n" + MsgUtil.getMessage("shop-out-of-space", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ()); MsgUtil.send(shop.getOwner(), msg); @@ -624,7 +636,6 @@ public class ShopManager { } shop.buy(p, amount); MsgUtil.sendSellSuccess(p, shop, amount); - if(QuickShop.debug) {plugin.getLogger().info("Economy debug:Trade sell success completed");} plugin.log(p.getName() + " sold " + amount + " for " + (shop.getPrice() * amount) + " to " + shop.toString()); } shop.setSignText(); // Update the signs count diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java new file mode 100644 index 000000000..c612ead46 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java @@ -0,0 +1,23 @@ +package org.maxgamer.quickshop.Shop; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ShopUnloadEvent extends Event{ + /**Getting the unloading shop, Can't cancel.**/ + public ShopUnloadEvent(Shop shop) { + this.shop = shop; + } + private static final HandlerList handlers = new HandlerList(); + private Shop shop; + public Shop getShop() { + return shop; + } + @Override + public HandlerList getHandlers() { + return handlers; + } + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java b/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java new file mode 100644 index 000000000..9cee5ca96 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Util/ItemNMS.java @@ -0,0 +1,45 @@ +package org.maxgamer.quickshop.Util; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.bukkit.Bukkit; +import org.bukkit.inventory.ItemStack; +import org.maxgamer.quickshop.QuickShop; + +public class ItemNMS { + public String getItemJSON(ItemStack iStack) { + ItemStack itemStack = iStack.clone(); + String name = Bukkit.getServer().getClass().getPackage().getName(); + String version = name.substring(name.lastIndexOf('.') + 1); + Class nmsClass; + try { + nmsClass = Class.forName("org.maxgamer.quickshop.Nms." + version); + } catch (ClassNotFoundException e2) { + return null; + } + Object obj; + try { + obj = nmsClass.newInstance(); + } catch (InstantiationException e1) { + // TODO Auto-generated catch block + return null; + } catch (IllegalAccessException e1) { + // TODO Auto-generated catch block + return null; + } + try { + Method method = nmsClass.getMethod("getNBTJson", ItemStack.class); + String itemJSON = (String) method.invoke(obj, new Object[] { itemStack }); + return itemJSON; + } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException + | IllegalAccessException e) { + // do somethings + QuickShop.instance.getLogger().info("A error happend:"); + e.printStackTrace(); + return null; + } + } + + +} diff --git a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java index cdf93ee66..3e64ee2d1 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java +++ b/src/main/java/org/maxgamer/quickshop/Util/MsgUtil.java @@ -300,7 +300,7 @@ public class MsgUtil { p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.owner", shop.ownerName())); //p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item", MsgUtil.getItemi18n(shop.getDataName()))); //Enabled - Util.sendItemholochat(shop.getItem(),p,ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item", MsgUtil.getItemi18n(shop.getDataName()))); + Util.sendItemholochat(shop.getItem(),p,ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item", MsgUtil.getDisplayName(shop.getItem()))); // if (NMS.isPotion(items.getType())) { // String effects = CustomPotionsName.getEffects(items); @@ -352,7 +352,7 @@ public class MsgUtil { public static void sendPurchaseSuccess(Player p, Shop shop, int amount) { p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successful-purchase")); - p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, MsgUtil.getItemi18n(shop.getDataName()), Util.format((amount * shop.getPrice())))); + p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, MsgUtil.getDisplayName(shop.getItem()), Util.format((amount * shop.getPrice())))); Map enchs = shop.getItem().getItemMeta().getEnchants(); if (enchs != null && !enchs.isEmpty()) { p.sendMessage(ChatColor.DARK_PURPLE + "+--------------------" + MsgUtil.getMessage("menu.enchants") + "-----------------------+"); @@ -390,7 +390,7 @@ public class MsgUtil { public static void sendSellSuccess(Player p, Shop shop, int amount) { p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.successfully-sold")); - p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, MsgUtil.getItemi18n(shop.getDataName()), Util.format((amount * shop.getPrice())))); + p.sendMessage(ChatColor.DARK_PURPLE + "| " + MsgUtil.getMessage("menu.item-name-and-price", "" + amount, MsgUtil.getDisplayName(shop.getItem()), Util.format((amount * shop.getPrice())))); if (plugin.getConfig().getBoolean("show-tax")) { double tax = plugin.getConfig().getDouble("tax"); double total = amount * shop.getPrice(); @@ -428,13 +428,13 @@ public class MsgUtil { } p.sendMessage(ChatColor.DARK_PURPLE + "+---------------------------------------------------+"); } - public static String getDisplayName(ItemStack iStack, String DataName){ + public static String getDisplayName(ItemStack iStack){ ItemStack is = iStack.clone(); if(is.hasItemMeta()&&is.getItemMeta().hasDisplayName()) { return is.getItemMeta().getDisplayName(); }else { - return MsgUtil.getItemi18n(DataName); + return MsgUtil.getItemi18n(is.getType().name()); } } diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index dfc63131a..a5a70d632 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -30,12 +30,13 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.PotionMeta; -import org.bukkit.material.MaterialData; import org.bukkit.material.Sign; import org.bukkit.potion.PotionEffect; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Shop.Shop; + + import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; @@ -265,35 +266,6 @@ public class Util { return false; } - /** - * Converts a string into an item from the database. - * - * @param itemString - * The database string. Is the result of makeString(ItemStack - * item). - * @return A new itemstack, with the properties given in the string - */ - public static ItemStack makeItem(String itemString) { - String[] itemInfo = itemString.split(":"); - ItemStack item = new ItemStack(Material.getMaterial(itemInfo[0])); - MaterialData data = new MaterialData(Material.matchMaterial(itemInfo[1])); - item.setData(data); - item.setDurability(Short.parseShort(itemInfo[2])); - item.setAmount(Integer.parseInt(itemInfo[3])); - for (int i = 4; i < itemInfo.length; i = i + 2) { - int level = Integer.parseInt(itemInfo[i + 1]); - Enchantment ench = Enchantment.getByName(itemInfo[i]); - if (ench == null) - continue; // Invalid - if (ench.canEnchantItem(item)) { - if (level <= 0) - continue; - level = Math.min(ench.getMaxLevel(), level); - item.addEnchantment(ench, level); - } - } - return item; - } public static String serialize(ItemStack iStack) { YamlConfiguration cfg = new YamlConfiguration(); @@ -627,8 +599,26 @@ public class Util { } } } - + //Use NMS public static void sendItemholochat(ItemStack itemStack, Player player, String normalText) { + ItemNMS nms = new ItemNMS(); + String json = nms.getItemJSON(itemStack); + if(json == null) { + sendItemholochatAsNormaly(itemStack, player, normalText); + return; + } + try { + TextComponent normalmessage = new TextComponent(normalText+" "+MsgUtil.getMessage("menu.preview")); + ComponentBuilder cBuilder = new ComponentBuilder(json); + HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_ITEM, cBuilder.create()); + normalmessage.setHoverEvent(he); + player.spigot().sendMessage(normalmessage); + }catch (Exception e) { + sendItemholochatAsNormaly(itemStack, player, normalText); + } + } + + public static void sendItemholochatAsNormaly(ItemStack itemStack, Player player, String normalText) { try { String Itemname = null; List Itemlore = new ArrayList<>(); @@ -659,7 +649,7 @@ public class Util { } } } else { - Itemname = MsgUtil.getDisplayName(itemStack,MsgUtil.getItemi18n(itemStack.getType().name())); + Itemname = MsgUtil.getDisplayName(itemStack); Itemlore = null; Itemenchs = null; } @@ -695,6 +685,7 @@ public class Util { player.spigot().sendMessage(normalmessage); }catch (Exception e) { player.sendMessage(normalText); + QuickShop.instance.getLogger().severe("QuickShop cannot send Advanced chat message, Are you using CraftBukkit? Please use Spigot or SpigotFork."); } } private static String formatEnchLevel(Integer level) { diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index e203999f7..28cc65b47 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -55,7 +55,8 @@ empty-success: "&aEmpty success" admin-shop: "AdminShop" unknown-owner: "Unknown" owner-bypass-check: "&eBypass all check,Trade successful! (You are shop owner)" - +reached-maximum-can-create: "&cYou have already created a maximum of {0}/{1} shops!" +restricted-prices: "&cRestricted prices for {0}: min {1} , max {2}" menu: successful-purchase: "&aSuccessfully Purchased:" successfully-sold: "&aSuccessfully Sold:"