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