SkillCommand.java 5.9 KB

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