SkillCommand.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.gmail.nossr50.skills;
  2. import java.text.DecimalFormat;
  3. import org.bukkit.command.Command;
  4. import org.bukkit.command.CommandExecutor;
  5. import org.bukkit.command.CommandSender;
  6. import org.bukkit.entity.Player;
  7. import com.gmail.nossr50.commands.CommandHelper;
  8. import com.gmail.nossr50.datatypes.PlayerProfile;
  9. import com.gmail.nossr50.locale.LocaleLoader;
  10. import com.gmail.nossr50.skills.utilities.SkillTools;
  11. import com.gmail.nossr50.skills.utilities.SkillType;
  12. import com.gmail.nossr50.util.Misc;
  13. import com.gmail.nossr50.util.Permissions;
  14. import com.gmail.nossr50.util.Users;
  15. public abstract class SkillCommand implements CommandExecutor {
  16. private SkillType skill;
  17. private String skillString;
  18. private String permission;
  19. protected Player player;
  20. protected PlayerProfile profile;
  21. protected float skillValue;
  22. protected boolean isLucky;
  23. protected boolean hasEndurance;
  24. protected DecimalFormat percent = new DecimalFormat("##0.00%");
  25. protected DecimalFormat decimal = new DecimalFormat("##0.00");
  26. public SkillCommand(SkillType skill) {
  27. this.skill = skill;
  28. this.skillString = Misc.getCapitalized(skill.toString());
  29. this.permission = "mcmmo.skills." + skillString.toLowerCase();
  30. }
  31. @Override
  32. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  33. if (CommandHelper.noConsoleUsage(sender)) {
  34. return true;
  35. }
  36. if (CommandHelper.noCommandPermissions(sender, permission)) {
  37. return true;
  38. }
  39. player = (Player) sender;
  40. profile = Users.getProfile(player);
  41. if (profile == null) {
  42. sender.sendMessage(LocaleLoader.getString("Commands.DoesNotExist"));
  43. return true;
  44. }
  45. skillValue = profile.getSkillLevel(skill);
  46. isLucky = Permissions.lucky(player, skill);
  47. hasEndurance = (Permissions.activationTwelve(player) || Permissions.activationEight(player) || Permissions.activationFour(player));
  48. dataCalculations();
  49. permissionsCheck();
  50. player.sendMessage(LocaleLoader.getString("Skills.Header", new Object[] { LocaleLoader.getString(skillString + ".SkillName") }));
  51. if (!skill.isChildSkill()) {
  52. player.sendMessage(LocaleLoader.getString("Commands.XPGain", new Object[] { LocaleLoader.getString("Commands.XPGain." + skillString) }));
  53. player.sendMessage(LocaleLoader.getString("Effects.Level", new Object[] { profile.getSkillLevel(skill), profile.getSkillXpLevel(skill), profile.getXpToLevel(skill) }));
  54. }
  55. if (effectsHeaderPermissions()) {
  56. player.sendMessage(LocaleLoader.getString("Skills.Header", new Object[] { LocaleLoader.getString("Effects.Effects") }));
  57. }
  58. effectsDisplay();
  59. if (statsHeaderPermissions()) {
  60. player.sendMessage(LocaleLoader.getString("Skills.Header", new Object[] { LocaleLoader.getString("Commands.Stats.Self") }));
  61. }
  62. statsDisplay();
  63. SkillGuide.grabGuidePageForSkill(skill, player, args);
  64. return true;
  65. }
  66. protected String calculateRank(int maxLevel, int rankChangeLevel) {
  67. if (skillValue >= maxLevel) {
  68. return String.valueOf(maxLevel / rankChangeLevel);
  69. }
  70. return String.valueOf((int) (skillValue / rankChangeLevel));
  71. }
  72. protected String[] calculateAbilityDisplayValues(double chance) {
  73. if (isLucky) {
  74. double luckyChance = chance * 1.3333D;
  75. if (luckyChance >= 100D) {
  76. return new String[] { percent.format(chance / 100.0D), percent.format(1.0D) };
  77. }
  78. return new String[] { percent.format(chance / 100.0D), percent.format(luckyChance / 100.0D) };
  79. }
  80. return new String[] { percent.format(chance / 100.0D), null };
  81. }
  82. protected String[] calculateAbilityDisplayValues(int maxBonusLevel, double maxChance) {
  83. double abilityChance;
  84. if (skillValue >= maxBonusLevel) {
  85. abilityChance = maxChance;
  86. }
  87. else {
  88. abilityChance = (maxChance / maxBonusLevel) * skillValue;
  89. }
  90. if (isLucky) {
  91. double luckyChance = abilityChance * 1.3333D;
  92. if (luckyChance >= 100D) {
  93. return new String[] { percent.format(abilityChance / 100.0D), percent.format(1.0D) };
  94. }
  95. return new String[] { percent.format(abilityChance / 100.0D), percent.format(luckyChance / 100.0D) };
  96. }
  97. return new String[] { percent.format(abilityChance / 100.0D), null };
  98. }
  99. protected String[] calculateLengthDisplayValues() {
  100. int maxLength = skill.getAbility().getMaxTicks();
  101. int length = 2 + (int) (skillValue / Misc.abilityLengthIncreaseLevel);
  102. int enduranceLength = 0;
  103. if (Permissions.activationTwelve(player)) {
  104. enduranceLength = length + 12;
  105. }
  106. else if (Permissions.activationEight(player)) {
  107. enduranceLength = length + 8;
  108. }
  109. else if (Permissions.activationFour(player)) {
  110. enduranceLength = length + 4;
  111. }
  112. if (maxLength != 0) {
  113. if (length > maxLength) {
  114. length = maxLength;
  115. }
  116. if (enduranceLength > maxLength) {
  117. enduranceLength = maxLength;
  118. }
  119. }
  120. return new String[] { String.valueOf(length), String.valueOf(enduranceLength) };
  121. }
  122. protected void luckyEffectsDisplay() {
  123. if (isLucky) {
  124. String perkPrefix = LocaleLoader.getString("MOTD.PerksPrefix");
  125. player.sendMessage(perkPrefix + LocaleLoader.getString("Effects.Template", new Object[] { LocaleLoader.getString("Perks.lucky.name"), LocaleLoader.getString("Perks.lucky.desc", new Object[] { SkillTools.localizeSkillName(skill) }) }));
  126. }
  127. }
  128. protected abstract void dataCalculations();
  129. protected abstract void permissionsCheck();
  130. protected abstract boolean effectsHeaderPermissions();
  131. protected abstract void effectsDisplay();
  132. protected abstract boolean statsHeaderPermissions();
  133. protected abstract void statsDisplay();
  134. }