From 74eb1a3bf47a6fb840e45350ddcaa8806e247efa Mon Sep 17 00:00:00 2001 From: Reldeam Date: Wed, 2 Feb 2022 08:26:23 +1100 Subject: [PATCH] Added the option for physical currency --- .../CustomVillagerTrades.java | 7 + .../customvillagertrades/PluginConfig.java | 1 + .../listener/InventoryClickListener.java | 162 +++++++++++++----- .../listener/InventoryCloseListener.java | 8 +- .../listener/TradeSelectListener.java | 17 +- .../task/RemoveMoneyFromHandTask.java | 10 +- .../task/RemoveMoneyFromInventoryTask.java | 10 +- .../trade/CustomTradeLoader.java | 10 ++ src/main/resources/config.yml | 9 +- 9 files changed, 174 insertions(+), 60 deletions(-) diff --git a/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java b/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java index b4d2900..4c3cc3c 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java +++ b/src/main/java/online/meinkraft/customvillagertrades/CustomVillagerTrades.java @@ -51,6 +51,7 @@ public class CustomVillagerTrades extends JavaPlugin implements PluginConfig { private boolean isVanillaTradesAllowed = false; private boolean isDuplicateTradesAllowed = false; private boolean isEconomyEnabled = false; + private boolean isCurrencyPhysical = false; private Material toolMaterial; private Material currencyMaterial; private String currencyPrefix; @@ -104,6 +105,7 @@ public void onEnable() { isDuplicateTradesAllowed = getConfig().getBoolean("allowDuplicateTrades"); isVanillaTradesAllowed = !getConfig().getBoolean("disableVanillaTrades"); isEconomyEnabled = getConfig().getBoolean("enableEconomy"); + isCurrencyPhysical = getConfig().getBoolean("enablePhysicalCurrency"); toolMaterial = Material.getMaterial(getConfig().getString("tool")); currencyMaterial = Material.getMaterial(getConfig().getString("currencyItem")); currencyPrefix = getConfig().getString("currencyPrefix"); @@ -305,6 +307,11 @@ public String getCurrencySuffix() { return this.currencySuffix; } + @Override + public boolean isCurrencyPhysical() { + return isCurrencyPhysical; + } + diff --git a/src/main/java/online/meinkraft/customvillagertrades/PluginConfig.java b/src/main/java/online/meinkraft/customvillagertrades/PluginConfig.java index 1fc1130..8043de0 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/PluginConfig.java +++ b/src/main/java/online/meinkraft/customvillagertrades/PluginConfig.java @@ -11,6 +11,7 @@ public interface PluginConfig { public boolean isDuplicateTradesAllowed(); public boolean isVanillaTradesAllowed(); public boolean isEconomyEnabled(); + public boolean isCurrencyPhysical(); public Material getToolMaterial(); public Material getCurrencyMaterial(); diff --git a/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryClickListener.java b/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryClickListener.java index d1a928d..0f8e286 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryClickListener.java +++ b/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryClickListener.java @@ -1,5 +1,6 @@ package online.meinkraft.customvillagertrades.listener; +import org.bukkit.ChatColor; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -31,60 +32,127 @@ public void onInventoryClickEvent(InventoryClickEvent event) { // clicked outside inventory if(event.getClickedInventory() == null) return; - // economy (money) related logic - if(event.getClickedInventory().getType() == InventoryType.MERCHANT) { + // economy (money) related logic for handling money + if(plugin.isEconomyEnabled()) { - MerchantInventory inventory = (MerchantInventory) event.getClickedInventory(); - ItemStack item = inventory.getItem(event.getRawSlot()); + InventoryType inventoryType = event.getClickedInventory().getType(); - // stop player from clicking money in merchant inventories - if(item != null) { - ItemMeta itemMeta = item.getItemMeta(); - if(itemMeta != null) { - - PersistentDataContainer itemData = itemMeta.getPersistentDataContainer(); - Double money = itemData.get( - NamespacedKey.fromString("money", plugin), - PersistentDataType.DOUBLE + // check for clicking money ingredients in a merchant inventory + if(inventoryType == InventoryType.MERCHANT) { + + MerchantInventory inventory = (MerchantInventory) event.getClickedInventory(); + ItemStack item = inventory.getItem(event.getRawSlot()); + + // stop player from clicking money in merchant inventories + if(isItemMoney(item) && !plugin.isCurrencyPhysical()) { + + // add result amount to player balance + if(event.getSlotType() == InventoryType.SlotType.RESULT) { + + Player player = (Player) event.getWhoClicked(); + + plugin.getServer().getScheduler().runTask( + plugin, + new RemoveMoneyFromInventoryTask( + plugin, + player.getInventory(), + player + ) ); - - if(money != null) { - - // add result amount to player balance - if(event.getSlotType() == InventoryType.SlotType.RESULT) { - - Player player = (Player) event.getWhoClicked(); - - if(event.getClick() == ClickType.SHIFT_LEFT) { - plugin.getServer().getScheduler().runTask( - plugin, - new RemoveMoneyFromInventoryTask( - plugin, - player.getInventory(), - player - ) - ); - } - else { - plugin.getServer().getScheduler().runTask( - plugin, - new RemoveMoneyFromHandTask( - plugin, - player - ) - ); - } - } - else { - event.setCancelled(true); - } - } - } + plugin.getServer().getScheduler().runTask( + plugin, + new RemoveMoneyFromHandTask( + plugin, + player + ) + ); + + } + else { + event.setCancelled(true); + } + + } + } + + // if money somehow gets into a players inventory, then we need to + // remove it when they click on it if its not physical + else if(inventoryType == InventoryType.PLAYER) { + + Player player = (Player) event.getWhoClicked(); + ItemStack item = event.getCurrentItem(); + Double money = getMoney(item); + + if(money != null) { + + if(!plugin.isCurrencyPhysical()) { + event.setCancelled(true); + + plugin.getServer().getScheduler().runTask( + plugin, + new RemoveMoneyFromInventoryTask( + plugin, + player.getInventory(), + player + ) + ); + } + else if(event.getClick() == ClickType.DROP) { + + event.setCancelled(true); + event.getView().setItem(event.getRawSlot(), null); + + double totalAmount = money * item.getAmount(); + plugin.getEconomy().depositPlayer(player, totalAmount); + player.sendMessage( + ChatColor.GREEN + + "Deposited " + + plugin.getEconomy().format(totalAmount) + + " into your account" + ); + + } + + } + + } } + + } - } + private boolean isItemMoney(ItemStack item) { + + if(item == null) return false; + ItemMeta itemMeta = item.getItemMeta(); + if(itemMeta == null) return false; + + PersistentDataContainer itemData = itemMeta.getPersistentDataContainer(); + Double money = itemData.get( + NamespacedKey.fromString("money", plugin), + PersistentDataType.DOUBLE + ); + + return money != null; + + } + + private Double getMoney(ItemStack item) { + + if(item == null) return null; + ItemMeta itemMeta = item.getItemMeta(); + if(itemMeta == null) return null; + + PersistentDataContainer itemData = itemMeta.getPersistentDataContainer(); + Double money = itemData.get( + NamespacedKey.fromString("money", plugin), + PersistentDataType.DOUBLE + ); + + return money; + + } } \ No newline at end of file diff --git a/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryCloseListener.java b/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryCloseListener.java index 4b008f3..3001cba 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryCloseListener.java +++ b/src/main/java/online/meinkraft/customvillagertrades/listener/InventoryCloseListener.java @@ -22,7 +22,13 @@ public InventoryCloseListener(CustomVillagerTrades plugin) { @EventHandler public void onInventoryCloseEvent(InventoryCloseEvent event) { - if(event.getView().getTopInventory().getType() == InventoryType.MERCHANT) { + InventoryType inventoryType = event.getView().getTopInventory().getType(); + + // check for money if currency isn't physical + if( + inventoryType == InventoryType.MERCHANT && + !plugin.isCurrencyPhysical() + ) { MerchantInventory merchantInventory = (MerchantInventory) event.getView().getTopInventory(); // delete result diff --git a/src/main/java/online/meinkraft/customvillagertrades/listener/TradeSelectListener.java b/src/main/java/online/meinkraft/customvillagertrades/listener/TradeSelectListener.java index 6e38ba6..8ba0767 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/listener/TradeSelectListener.java +++ b/src/main/java/online/meinkraft/customvillagertrades/listener/TradeSelectListener.java @@ -108,15 +108,16 @@ public void onTradeSelectEvent(TradeSelectEvent event) { } // prevent money getting into players inventories - plugin.getServer().getScheduler().runTask( - plugin, - new RemoveMoneyFromInventoryTask( + if(!plugin.isCurrencyPhysical()) { + plugin.getServer().getScheduler().runTask( plugin, - event.getView().getBottomInventory(), - player - ) - ); - + new RemoveMoneyFromInventoryTask( + plugin, + event.getView().getBottomInventory(), + player + ) + ); + } } } \ No newline at end of file diff --git a/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromHandTask.java b/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromHandTask.java index cb3f882..9cf61fa 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromHandTask.java +++ b/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromHandTask.java @@ -7,6 +7,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import net.md_5.bungee.api.ChatColor; import online.meinkraft.customvillagertrades.CustomVillagerTrades; public class RemoveMoneyFromHandTask implements Runnable { @@ -51,7 +52,14 @@ private boolean depositMoney(ItemStack item) { if(money != null) { if(plugin.isEconomyEnabled() && player != null) { - plugin.getEconomy().depositPlayer(player, money * item.getAmount()); + double totalAmount = money * item.getAmount(); + plugin.getEconomy().depositPlayer(player, totalAmount); + player.sendMessage( + ChatColor.GREEN + + "Deposited " + + plugin.getEconomy().format(totalAmount) + + " into your account" + ); } return true; diff --git a/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromInventoryTask.java b/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromInventoryTask.java index 11a15d1..e00bf14 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromInventoryTask.java +++ b/src/main/java/online/meinkraft/customvillagertrades/task/RemoveMoneyFromInventoryTask.java @@ -8,6 +8,7 @@ import org.bukkit.persistence.PersistentDataContainer; import org.bukkit.persistence.PersistentDataType; +import net.md_5.bungee.api.ChatColor; import online.meinkraft.customvillagertrades.CustomVillagerTrades; public class RemoveMoneyFromInventoryTask implements Runnable { @@ -59,7 +60,14 @@ private boolean depositMoney(ItemStack item) { if(money != null) { if(plugin.isEconomyEnabled() && player != null) { - plugin.getEconomy().depositPlayer(player, money * item.getAmount()); + double totalAmount = money * item.getAmount(); + plugin.getEconomy().depositPlayer(player, totalAmount); + player.sendMessage( + ChatColor.GREEN + + "Deposited " + + plugin.getEconomy().format(totalAmount) + + " into your account" + ); } return true; diff --git a/src/main/java/online/meinkraft/customvillagertrades/trade/CustomTradeLoader.java b/src/main/java/online/meinkraft/customvillagertrades/trade/CustomTradeLoader.java index c6b0002..0824270 100644 --- a/src/main/java/online/meinkraft/customvillagertrades/trade/CustomTradeLoader.java +++ b/src/main/java/online/meinkraft/customvillagertrades/trade/CustomTradeLoader.java @@ -19,6 +19,7 @@ import org.bukkit.enchantments.EnchantmentWrapper; import org.bukkit.entity.Villager; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.persistence.PersistentDataContainer; @@ -233,14 +234,23 @@ static public ItemStack toItemStack(CustomVillagerTrades plugin, Map map) } ItemStack itemStack = new ItemStack(plugin.getCurrencyMaterial()); + + // Make item look shiny + itemStack.addUnsafeEnchantment(Enchantment.VANISHING_CURSE, 1); + ItemMeta itemMeta = itemStack.getItemMeta(); PersistentDataContainer data = itemMeta.getPersistentDataContainer(); + data.set(new NamespacedKey(plugin, "money"), PersistentDataType.DOUBLE, amount); itemMeta.setDisplayName( plugin.getCurrencyPrefix() + String.format("%,.2f", amount) + plugin.getCurrencySuffix() ); + + // Hide the enchantment details + itemMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + itemStack.setItemMeta(itemMeta); return itemStack; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5632d6d..d384184 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -33,11 +33,16 @@ tool: CLOCK # trade enableEconomy: false +# This will allow players to pick up and carry the money generated in the +# trading windows. They can press the DROP key while hovering over their +# money in their inventory at any time to deposit it into their account +enablePhysicalCurrency: false + # The item that will represent a trade involving your Vault Economy as an # ingredient or as a result currencyItem: PAPER # The strings that goes before and after on the cost on a money item: # "" -currencyPrefix: '$' -currencySuffix: '' +currencyPrefix: "$" +currencySuffix: ""