SkillCommand.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package com.gmail.nossr50.commands.skills;
  2. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  3. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  4. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.skills.child.FamilyTree;
  7. import com.gmail.nossr50.util.Permissions;
  8. import com.gmail.nossr50.util.StringUtils;
  9. import com.gmail.nossr50.util.random.RandomChanceUtil;
  10. import com.gmail.nossr50.util.skills.RankUtils;
  11. import com.gmail.nossr50.util.skills.SkillActivationType;
  12. import com.google.common.collect.ImmutableList;
  13. import net.md_5.bungee.api.ChatColor;
  14. import net.md_5.bungee.api.chat.TextComponent;
  15. import org.bukkit.command.Command;
  16. import org.bukkit.command.CommandExecutor;
  17. import org.bukkit.command.CommandSender;
  18. import org.bukkit.command.TabExecutor;
  19. import org.bukkit.entity.Player;
  20. import java.text.DecimalFormat;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Set;
  24. public abstract class SkillCommand implements TabExecutor {
  25. protected PrimarySkillType skill;
  26. protected DecimalFormat percent = new DecimalFormat("##0.00%");
  27. protected DecimalFormat decimal = new DecimalFormat("##0.00");
  28. private String skillName;
  29. private CommandExecutor skillGuideCommand;
  30. protected mcMMO pluginRef;
  31. public SkillCommand(PrimarySkillType primarySkillType, mcMMO pluginRef) {
  32. this.pluginRef = pluginRef;
  33. this.skill = primarySkillType;
  34. skillName = pluginRef.getSkillTools().getLocalizedSkillName(primarySkillType);
  35. skillGuideCommand = new SkillGuideCommand(primarySkillType, pluginRef);
  36. }
  37. public static String[] addItemToFirstPositionOfArray(String itemToAdd, String... existingArray) {
  38. String[] newArray = new String[existingArray.length + 1];
  39. newArray[0] = itemToAdd;
  40. System.arraycopy(existingArray, 0, newArray, 1, existingArray.length);
  41. return newArray;
  42. }
  43. @Override
  44. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  45. if (pluginRef.getCommandTools().noConsoleUsage(sender)) {
  46. return true;
  47. }
  48. if (!pluginRef.getCommandTools().hasPlayerDataKey(sender)) {
  49. return true;
  50. }
  51. if (pluginRef.getUserManager().getPlayer((Player) sender) == null) {
  52. sender.sendMessage(pluginRef.getLocaleManager().getString("Profile.PendingLoad"));
  53. return true;
  54. }
  55. switch (args.length) {
  56. case 0:
  57. Player player = (Player) sender;
  58. McMMOPlayer mcMMOPlayer = pluginRef.getUserManager().getPlayer(player);
  59. boolean isLucky = Permissions.lucky(player, skill);
  60. boolean hasEndurance = pluginRef.getSkillTools().getEnduranceLength(player) > 0;
  61. double skillValue = mcMMOPlayer.getSkillLevel(skill);
  62. //Send the players a few blank lines to make finding the top of the skill command easier
  63. if (pluginRef.getConfigManager().getConfigCommands().isSendBlankLines())
  64. for (int i = 0; i < 2; i++) {
  65. player.sendMessage("");
  66. }
  67. permissionsCheck(player);
  68. dataCalculations(player, skillValue);
  69. sendSkillCommandHeader(player, mcMMOPlayer, (int) skillValue);
  70. //Make JSON text components
  71. List<TextComponent> subskillTextComponents = getTextComponents(player);
  72. //Subskills Header
  73. player.sendMessage(pluginRef.getLocaleManager().getString("Skills.Overhaul.Header", pluginRef.getLocaleManager().getString("Effects.SubSkills.Overhaul")));
  74. //Send JSON text components
  75. pluginRef.getTextComponentFactory().sendPlayerSubSkillList(player, subskillTextComponents);
  76. /*for(TextComponent tc : subskillTextComponents)
  77. {
  78. player.spigot().sendMessage(new TextComponent[]{tc, new TextComponent(": TESTING")});
  79. }*/
  80. //Stats
  81. getStatMessages(player, isLucky, hasEndurance, skillValue);
  82. //Header
  83. //Link Header
  84. if (pluginRef.getConfigManager().getConfigAds().isShowWebsiteLinks()) {
  85. player.sendMessage(pluginRef.getLocaleManager().getString("Overhaul.mcMMO.Header"));
  86. pluginRef.getTextComponentFactory().sendPlayerUrlHeader(player);
  87. }
  88. if (pluginRef.getScoreboardSettings().getScoreboardsEnabled()
  89. && pluginRef.getScoreboardSettings().getConfigSectionScoreboardTypes()
  90. .getConfigSectionSkillBoard().isUseThisBoard()) {
  91. pluginRef.getScoreboardManager().enablePlayerSkillScoreboard(player, skill);
  92. }
  93. return true;
  94. default:
  95. return skillGuideCommand.onCommand(sender, command, label, args);
  96. }
  97. }
  98. private void getStatMessages(Player player, boolean isLucky, boolean hasEndurance, double skillValue) {
  99. List<String> statsMessages = statsDisplay(player, skillValue, hasEndurance, isLucky);
  100. if (!statsMessages.isEmpty()) {
  101. player.sendMessage(pluginRef.getLocaleManager().getString("Skills.Overhaul.Header", pluginRef.getLocaleManager().getString("Commands.Stats.Self.Overhaul")));
  102. for (String message : statsMessages) {
  103. player.sendMessage(message);
  104. }
  105. }
  106. player.sendMessage(pluginRef.getLocaleManager().getString("Guides.Available", skillName, skillName.toLowerCase()));
  107. }
  108. private void sendSkillCommandHeader(Player player, McMMOPlayer mcMMOPlayer, int skillValue) {
  109. ChatColor hd1 = ChatColor.DARK_AQUA;
  110. ChatColor c1 = ChatColor.GOLD;
  111. ChatColor c2 = ChatColor.RED;
  112. player.sendMessage(pluginRef.getLocaleManager().getString("Skills.Overhaul.Header", skillName));
  113. if (!pluginRef.getSkillTools().isChildSkill(skill)) {
  114. /*
  115. * NON-CHILD SKILLS
  116. */
  117. //XP GAIN METHOD
  118. player.sendMessage(pluginRef.getLocaleManager().getString("Commands.XPGain.Overhaul", pluginRef.getLocaleManager().getString("Commands.XPGain." + StringUtils.getCapitalized(skill.toString()))));
  119. //LEVEL
  120. player.sendMessage(pluginRef.getLocaleManager().getString("Effects.Level.Overhaul", skillValue, mcMMOPlayer.getSkillXpLevel(skill), mcMMOPlayer.getXpToLevel(skill)));
  121. } else {
  122. /*
  123. * CHILD SKILLS
  124. */
  125. Set<PrimarySkillType> parents = FamilyTree.getParents(skill);
  126. ArrayList<PrimarySkillType> parentList = new ArrayList<>();
  127. //TODO: Add JSON here
  128. /*player.sendMessage(parent.getName() + " - " + pluginRef.getLocaleManager().getString("Effects.Level.Overhaul", mcMMOPlayer.getSkillLevel(parent), mcMMOPlayer.getSkillXpLevel(parent), mcMMOPlayer.getXpToLevel(parent)))*/
  129. parentList.addAll(parents);
  130. StringBuilder parentMessage = new StringBuilder();
  131. for (int i = 0; i < parentList.size(); i++) {
  132. if (i + 1 < parentList.size()) {
  133. parentMessage.append(pluginRef.getLocaleManager().getString("Effects.Child.ParentList", pluginRef.getSkillTools().getLocalizedSkillName(parentList.get(i)), mcMMOPlayer.getSkillLevel(parentList.get(i))));
  134. parentMessage.append(ChatColor.GRAY + ", ");
  135. } else {
  136. parentMessage.append(pluginRef.getLocaleManager().getString("Effects.Child.ParentList", pluginRef.getSkillTools().getLocalizedSkillName(parentList.get(i)), mcMMOPlayer.getSkillLevel(parentList.get(i))));
  137. }
  138. }
  139. //XP GAIN METHOD
  140. player.sendMessage(pluginRef.getLocaleManager().getString("Commands.XPGain.Overhaul", pluginRef.getLocaleManager().getString("Commands.XPGain.Child")));
  141. player.sendMessage(pluginRef.getLocaleManager().getString("Effects.Child.Overhaul", skillValue, parentMessage.toString()));
  142. //LEVEL
  143. //player.sendMessage(pluginRef.getLocaleManager().getString("Effects.Child.Overhaul", skillValue, skillValue));
  144. }
  145. }
  146. @Override
  147. public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
  148. switch (args.length) {
  149. case 1:
  150. return ImmutableList.of("?");
  151. default:
  152. return ImmutableList.of();
  153. }
  154. }
  155. protected int calculateRank(double skillValue, int maxLevel, int rankChangeLevel) {
  156. return Math.min((int) skillValue, maxLevel) / rankChangeLevel;
  157. }
  158. protected String[] getAbilityDisplayValues(Player player, SubSkillType subSkill) {
  159. return RandomChanceUtil.calculateAbilityDisplayValues(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, player, subSkill);
  160. }
  161. protected String[] formatLengthDisplayValues(Player player, double skillValue) {
  162. int length = pluginRef.getSkillTools().calculateAbilityLength(pluginRef.getUserManager().getPlayer(player), skill, pluginRef.getSkillTools().getSuperAbility(skill));
  163. int enduranceLength = pluginRef.getSkillTools().calculateAbilityLengthPerks(pluginRef.getUserManager().getPlayer(player), skill, pluginRef.getSkillTools().getSuperAbility(skill));
  164. return new String[]{String.valueOf(length), String.valueOf(enduranceLength)};
  165. }
  166. protected String getStatMessage(SubSkillType subSkillType, String... vars) {
  167. return getStatMessage(false, false, subSkillType, vars);
  168. }
  169. protected String getStatMessage(boolean isExtra, boolean isCustom, SubSkillType subSkillType, String... vars) {
  170. String templateKey = isCustom ? "Ability.Generic.Template.Custom" : "Ability.Generic.Template";
  171. String statDescriptionKey = !isExtra ? subSkillType.getLocaleKeyStatDescription(pluginRef) : subSkillType.getLocaleKeyStatExtraDescription(pluginRef);
  172. if (isCustom)
  173. return pluginRef.getLocaleManager().getString(templateKey, pluginRef.getLocaleManager().getString(statDescriptionKey, vars));
  174. else {
  175. String[] mergedList = pluginRef.getNotificationManager().addItemToFirstPositionOfArray(pluginRef.getLocaleManager().getString(statDescriptionKey), vars);
  176. return pluginRef.getLocaleManager().getString(templateKey, mergedList);
  177. }
  178. }
  179. protected abstract void dataCalculations(Player player, double skillValue);
  180. protected abstract void permissionsCheck(Player player);
  181. //protected abstract List<String> effectsDisplay();
  182. protected abstract List<String> statsDisplay(Player player, double skillValue, boolean hasEndurance, boolean isLucky);
  183. protected abstract List<TextComponent> getTextComponents(Player player);
  184. /**
  185. * Checks if a player can use a skill
  186. *
  187. * @param player target player
  188. * @param subSkillType target subskill
  189. * @return true if the player has permission and has the skill unlocked
  190. */
  191. protected boolean canUseSubskill(Player player, SubSkillType subSkillType) {
  192. return Permissions.isSubSkillEnabled(player, subSkillType) && RankUtils.hasUnlockedSubskill(player, subSkillType);
  193. }
  194. }