SkillCommand.java 6.1 KB

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