added Vault support

This commit is contained in:
aPunch 2012-02-21 19:23:01 -06:00
parent ba4ab654b2
commit db945616f2
5 changed files with 28 additions and 4 deletions

BIN
lib/Vault.jar Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
name: Blacksmith name: Blacksmith
author: aPunch author: aPunch
version: 1.0 version: 1.0
main: net.apunch.blacksmith.Blacksmith main: net.apunch.blacksmith.Blacksmith
depend: [Citizens, Vault]

View File

@ -7,13 +7,17 @@ import net.apunch.blacksmith.util.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.DataKey;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Blacksmith extends JavaPlugin { public class Blacksmith extends JavaPlugin {
private Settings config; private Settings config;
private Economy economy;
@Override @Override
public void onDisable() { public void onDisable() {
@ -27,6 +31,12 @@ public class Blacksmith extends JavaPlugin {
config = new Settings(this); config = new Settings(this);
config.load(); config.load();
// Setup Vault
RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(
Economy.class);
if (economyProvider != null)
economy = economyProvider.getProvider();
CitizensAPI.getCharacterManager().register(BlacksmithCharacter.class); CitizensAPI.getCharacterManager().register(BlacksmithCharacter.class);
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled."); 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(""); DataKey root = config.getConfig().getKey("");
double price = Setting.BASE_PRICE.asDouble(); double price = Setting.BASE_PRICE.asDouble();
if (root.keyExists("base-prices." + item.getType().name().toLowerCase().replace('_', '-'))) if (root.keyExists("base-prices." + item.getType().name().toLowerCase().replace('_', '-')))

View File

@ -119,7 +119,7 @@ public class BlacksmithCharacter extends Character {
return; return;
} }
session = new RepairSession(player, npc); session = new RepairSession(player, npc);
npc.chat(player, costMsg.replace("<price>", String.valueOf(plugin.getCost(hand))).replace("<item>", npc.chat(player, costMsg.replace("<price>", plugin.formatCost(player)).replace("<item>",
hand.getType().name().toLowerCase().replace('_', ' '))); hand.getType().name().toLowerCase().replace('_', ' ')));
} }
} }
@ -152,6 +152,7 @@ public class BlacksmithCharacter extends Character {
private void reforge(NPC npc, Player player) { private void reforge(NPC npc, Player player) {
npc.chat(player, startReforgeMsg); npc.chat(player, startReforgeMsg);
plugin.withdraw(player);
session.setTask(plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, session.setTask(plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin,
new ReforgeTask(npc, player), (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20)); new ReforgeTask(npc, player), (new Random().nextInt(maxReforgeDelay) + minReforgeDelay) * 20));
if (npc.getBukkitEntity() instanceof Player) if (npc.getBukkitEntity() instanceof Player)

View File

@ -27,7 +27,7 @@ public class RepairSession {
npc.chat(player, "<c>That's not the item you wanted to reforge before!"); npc.chat(player, "<c>That's not the item you wanted to reforge before!");
return true; return true;
} }
if (plugin.getCost(player.getItemInHand()) < 0 /* TODO hasEnough */) { if (!plugin.doesPlayerHaveEnough(player)) {
npc.chat(player, ((BlacksmithCharacter) npc.getCharacter()).getInsufficientFundsMessage()); npc.chat(player, ((BlacksmithCharacter) npc.getCharacter()).getInsufficientFundsMessage());
return true; return true;
} }