From b18d81836d85ce225f0391fea6ffe44b03dc7da0 Mon Sep 17 00:00:00 2001 From: ile123ile Date: Sun, 31 Aug 2014 17:42:23 -0700 Subject: [PATCH] Turned ToolType to public static final over enum --- .../nossr50/datatypes/player/McMMOPlayer.java | 9 +- .../nossr50/datatypes/skills/SkillType.java | 14 +-- .../nossr50/datatypes/skills/ToolType.java | 110 +++++++++++++----- .../nossr50/listeners/BlockListener.java | 10 +- .../nossr50/skills/axes/AxesManager.java | 2 +- .../skills/herbalism/HerbalismManager.java | 2 +- .../nossr50/skills/swords/SwordsManager.java | 2 +- .../skills/unarmed/UnarmedManager.java | 2 +- 8 files changed, 99 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java index 6fe77bb4e..9a2eb37fb 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java +++ b/src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java @@ -105,11 +105,6 @@ public class McMMOPlayer { profile.setUniqueId(uuid); } - /* - * I'm using this method because it makes code shorter and safer (we don't have to add all SkillTypes manually), - * but I actually have no idea about the performance impact, if there is any. - * If in the future someone wants to remove this, don't forget to also remove what is in the SkillType enum. - bm01 - */ try { for (SkillType skillType : SkillType.getSkillList()) { skillManagers.put(skillType, skillType.getManagerClass().getConstructor(McMMOPlayer.class).newInstance(this)); @@ -125,7 +120,7 @@ public class McMMOPlayer { abilityInformed.put(abilityType, true); // This is intended } - for (ToolType toolType : ToolType.values()) { + for (ToolType toolType : ToolType.getToolList()) { toolMode.put(toolType, false); } } @@ -274,7 +269,7 @@ public class McMMOPlayer { * Reset the prep modes of all tools. */ public void resetToolPrepMode() { - for (ToolType tool : ToolType.values()) { + for (ToolType tool : ToolType.getToolList()) { setToolPreparationMode(tool, false); } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/skills/SkillType.java b/src/main/java/com/gmail/nossr50/datatypes/skills/SkillType.java index 29b145b3b..da5f4af74 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/skills/SkillType.java +++ b/src/main/java/com/gmail/nossr50/datatypes/skills/SkillType.java @@ -72,18 +72,18 @@ public class SkillType { public static final SkillType acrobatics = createSkill("ACROBATICS" , AcrobaticsManager.class , AcrobaticsCommand.class , false, Color.WHITE, SkillUseType.MISC, ImmutableList.of(SecondaryAbility.dodge, SecondaryAbility.gracefullRoll, SecondaryAbility.roll)); public static final SkillType alchemy = createSkill("ALCHEMY" , AlchemyManager.class , AlchemyCommand.class , false, Color.FUCHSIA, SkillUseType.MISC, ImmutableList.of(SecondaryAbility.catalysis, SecondaryAbility.concoctions)); public static final SkillType archery = createSkill("ARCHERY" , ArcheryManager.class , ArcheryCommand.class , false, Color.MAROON, SkillUseType.COMBAT, ImmutableList.of(SecondaryAbility.daze, SecondaryAbility.retrieve, SecondaryAbility.skillShot)); - public static final SkillType axes = createSkill("AXES" , AxesManager.class , AxesCommand.class , false, Color.AQUA, SkillUseType.COMBAT, AbilityType.skullSplitter, ToolType.AXE, ImmutableList.of(SecondaryAbility.armorImpact, SecondaryAbility.axeMastery, SecondaryAbility.criticalHit, SecondaryAbility.greaterImpact)); - public static final SkillType excavation = createSkill("EXCAVATION" , ExcavationManager.class , ExcavationCommand.class , false, Color.fromRGB(139, 69, 19), SkillUseType.GATHERING, AbilityType.gigaDrillBreaker, ToolType.SHOVEL, ImmutableList.of(SecondaryAbility.excavationTreasureHunter)); + public static final SkillType axes = createSkill("AXES" , AxesManager.class , AxesCommand.class , false, Color.AQUA, SkillUseType.COMBAT, AbilityType.skullSplitter, ToolType.axe, ImmutableList.of(SecondaryAbility.armorImpact, SecondaryAbility.axeMastery, SecondaryAbility.criticalHit, SecondaryAbility.greaterImpact)); + public static final SkillType excavation = createSkill("EXCAVATION" , ExcavationManager.class , ExcavationCommand.class , false, Color.fromRGB(139, 69, 19), SkillUseType.GATHERING, AbilityType.gigaDrillBreaker, ToolType.shovel, ImmutableList.of(SecondaryAbility.excavationTreasureHunter)); public static final SkillType fishing = createSkill("FISHING" , FishingManager.class , FishingCommand.class , false, Color.NAVY, SkillUseType.GATHERING, ImmutableList.of(SecondaryAbility.fishermansDiet, SecondaryAbility.fishingTreasureHunter, SecondaryAbility.iceFishing, SecondaryAbility.magicHunter, SecondaryAbility.masterAngler, SecondaryAbility.shake)); - public static final SkillType herbalism = createSkill("HERBALISM" , HerbalismManager.class , HerbalismCommand.class , false, Color.GREEN, SkillUseType.GATHERING, AbilityType.greenTerra, ToolType.HOE, ImmutableList.of(SecondaryAbility.farmersDiet, SecondaryAbility.greenThumbPlant, SecondaryAbility.greenThumbBlock, SecondaryAbility.herbalismDoubleDrops, SecondaryAbility.hylianLuck, SecondaryAbility.shroomThumb)); - public static final SkillType mining = createSkill("MINING" , MiningManager.class , MiningCommand.class , false, Color.GRAY, SkillUseType.GATHERING, AbilityType.superBreaker, ToolType.PICKAXE, ImmutableList.of(SecondaryAbility.miningDoubleDrops)); + public static final SkillType herbalism = createSkill("HERBALISM" , HerbalismManager.class , HerbalismCommand.class , false, Color.GREEN, SkillUseType.GATHERING, AbilityType.greenTerra, ToolType.hoe, ImmutableList.of(SecondaryAbility.farmersDiet, SecondaryAbility.greenThumbPlant, SecondaryAbility.greenThumbBlock, SecondaryAbility.herbalismDoubleDrops, SecondaryAbility.hylianLuck, SecondaryAbility.shroomThumb)); + public static final SkillType mining = createSkill("MINING" , MiningManager.class , MiningCommand.class , false, Color.GRAY, SkillUseType.GATHERING, AbilityType.superBreaker, ToolType.pickaxe, ImmutableList.of(SecondaryAbility.miningDoubleDrops)); public static final SkillType repair = createSkill("REPAIR" , RepairManager.class , RepairCommand.class , false, Color.SILVER, SkillUseType.MISC, ImmutableList.of(SecondaryAbility.arcaneForging, SecondaryAbility.repairMastery, SecondaryAbility.superRepair)); public static final SkillType salvage = createSkill("SALVAGE" , SalvageManager.class , SalvageCommand.class , true, Color.ORANGE, SkillUseType.MISC, ImmutableList.of(SecondaryAbility.advancedSalvage, SecondaryAbility.arcaneSalvage)); public static final SkillType smelting = createSkill("SMELTING" , SmeltingManager.class , SmeltingCommand.class , true, Color.YELLOW, SkillUseType.MISC, ImmutableList.of(SecondaryAbility.fluxMining, SecondaryAbility.fuelEfficiency, SecondaryAbility.secondSmelt)); - public static final SkillType swords = createSkill("SWORDS" , SwordsManager.class , SwordsCommand.class , false, Color.fromRGB(178, 34, 34), SkillUseType.COMBAT, AbilityType.serratedStrikes, ToolType.SWORD, ImmutableList.of(SecondaryAbility.bleed, SecondaryAbility.counter)); + public static final SkillType swords = createSkill("SWORDS" , SwordsManager.class , SwordsCommand.class , false, Color.fromRGB(178, 34, 34), SkillUseType.COMBAT, AbilityType.serratedStrikes, ToolType.sword, ImmutableList.of(SecondaryAbility.bleed, SecondaryAbility.counter)); public static final SkillType taming = createSkill("TAMING" , TamingManager.class , TamingCommand.class , false, Color.PURPLE, SkillUseType.COMBAT, ImmutableList.of(SecondaryAbility.beastLore, SecondaryAbility.callOfTheWild, SecondaryAbility.enviromentallyAware, SecondaryAbility.fastFood, SecondaryAbility.gore, SecondaryAbility.holyHound, SecondaryAbility.sharpenedClaws, SecondaryAbility.shockProof, SecondaryAbility.thickFur)); - public static final SkillType unarmed = createSkill("UNARMED" , UnarmedManager.class , UnarmedCommand.class , false, Color.BLACK, SkillUseType.COMBAT, AbilityType.berserk, ToolType.FISTS, ImmutableList.of(SecondaryAbility.blockCracker, SecondaryAbility.deflect, SecondaryAbility.disarm, SecondaryAbility.ironArm, SecondaryAbility.ironGrip)); - public static final SkillType woodcutting = createSkill("WOODCUTTING", WoodcuttingManager.class , WoodcuttingCommand.class , false, Color.OLIVE, SkillUseType.GATHERING, AbilityType.treeFeller, ToolType.AXE, ImmutableList.of(SecondaryAbility.leafBlower, SecondaryAbility.woodcuttingDoubleDrops)); + public static final SkillType unarmed = createSkill("UNARMED" , UnarmedManager.class , UnarmedCommand.class , false, Color.BLACK, SkillUseType.COMBAT, AbilityType.berserk, ToolType.fists, ImmutableList.of(SecondaryAbility.blockCracker, SecondaryAbility.deflect, SecondaryAbility.disarm, SecondaryAbility.ironArm, SecondaryAbility.ironGrip)); + public static final SkillType woodcutting = createSkill("WOODCUTTING", WoodcuttingManager.class , WoodcuttingCommand.class , false, Color.OLIVE, SkillUseType.GATHERING, AbilityType.treeFeller, ToolType.axe, ImmutableList.of(SecondaryAbility.leafBlower, SecondaryAbility.woodcuttingDoubleDrops)); private String name; diff --git a/src/main/java/com/gmail/nossr50/datatypes/skills/ToolType.java b/src/main/java/com/gmail/nossr50/datatypes/skills/ToolType.java index 666acae32..60daf8468 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/skills/ToolType.java +++ b/src/main/java/com/gmail/nossr50/datatypes/skills/ToolType.java @@ -1,18 +1,56 @@ package com.gmail.nossr50.datatypes.skills; +import java.util.ArrayList; +import java.util.List; + import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.util.ItemUtils; -public enum ToolType { - AXE(LocaleLoader.getString("Axes.Ability.Lower"), LocaleLoader.getString("Axes.Ability.Ready")), - FISTS(LocaleLoader.getString("Unarmed.Ability.Lower"), LocaleLoader.getString("Unarmed.Ability.Ready")), - HOE(LocaleLoader.getString("Herbalism.Ability.Lower"), LocaleLoader.getString("Herbalism.Ability.Ready")), - PICKAXE(LocaleLoader.getString("Mining.Ability.Lower"), LocaleLoader.getString("Mining.Ability.Ready")), - SHOVEL(LocaleLoader.getString("Excavation.Ability.Lower"), LocaleLoader.getString("Excavation.Ability.Ready")), - SWORD(LocaleLoader.getString("Swords.Ability.Lower"), LocaleLoader.getString("Swords.Ability.Ready")); +public class ToolType { + private static List tools = new ArrayList(); + + public static final ToolType axe = new ToolType("Axes") { + @Override + public boolean inHand(ItemStack itemStack) { + return ItemUtils.isAxe(itemStack); + } + }; + public static final ToolType fists = createToolType("Unarmed", Material.AIR); + public static final ToolType hoe = new ToolType("Herbalism") { + @Override + public boolean inHand(ItemStack itemStack) { + return ItemUtils.isHoe(itemStack); + } + }; + public static final ToolType pickaxe = new ToolType("Mining") { + @Override + public boolean inHand(ItemStack itemStack) { + return ItemUtils.isPickaxe(itemStack); + } + }; + public static final ToolType shovel = new ToolType("Excavation") { + @Override + public boolean inHand(ItemStack itemStack) { + return ItemUtils.isShovel(itemStack); + } + }; + public static final ToolType sword = new ToolType("Swords") { + @Override + public boolean inHand(ItemStack itemStack) { + return ItemUtils.isSword(itemStack); + } + }; + + static { + tools.add(axe); + tools.add(hoe); + tools.add(pickaxe); + tools.add(shovel); + tools.add(sword); + } private String lowerTool; private String raiseTool; @@ -21,6 +59,10 @@ public enum ToolType { this.lowerTool = lowerTool; this.raiseTool = raiseTool; } + + private ToolType(String toolName) { + this(LocaleLoader.getString(toolName + ".Ability.Lower"), LocaleLoader.getString(toolName + ".Ability.Ready")); + } public String getLowerTool() { return lowerTool; @@ -30,6 +72,33 @@ public enum ToolType { return raiseTool; } + public static ToolType createToolType(String toolName, final Material material) { + ToolType tool = new ToolType(toolName) { + @Override + public boolean inHand(ItemStack itemStack) { + return itemStack.getType() == material; + } + }; + tools.add(tool); + return tool; + } + + public static ToolType createToolType(String toolName, final Material... materials) { + ToolType tool = new ToolType(toolName) { + @Override + public boolean inHand(ItemStack itemStack) { + for(Material material : materials) { + if(itemStack.getType() == material) { + return true; + } + } + return false; + } + }; + tools.add(tool); + return tool; + } + /** * Check to see if the item is of the appropriate type. * @@ -37,27 +106,10 @@ public enum ToolType { * @return true if the item is the right type, false otherwise */ public boolean inHand(ItemStack itemStack) { - switch (this) { - case AXE: - return ItemUtils.isAxe(itemStack); - - case FISTS: - return itemStack.getType() == Material.AIR; - - case HOE: - return ItemUtils.isHoe(itemStack); - - case PICKAXE: - return ItemUtils.isPickaxe(itemStack); - - case SHOVEL: - return ItemUtils.isShovel(itemStack); - - case SWORD: - return ItemUtils.isSword(itemStack); - - default: - return false; - } + return false; } + + public static List getToolList() { + return tools; + } } diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 48632b198..1fb2314f2 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -311,19 +311,19 @@ public class BlockListener implements Listener { } } - if (mcMMOPlayer.getToolPreparationMode(ToolType.HOE) && ItemUtils.isHoe(heldItem) && (BlockUtils.affectedByGreenTerra(blockState) || BlockUtils.canMakeMossy(blockState)) && Permissions.greenTerra(player)) { + if (mcMMOPlayer.getToolPreparationMode(ToolType.hoe) && ItemUtils.isHoe(heldItem) && (BlockUtils.affectedByGreenTerra(blockState) || BlockUtils.canMakeMossy(blockState)) && Permissions.greenTerra(player)) { mcMMOPlayer.checkAbilityActivation(SkillType.herbalism); } - else if (mcMMOPlayer.getToolPreparationMode(ToolType.AXE) && ItemUtils.isAxe(heldItem) && BlockUtils.isLog(blockState) && Permissions.treeFeller(player)) { + else if (mcMMOPlayer.getToolPreparationMode(ToolType.axe) && ItemUtils.isAxe(heldItem) && BlockUtils.isLog(blockState) && Permissions.treeFeller(player)) { mcMMOPlayer.checkAbilityActivation(SkillType.woodcutting); } - else if (mcMMOPlayer.getToolPreparationMode(ToolType.PICKAXE) && ItemUtils.isPickaxe(heldItem) && BlockUtils.affectedBySuperBreaker(blockState) && Permissions.superBreaker(player)) { + else if (mcMMOPlayer.getToolPreparationMode(ToolType.pickaxe) && ItemUtils.isPickaxe(heldItem) && BlockUtils.affectedBySuperBreaker(blockState) && Permissions.superBreaker(player)) { mcMMOPlayer.checkAbilityActivation(SkillType.mining); } - else if (mcMMOPlayer.getToolPreparationMode(ToolType.SHOVEL) && ItemUtils.isShovel(heldItem) && BlockUtils.affectedByGigaDrillBreaker(blockState) && Permissions.gigaDrillBreaker(player)) { + else if (mcMMOPlayer.getToolPreparationMode(ToolType.shovel) && ItemUtils.isShovel(heldItem) && BlockUtils.affectedByGigaDrillBreaker(blockState) && Permissions.gigaDrillBreaker(player)) { mcMMOPlayer.checkAbilityActivation(SkillType.excavation); } - else if (mcMMOPlayer.getToolPreparationMode(ToolType.FISTS) && heldItem.getType() == Material.AIR && (BlockUtils.affectedByGigaDrillBreaker(blockState) || blockState.getType() == Material.SNOW || BlockUtils.affectedByBlockCracker(blockState) && Permissions.berserk(player))) { + else if (mcMMOPlayer.getToolPreparationMode(ToolType.fists) && heldItem.getType() == Material.AIR && (BlockUtils.affectedByGigaDrillBreaker(blockState) || blockState.getType() == Material.SNOW || BlockUtils.affectedByBlockCracker(blockState) && Permissions.berserk(player))) { mcMMOPlayer.checkAbilityActivation(SkillType.unarmed); } } diff --git a/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java b/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java index 50c8e02af..0780f6ec6 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java @@ -47,7 +47,7 @@ public class AxesManager extends SkillManager { } public boolean canActivateAbility() { - return mcMMOPlayer.getToolPreparationMode(ToolType.AXE) && Permissions.skullSplitter(getPlayer()); + return mcMMOPlayer.getToolPreparationMode(ToolType.axe) && Permissions.skullSplitter(getPlayer()); } /** diff --git a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java index 52ff6fad9..4fdeea4e6 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java @@ -69,7 +69,7 @@ public class HerbalismManager extends SkillManager { } public boolean canActivateAbility() { - return mcMMOPlayer.getToolPreparationMode(ToolType.HOE) && Permissions.greenTerra(getPlayer()); + return mcMMOPlayer.getToolPreparationMode(ToolType.hoe) && Permissions.greenTerra(getPlayer()); } public boolean canGreenTerraPlant() { diff --git a/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java b/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java index f52253576..314de6428 100644 --- a/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java +++ b/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java @@ -27,7 +27,7 @@ public class SwordsManager extends SkillManager { } public boolean canActivateAbility() { - return mcMMOPlayer.getToolPreparationMode(ToolType.SWORD) && Permissions.serratedStrikes(getPlayer()); + return mcMMOPlayer.getToolPreparationMode(ToolType.sword) && Permissions.serratedStrikes(getPlayer()); } public boolean canUseBleed() { diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java index 857aea0c8..204c98696 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java @@ -30,7 +30,7 @@ public class UnarmedManager extends SkillManager { } public boolean canActivateAbility() { - return mcMMOPlayer.getToolPreparationMode(ToolType.FISTS) && Permissions.berserk(getPlayer()); + return mcMMOPlayer.getToolPreparationMode(ToolType.fists) && Permissions.berserk(getPlayer()); } public boolean canUseIronArm() {