PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/ru/tehkode/permissions/bukkit/commands/PromotionCommands.java

https://gitlab.com/Slind/PermissionsEx
Java | 178 lines | 130 code | 30 blank | 18 comment | 21 complexity | d89e81150bfaf5f4f878abe453c97262 MD5 | raw file
  1. /*
  2. * PermissionsEx - Permissions plugin for Bukkit
  3. * Copyright (C) 2011 t3hk0d3 http://www.tehkode.ru
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. package ru.tehkode.permissions.bukkit.commands;
  20. import java.util.Map;
  21. import java.util.logging.Logger;
  22. import org.bukkit.ChatColor;
  23. import org.bukkit.command.CommandSender;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.plugin.Plugin;
  26. import ru.tehkode.permissions.PermissionGroup;
  27. import ru.tehkode.permissions.PermissionUser;
  28. import ru.tehkode.permissions.bukkit.PermissionsEx;
  29. import ru.tehkode.permissions.commands.Command;
  30. import ru.tehkode.permissions.exceptions.RankingException;
  31. public class PromotionCommands extends PermissionsCommand {
  32. @Command(name = "pex",
  33. syntax = "group <group> rank [rank] [ladder]",
  34. description = "Get or set <group> [rank] [ladder]",
  35. isPrimary = true,
  36. permission = "permissions.groups.rank.<group>")
  37. public void rankGroup(Plugin plugin, CommandSender sender, Map<String, String> args) {
  38. String groupName = this.autoCompleteGroupName(args.get("group"));
  39. PermissionGroup group = PermissionsEx.getPermissionManager().getGroup(groupName);
  40. if (group == null) {
  41. sender.sendMessage(ChatColor.RED + "Group \"" + groupName + "\" not found");
  42. return;
  43. }
  44. if (args.get("rank") != null) {
  45. String newRank = args.get("rank").trim();
  46. try {
  47. group.setRank(Integer.parseInt(newRank));
  48. } catch (NumberFormatException e) {
  49. sender.sendMessage("Wrong rank. Make sure it's number.");
  50. }
  51. if (args.containsKey("ladder")) {
  52. group.setRankLadder(args.get("ladder"));
  53. }
  54. }
  55. int rank = group.getRank();
  56. if (rank > 0) {
  57. sender.sendMessage("Group " + group.getName() + " rank is " + rank + " (ladder = " + group.getRankLadder() + ")");
  58. } else {
  59. sender.sendMessage("Group " + group.getName() + " is unranked");
  60. }
  61. }
  62. @Command(name = "pex",
  63. syntax = "promote <user> [ladder]",
  64. description = "Promotes <user> to next group on [ladder]",
  65. isPrimary = true)
  66. public void promoteUser(Plugin plugin, CommandSender sender, Map<String, String> args) {
  67. String userName = this.autoCompletePlayerName(args.get("user"));
  68. PermissionUser user = PermissionsEx.getPermissionManager().getUser(userName);
  69. if (user == null) {
  70. sender.sendMessage("Specified user \"" + args.get("user") + "\" not found!");
  71. return;
  72. }
  73. String promoterName = "console";
  74. String ladder = "default";
  75. if (args.containsKey("ladder")) {
  76. ladder = args.get("ladder");
  77. }
  78. PermissionUser promoter = null;
  79. if (sender instanceof Player) {
  80. promoter = PermissionsEx.getPermissionManager().getUser(((Player) sender).getName());
  81. if (promoter == null || !promoter.has("permissions.user.promote." + ladder, ((Player) sender).getWorld().getName())) {
  82. sender.sendMessage(ChatColor.RED + "You don't have enough permissions to promote on this ladder");
  83. return;
  84. }
  85. promoterName = promoter.getName();
  86. }
  87. try {
  88. PermissionGroup targetGroup = user.promote(promoter, ladder);
  89. this.informPlayer(plugin, user.getName(), "You have been promoted on " + targetGroup.getRankLadder() + " ladder to " + targetGroup.getName() + " group");
  90. sender.sendMessage("User " + user.getName() + " promoted to " + targetGroup.getName() + " group");
  91. Logger.getLogger("Minecraft").info("User " + user.getName() + " has been promoted to " + targetGroup.getName() + " group on " + targetGroup.getRankLadder() + " ladder by " + promoterName);
  92. } catch (RankingException e) {
  93. sender.sendMessage(ChatColor.RED + "Promotion error: " + e.getMessage());
  94. Logger.getLogger("Minecraft").severe("Ranking Error (" + promoterName + " > " + e.getTarget().getName() + "): " + e.getMessage());
  95. }
  96. }
  97. @Command(name = "pex",
  98. syntax = "demote <user> [ladder]",
  99. description = "Demotes <user> to previous group or [ladder]",
  100. isPrimary = true)
  101. public void demoteUser(Plugin plugin, CommandSender sender, Map<String, String> args) {
  102. String userName = this.autoCompletePlayerName(args.get("user"));
  103. PermissionUser user = PermissionsEx.getPermissionManager().getUser(userName);
  104. if (user == null) {
  105. sender.sendMessage(ChatColor.RED + "Specified user \"" + args.get("user") + "\" not found!");
  106. return;
  107. }
  108. String demoterName = "console";
  109. String ladder = "default";
  110. if (args.containsKey("ladder")) {
  111. ladder = args.get("ladder");
  112. }
  113. PermissionUser demoter = null;
  114. if (sender instanceof Player) {
  115. demoter = PermissionsEx.getPermissionManager().getUser(((Player) sender).getName());
  116. if (demoter == null || !demoter.has("permissions.user.demote." + ladder, ((Player) sender).getWorld().getName())) {
  117. sender.sendMessage(ChatColor.RED + "You don't have enough permissions to demote on this ladder");
  118. return;
  119. }
  120. demoterName = demoter.getName();
  121. }
  122. try {
  123. PermissionGroup targetGroup = user.demote(demoter, args.get("ladder"));
  124. this.informPlayer(plugin, user.getName(), "You have been demoted on " + targetGroup.getRankLadder() + " ladder to " + targetGroup.getName() + " group");
  125. sender.sendMessage("User " + user.getName() + " demoted to " + targetGroup.getName() + " group");
  126. Logger.getLogger("Minecraft").info("User " + user.getName() + " has been demoted to " + targetGroup.getName() + " group on " + targetGroup.getRankLadder() + " ladder by " + demoterName);
  127. } catch (RankingException e) {
  128. sender.sendMessage(ChatColor.RED + "Demotion error: " + e.getMessage());
  129. Logger.getLogger("Minecraft").severe("Ranking Error (" + demoterName + " demotes " + e.getTarget().getName() + "): " + e.getMessage());
  130. }
  131. }
  132. @Command(name = "promote",
  133. syntax = "<user>",
  134. description = "Promotes <user> to next group",
  135. isPrimary = true,
  136. permission = "permissions.user.rank.promote")
  137. public void promoteUserAlias(Plugin plugin, CommandSender sender, Map<String, String> args) {
  138. this.promoteUser(plugin, sender, args);
  139. }
  140. @Command(name = "demote",
  141. syntax = "<user>",
  142. description = "Demotes <user> to previous group",
  143. isPrimary = true,
  144. permission = "permissions.user.rank.demote")
  145. public void demoteUserAlias(Plugin plugin, CommandSender sender, Map<String, String> args) {
  146. this.demoteUser(plugin, sender, args);
  147. }
  148. }