From a9527f76779a08f3a4c1e0bf85368f174c35ee4c Mon Sep 17 00:00:00 2001 From: aPunch Date: Fri, 24 Feb 2012 05:58:06 -0600 Subject: [PATCH] add max-enchantments setting; --- .../blacksmith/BlacksmithCharacter.java | 20 ++++++++++++++----- src/net/apunch/blacksmith/util/Settings.java | 1 + 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/net/apunch/blacksmith/BlacksmithCharacter.java b/src/net/apunch/blacksmith/BlacksmithCharacter.java index df531fc..b65a28a 100644 --- a/src/net/apunch/blacksmith/BlacksmithCharacter.java +++ b/src/net/apunch/blacksmith/BlacksmithCharacter.java @@ -22,6 +22,8 @@ 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(); @@ -41,10 +43,14 @@ public class BlacksmithCharacter extends Character { 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 @@ -80,6 +86,8 @@ public class BlacksmithCharacter extends Character { 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"); } @@ -143,6 +151,7 @@ public class BlacksmithCharacter extends Character { 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); } @@ -213,17 +222,18 @@ public class BlacksmithCharacter extends Character { reforge.setDurability(durability); return false; } + // Add random enchantments int chance = 10; if (reforge.getDurability() == 0) chance *= 4; else reforge.setDurability((short) 0); - // Add random enchantments for (int i = 0; i < chance; i++) { - int id = random.nextInt(100); - Enchantment enchantment = Enchantment.getById(id); - if (enchantment != null && enchantment.canEnchantItem(reforge)) - reforge.addEnchantment(Enchantment.getById(id), random.nextInt(enchantment.getMaxLevel()) + 1); + 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; } diff --git a/src/net/apunch/blacksmith/util/Settings.java b/src/net/apunch/blacksmith/util/Settings.java index b2cf283..774c619 100644 --- a/src/net/apunch/blacksmith/util/Settings.java +++ b/src/net/apunch/blacksmith/util/Settings.java @@ -45,6 +45,7 @@ public class Settings { FAIL_MESSAGE("defaults.messages.fail-reforge", "Whoops! Didn't mean to do that! Maybe next time?"), INSUFFICIENT_FUNDS_MESSAGE("defaults.messages.insufficient-funds", "You don't have enough money to reforge that item!"), INVALID_ITEM_MESSAGE("defaults.messages.invalid-item", "I'm sorry, but I don't know how to reforge that!"), + MAX_ENCHANTMENTS("defaults.maximum-enchantments", 3), MAX_REFORGE_DELAY("defaults.delays-in-seconds.maximum", 30), MIN_REFORGE_DELAY("defaults.delays-in-seconds.minimum", 5), REFORGE_COOLDOWN("defaults.delays-in-seconds.reforge-cooldown", 60),