PageRenderTime 131ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Slind/PermissionsEx
Java | 395 lines | 288 code | 82 blank | 25 comment | 97 complexity | 3213a0543331c8b99c8cfd9e1125b1fd 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.HashSet;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.logging.Logger;
  26. import org.bukkit.Bukkit;
  27. import org.bukkit.ChatColor;
  28. import org.bukkit.World;
  29. import org.bukkit.command.CommandSender;
  30. import org.bukkit.entity.Player;
  31. import org.bukkit.plugin.Plugin;
  32. import ru.tehkode.permissions.PermissionEntity;
  33. import ru.tehkode.permissions.PermissionGroup;
  34. import ru.tehkode.permissions.PermissionUser;
  35. import ru.tehkode.permissions.bukkit.PermissionsEx;
  36. import ru.tehkode.permissions.commands.CommandListener;
  37. import ru.tehkode.permissions.commands.CommandsManager;
  38. import ru.tehkode.permissions.commands.exceptions.AutoCompleteChoicesException;
  39. import ru.tehkode.utils.StringUtils;
  40. public abstract class PermissionsCommand implements CommandListener {
  41. protected static final Logger logger = Logger.getLogger("Minecraft");
  42. protected CommandsManager manager;
  43. @Override
  44. public void onRegistered(CommandsManager manager) {
  45. this.manager = manager;
  46. }
  47. protected void informGroup(Plugin plugin, PermissionGroup group, String message) {
  48. for (PermissionUser user : group.getUsers()) {
  49. this.informPlayer(plugin, user.getName(), message);
  50. }
  51. }
  52. protected void informPlayer(Plugin plugin, String playerName, String message) {
  53. if (!(plugin instanceof PermissionsEx) || !((PermissionsEx) plugin).getConfig().getBoolean("permissions.informplayers.changes", false)) {
  54. return; // User informing is disabled
  55. }
  56. Player player = Bukkit.getServer().getPlayer(playerName);
  57. if (player == null) {
  58. return;
  59. }
  60. player.sendMessage(ChatColor.BLUE + "[PermissionsEx] " + ChatColor.WHITE + message);
  61. }
  62. protected String autoCompletePlayerName(String playerName) {
  63. return autoCompletePlayerName(playerName, "user");
  64. }
  65. protected void printEntityInheritance(CommandSender sender, PermissionGroup[] groups) {
  66. for (PermissionGroup group : groups) {
  67. String rank = "not ranked";
  68. if (group.isRanked()) {
  69. rank = "rank " + group.getRank() + " @ " + group.getRankLadder();
  70. }
  71. sender.sendMessage(" " + group.getName() + " (" + rank + ")");
  72. }
  73. }
  74. protected String autoCompletePlayerName(String playerName, String argName) {
  75. if (playerName == null) {
  76. return null;
  77. }
  78. if (playerName.startsWith("#")) {
  79. return playerName.substring(1);
  80. }
  81. List<String> players = new LinkedList<String>();
  82. // Collect online Player names
  83. for (Player player : Bukkit.getServer().getOnlinePlayers()) {
  84. if (player.getName().equalsIgnoreCase(playerName)) {
  85. return player.getName();
  86. }
  87. if (player.getName().toLowerCase().startsWith(playerName.toLowerCase()) && !players.contains(player.getName())) {
  88. players.add(player.getName());
  89. }
  90. }
  91. // Collect registered PEX user names
  92. for (String user : PermissionsEx.getPermissionManager().getUserNames()) {
  93. if (user.equalsIgnoreCase(playerName)) {
  94. return user;
  95. }
  96. if (user.toLowerCase().startsWith(playerName.toLowerCase()) && !players.contains(user)) {
  97. players.add(user);
  98. }
  99. }
  100. if (players.size() > 1) {
  101. throw new AutoCompleteChoicesException(players.toArray(new String[0]), argName);
  102. } else if (players.size() == 1) {
  103. return players.get(0);
  104. }
  105. // Nothing found
  106. return playerName;
  107. }
  108. protected String getSenderName(CommandSender sender) {
  109. if (sender instanceof Player) {
  110. return ((Player) sender).getName();
  111. }
  112. return "console";
  113. }
  114. protected String autoCompleteGroupName(String groupName) {
  115. return this.autoCompleteGroupName(groupName, "group");
  116. }
  117. protected String autoCompleteGroupName(String groupName, String argName) {
  118. if (groupName.startsWith("#")) {
  119. return groupName.substring(1);
  120. }
  121. List<String> groups = new LinkedList<String>();
  122. for (String group : PermissionsEx.getPermissionManager().getGroupNames()) {
  123. if (group.equalsIgnoreCase(groupName)) {
  124. return group;
  125. }
  126. if (group.toLowerCase().startsWith(groupName.toLowerCase()) && !groups.contains(group)) {
  127. groups.add(group);
  128. }
  129. }
  130. if (groups.size() > 1) { // Found several choices
  131. throw new AutoCompleteChoicesException(groups.toArray(new String[0]), argName);
  132. } else if (groups.size() == 1) { // Found one name
  133. return groups.get(0);
  134. }
  135. // Nothing found
  136. return groupName;
  137. }
  138. protected String autoCompleteWorldName(String worldName) {
  139. return this.autoCompleteWorldName(worldName, "world");
  140. }
  141. protected String autoCompleteWorldName(String worldName, String argName) {
  142. if (worldName == null || worldName.isEmpty() || "*".equals(worldName)) {
  143. return null;
  144. }
  145. List<String> worlds = new LinkedList<String>();
  146. for (World world : Bukkit.getServer().getWorlds()) {
  147. if (world.getName().equalsIgnoreCase(worldName)) {
  148. return world.getName();
  149. }
  150. if (world.getName().toLowerCase().startsWith(worldName.toLowerCase()) && !worlds.contains(world.getName())) {
  151. worlds.add(world.getName());
  152. }
  153. }
  154. if (worlds.size() > 1) { // Found several choices
  155. throw new AutoCompleteChoicesException(worlds.toArray(new String[0]), argName);
  156. } else if (worlds.size() == 1) { // Found one name
  157. return worlds.get(0);
  158. }
  159. return worldName;
  160. }
  161. protected String getSafeWorldName(String worldName, String userName) {
  162. if (worldName == null) {
  163. Player player = Bukkit.getServer().getPlayer(userName);
  164. if (player != null) {
  165. worldName = player.getWorld().getName();
  166. } else {
  167. worldName = Bukkit.getServer().getWorlds().get(0).getName();
  168. }
  169. }
  170. return worldName;
  171. }
  172. protected String autoCompletePermission(PermissionEntity entity, String permission, String worldName) {
  173. return this.autoCompletePermission(entity, permission, worldName, "permission");
  174. }
  175. protected String autoCompletePermission(PermissionEntity entity, String permission, String worldName, String argName) {
  176. if (permission == null) {
  177. return permission;
  178. }
  179. Set<String> permissions = new HashSet<String>();
  180. for (String currentPermission : entity.getPermissions(worldName)) {
  181. if (currentPermission.equalsIgnoreCase(permission)) {
  182. return currentPermission;
  183. }
  184. if (currentPermission.toLowerCase().startsWith(permission.toLowerCase())) {
  185. permissions.add(currentPermission);
  186. }
  187. }
  188. if (permissions.size() > 0) {
  189. String[] permissionArray = permissions.toArray(new String[0]);
  190. if (permissionArray.length == 1) {
  191. return permissionArray[0];
  192. }
  193. throw new AutoCompleteChoicesException(permissionArray, argName);
  194. }
  195. return permission;
  196. }
  197. protected int getPosition(String permission, String[] permissions) {
  198. try {
  199. // permission is permission index
  200. int position = Integer.parseInt(permission) - 1;
  201. if (position < 0 || position >= permissions.length) {
  202. throw new RuntimeException("Wrong permission index specified!");
  203. }
  204. return position;
  205. } catch (NumberFormatException e) {
  206. // permission is permission text
  207. for (int i = 0; i < permissions.length; i++) {
  208. if (permission.equalsIgnoreCase(permissions[i])) {
  209. return i;
  210. }
  211. }
  212. }
  213. throw new RuntimeException("Specified permission not found");
  214. }
  215. protected String printHierarchy(PermissionGroup parent, String worldName, int level) {
  216. StringBuilder buffer = new StringBuilder();
  217. PermissionGroup[] groups;
  218. if (parent == null) {
  219. groups = PermissionsEx.getPermissionManager().getGroups();
  220. } else {
  221. groups = parent.getChildGroups(worldName);
  222. }
  223. for (PermissionGroup group : groups) {
  224. if (parent == null && group.getParentGroups(worldName).length > 0) {
  225. continue;
  226. }
  227. buffer.append(StringUtils.repeat(" ", level)).append(" - ").append(group.getName()).append("\n");
  228. // Groups
  229. buffer.append(printHierarchy(group, worldName, level + 1));
  230. for (PermissionUser user : group.getUsers(worldName)) {
  231. buffer.append(StringUtils.repeat(" ", level + 1)).append(" + ").append(user.getName()).append("\n");
  232. }
  233. }
  234. return buffer.toString();
  235. }
  236. protected String mapPermissions(String worldName, PermissionEntity entity, int level) {
  237. StringBuilder builder = new StringBuilder();
  238. int index = 1;
  239. for (String permission : this.getPermissionsTree(entity, worldName, 0)) {
  240. if (level > 0) {
  241. builder.append(" ");
  242. } else {
  243. builder.append(index++).append(") ");
  244. }
  245. builder.append(permission);
  246. if (level > 0) {
  247. builder.append(" (from ").append(entity.getName()).append(")");
  248. } else {
  249. builder.append(" (own)");
  250. }
  251. builder.append("\n");
  252. }
  253. PermissionGroup[] parents;
  254. if (entity instanceof PermissionUser) {
  255. parents = ((PermissionUser) entity).getGroups(worldName);
  256. } else if (entity instanceof PermissionGroup) {
  257. parents = ((PermissionGroup) entity).getParentGroups(worldName);
  258. } else {
  259. throw new RuntimeException("Unknown class in hierarchy. Nag t3hk0d3 please.");
  260. }
  261. level++; // Just increment level once
  262. return builder.toString();
  263. }
  264. protected List<String> getPermissionsTree(PermissionEntity entity, String world, int level) {
  265. List<String> permissions = new LinkedList<String>();
  266. Map<String, String[]> allPermissions = entity.getAllPermissions();
  267. String[] worldsPermissions = allPermissions.get(world);
  268. if (worldsPermissions != null) {
  269. permissions.addAll(sprintPermissions(world, worldsPermissions));
  270. }
  271. for (String parentWorld : PermissionsEx.getPermissionManager().getWorldInheritance(world)) {
  272. if (parentWorld != null && !parentWorld.isEmpty()) {
  273. permissions.addAll(getPermissionsTree(entity, parentWorld, level + 1));
  274. }
  275. }
  276. if (level == 0 && allPermissions.get("") != null) { // default world permissions
  277. permissions.addAll(sprintPermissions("common", allPermissions.get("")));
  278. }
  279. return permissions;
  280. }
  281. protected List<String> sprintPermissions(String world, String[] permissions) {
  282. List<String> permissionList = new LinkedList<String>();
  283. if (permissions == null) {
  284. return permissionList;
  285. }
  286. for (String permission : permissions) {
  287. permissionList.add(permission + (world != null ? " @" + world : ""));
  288. }
  289. return permissionList;
  290. }
  291. protected Object parseValue(String value) {
  292. if (value == null) {
  293. return null;
  294. }
  295. if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
  296. return Boolean.parseBoolean(value);
  297. }
  298. try {
  299. return Integer.parseInt(value);
  300. } catch (NumberFormatException e) {
  301. }
  302. try {
  303. return Double.parseDouble(value);
  304. } catch (NumberFormatException e) {
  305. }
  306. return value;
  307. }
  308. protected void sendMessage(CommandSender sender, String message) {
  309. for (String messagePart : message.split("\n")) {
  310. sender.sendMessage(messagePart);
  311. }
  312. }
  313. }