2
0

MctopCommand.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.gmail.nossr50.commands.player;
  2. import com.gmail.nossr50.core.MetadataConstants;
  3. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  4. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.runnables.commands.MctopCommandAsyncTask;
  7. import com.gmail.nossr50.util.Permissions;
  8. import com.gmail.nossr50.util.StringUtils;
  9. import com.gmail.nossr50.util.player.UserManager;
  10. import com.gmail.nossr50.util.scoreboards.ScoreboardManager;
  11. import com.google.common.collect.ImmutableList;
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandSender;
  14. import org.bukkit.command.TabExecutor;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.metadata.FixedMetadataValue;
  17. import org.bukkit.util.StringUtil;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. public class MctopCommand implements TabExecutor {
  21. private mcMMO pluginRef;
  22. public MctopCommand(mcMMO pluginRef) {
  23. this.pluginRef = pluginRef;
  24. }
  25. @Override
  26. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  27. PrimarySkillType skill = null;
  28. switch (args.length) {
  29. case 0:
  30. display(1, skill, sender, command);
  31. return true;
  32. case 1:
  33. if (StringUtils.isInt(args[0])) {
  34. display(Math.abs(Integer.parseInt(args[0])), skill, sender, command);
  35. return true;
  36. }
  37. skill = extractSkill(sender, args[0]);
  38. if (skill == null) {
  39. return true;
  40. }
  41. display(1, skill, sender, command);
  42. return true;
  43. case 2:
  44. if (pluginRef.getCommandTools().isInvalidInteger(sender, args[1])) {
  45. return true;
  46. }
  47. skill = extractSkill(sender, args[0]);
  48. if (skill == null) {
  49. return true;
  50. }
  51. display(Math.abs(Integer.parseInt(args[1])), skill, sender, command);
  52. return true;
  53. default:
  54. return false;
  55. }
  56. }
  57. @Override
  58. public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
  59. switch (args.length) {
  60. case 1:
  61. return StringUtil.copyPartialMatches(args[0], PrimarySkillType.SKILL_NAMES, new ArrayList<>(PrimarySkillType.SKILL_NAMES.size()));
  62. default:
  63. return ImmutableList.of();
  64. }
  65. }
  66. private void display(int page, PrimarySkillType skill, CommandSender sender, Command command) {
  67. if (skill != null && !Permissions.mctop(sender, skill)) {
  68. sender.sendMessage(command.getPermissionMessage());
  69. return;
  70. }
  71. if (sender instanceof Player) {
  72. if (!pluginRef.getCommandTools().hasPlayerDataKey(sender)) {
  73. return;
  74. }
  75. McMMOPlayer mcMMOPlayer = UserManager.getPlayer(sender.getName());
  76. long cooldownMillis = 5000;
  77. if (mcMMOPlayer.getDatabaseATS() + cooldownMillis > System.currentTimeMillis()) {
  78. double seconds = ((mcMMOPlayer.getDatabaseATS() + cooldownMillis) - System.currentTimeMillis()) / 1000;
  79. if (seconds < 1) {
  80. seconds = 1;
  81. }
  82. sender.sendMessage(pluginRef.getLocaleManager().formatString(pluginRef.getLocaleManager().getString("Commands.Database.Cooldown"), seconds));
  83. return;
  84. }
  85. if (((Player) sender).hasMetadata(MetadataConstants.DATABASE_PROCESSING_COMMAND_METAKEY)) {
  86. sender.sendMessage(pluginRef.getLocaleManager().getString("Commands.Database.Processing"));
  87. return;
  88. } else {
  89. ((Player) sender).setMetadata(MetadataConstants.DATABASE_PROCESSING_COMMAND_METAKEY, new FixedMetadataValue(pluginRef, null));
  90. }
  91. mcMMOPlayer.actualizeDatabaseATS();
  92. }
  93. display(page, skill, sender);
  94. }
  95. private void display(int page, PrimarySkillType skill, CommandSender sender) {
  96. boolean useBoard = (sender instanceof Player) && (pluginRef.getScoreboardSettings().isScoreboardEnabled(ScoreboardManager.SidebarType.TOP_BOARD));
  97. boolean useChat = !useBoard || pluginRef.getScoreboardSettings().isScoreboardPrinting(ScoreboardManager.SidebarType.TOP_BOARD);
  98. new MctopCommandAsyncTask(page, skill, sender, useBoard, useChat).runTaskAsynchronously(pluginRef);
  99. }
  100. private PrimarySkillType extractSkill(CommandSender sender, String skillName) {
  101. if (pluginRef.getCommandTools().isInvalidSkill(sender, skillName)) {
  102. return null;
  103. }
  104. PrimarySkillType skill = PrimarySkillType.getSkill(skillName);
  105. if (pluginRef.getCommandTools().isChildSkill(sender, skill)) {
  106. return null;
  107. }
  108. return skill;
  109. }
  110. }