Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. package ru.tehkode.permissions;
  2.  
  3. import java.util.HashSet;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.logging.Logger;
  9. import ru.tehkode.permissions.events.PermissionEntityEvent.Action;
  10.  
  11. public class PermissionGroup
  12. extends PermissionEntity
  13. implements Comparable<PermissionGroup>
  14. {
  15. protected int weight = 0;
  16. protected boolean dirtyWeight = true;
  17. private final PermissionsGroupData data;
  18.  
  19. public PermissionGroup(String groupName, PermissionsGroupData data, PermissionManager manager)
  20. {
  21. super(groupName, manager);
  22. this.data = data;
  23. }
  24.  
  25. protected PermissionsGroupData getData()
  26. {
  27. return this.data;
  28. }
  29.  
  30. public void initialize()
  31. {
  32. super.initialize();
  33. if (isDebug()) {
  34. this.manager.getLogger().info("Group " + getIdentifier() + " initialized");
  35. }
  36. }
  37.  
  38. public PermissionEntity.Type getType()
  39. {
  40. return PermissionEntity.Type.GROUP;
  41. }
  42.  
  43. public int getWeight()
  44. {
  45. if (this.dirtyWeight)
  46. {
  47. this.weight = getOptionInteger("weight", null, 0);
  48. this.dirtyWeight = false;
  49. }
  50. return this.weight;
  51. }
  52.  
  53. public void setWeight(int weight)
  54. {
  55. setOption("weight", Integer.toString(weight));
  56.  
  57. this.dirtyWeight = true;
  58. callEvent(PermissionEntityEvent.Action.WEIGHT_CHANGED);
  59. }
  60.  
  61. public boolean isRanked()
  62. {
  63. return getRank() > 0;
  64. }
  65.  
  66. public int getRank()
  67. {
  68. return getOwnOptionInteger("rank", null, 0);
  69. }
  70.  
  71. public void setRank(int rank)
  72. {
  73. if (rank > 0) {
  74. setOption("rank", Integer.toString(rank));
  75. } else {
  76. setOption("rank", null);
  77. }
  78. callEvent(PermissionEntityEvent.Action.RANK_CHANGED);
  79. }
  80.  
  81. public String getRankLadder()
  82. {
  83. return getOption("rank-ladder", "", "default");
  84. }
  85.  
  86. public void setRankLadder(String rankLadder)
  87. {
  88. if ((rankLadder.isEmpty()) || (rankLadder.equals("default"))) {
  89. rankLadder = null;
  90. }
  91. setOption("rank-ladder", rankLadder);
  92.  
  93. callEvent(PermissionEntityEvent.Action.RANK_CHANGED);
  94. }
  95.  
  96. public boolean isChildOf(PermissionGroup group, String worldName, boolean checkInheritance)
  97. {
  98. return isChildOf(group, worldName, checkInheritance ? new HashSet() : null);
  99. }
  100.  
  101. private boolean isChildOf(PermissionGroup group, String worldName, Set<String> visitedParents)
  102. {
  103. if (group == null) {
  104. return false;
  105. }
  106. if (visitedParents != null) {
  107. visitedParents.add(getIdentifier());
  108. }
  109. for (String parentGroup : getData().getParents(worldName)) {
  110. if ((visitedParents == null) || (!visitedParents.contains(parentGroup)))
  111. {
  112. if (group.getIdentifier().equals(parentGroup)) {
  113. return true;
  114. }
  115. if ((visitedParents != null) && (this.manager.getGroup(parentGroup).isChildOf(group, worldName, visitedParents))) {
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122.  
  123. public boolean isChildOf(PermissionGroup group, boolean checkInheritance)
  124. {
  125. for (String worldName : getWorlds()) {
  126. if (isChildOf(group, worldName, checkInheritance)) {
  127. return true;
  128. }
  129. }
  130. return isChildOf(group, null, checkInheritance);
  131. }
  132.  
  133. public boolean isChildOf(PermissionGroup group, String worldName)
  134. {
  135. return isChildOf(group, worldName, false);
  136. }
  137.  
  138. public boolean isChildOf(PermissionGroup group)
  139. {
  140. return isChildOf(group, false);
  141. }
  142.  
  143. public boolean isChildOf(String groupName, String worldName, boolean checkInheritance)
  144. {
  145. return isChildOf(this.manager.getGroup(groupName), worldName, checkInheritance);
  146. }
  147.  
  148. public boolean isChildOf(String groupName, boolean checkInheritance)
  149. {
  150. return isChildOf(this.manager.getGroup(groupName), checkInheritance);
  151. }
  152.  
  153. public boolean isChildOf(String groupName, String worldName)
  154. {
  155. return isChildOf(groupName, worldName, false);
  156. }
  157.  
  158. public boolean isChildOf(String groupName)
  159. {
  160. return isChildOf(groupName, false);
  161. }
  162.  
  163. public List<PermissionGroup> getChildGroups(String worldName)
  164. {
  165. return this.manager.getGroups(getIdentifier(), worldName, false);
  166. }
  167.  
  168. public List<PermissionGroup> getChildGroups()
  169. {
  170. return this.manager.getGroups(getIdentifier(), false);
  171. }
  172.  
  173. public List<PermissionGroup> getDescendantGroups(String worldName)
  174. {
  175. return this.manager.getGroups(getIdentifier(), worldName, true);
  176. }
  177.  
  178. public List<PermissionGroup> getDescendantGroups()
  179. {
  180. return this.manager.getGroups(getIdentifier(), true);
  181. }
  182.  
  183. public Set<PermissionUser> getUsers(String worldName)
  184. {
  185. return this.manager.getUsers(getIdentifier(), worldName, false);
  186. }
  187.  
  188. public Set<PermissionUser> getUsers()
  189. {
  190. return this.manager.getUsers(getIdentifier());
  191. }
  192.  
  193. public Set<PermissionUser> getActiveUsers()
  194. {
  195. return this.manager.getActiveUsers(getIdentifier());
  196. }
  197.  
  198. public Set<PermissionUser> getActiveUsers(boolean inheritance)
  199. {
  200. return this.manager.getActiveUsers(getIdentifier(), inheritance);
  201. }
  202.  
  203. public boolean isDefault(String worldName)
  204. {
  205. return getOwnOptionBoolean("default", worldName, false);
  206. }
  207.  
  208. public void setDefault(boolean def, String worldName)
  209. {
  210. setOption("default", String.valueOf(def), worldName);
  211. callEvent(PermissionEntityEvent.Action.DEFAULTGROUP_CHANGED);
  212. }
  213.  
  214. protected void clearCache()
  215. {
  216. for (PermissionUser user : getActiveUsers()) {
  217. user.clearCache();
  218. }
  219. }
  220.  
  221. public final void remove()
  222. {
  223. for (String world : getWorlds()) {
  224. clearChildren(world);
  225. }
  226. clearChildren(null);
  227. super.remove();
  228. }
  229.  
  230. private void clearChildren(String worldName)
  231. {
  232. for (PermissionGroup group : getChildGroups(worldName))
  233. {
  234. List<PermissionGroup> parentGroups = new LinkedList(group.getOwnParents(worldName));
  235. parentGroups.remove(this);
  236.  
  237. group.setParents(parentGroups, worldName);
  238. }
  239. for (PermissionUser user : getUsers(worldName)) {
  240. user.removeGroup(this, worldName);
  241. }
  242. }
  243.  
  244. public int compareTo(PermissionGroup o)
  245. {
  246. return getWeight() - o.getWeight();
  247. }
  248.  
  249. @Deprecated
  250. public String[] getParentGroupsNames(String worldName)
  251. {
  252. return (String[])getParentIdentifiers(worldName).toArray(new String[0]);
  253. }
  254.  
  255. @Deprecated
  256. public String[] getParentGroupsNames()
  257. {
  258. return (String[])getParentIdentifiers().toArray(new String[0]);
  259. }
  260.  
  261. @Deprecated
  262. public void setParentGroups(List<String> parentGroups, String worldName)
  263. {
  264. setParentsIdentifier(parentGroups, worldName);
  265. }
  266.  
  267. @Deprecated
  268. public void setParentGroups(List<String> parentGroups)
  269. {
  270. setParentsIdentifier(parentGroups);
  271. }
  272.  
  273. @Deprecated
  274. public void setParentGroupObjects(List<PermissionGroup> parentGroups, String worldName)
  275. {
  276. setParents(parentGroups, worldName);
  277. }
  278.  
  279. @Deprecated
  280. public void setParentGroupObjects(List<PermissionGroup> parentGroups)
  281. {
  282. setParents(parentGroups);
  283. }
  284.  
  285. @Deprecated
  286. public List<PermissionGroup> getParentGroups(String worldName)
  287. {
  288. return getParents(worldName);
  289. }
  290.  
  291. @Deprecated
  292. public List<PermissionGroup> getParentGroups()
  293. {
  294. return getParentGroups(null);
  295. }
  296.  
  297. @Deprecated
  298. public Map<String, List<PermissionGroup>> getAllParentGroups()
  299. {
  300. return getAllParents();
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement