From 7a9b974c77ec48de358d979588a6af1c803a9217 Mon Sep 17 00:00:00 2001 From: Leandro Date: Sat, 3 Jul 2021 13:40:05 -0300 Subject: [PATCH 1/3] Implement basic economy using Vault. Prices for heads can be set for each category in the config file. --- pom.xml | 7 +++ src/main/java/tsp/headdb/HeadDB.java | 26 +++++++++ .../tsp/headdb/inventory/InventoryUtils.java | 54 ++++++++++++------- src/main/resources/config.yml | 16 ++++++ src/main/resources/plugin.yml | 1 + 5 files changed, 84 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index dd2781b..91fc3f6 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,13 @@ anvilgui 1.5.0-SNAPSHOT + + + com.github.MilkBowl + VaultAPI + 1.7 + provided + diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index 276816b..f8c7035 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -13,12 +13,15 @@ import tsp.headdb.listener.MenuListener; import tsp.headdb.util.Log; import tsp.headdb.util.Metrics; import tsp.headdb.util.Utils; +import org.bukkit.plugin.RegisteredServiceProvider; +import net.milkbowl.vault.economy.Economy; public class HeadDB extends JavaPlugin { private static HeadDB instance; private Config config; private Json playerdata; + private Economy economy = null; @Override public void onEnable() { @@ -28,6 +31,17 @@ public class HeadDB extends JavaPlugin { config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream(); playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson(); + if (config.getBoolean("economy.enable")) + { + Log.debug("Starting economy..."); + this.economy = this.setupEconomy(); + if (this.economy == null) { + Log.error("Economy support requires Vault and an economy provider plugin."); + } else { + Log.info("Economy provider: " + this.economy.getName()); + } + } + Log.debug("Starting metrics..."); new Metrics(this, Utils.METRICS_ID); @@ -52,6 +66,15 @@ public class HeadDB extends JavaPlugin { Log.info("Done!"); } + private Economy setupEconomy() { + if (!this.getServer().getPluginManager().isPluginEnabled("Vault")) return null; + + RegisteredServiceProvider economyProvider = this.getServer().getServicesManager().getRegistration(Economy.class); + if (economyProvider == null) return null; + + return this.economy = economyProvider.getProvider(); + } + public Config getCfg() { return config; } @@ -64,4 +87,7 @@ public class HeadDB extends JavaPlugin { return instance; } + public Economy getEconomy() { + return this.economy; + } } diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 16f7323..15a9c19 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -7,12 +7,14 @@ import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import tsp.headdb.HeadDB; import tsp.headdb.api.Head; import tsp.headdb.api.HeadAPI; import tsp.headdb.api.LocalHead; import tsp.headdb.database.Category; import tsp.headdb.util.Utils; import tsp.headdb.util.XMaterial; +import net.milkbowl.vault.economy.Economy; import java.util.ArrayList; import java.util.Arrays; @@ -27,13 +29,11 @@ public class InventoryUtils { for (LocalHead localHead : heads) { pane.addButton(new Button(localHead.getItemStack(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { - ItemStack item = localHead.getItemStack(); - item.setAmount(64); - player.getInventory().addItem(item); + purchaseItem(player, localHead.getItemStack(), 64, "local", localHead.getName()); return; } if (e.getClick() == ClickType.LEFT) { - player.getInventory().addItem(localHead.getItemStack()); + purchaseItem(player, localHead.getItemStack(), 1, "local", localHead.getName()); return; } if (e.getClick() == ClickType.RIGHT) { @@ -53,13 +53,11 @@ public class InventoryUtils { for (Head head : heads) { pane.addButton(new Button(head.getItemStack(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { - ItemStack item = head.getItemStack(); - item.setAmount(64); - player.getInventory().addItem(item); + purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); return; } if (e.getClick() == ClickType.LEFT) { - player.getInventory().addItem(head.getItemStack()); + purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); } if (e.getClick() == ClickType.RIGHT) { HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getId()); @@ -79,13 +77,11 @@ public class InventoryUtils { for (Head head : heads) { pane.addButton(new Button(head.getItemStack(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { - ItemStack item = head.getItemStack(); - item.setAmount(64); - player.getInventory().addItem(item); + purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); return; } if (e.getClick() == ClickType.LEFT) { - player.getInventory().addItem(head.getItemStack()); + purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); } if (e.getClick() == ClickType.RIGHT) { HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); @@ -105,13 +101,11 @@ public class InventoryUtils { for (Head head : heads) { pane.addButton(new Button(head.getItemStack(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { - ItemStack item = head.getItemStack(); - item.setAmount(64); - player.getInventory().addItem(item); + purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); return; } if (e.getClick() == ClickType.LEFT) { - player.getInventory().addItem(head.getItemStack()); + purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); } if (e.getClick() == ClickType.RIGHT) { HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); @@ -130,13 +124,11 @@ public class InventoryUtils { for (Head head : heads) { pane.addButton(new Button(head.getItemStack(), e -> { if (e.getClick() == ClickType.SHIFT_LEFT) { - ItemStack item = head.getItemStack(); - item.setAmount(64); - player.getInventory().addItem(item); + purchaseItem(player, head.getItemStack(), 64, head.getCategory().getName(), head.getName()); return; } if (e.getClick() == ClickType.LEFT) { - player.getInventory().addItem(head.getItemStack()); + purchaseItem(player, head.getItemStack(), 1, head.getCategory().getName(), head.getName()); } if (e.getClick() == ClickType.RIGHT) { HeadAPI.addFavoriteHead(player.getUniqueId(), head.getId()); @@ -222,4 +214,26 @@ public class InventoryUtils { return item; } + public static void purchaseItem(Player player, ItemStack item, int amount, String costCfg, String description) { + Economy economy = HeadDB.getInstance().getEconomy(); + if (economy != null) { + double cost = amount * HeadDB.getInstance().getCfg().getDouble("economy.cost." + costCfg); + if (cost > 0) { + if (economy.has(player, cost)) { + economy.withdrawPlayer(player, cost); + player.sendMessage(String.format("You purchased %d x %s for %.2f %s!", amount, description, cost, economy.currencyNamePlural())); + } else { + player.sendMessage(String.format("You do not have enough %s to purchase %d x %s.", economy.currencyNamePlural(), amount, description)); + return; + } + } + else + { + player.sendMessage(String.format("You purchased %d x %s for free!", amount, description)); + } + } + + item.setAmount(amount); + player.getInventory().addItem(item); + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9b5f3d5..63cdfcd 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,5 +7,21 @@ asyncStartup: false # If the cached heads are older than these amount of seconds, the plugin will refresh the database refresh: 3600 +# Economy options +economy: + enable: false + cost: + alphabet: 100 + animals: 100 + blocks: 100 + decoration: 100 + food-drinks: 100 + humans: 100 + humanoid: 100 + miscellaneous: 100 + monsters: 100 + plants: 100 + local: 1000 + # Debug Mode debug: false \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2308a61..3913626 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,6 +3,7 @@ description: ${project.description} main: tsp.headdb.HeadDB version: ${project.version} +softdepend: [Vault] api-version: 1.16 author: Silent From 85544634b8eb5be594eef3e14447a5700eec67d0 Mon Sep 17 00:00:00 2001 From: Leandro Date: Sat, 3 Jul 2021 13:54:55 -0300 Subject: [PATCH 2/3] Bracket style fix. --- src/main/java/tsp/headdb/HeadDB.java | 3 +-- src/main/java/tsp/headdb/inventory/InventoryUtils.java | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/tsp/headdb/HeadDB.java b/src/main/java/tsp/headdb/HeadDB.java index f8c7035..4583da3 100644 --- a/src/main/java/tsp/headdb/HeadDB.java +++ b/src/main/java/tsp/headdb/HeadDB.java @@ -31,8 +31,7 @@ public class HeadDB extends JavaPlugin { config = LightningBuilder.fromPath("config.yml", "plugins/HeadDB").createConfig().addDefaultsFromInputStream(); playerdata = LightningBuilder.fromPath("playerdata.json", "plugins/HeadDB").createJson(); - if (config.getBoolean("economy.enable")) - { + if (config.getBoolean("economy.enable")) { Log.debug("Starting economy..."); this.economy = this.setupEconomy(); if (this.economy == null) { diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 15a9c19..0fd1df8 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -226,9 +226,7 @@ public class InventoryUtils { player.sendMessage(String.format("You do not have enough %s to purchase %d x %s.", economy.currencyNamePlural(), amount, description)); return; } - } - else - { + } else { player.sendMessage(String.format("You purchased %d x %s for free!", amount, description)); } } From b8bc31f900f894fe4d47d6f592977646fafa1a22 Mon Sep 17 00:00:00 2001 From: Leandro Date: Sun, 4 Jul 2021 17:20:23 -0300 Subject: [PATCH 3/3] Add permissions headdb.economy.free and headdb.economy.CATEGORY.free to make heads free for specific players. --- .../tsp/headdb/inventory/InventoryUtils.java | 49 +++++++++++++------ 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/main/java/tsp/headdb/inventory/InventoryUtils.java b/src/main/java/tsp/headdb/inventory/InventoryUtils.java index 0fd1df8..d03cff5 100644 --- a/src/main/java/tsp/headdb/inventory/InventoryUtils.java +++ b/src/main/java/tsp/headdb/inventory/InventoryUtils.java @@ -214,23 +214,44 @@ public class InventoryUtils { return item; } - public static void purchaseItem(Player player, ItemStack item, int amount, String costCfg, String description) { + public static double getCategoryCost(Player player, String category) { + // If the player has the permission headdb.economy.free or headdb.economy.CATEGORY.free, the item is free. + if (player.hasPermission("headdb.economy.free") || player.hasPermission("headdb.economy." + category + ".free")) return 0; + + // Otherwise, get the price for this category from the config file. + return HeadDB.getInstance().getCfg().getDouble("economy.cost." + category); + } + + public static boolean processPayment(Player player, int amount, String category, String description) { Economy economy = HeadDB.getInstance().getEconomy(); - if (economy != null) { - double cost = amount * HeadDB.getInstance().getCfg().getDouble("economy.cost." + costCfg); - if (cost > 0) { - if (economy.has(player, cost)) { - economy.withdrawPlayer(player, cost); - player.sendMessage(String.format("You purchased %d x %s for %.2f %s!", amount, description, cost, economy.currencyNamePlural())); - } else { - player.sendMessage(String.format("You do not have enough %s to purchase %d x %s.", economy.currencyNamePlural(), amount, description)); - return; - } - } else { - player.sendMessage(String.format("You purchased %d x %s for free!", amount, description)); - } + + // If economy is disabled or no plugin is present, the item is free. + // Don't mention receiving it for free in this case, since it is always free. + if (economy == null) { + player.sendMessage(String.format("You received %d x %s!", amount, description)); + return true; } + double cost = getCategoryCost(player, category) * amount; + + // If the cost is higher than zero, attempt to charge for it. + if (cost > 0) { + if (economy.has(player, cost)) { + economy.withdrawPlayer(player, cost); + player.sendMessage(String.format("You purchased %d x %s for %.2f %s!", amount, description, cost, economy.currencyNamePlural())); + return true; + } + player.sendMessage(String.format("You do not have enough %s to purchase %d x %s.", economy.currencyNamePlural(), amount, description)); + return false; + } + + // Otherwise, the item is free. + player.sendMessage(String.format("You received %d x %s for free!", amount, description)); + return true; + } + + public static void purchaseItem(Player player, ItemStack item, int amount, String category, String description) { + if (!processPayment(player, amount, category, description)) return; item.setAmount(amount); player.getInventory().addItem(item); }