From 351eeb1544c0d1a14596bf3b8fda77e327e4212e Mon Sep 17 00:00:00 2001 From: aPunch Date: Sat, 25 Feb 2012 00:52:17 -0600 Subject: [PATCH] refactor --- plugin.yml | 2 +- src/net/apunch/blacksmith/Blacksmith.java | 343 ++++++++++++------ .../blacksmith/BlacksmithCharacter.java | 241 ------------ .../apunch/blacksmith/BlacksmithPlugin.java | 140 +++++++ src/net/apunch/blacksmith/ReforgeSession.java | 6 +- src/net/apunch/blacksmith/util/Settings.java | 4 +- 6 files changed, 368 insertions(+), 368 deletions(-) delete mode 100644 src/net/apunch/blacksmith/BlacksmithCharacter.java create mode 100644 src/net/apunch/blacksmith/BlacksmithPlugin.java diff --git a/plugin.yml b/plugin.yml index 157d2b2..3bf551e 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: Blacksmith author: aPunch version: 1.0 -main: net.apunch.blacksmith.Blacksmith +main: net.apunch.blacksmith.BlacksmithPlugin 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 315547e..0a9bd76 100644 --- a/src/net/apunch/blacksmith/Blacksmith.java +++ b/src/net/apunch/blacksmith/Blacksmith.java @@ -1,140 +1,241 @@ package net.apunch.blacksmith; -import java.util.logging.Level; - -import net.apunch.blacksmith.util.Settings; -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 java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import org.bukkit.Bukkit; +import org.bukkit.Material; 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; +import net.apunch.blacksmith.util.Settings.Setting; - @Override - public void onDisable() { - config.save(); +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.api.trait.Character; +import net.citizensnpcs.api.trait.SaveId; +import net.citizensnpcs.api.util.DataKey; - getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled."); +@SaveId("blacksmith") +public class Blacksmith extends Character { + private static final int[] enchantments = new int[Enchantment.values().length]; + + private final BlacksmithPlugin plugin; + private final List reforgeableItems = new ArrayList(); + private final Map cooldowns = new HashMap(); + private ReforgeSession session; + + // Defaults + private String busyWithPlayerMsg = Setting.BUSY_WITH_PLAYER_MESSAGE.asString(); + private String busyReforgingMsg = Setting.BUSY_WITH_REFORGE_MESSAGE.asString(); + private String costMsg = Setting.COST_MESSAGE.asString(); + private String invalidItemMsg = Setting.INVALID_ITEM_MESSAGE.asString(); + private String startReforgeMsg = Setting.START_REFORGE_MESSAGE.asString(); + private String successMsg = Setting.SUCCESS_MESSAGE.asString(); + private String failMsg = Setting.FAIL_MESSAGE.asString(); + private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString(); + private String cooldownUnexpiredMsg = Setting.COOLDOWN_UNEXPIRED_MESSAGE.asString(); + private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt(); + private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt(); + private int reforgeCooldown = Setting.REFORGE_COOLDOWN.asInt(); + private int failChance = Setting.FAIL_CHANCE.asInt(); + private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt(); + private boolean dropItem = Setting.DROP_ITEM.asBoolean(); + + public Blacksmith() { + plugin = (BlacksmithPlugin) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith"); + int i = 0; + for (Enchantment enchantment : Enchantment.values()) + enchantments[i++] = enchantment.getId(); } @Override - public void onEnable() { - config = new Settings(this); - config.load(); + public void load(DataKey key) { + for (DataKey sub : key.getRelative("reforgeable-items").getIntegerSubKeys()) + if (Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')) != null) + reforgeableItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_'))); - // 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."); + // Override defaults if they exist + if (key.keyExists("messages.busy-with-player")) + busyWithPlayerMsg = key.getString("messages.busy-with-player"); + if (key.keyExists("messages.busy-with-reforge")) + busyReforgingMsg = key.getString("messages.busy-with-reforge"); + if (key.keyExists("messages.cost")) + costMsg = key.getString("messages.cost"); + if (key.keyExists("messages.invalid-item")) + invalidItemMsg = key.getString("messages.invalid-item"); + if (key.keyExists("messages.start-reforge")) + startReforgeMsg = key.getString("messages.start-reforge"); + if (key.keyExists("messages.successful-reforge")) + successMsg = key.getString("messages.successful-reforge"); + if (key.keyExists("messages.fail-reforge")) + failMsg = key.getString("messages.fail-reforge"); + if (key.keyExists("messages.insufficient-funds")) + insufficientFundsMsg = key.getString("messages.insufficient-funds"); + if (key.keyExists("messages.cooldown-not-expired")) + cooldownUnexpiredMsg = key.getString("messages.cooldown-not-expired"); + if (key.keyExists("delays-in-seconds.minimum")) + minReforgeDelay = key.getInt("delays-in-seconds.minimum"); + if (key.keyExists("delays-in-seconds.maximum")) + maxReforgeDelay = key.getInt("delays-in-seconds.maximum"); + if (key.keyExists("delays-in-seconds.reforge-cooldown")) + reforgeCooldown = key.getInt("delays-in-seconds.reforge-cooldown"); + if (key.keyExists("percent-chance-to-fail-reforge")) + failChance = key.getInt("percent-chance-to-fail-reforge"); + if (key.keyExists("maximum-enchantments")) + maxEnchantments = key.getInt("maximum-enchantments"); + if (key.keyExists("drop-item")) + dropItem = key.getBoolean("drop-item"); } - public boolean isTool(ItemStack item) { - switch (item.getType()) { - case WOOD_PICKAXE: - case WOOD_SPADE: - case WOOD_HOE: - case WOOD_SWORD: - case WOOD_AXE: - case STONE_PICKAXE: - case STONE_SPADE: - case STONE_HOE: - case STONE_SWORD: - case STONE_AXE: - case GOLD_PICKAXE: - case GOLD_SPADE: - case GOLD_HOE: - case GOLD_SWORD: - case GOLD_AXE: - case IRON_PICKAXE: - case IRON_SPADE: - case IRON_HOE: - case IRON_SWORD: - case IRON_AXE: - case DIAMOND_PICKAXE: - case DIAMOND_SPADE: - case DIAMOND_HOE: - case DIAMOND_SWORD: - case DIAMOND_AXE: - case BOW: - case FLINT_AND_STEEL: - case FISHING_ROD: - case SHEARS: + @Override + public void onRightClick(NPC npc, Player player) { + if (!player.hasPermission("blacksmith.reforge")) + return; + + if (cooldowns.get(player.getName()) != null) { + if (!Calendar.getInstance().after(cooldowns.get(player.getName()))) { + npc.chat(player, cooldownUnexpiredMsg); + return; + } + cooldowns.remove(player.getName()); + } + + ItemStack hand = player.getItemInHand(); + if (session != null) { + if (!session.isInSession(player)) { + npc.chat(busyWithPlayerMsg); + return; + } + + if (session.isRunning()) { + npc.chat(player, busyReforgingMsg); + return; + } + if (session.handleClick()) + session = null; + else + reforge(npc, player); + } else { + if ((!plugin.isTool(hand) && !plugin.isArmor(hand)) + || (!reforgeableItems.isEmpty() && !reforgeableItems.contains(hand.getType()))) { + npc.chat(player, invalidItemMsg); + return; + } + session = new ReforgeSession(player, npc); + npc.chat(player, costMsg.replace("", plugin.formatCost(player)).replace("", + hand.getType().name().toLowerCase().replace('_', ' '))); + } + } + + @Override + public void save(DataKey key) { + for (int i = 0; i < reforgeableItems.size(); i++) + key.getRelative("reforgeable-items").setString(String.valueOf(i), + reforgeableItems.get(i).name().toLowerCase().replace('_', '-')); + + key.setString("messages.busy-with-player", busyWithPlayerMsg); + key.setString("messages.busy-with-reforge", busyReforgingMsg); + key.setString("messages.cost", costMsg); + key.setString("messages.invalid-item", invalidItemMsg); + key.setString("messages.start-reforge", startReforgeMsg); + key.setString("messages.successful-reforge", successMsg); + key.setString("messages.fail-reforge", failMsg); + key.setString("messages.insufficient-funds", insufficientFundsMsg); + key.setString("messages.cooldown-not-expired", cooldownUnexpiredMsg); + key.setInt("delays-in-seconds.minimum", minReforgeDelay); + key.setInt("delays-in-seconds.maximum", maxReforgeDelay); + key.setInt("delays-in-seconds.reforge-cooldown", reforgeCooldown); + key.setInt("percent-chance-to-fail-reforge", failChance); + key.setInt("maximum-enchantments", maxEnchantments); + key.setBoolean("drop-item", dropItem); + } + + public String getInsufficientFundsMessage() { + return insufficientFundsMsg; + } + + 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) + ((Player) npc.getBukkitEntity()).setItemInHand(player.getItemInHand()); + player.setItemInHand(null); + } + + private class ReforgeTask implements Runnable { + private final NPC npc; + private final Player player; + private final ItemStack reforge; + + private ReforgeTask(NPC npc, Player player) { + this.npc = npc; + this.player = player; + reforge = player.getItemInHand(); + } + + @Override + public void run() { + npc.chat(player, reforgeItemInHand() ? successMsg : failMsg); + if (npc.getBukkitEntity() instanceof Player) + ((Player) npc.getBukkitEntity()).setItemInHand(null); + if (dropItem) + player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), reforge); + else { + for (ItemStack stack : player.getInventory().addItem(reforge).values()) + player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), stack); + } + session = null; + // Start cooldown + Calendar wait = Calendar.getInstance(); + wait.add(Calendar.SECOND, reforgeCooldown); + cooldowns.put(player.getName(), wait); + } + + private boolean reforgeItemInHand() { + Random random = new Random(); + if (random.nextInt(100) < failChance) { + for (Enchantment enchantment : reforge.getEnchantments().keySet()) { + // Remove or downgrade enchantments + if (random.nextBoolean()) + reforge.removeEnchantment(enchantment); + else { + if (reforge.getEnchantmentLevel(enchantment) > 1) { + reforge.removeEnchantment(enchantment); + reforge.addEnchantment(enchantment, 1); + } + } + } + // Damage the item + short durability = (short) (reforge.getDurability() + reforge.getDurability() * random.nextInt(8)); + short maxDurability = reforge.getType().getMaxDurability(); + if (durability <= 0) + durability = (short) (maxDurability / 3); + else if (reforge.getDurability() + durability > maxDurability) + durability = (short) (maxDurability - random.nextInt(maxDurability - 25)); + reforge.setDurability(durability); + return false; + } + // Add random enchantments + int chance = 10; + if (reforge.getDurability() == 0) + chance *= 4; + else + reforge.setDurability((short) 0); + for (int i = 0; i < chance; i++) { + if (reforge.getEnchantments().keySet().size() == maxEnchantments) + break; + Enchantment enchantment = Enchantment.getById(enchantments[random.nextInt(enchantments.length)]); + if (enchantment.canEnchantItem(reforge)) + reforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel()) + 1); + } return true; - default: - return false; } } - - public boolean isArmor(ItemStack item) { - switch (item.getType()) { - case LEATHER_HELMET: - case LEATHER_CHESTPLATE: - case LEATHER_LEGGINGS: - case LEATHER_BOOTS: - case CHAINMAIL_HELMET: - case CHAINMAIL_CHESTPLATE: - case CHAINMAIL_LEGGINGS: - case CHAINMAIL_BOOTS: - case GOLD_HELMET: - case GOLD_CHESTPLATE: - case GOLD_LEGGINGS: - case GOLD_BOOTS: - case IRON_HELMET: - case IRON_CHESTPLATE: - case IRON_LEGGINGS: - case IRON_BOOTS: - case DIAMOND_HELMET: - case DIAMOND_CHESTPLATE: - case DIAMOND_LEGGINGS: - case DIAMOND_BOOTS: - return true; - default: - return false; - } - } - - 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('_', '-'))) - price = root.getDouble("base-prices." + item.getType().name().toLowerCase().replace('_', '-')); - - // Adjust price based on durability and enchantments - price += (item.getType().getMaxDurability() - item.getDurability()); - - double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble(); - for (Enchantment enchantment : item.getEnchantments().keySet()) { - if (root.keyExists("enchantment-modifiers." + enchantment.getName().toLowerCase().replace('_', '-'))) - enchantmentModifier = root.getDouble("enchantment-modifiers." - + enchantment.getName().toLowerCase().replace('_', '-')); - price += enchantmentModifier * item.getEnchantmentLevel(enchantment); - } - return price; - } } \ No newline at end of file diff --git a/src/net/apunch/blacksmith/BlacksmithCharacter.java b/src/net/apunch/blacksmith/BlacksmithCharacter.java deleted file mode 100644 index b65a28a..0000000 --- a/src/net/apunch/blacksmith/BlacksmithCharacter.java +++ /dev/null @@ -1,241 +0,0 @@ -package net.apunch.blacksmith; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; - -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -import net.apunch.blacksmith.util.Settings.Setting; - -import net.citizensnpcs.api.npc.NPC; -import net.citizensnpcs.api.trait.Character; -import net.citizensnpcs.api.trait.SaveId; -import net.citizensnpcs.api.util.DataKey; - -@SaveId("blacksmith") -public class BlacksmithCharacter extends Character { - private static final int[] enchantments = new int[Enchantment.values().length]; - - private final Blacksmith plugin; - private final List reforgeableItems = new ArrayList(); - private final Map cooldowns = new HashMap(); - private ReforgeSession session; - - // Defaults - private String busyWithPlayerMsg = Setting.BUSY_WITH_PLAYER_MESSAGE.asString(); - private String busyReforgingMsg = Setting.BUSY_WITH_REFORGE_MESSAGE.asString(); - private String costMsg = Setting.COST_MESSAGE.asString(); - private String invalidItemMsg = Setting.INVALID_ITEM_MESSAGE.asString(); - private String startReforgeMsg = Setting.START_REFORGE_MESSAGE.asString(); - private String successMsg = Setting.SUCCESS_MESSAGE.asString(); - private String failMsg = Setting.FAIL_MESSAGE.asString(); - private String insufficientFundsMsg = Setting.INSUFFICIENT_FUNDS_MESSAGE.asString(); - private String cooldownUnexpiredMsg = Setting.COOLDOWN_UNEXPIRED_MESSAGE.asString(); - private int minReforgeDelay = Setting.MIN_REFORGE_DELAY.asInt(); - private int maxReforgeDelay = Setting.MAX_REFORGE_DELAY.asInt(); - private int reforgeCooldown = Setting.REFORGE_COOLDOWN.asInt(); - private int failChance = Setting.FAIL_CHANCE.asInt(); - private int maxEnchantments = Setting.MAX_ENCHANTMENTS.asInt(); - private boolean dropItem = Setting.DROP_ITEM.asBoolean(); - - public BlacksmithCharacter() { - plugin = (Blacksmith) Bukkit.getServer().getPluginManager().getPlugin("Blacksmith"); - int i = 0; - for (Enchantment enchantment : Enchantment.values()) - enchantments[i++] = enchantment.getId(); - } - - @Override - public void load(DataKey key) { - for (DataKey sub : key.getRelative("reforgeable-items").getIntegerSubKeys()) - if (Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_')) != null) - reforgeableItems.add(Material.getMaterial(sub.getString("").toUpperCase().replace('-', '_'))); - - // Override defaults if they exist - if (key.keyExists("messages.busy-with-player")) - busyWithPlayerMsg = key.getString("messages.busy-with-player"); - if (key.keyExists("messages.busy-with-reforge")) - busyReforgingMsg = key.getString("messages.busy-with-reforge"); - if (key.keyExists("messages.cost")) - costMsg = key.getString("messages.cost"); - if (key.keyExists("messages.invalid-item")) - invalidItemMsg = key.getString("messages.invalid-item"); - if (key.keyExists("messages.start-reforge")) - startReforgeMsg = key.getString("messages.start-reforge"); - if (key.keyExists("messages.successful-reforge")) - successMsg = key.getString("messages.successful-reforge"); - if (key.keyExists("messages.fail-reforge")) - failMsg = key.getString("messages.fail-reforge"); - if (key.keyExists("messages.insufficient-funds")) - insufficientFundsMsg = key.getString("messages.insufficient-funds"); - if (key.keyExists("messages.cooldown-not-expired")) - cooldownUnexpiredMsg = key.getString("messages.cooldown-not-expired"); - if (key.keyExists("delays-in-seconds.minimum")) - minReforgeDelay = key.getInt("delays-in-seconds.minimum"); - if (key.keyExists("delays-in-seconds.maximum")) - maxReforgeDelay = key.getInt("delays-in-seconds.maximum"); - if (key.keyExists("delays-in-seconds.reforge-cooldown")) - reforgeCooldown = key.getInt("delays-in-seconds.reforge-cooldown"); - if (key.keyExists("percent-chance-to-fail-reforge")) - failChance = key.getInt("percent-chance-to-fail-reforge"); - if (key.keyExists("maximum-enchantments")) - maxEnchantments = key.getInt("maximum-enchantments"); - if (key.keyExists("drop-item")) - dropItem = key.getBoolean("drop-item"); - } - - @Override - public void onRightClick(NPC npc, Player player) { - if (!player.hasPermission("blacksmith.reforge")) - return; - - if (cooldowns.get(player.getName()) != null) { - if (!Calendar.getInstance().after(cooldowns.get(player.getName()))) { - npc.chat(player, cooldownUnexpiredMsg); - return; - } - cooldowns.remove(player.getName()); - } - - ItemStack hand = player.getItemInHand(); - if (session != null) { - if (!session.isInSession(player)) { - npc.chat(busyWithPlayerMsg); - return; - } - - if (session.isRunning()) { - npc.chat(player, busyReforgingMsg); - return; - } - if (session.handleClick()) - session = null; - else - reforge(npc, player); - } else { - if ((!plugin.isTool(hand) && !plugin.isArmor(hand)) - || (!reforgeableItems.isEmpty() && !reforgeableItems.contains(hand.getType()))) { - npc.chat(player, invalidItemMsg); - return; - } - session = new ReforgeSession(player, npc); - npc.chat(player, costMsg.replace("", plugin.formatCost(player)).replace("", - hand.getType().name().toLowerCase().replace('_', ' '))); - } - } - - @Override - public void save(DataKey key) { - for (int i = 0; i < reforgeableItems.size(); i++) - key.getRelative("reforgeable-items").setString(String.valueOf(i), - reforgeableItems.get(i).name().toLowerCase().replace('_', '-')); - - key.setString("messages.busy-with-player", busyWithPlayerMsg); - key.setString("messages.busy-with-reforge", busyReforgingMsg); - key.setString("messages.cost", costMsg); - key.setString("messages.invalid-item", invalidItemMsg); - key.setString("messages.start-reforge", startReforgeMsg); - key.setString("messages.successful-reforge", successMsg); - key.setString("messages.fail-reforge", failMsg); - key.setString("messages.insufficient-funds", insufficientFundsMsg); - key.setString("messages.cooldown-not-expired", cooldownUnexpiredMsg); - key.setInt("delays-in-seconds.minimum", minReforgeDelay); - key.setInt("delays-in-seconds.maximum", maxReforgeDelay); - key.setInt("delays-in-seconds.reforge-cooldown", reforgeCooldown); - key.setInt("percent-chance-to-fail-reforge", failChance); - key.setInt("maximum-enchantments", maxEnchantments); - key.setBoolean("drop-item", dropItem); - } - - public String getInsufficientFundsMessage() { - return insufficientFundsMsg; - } - - 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) - ((Player) npc.getBukkitEntity()).setItemInHand(player.getItemInHand()); - player.setItemInHand(null); - } - - private class ReforgeTask implements Runnable { - private final NPC npc; - private final Player player; - private final ItemStack reforge; - - private ReforgeTask(NPC npc, Player player) { - this.npc = npc; - this.player = player; - reforge = player.getItemInHand(); - } - - @Override - public void run() { - npc.chat(player, reforgeItemInHand() ? successMsg : failMsg); - if (npc.getBukkitEntity() instanceof Player) - ((Player) npc.getBukkitEntity()).setItemInHand(null); - if (dropItem) - player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), reforge); - else { - for (ItemStack stack : player.getInventory().addItem(reforge).values()) - player.getWorld().dropItemNaturally(npc.getBukkitEntity().getLocation(), stack); - } - session = null; - // Start cooldown - Calendar wait = Calendar.getInstance(); - wait.add(Calendar.SECOND, reforgeCooldown); - cooldowns.put(player.getName(), wait); - } - - private boolean reforgeItemInHand() { - Random random = new Random(); - if (random.nextInt(100) < failChance) { - for (Enchantment enchantment : reforge.getEnchantments().keySet()) { - // Remove or downgrade enchantments - if (random.nextBoolean()) - reforge.removeEnchantment(enchantment); - else { - if (reforge.getEnchantmentLevel(enchantment) > 1) { - reforge.removeEnchantment(enchantment); - reforge.addEnchantment(enchantment, 1); - } - } - } - // Damage the item - short durability = (short) (reforge.getDurability() + reforge.getDurability() * random.nextInt(8)); - short maxDurability = reforge.getType().getMaxDurability(); - if (durability <= 0) - durability = (short) (maxDurability / 3); - else if (reforge.getDurability() + durability > maxDurability) - durability = (short) (maxDurability - random.nextInt(maxDurability - 25)); - reforge.setDurability(durability); - return false; - } - // Add random enchantments - int chance = 10; - if (reforge.getDurability() == 0) - chance *= 4; - else - reforge.setDurability((short) 0); - for (int i = 0; i < chance; i++) { - if (reforge.getEnchantments().keySet().size() == maxEnchantments) - break; - Enchantment enchantment = Enchantment.getById(enchantments[random.nextInt(enchantments.length)]); - if (enchantment.canEnchantItem(reforge)) - reforge.addEnchantment(enchantment, random.nextInt(enchantment.getMaxLevel()) + 1); - } - return true; - } - } -} \ No newline at end of file diff --git a/src/net/apunch/blacksmith/BlacksmithPlugin.java b/src/net/apunch/blacksmith/BlacksmithPlugin.java new file mode 100644 index 0000000..063b978 --- /dev/null +++ b/src/net/apunch/blacksmith/BlacksmithPlugin.java @@ -0,0 +1,140 @@ +package net.apunch.blacksmith; + +import java.util.logging.Level; + +import net.apunch.blacksmith.util.Settings; +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 BlacksmithPlugin extends JavaPlugin { + private Settings config; + private Economy economy; + + @Override + public void onDisable() { + config.save(); + + getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled."); + } + + @Override + public void onEnable() { + config = new Settings(this); + config.load(); + + // Setup Vault + RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration( + Economy.class); + if (economyProvider != null) + economy = economyProvider.getProvider(); + + CitizensAPI.getCharacterManager().register(Blacksmith.class); + + getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled."); + } + + public boolean isTool(ItemStack item) { + switch (item.getType()) { + case WOOD_PICKAXE: + case WOOD_SPADE: + case WOOD_HOE: + case WOOD_SWORD: + case WOOD_AXE: + case STONE_PICKAXE: + case STONE_SPADE: + case STONE_HOE: + case STONE_SWORD: + case STONE_AXE: + case GOLD_PICKAXE: + case GOLD_SPADE: + case GOLD_HOE: + case GOLD_SWORD: + case GOLD_AXE: + case IRON_PICKAXE: + case IRON_SPADE: + case IRON_HOE: + case IRON_SWORD: + case IRON_AXE: + case DIAMOND_PICKAXE: + case DIAMOND_SPADE: + case DIAMOND_HOE: + case DIAMOND_SWORD: + case DIAMOND_AXE: + case BOW: + case FLINT_AND_STEEL: + case FISHING_ROD: + case SHEARS: + return true; + default: + return false; + } + } + + public boolean isArmor(ItemStack item) { + switch (item.getType()) { + case LEATHER_HELMET: + case LEATHER_CHESTPLATE: + case LEATHER_LEGGINGS: + case LEATHER_BOOTS: + case CHAINMAIL_HELMET: + case CHAINMAIL_CHESTPLATE: + case CHAINMAIL_LEGGINGS: + case CHAINMAIL_BOOTS: + case GOLD_HELMET: + case GOLD_CHESTPLATE: + case GOLD_LEGGINGS: + case GOLD_BOOTS: + case IRON_HELMET: + case IRON_CHESTPLATE: + case IRON_LEGGINGS: + case IRON_BOOTS: + case DIAMOND_HELMET: + case DIAMOND_CHESTPLATE: + case DIAMOND_LEGGINGS: + case DIAMOND_BOOTS: + return true; + default: + return false; + } + } + + 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('_', '-'))) + price = root.getDouble("base-prices." + item.getType().name().toLowerCase().replace('_', '-')); + + // Adjust price based on durability and enchantments + price += (item.getType().getMaxDurability() - item.getDurability()); + + double enchantmentModifier = Setting.ENCHANTMENT_MODIFIER.asDouble(); + for (Enchantment enchantment : item.getEnchantments().keySet()) { + if (root.keyExists("enchantment-modifiers." + enchantment.getName().toLowerCase().replace('_', '-'))) + enchantmentModifier = root.getDouble("enchantment-modifiers." + + enchantment.getName().toLowerCase().replace('_', '-')); + price += enchantmentModifier * item.getEnchantmentLevel(enchantment); + } + return price; + } +} \ No newline at end of file diff --git a/src/net/apunch/blacksmith/ReforgeSession.java b/src/net/apunch/blacksmith/ReforgeSession.java index 5c1afd0..d8c5f76 100644 --- a/src/net/apunch/blacksmith/ReforgeSession.java +++ b/src/net/apunch/blacksmith/ReforgeSession.java @@ -6,7 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public class ReforgeSession { - private final Blacksmith plugin; + private final BlacksmithPlugin plugin; private final Player player; private final ItemStack reforge; private final NPC npc; @@ -17,7 +17,7 @@ public class ReforgeSession { reforge = player.getItemInHand(); this.npc = npc; - plugin = (Blacksmith) player.getServer().getPluginManager().getPlugin("Blacksmith"); + plugin = (BlacksmithPlugin) player.getServer().getPluginManager().getPlugin("Blacksmith"); } // Return is the session should end @@ -28,7 +28,7 @@ public class ReforgeSession { return true; } if (!plugin.doesPlayerHaveEnough(player)) { - npc.chat(player, ((BlacksmithCharacter) npc.getCharacter()).getInsufficientFundsMessage()); + npc.chat(player, ((Blacksmith) npc.getCharacter()).getInsufficientFundsMessage()); return true; } return false; diff --git a/src/net/apunch/blacksmith/util/Settings.java b/src/net/apunch/blacksmith/util/Settings.java index 774c619..b8fade0 100644 --- a/src/net/apunch/blacksmith/util/Settings.java +++ b/src/net/apunch/blacksmith/util/Settings.java @@ -2,7 +2,7 @@ package net.apunch.blacksmith.util; import java.io.File; -import net.apunch.blacksmith.Blacksmith; +import net.apunch.blacksmith.BlacksmithPlugin; import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.YamlStorage; @@ -10,7 +10,7 @@ import net.citizensnpcs.api.util.YamlStorage; public class Settings { private final YamlStorage config; - public Settings(Blacksmith plugin) { + public Settings(BlacksmithPlugin plugin) { config = new YamlStorage(plugin.getDataFolder() + File.separator + "config.yml", "Blacksmith Configuration"); }