diff --git a/pom.xml b/pom.xml
index eb1a135..5600a44 100644
--- a/pom.xml
+++ b/pom.xml
@@ -68,6 +68,13 @@
anvilgui
1.5.1-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..4583da3 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,16 @@ 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 +65,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 +86,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 a226a92..d149c62 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());
@@ -228,4 +220,45 @@ public class InventoryUtils {
return item;
}
+ 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 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);
+ }
}
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