From ba32e725f8bd92297c50d05ac67d7e65880ff6a0 Mon Sep 17 00:00:00 2001 From: ile123ile Date: Sat, 30 Aug 2014 22:21:43 -0700 Subject: [PATCH] Got / command working --- .../com/gmail/nossr50/api/AbilityAPI.java | 9 ++- .../java/com/gmail/nossr50/api/SkillAPI.java | 18 ++++- .../nossr50/commands/skills/SkillCommand.java | 5 +- .../gmail/nossr50/config/AdvancedConfig.java | 13 +++- .../commands/CommandRegistrationManager.java | 73 +++++++++++-------- 5 files changed, 78 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/AbilityAPI.java b/src/main/java/com/gmail/nossr50/api/AbilityAPI.java index 7dc7da5ff..c59ad2cd3 100644 --- a/src/main/java/com/gmail/nossr50/api/AbilityAPI.java +++ b/src/main/java/com/gmail/nossr50/api/AbilityAPI.java @@ -3,6 +3,7 @@ package com.gmail.nossr50.api; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SecondaryAbility; @@ -97,6 +98,12 @@ public final class AbilityAPI { } public static SecondaryAbility createSecondaryAbility(String name) { - return new SecondaryAbility(name); + return createSecondaryAbility(name, 0, 100); + } + + public static SecondaryAbility createSecondaryAbility(String name, int maxBonusLevel, double maxChance) { + SecondaryAbility ability = new SecondaryAbility(name); + AdvancedConfig.getInstance().createNewSkill(ability, maxBonusLevel, maxChance); + return ability; } } diff --git a/src/main/java/com/gmail/nossr50/api/SkillAPI.java b/src/main/java/com/gmail/nossr50/api/SkillAPI.java index 2bb80b171..90bcdcb0b 100644 --- a/src/main/java/com/gmail/nossr50/api/SkillAPI.java +++ b/src/main/java/com/gmail/nossr50/api/SkillAPI.java @@ -6,12 +6,14 @@ import java.util.List; import org.bukkit.Color; import com.gmail.nossr50.commands.skills.SkillCommand; +import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.ToolType; import com.gmail.nossr50.datatypes.skills.SkillType.SkillUseType; import com.gmail.nossr50.skills.SkillManager; +import com.gmail.nossr50.util.commands.CommandRegistrationManager; import com.google.common.collect.ImmutableList; public final class SkillAPI { @@ -100,19 +102,27 @@ public final class SkillAPI { } public static SkillType createSkill(String name, Class managerClass, Class commandClass, boolean isChild, Color runescapeColor, SkillUseType skillUseType, List secondaryAbilities) { - return SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, secondaryAbilities); + SkillType skill = SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, secondaryAbilities); + CommandRegistrationManager.registerSkillCommandAndPassSkillToConstructor(skill); + return skill; } public static SkillType createSkill(String name, Class managerClass, Class commandClass, boolean isChild, Color runescapeColor, SkillUseType skillUseType, AbilityType ability, ToolType tool, List secondaryAbilities) { - return SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ability, tool, secondaryAbilities); + SkillType skill = SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ability, tool, secondaryAbilities); + CommandRegistrationManager.registerSkillCommandAndPassSkillToConstructor(skill); + return skill; } public static SkillType createSkill(String name, Class managerClass, Class commandClass, boolean isChild, Color runescapeColor, SkillUseType skillUseType, SecondaryAbility... secondaryAbilities) { - return SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ImmutableList.copyOf(secondaryAbilities)); + SkillType skill = SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ImmutableList.copyOf(secondaryAbilities)); + CommandRegistrationManager.registerSkillCommandAndPassSkillToConstructor(skill); + return skill; } public static SkillType createSkill(String name, Class managerClass, Class commandClass, boolean isChild, Color runescapeColor, SkillUseType skillUseType, AbilityType ability, ToolType tool, SecondaryAbility... secondaryAbilities) { - return SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ability, tool, ImmutableList.copyOf(secondaryAbilities)); + SkillType skill = SkillType.createSkill(name, managerClass, commandClass, isChild, runescapeColor, skillUseType, ability, tool, ImmutableList.copyOf(secondaryAbilities)); + CommandRegistrationManager.registerSkillCommandAndPassSkillToConstructor(skill); + return skill; } public static void loadNewSkills() { diff --git a/src/main/java/com/gmail/nossr50/commands/skills/SkillCommand.java b/src/main/java/com/gmail/nossr50/commands/skills/SkillCommand.java index 220297198..0fc4d2679 100644 --- a/src/main/java/com/gmail/nossr50/commands/skills/SkillCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/skills/SkillCommand.java @@ -143,8 +143,9 @@ public abstract class SkillCommand implements TabExecutor { protected String[] calculateAbilityDisplayValues(float skillValue, SecondaryAbility skillAbility, boolean isLucky) { int maxBonusLevel = AdvancedConfig.getInstance().getMaxBonusLevel(skillAbility); - - return calculateAbilityDisplayValues((AdvancedConfig.getInstance().getMaxChance(skillAbility) / maxBonusLevel) * Math.min(skillValue, maxBonusLevel), isLucky); + double maxChance = AdvancedConfig.getInstance().getMaxChance(skillAbility); + double chance = (maxBonusLevel == 0) ? maxChance : (maxChance / maxBonusLevel) * Math.min(skillValue, maxBonusLevel); + return calculateAbilityDisplayValues(chance, isLucky); } protected String[] calculateLengthDisplayValues(Player player, float skillValue) { diff --git a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java index 1baf6c1d1..d634e8bb5 100644 --- a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java +++ b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java @@ -1,10 +1,10 @@ package com.gmail.nossr50.config; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.skills.SecondaryAbility; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.skills.alchemy.Alchemy; @@ -685,6 +685,17 @@ public class AdvancedConfig extends AutoUpdateConfigLoader { @Override protected void loadKeys() {} + + public void createNewSkill(SecondaryAbility skillAbility, int maxBonusLevel, double maxChance) { + String skillAbilityString = "Skills." + StringUtils.getCapitalized(SkillType.bySecondaryAbility(skillAbility).toString()) + "." + StringUtils.getPrettySecondaryAbilityString(skillAbility).replace(" ", ""); + config.set(skillAbilityString + ".MaxBonusLevel", maxBonusLevel); + config.set(skillAbilityString + ".ChanceMax", maxChance); + try { + config.save(getFile()); + } catch (IOException e) { + e.printStackTrace(); + } + } /* GENERAL */ public int getAbilityLength() { return config.getInt("Skills.General.Ability.IncreaseLevel", 50); } diff --git a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java index b036449ac..daa030b16 100644 --- a/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java +++ b/src/main/java/com/gmail/nossr50/util/commands/CommandRegistrationManager.java @@ -35,21 +35,6 @@ import com.gmail.nossr50.commands.player.MccooldownCommand; import com.gmail.nossr50.commands.player.McrankCommand; import com.gmail.nossr50.commands.player.McstatsCommand; import com.gmail.nossr50.commands.player.MctopCommand; -import com.gmail.nossr50.commands.skills.AcrobaticsCommand; -import com.gmail.nossr50.commands.skills.AlchemyCommand; -import com.gmail.nossr50.commands.skills.ArcheryCommand; -import com.gmail.nossr50.commands.skills.AxesCommand; -import com.gmail.nossr50.commands.skills.ExcavationCommand; -import com.gmail.nossr50.commands.skills.FishingCommand; -import com.gmail.nossr50.commands.skills.HerbalismCommand; -import com.gmail.nossr50.commands.skills.MiningCommand; -import com.gmail.nossr50.commands.skills.RepairCommand; -import com.gmail.nossr50.commands.skills.SalvageCommand; -import com.gmail.nossr50.commands.skills.SmeltingCommand; -import com.gmail.nossr50.commands.skills.SwordsCommand; -import com.gmail.nossr50.commands.skills.TamingCommand; -import com.gmail.nossr50.commands.skills.UnarmedCommand; -import com.gmail.nossr50.commands.skills.WoodcuttingCommand; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.locale.LocaleLoader; @@ -62,25 +47,49 @@ public final class CommandRegistrationManager { private static void registerSkillCommands() { for (SkillType skill : SkillType.getSkillList()) { - String commandName = skill.toString().toLowerCase(); - String localizedName = skill.getLocalizedName().toLowerCase(); - - PluginCommand command; - - command = mcMMO.p.getCommand(commandName); - command.setDescription(LocaleLoader.getString("Commands.Description.Skill", StringUtils.getCapitalized(localizedName))); - command.setPermission("mcmmo.commands." + commandName); - command.setPermissionMessage(permissionsMessage); - command.setUsage(LocaleLoader.getString("Commands.Usage.0", localizedName)); - command.setUsage(command.getUsage() + "\n" + LocaleLoader.getString("Commands.Usage.2", localizedName, "?", "[" + LocaleLoader.getString("Commands.Usage.Page") + "]")); - - try { - command.setExecutor(skill.getCommandClass().newInstance()); - } catch (Exception e) { - e.printStackTrace(); - } + registerSkillCommand(skill); } } + + public static void registerSkillCommand(SkillType skill) { + String commandName = skill.toString().toLowerCase(); + String localizedName = skill.getLocalizedName().toLowerCase(); + + PluginCommand command; + + command = mcMMO.p.getCommand(commandName); + command.setDescription(LocaleLoader.getString("Commands.Description.Skill", StringUtils.getCapitalized(localizedName))); + command.setPermission("mcmmo.commands." + commandName); + command.setPermissionMessage(permissionsMessage); + command.setUsage(LocaleLoader.getString("Commands.Usage.0", localizedName)); + command.setUsage(command.getUsage() + "\n" + LocaleLoader.getString("Commands.Usage.2", localizedName, "?", "[" + LocaleLoader.getString("Commands.Usage.Page") + "]")); + + try { + command.setExecutor(skill.getCommandClass().newInstance()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void registerSkillCommandAndPassSkillToConstructor(SkillType skill) { + String commandName = skill.toString().toLowerCase(); + String localizedName = skill.getLocalizedName().toLowerCase(); + + PluginCommand command; + + command = mcMMO.p.getCommand(commandName); + command.setDescription(LocaleLoader.getString("Commands.Description.Skill", StringUtils.getCapitalized(localizedName))); + command.setPermission("mcmmo.commands." + commandName); + command.setPermissionMessage(permissionsMessage); + command.setUsage(LocaleLoader.getString("Commands.Usage.0", localizedName)); + command.setUsage(command.getUsage() + "\n" + LocaleLoader.getString("Commands.Usage.2", localizedName, "?", "[" + LocaleLoader.getString("Commands.Usage.Page") + "]")); + + try { + command.setExecutor(skill.getCommandClass().getDeclaredConstructor(SkillType.class).newInstance(skill)); + } catch (Exception e) { + e.printStackTrace(); + } + } private static void registerAddlevelsCommand() { PluginCommand command = mcMMO.p.getCommand("addlevels");