SkillCommand.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package com.gmail.nossr50.commands.skills;
  2. import java.text.DecimalFormat;
  3. import java.util.Set;
  4. import org.bukkit.command.Command;
  5. import org.bukkit.command.CommandExecutor;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.entity.Player;
  8. import com.gmail.nossr50.config.AdvancedConfig;
  9. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  10. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  11. import com.gmail.nossr50.datatypes.skills.SkillType;
  12. import com.gmail.nossr50.locale.LocaleLoader;
  13. import com.gmail.nossr50.skills.child.FamilyTree;
  14. import com.gmail.nossr50.util.Permissions;
  15. import com.gmail.nossr50.util.StringUtils;
  16. import com.gmail.nossr50.util.commands.CommandUtils;
  17. import com.gmail.nossr50.util.player.UserManager;
  18. import com.gmail.nossr50.util.skills.PerksUtils;
  19. import com.gmail.nossr50.util.skills.SkillUtils;
  20. public abstract class SkillCommand implements CommandExecutor {
  21. protected SkillType skill;
  22. protected String skillName;
  23. protected Player player;
  24. protected PlayerProfile profile;
  25. protected McMMOPlayer mcMMOPlayer;
  26. protected float skillValue;
  27. protected boolean isLucky;
  28. protected boolean hasEndurance;
  29. protected DecimalFormat percent = new DecimalFormat("##0.00%");
  30. protected DecimalFormat decimal = new DecimalFormat("##0.00");
  31. private CommandExecutor skillGuideCommand;
  32. public SkillCommand(SkillType skill) {
  33. this.skill = skill;
  34. skillName = SkillUtils.getSkillName(skill);
  35. skillGuideCommand = new SkillGuideCommand(skill);
  36. }
  37. @Override
  38. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  39. if (CommandUtils.noConsoleUsage(sender)) {
  40. return true;
  41. }
  42. mcMMOPlayer = UserManager.getPlayer(sender.getName());
  43. player = mcMMOPlayer.getPlayer();
  44. switch (args.length) {
  45. case 0:
  46. profile = mcMMOPlayer.getProfile();
  47. skillValue = profile.getSkillLevel(skill);
  48. isLucky = Permissions.lucky(sender, skill);
  49. hasEndurance = (PerksUtils.handleActivationPerks(player, 0, 0) != 0);
  50. permissionsCheck();
  51. dataCalculations();
  52. if (!skill.isChildSkill()) {
  53. player.sendMessage(LocaleLoader.getString("Skills.Header", skillName));
  54. player.sendMessage(LocaleLoader.getString("Commands.XPGain", LocaleLoader.getString("Commands.XPGain." + StringUtils.getCapitalized(skill.toString()))));
  55. player.sendMessage(LocaleLoader.getString("Effects.Level", (int) skillValue, profile.getSkillXpLevel(skill), profile.getXpToLevel(skill)));
  56. }
  57. else {
  58. player.sendMessage(LocaleLoader.getString("Skills.Header", skillName + " " + LocaleLoader.getString("Skills.Child")));
  59. player.sendMessage(LocaleLoader.getString("Commands.XPGain", LocaleLoader.getString("Commands.XPGain.Child")));
  60. player.sendMessage(LocaleLoader.getString("Effects.Child", (int) skillValue));
  61. player.sendMessage(LocaleLoader.getString("Skills.Header", LocaleLoader.getString("Skills.Parents")));
  62. Set<SkillType> parents = FamilyTree.getParents(skill);
  63. for (SkillType parent : parents) {
  64. player.sendMessage(SkillUtils.getSkillName(parent) + " - " + LocaleLoader.getString("Effects.Level", profile.getSkillLevel(parent), profile.getSkillXpLevel(parent), profile.getXpToLevel(parent)));
  65. }
  66. }
  67. if (effectsHeaderPermissions()) {
  68. player.sendMessage(LocaleLoader.getString("Skills.Header", LocaleLoader.getString("Effects.Effects")));
  69. }
  70. effectsDisplay();
  71. if (statsHeaderPermissions()) {
  72. player.sendMessage(LocaleLoader.getString("Skills.Header", LocaleLoader.getString("Commands.Stats.Self")));
  73. }
  74. statsDisplay();
  75. player.sendMessage(LocaleLoader.getString("Guides.Available", skillName, skillName.toLowerCase()));
  76. return true;
  77. default:
  78. return skillGuideCommand.onCommand(sender, command, label, args);
  79. }
  80. }
  81. protected int calculateRank(int maxLevel, int rankChangeLevel) {
  82. return Math.min((int) skillValue, maxLevel) / rankChangeLevel;
  83. }
  84. protected String[] calculateAbilityDisplayValues(double chance) {
  85. String[] displayValues = new String[2];
  86. displayValues[0] = percent.format(Math.min(chance, 100.0D) / 100.0D);
  87. displayValues[1] = isLucky ? percent.format(Math.min(chance * 1.3333D, 100.0D) / 100.0D) : null;
  88. return displayValues;
  89. }
  90. protected String[] calculateAbilityDisplayValues(int maxBonusLevel, double maxChance) {
  91. return calculateAbilityDisplayValues((maxChance / Math.min(skillValue, maxBonusLevel)) * skillValue);
  92. }
  93. protected String[] calculateLengthDisplayValues() {
  94. int maxLength = skill.getAbility().getMaxTicks();
  95. int length = 2 + (int) (skillValue / AdvancedConfig.getInstance().getAbilityLength());
  96. int enduranceLength = PerksUtils.handleActivationPerks(player, length, maxLength);
  97. if (maxLength != 0) {
  98. length = Math.min(length, maxLength);
  99. }
  100. return new String[] { String.valueOf(length), String.valueOf(enduranceLength) };
  101. }
  102. protected void luckyEffectsDisplay() {
  103. if (isLucky) {
  104. String perkPrefix = LocaleLoader.getString("MOTD.PerksPrefix");
  105. player.sendMessage(perkPrefix + LocaleLoader.getString("Effects.Template", LocaleLoader.getString("Perks.lucky.name"), LocaleLoader.getString("Perks.lucky.desc", skillName)));
  106. }
  107. }
  108. protected abstract void dataCalculations();
  109. protected abstract void permissionsCheck();
  110. protected abstract boolean effectsHeaderPermissions();
  111. protected abstract void effectsDisplay();
  112. protected abstract boolean statsHeaderPermissions();
  113. protected abstract void statsDisplay();
  114. }