diff --git a/lib/Vault.jar b/lib/Vault.jar new file mode 100644 index 0000000..66e0d1c Binary files /dev/null and b/lib/Vault.jar differ diff --git a/plugin.yml b/plugin.yml index f09fbb0..157d2b2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,4 +1,5 @@ name: Blacksmith author: aPunch version: 1.0 -main: net.apunch.blacksmith.Blacksmith \ No newline at end of file +main: net.apunch.blacksmith.Blacksmith +depend: [Citizens, Vault] \ No newline at end of file diff --git a/src/net/apunch/blacksmith/Blacksmith.java b/src/net/apunch/blacksmith/Blacksmith.java index 16d2e80..315547e 100644 --- a/src/net/apunch/blacksmith/Blacksmith.java +++ b/src/net/apunch/blacksmith/Blacksmith.java @@ -7,13 +7,17 @@ import net.apunch.blacksmith.util.Settings.Setting; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.util.DataKey; +import net.milkbowl.vault.economy.Economy; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; public class Blacksmith extends JavaPlugin { private Settings config; + private Economy economy; @Override public void onDisable() { @@ -27,6 +31,12 @@ public class Blacksmith extends JavaPlugin { config = new Settings(this); config.load(); + // Setup Vault + RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration( + Economy.class); + if (economyProvider != null) + economy = economyProvider.getProvider(); + CitizensAPI.getCharacterManager().register(BlacksmithCharacter.class); getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled."); @@ -97,7 +107,19 @@ public class Blacksmith extends JavaPlugin { } } - public double getCost(ItemStack item) { + public boolean doesPlayerHaveEnough(Player player) { + return economy.getBalance(player.getName()) - getCost(player.getItemInHand()) >= 0; + } + + public String formatCost(Player player) { + return economy.format(getCost(player.getItemInHand())); + } + + public void withdraw(Player player) { + economy.withdrawPlayer(player.getName(), getCost(player.getItemInHand())); + } + + private double getCost(ItemStack item) { DataKey root = config.getConfig().getKey(""); double price = Setting.BASE_PRICE.asDouble(); if (root.keyExists("base-prices." + item.getType().name().toLowerCase().replace('_', '-'))) diff --git a/src/net/apunch/blacksmith/BlacksmithCharacter.java b/src/net/apunch/blacksmith/BlacksmithCharacter.java index 9e0e5e6..af4190a 100644 --- a/src/net/apunch/blacksmith/BlacksmithCharacter.java +++ b/src/net/apunch/blacksmith/BlacksmithCharacter.java @@ -119,7 +119,7 @@ public class BlacksmithCharacter extends Character { return; } session = new RepairSession(player, npc); - npc.chat(player, costMsg.replace("", String.valueOf(plugin.getCost(hand))).replace("", + npc.chat(player, costMsg.replace("", plugin.formatCost(player)).replace("", hand.getType().name().toLowerCase().replace('_', ' '))); } } @@ -152,6 +152,7 @@ public class BlacksmithCharacter extends Character { private void reforge(NPC npc, Player player) { npc.chat(player, startReforgeMsg); + plugin.withdraw(player); session.setTask(plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new ReforgeTask(npc, player), (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20)); if (npc.getBukkitEntity() instanceof Player) diff --git a/src/net/apunch/blacksmith/RepairSession.java b/src/net/apunch/blacksmith/RepairSession.java index 3341d24..38686a1 100644 --- a/src/net/apunch/blacksmith/RepairSession.java +++ b/src/net/apunch/blacksmith/RepairSession.java @@ -27,7 +27,7 @@ public class RepairSession { npc.chat(player, "That's not the item you wanted to reforge before!"); return true; } - if (plugin.getCost(player.getItemInHand()) < 0 /* TODO hasEnough */) { + if (!plugin.doesPlayerHaveEnough(player)) { npc.chat(player, ((BlacksmithCharacter) npc.getCharacter()).getInsufficientFundsMessage()); return true; }