From f3074461ed088a088fe15debd0a59e7c71b8bc18 Mon Sep 17 00:00:00 2001 From: GJ Date: Fri, 18 May 2012 14:29:53 -0400 Subject: [PATCH] More cleanup. --- .../config/mods/CustomArmorConfig.java | 7 +- .../config/mods/CustomToolsConfig.java | 8 +- .../nossr50/datatypes/PlayerProfile.java | 7 +- .../nossr50/listeners/BlockListener.java | 8 +- .../nossr50/listeners/PlayerListener.java | 4 +- .../com/gmail/nossr50/skills/misc/Repair.java | 77 ++++++------------- .../java/com/gmail/nossr50/util/Combat.java | 6 +- .../com/gmail/nossr50/util/ItemChecks.java | 30 -------- .../java/com/gmail/nossr50/util/Misc.java | 2 +- .../com/gmail/nossr50/util/ModChecks.java | 67 +++++++++------- 10 files changed, 87 insertions(+), 129 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java b/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java index cb7600384..5a43acf59 100644 --- a/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java +++ b/src/main/java/com/gmail/nossr50/config/mods/CustomArmorConfig.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.config.mods; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -28,7 +29,8 @@ public class CustomArmorConfig extends ModConfigLoader{ public List customLeggingIDs = new ArrayList(); public List customIDs = new ArrayList(); - public List customItems = new ArrayList(); + public List customArmorList = new ArrayList(); + public HashMap customArmor = new HashMap(); public CustomArmorConfig(mcMMO plugin) { super(plugin, "armor.yml"); @@ -93,7 +95,8 @@ public class CustomArmorConfig extends ModConfigLoader{ idList.add(id); customIDs.add(id); - customItems.add(armor); + customArmorList.add(armor); + customArmor.put(id, armor); } } } diff --git a/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java b/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java index e0afdfab5..5db742e99 100644 --- a/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java +++ b/src/main/java/com/gmail/nossr50/config/mods/CustomToolsConfig.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.config.mods; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Set; @@ -9,7 +10,6 @@ import org.bukkit.configuration.ConfigurationSection; import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.datatypes.mods.CustomItem; import com.gmail.nossr50.datatypes.mods.CustomTool; public class CustomToolsConfig extends ModConfigLoader { @@ -31,7 +31,8 @@ public class CustomToolsConfig extends ModConfigLoader { public List customSwordIDs = new ArrayList(); public List customIDs = new ArrayList(); - public List customItems = new ArrayList(); + public List customToolList = new ArrayList(); + public HashMap customTools = new HashMap(); private CustomToolsConfig(mcMMO plugin) { super(plugin, "tools.yml"); @@ -101,7 +102,8 @@ public class CustomToolsConfig extends ModConfigLoader { idList.add(id); customIDs.add(id); - customItems.add(tool); + customToolList.add(tool); + customTools.put(id, tool); } } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java index 30f849539..03ed587f0 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerProfile.java @@ -12,9 +12,9 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.datatypes.mods.CustomTool; import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; import com.gmail.nossr50.party.Party; -import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.ModChecks; import com.gmail.nossr50.util.Users; @@ -1032,9 +1032,10 @@ public class PlayerProfile { if (Config.getInstance().getToolModsEnabled()) { ItemStack item = player.getItemInHand(); + CustomTool tool = ModChecks.getToolFromItemStack(item); - if (ItemChecks.isCustomTool(item)) { - xp = (int) (xp * ModChecks.getToolFromItemStack(item).getXpMultiplier()); + if (tool != null) { + xp = (int) (xp * tool.getXpMultiplier()); } } diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 6a9ea88f7..ecb979b50 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -181,7 +181,7 @@ public class BlockListener implements Listener { } if (PP.getAbilityMode(AbilityType.TREE_FELLER) && permInstance.treeFeller(player) && ItemChecks.isAxe(inHand)) { - if (ItemChecks.isCustomTool(inHand)) { + if (ModChecks.isCustomTool(inHand)) { if (ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { WoodCutting.treeFeller(event); } @@ -255,7 +255,7 @@ public class BlockListener implements Listener { * ABILITY TRIGGER CHECKS */ if (PP.getAbilityMode(AbilityType.GREEN_TERRA) && permInstance.greenTerra(player) && BlockChecks.makeMossy(block)) { - if (ItemChecks.isCustomTool(inHand)) { + if (ModChecks.isCustomTool(inHand)) { if (ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { Herbalism.greenTerra(player, block); } @@ -267,7 +267,7 @@ public class BlockListener implements Listener { else if (PP.getAbilityMode(AbilityType.GIGA_DRILL_BREAKER) && Skills.triggerCheck(player, block, AbilityType.GIGA_DRILL_BREAKER)) { if (configInstance.getExcavationRequiresTool()) { if (ItemChecks.isShovel(inHand)) { - if (ItemChecks.isCustomTool(inHand)) { + if (ModChecks.isCustomTool(inHand)) { if (ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { event.setInstaBreak(true); Excavation.gigaDrillBreaker(player, block); @@ -299,7 +299,7 @@ public class BlockListener implements Listener { else if (PP.getAbilityMode(AbilityType.SUPER_BREAKER) && Skills.triggerCheck(player, block, AbilityType.SUPER_BREAKER)) { if (configInstance.getMiningRequiresTool()) { if (ItemChecks.isPickaxe(inHand)) { - if (ItemChecks.isCustomTool(inHand)) { + if (ModChecks.isCustomTool(inHand)) { if (ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { event.setInstaBreak(true); Mining.superBreakerBlockCheck(player, block); diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index f356bdc38..0b7821a02 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -209,12 +209,12 @@ public class PlayerListener implements Listener { event.setCancelled(true); player.updateInventory(); } - else if (ItemChecks.isCustomTool(is) && ModChecks.getToolFromItemStack(is).isRepairable()) { + else if (ModChecks.isCustomTool(is) && ModChecks.getToolFromItemStack(is).isRepairable()) { Repair.repairCheck(player, is); event.setCancelled(true); player.updateInventory(); } - else if (ItemChecks.isCustomArmor(is) && ModChecks.getArmorFromItemStack(is).isRepairable()) { + else if (ModChecks.isCustomArmor(is) && ModChecks.getArmorFromItemStack(is).isRepairable()) { Repair.repairCheck(player, is); event.setCancelled(true); player.updateInventory(); diff --git a/src/main/java/com/gmail/nossr50/skills/misc/Repair.java b/src/main/java/com/gmail/nossr50/skills/misc/Repair.java index 7e1018835..a72083404 100644 --- a/src/main/java/com/gmail/nossr50/skills/misc/Repair.java +++ b/src/main/java/com/gmail/nossr50/skills/misc/Repair.java @@ -16,8 +16,6 @@ import org.getspout.spoutapi.player.SpoutPlayer; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; -import com.gmail.nossr50.config.mods.CustomArmorConfig; -import com.gmail.nossr50.config.mods.CustomToolsConfig; import com.gmail.nossr50.spout.SpoutSounds; import com.gmail.nossr50.util.ItemChecks; import com.gmail.nossr50.util.Misc; @@ -28,6 +26,7 @@ import com.gmail.nossr50.util.Users; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.mods.CustomItem; +import com.gmail.nossr50.datatypes.mods.CustomTool; import com.gmail.nossr50.events.skills.McMMOPlayerRepairCheckEvent; import com.gmail.nossr50.locale.LocaleLoader; @@ -112,46 +111,30 @@ public class Repair { /* * REPAIR CUSTOM TOOLS */ - else if (ItemChecks.isCustomTool(is) && permInstance.toolRepair(player)) { - CustomToolsConfig toolsInstance = CustomToolsConfig.getInstance(); + else if (ModChecks.isCustomTool(is) && permInstance.toolRepair(player)) { + ItemStack repairMaterial = ModChecks.getToolFromItemStack(is).getRepairMaterial(); - for (CustomItem tool : toolsInstance.customItems) { - if (tool.getItemID() == is.getTypeId()) { - ItemStack repairMaterial = tool.getRepairMaterial(); - - if (inventory.contains(repairMaterial)) { - repairCustomItem(player, is, repairMaterial); - xpHandler(player, PP, is, durabilityBefore, 1); - } - else { - needMoreVespeneGas(is, player); - } - - break; - } + if (inventory.contains(repairMaterial)) { + repairCustomItem(player, is, repairMaterial); + xpHandler(player, PP, is, durabilityBefore, 1); + } + else { + needMoreVespeneGas(is, player); } } /* * REPAIR CUSTOM ARMOR */ - else if (ItemChecks.isCustomArmor(is) && permInstance.armorRepair(player)) { - CustomArmorConfig armorInstance = CustomArmorConfig.getInstance(); + else if (ModChecks.isCustomArmor(is) && permInstance.armorRepair(player)) { + ItemStack repairMaterial = ModChecks.getArmorFromItemStack(is).getRepairMaterial(); - for (CustomItem armor : armorInstance.customItems) { - if (armor.getItemID() == is.getTypeId()) { - ItemStack repairMaterial = armor.getRepairMaterial(); - - if (inventory.contains(repairMaterial)) { - repairCustomItem(player, is, repairMaterial); - xpHandler(player, PP, is, durabilityBefore, 1); - } - else { - needMoreVespeneGas(is, player); - } - - break; - } + if (inventory.contains(repairMaterial)) { + repairCustomItem(player, is, repairMaterial); + xpHandler(player, PP, is, durabilityBefore, 1); + } + else { + needMoreVespeneGas(is, player); } } } @@ -406,26 +389,16 @@ public class Repair { int materialsRequired = 0; int repairAmount = 0; - CustomToolsConfig toolInstance = CustomToolsConfig.getInstance(); - CustomArmorConfig armorInstance = CustomArmorConfig.getInstance(); + CustomTool tool = ModChecks.getToolFromItemStack(is); + CustomItem armor = ModChecks.getArmorFromItemStack(is); - if (ModChecks.getToolFromItemStack(is) != null) { - for (CustomItem tool : toolInstance.customItems) { - if (tool.getItemID() == is.getTypeId()) { - maxDurability = tool.getDurability(); - materialsRequired = tool.getRepairQuantity(); - break; - } - } + if (tool != null) { + maxDurability = tool.getDurability(); + materialsRequired = tool.getRepairQuantity(); } - else if (ModChecks.getArmorFromItemStack(is) != null) { - for (CustomItem armor : armorInstance.customItems) { - if (armor.getItemID() == is.getTypeId()) { - maxDurability = armor.getDurability(); - materialsRequired = armor.getRepairQuantity(); - break; - } - } + else if (armor != null) { + maxDurability = armor.getDurability(); + materialsRequired = armor.getRepairQuantity(); } repairAmount = maxDurability / materialsRequired; diff --git a/src/main/java/com/gmail/nossr50/util/Combat.java b/src/main/java/com/gmail/nossr50/util/Combat.java index 67852e4e4..1271394d7 100644 --- a/src/main/java/com/gmail/nossr50/util/Combat.java +++ b/src/main/java/com/gmail/nossr50/util/Combat.java @@ -368,10 +368,8 @@ public class Combat { private static void applyAbilityAoE(Player attacker, LivingEntity target, int damage, mcMMO plugin, SkillType type) { ItemStack inHand = attacker.getItemInHand(); - if (Config.getInstance().getToolModsEnabled()) { - if (ItemChecks.isCustomTool(inHand) && !ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { - return; - } + if (ModChecks.isCustomTool(inHand) && !ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) { + return; } int numberOfTargets = Misc.getTier(attacker.getItemInHand()); //The higher the weapon tier, the more targets you hit diff --git a/src/main/java/com/gmail/nossr50/util/ItemChecks.java b/src/main/java/com/gmail/nossr50/util/ItemChecks.java index 722b86cb6..54735af64 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemChecks.java +++ b/src/main/java/com/gmail/nossr50/util/ItemChecks.java @@ -242,21 +242,6 @@ public class ItemChecks { return isLeatherArmor(is) || isGoldArmor(is) || isIronArmor(is) || isDiamondArmor(is); } - /** - * Checks to see if an item is custom armor. - * - * @param is Item to check - * @return true if the item is custom armor, false otherwise - */ - public static boolean isCustomArmor(ItemStack is) { - if (customArmorEnabled && CustomArmorConfig.getInstance().customIDs.contains(is.getTypeId())) { - return true; - } - else { - return false; - } - } - /** * Checks to see if an item is a leather armor piece. * @@ -343,21 +328,6 @@ public class ItemChecks { return isStoneTool(is) || isWoodTool(is) || isGoldTool(is) || isIronTool(is) || isDiamondTool(is) || isStringTool(is); } - /** - * Checks to see if an item is a custom tool. - * - * @param is Item to check - * @return true if the item is a custom tool, false otherwise - */ - public static boolean isCustomTool(ItemStack is) { - if (customToolsEnabled && CustomToolsConfig.getInstance().customIDs.contains(is.getTypeId())) { - return true; - } - else { - return false; - } - } - /** * Checks to see if an item is a stone tool. * diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 62453a144..a7103760b 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -146,7 +146,7 @@ public class Misc { else if (ItemChecks.isDiamondTool(inHand)) { tier = 4; } - else if (ItemChecks.isCustomTool(inHand)) { + else if (ModChecks.isCustomTool(inHand)) { tier = ModChecks.getToolFromItemStack(inHand).getTier(); } diff --git a/src/main/java/com/gmail/nossr50/util/ModChecks.java b/src/main/java/com/gmail/nossr50/util/ModChecks.java index bdd497771..bfed23e2c 100644 --- a/src/main/java/com/gmail/nossr50/util/ModChecks.java +++ b/src/main/java/com/gmail/nossr50/util/ModChecks.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.util; import org.bukkit.block.Block; import org.bukkit.inventory.ItemStack; +import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.mods.CustomBlocksConfig; import com.gmail.nossr50.config.mods.CustomArmorConfig; import com.gmail.nossr50.config.mods.CustomToolsConfig; @@ -11,6 +12,10 @@ import com.gmail.nossr50.datatypes.mods.CustomItem; import com.gmail.nossr50.datatypes.mods.CustomTool; public class ModChecks { + private static Config configInstance = Config.getInstance(); + private static boolean customToolsEnabled = configInstance.getToolModsEnabled(); + private static boolean customArmorEnabled = configInstance.getArmorModsEnabled(); + private static CustomToolsConfig toolInstance = CustomToolsConfig.getInstance(); private static CustomArmorConfig armorInstance = CustomArmorConfig.getInstance(); private static CustomBlocksConfig blocksInstance = CustomBlocksConfig.getInstance(); @@ -19,44 +24,20 @@ public class ModChecks { * Get the custom armor associated with an item. * * @param item The item to check - * @return the ay if it exists, null otherwise + * @return the armor if it exists, null otherwise */ public static CustomItem getArmorFromItemStack(ItemStack item) { - int id = item.getTypeId(); - - if (!armorInstance.customIDs.contains(id)) { - return null; - } - - for (CustomItem armor : armorInstance.customItems) { - if (armor.getItemID() == id) { - return armor; - } - } - - return null; + return armorInstance.customArmor.get(item.getTypeId()); } /** * Get the custom tool associated with an item. * * @param item The item to check - * @return the armor if it exists, null otherwise + * @return the tool if it exists, null otherwise */ public static CustomTool getToolFromItemStack(ItemStack item) { - int id = item.getTypeId(); - - if (!toolInstance.customIDs.contains(id)) { - return null; - } - - for (CustomItem tool : toolInstance.customItems) { - if (tool.getItemID() == id) { - return (CustomTool) tool; - } - } - - return null; + return toolInstance.customTools.get(item.getTypeId()); } /** @@ -123,4 +104,34 @@ public class ModChecks { return false; } } + + /** + * Checks to see if an item is a custom tool. + * + * @param is Item to check + * @return true if the item is a custom tool, false otherwise + */ + public static boolean isCustomTool(ItemStack item) { + if (customToolsEnabled && toolInstance.customTools.containsKey(item.getTypeId())) { + return true; + } + else { + return false; + } + } + + /** + * Checks to see if an item is custom armor. + * + * @param is Item to check + * @return true if the item is custom armor, false otherwise + */ + public static boolean isCustomArmor(ItemStack item) { + if (customArmorEnabled && armorInstance.customArmor.containsKey(item.getTypeId())) { + return true; + } + else { + return false; + } + } }