PageRenderTime 69ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/nijiko/permissions/Control.java

https://github.com/Travo97/Permissions
Java | 1113 lines | 791 code | 260 blank | 62 comment | 229 complexity | d91abbfcb9eb2b7925cdeedbffe04031 MD5 | raw file
  1. package com.nijiko.permissions;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.HashMap;
  6. import java.util.HashSet;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Set;
  11. import java.util.logging.Logger;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.util.config.Configuration;
  14. import com.nijiko.Messaging;
  15. import com.nijiko.configuration.NotNullConfiguration;
  16. import com.nijikokun.bukkit.Permissions.FileManager;
  17. import com.nijikokun.bukkit.Permissions.Permissions;
  18. /**
  19. * Permissions 2.x
  20. * Copyright (C) 2011 Matt 'The Yeti' Burnett <admin@theyeticave.net>
  21. * Original Credit & Copyright (C) 2010 Nijikokun <nijikokun@gmail.com>
  22. *
  23. * This program is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Permissions Public License as published by
  25. * the Free Software Foundation, either version 2 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU Permissions Public License for more details.
  32. *
  33. * You should have received a copy of the GNU Permissions Public License
  34. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35. */
  36. /**
  37. * iControl.java
  38. * Permission handler
  39. *
  40. * @author Nijiko
  41. */
  42. public class Control extends PermissionHandler {
  43. public static final Logger log = Logger.getLogger("Minecraft");
  44. private List<String> Worlds = new LinkedList<String>();
  45. private Map<String, Configuration> WorldConfiguration = new HashMap<String, Configuration>();
  46. private Set<String> WorldConfigurationModification = new HashSet<String>();
  47. private Map<String, String> WorldBase = new HashMap<String, String>();
  48. private Map<String, String> WorldInheritance = new HashMap<String, String>();
  49. private Map<String, Map<String, Set<String>>> WorldUserPermissions = new HashMap<String, Map<String, Set<String>>>();
  50. private Map<String, Map<String, String>> WorldUserGroups = new HashMap<String, Map<String, String>>();
  51. private Map<String, Map<String, Set<String>>> WorldGroups = new HashMap<String, Map<String, Set<String>>>();
  52. private Map<String, Map<String, Object[]>> WorldGroupsData = new HashMap<String, Map<String, Object[]>>();
  53. private Map<String, Map<String, Set<String>>> WorldGroupsInheritance = new HashMap<String, Map<String, Set<String>>>();
  54. private Map<String, Map<String, Boolean>> WorldCache = new HashMap<String, Map<String, Boolean>>();
  55. private String defaultWorld = "";
  56. private Configuration config;
  57. public Control(Configuration config) {
  58. this.config = config;
  59. }
  60. public void reload() {
  61. this.clearAllCache();
  62. final List<String> w = new LinkedList<String>(Worlds);
  63. Worlds = new LinkedList<String>();
  64. synchronized (w) {
  65. for (String world : w) {
  66. this.forceLoadWorld(world);
  67. }
  68. }
  69. }
  70. public boolean reload(String world) {
  71. if (this.Worlds.contains(world)) {
  72. this.clearCache(world);
  73. synchronized (Worlds) {
  74. this.Worlds.remove(world);
  75. }
  76. this.forceLoadWorld(world);
  77. return true;
  78. }
  79. return false;
  80. }
  81. public void setDefaultWorld(String world) {
  82. this.defaultWorld = world;
  83. }
  84. public boolean loadWorld(String world) {
  85. if(!this.Worlds.contains(world)) {
  86. this.load(world, new NotNullConfiguration(new File(Permissions.instance.getDataFolder().getPath() + File.separator + world + ".yml")));
  87. log.info("Loaded world: " + world);
  88. return true;
  89. }
  90. return false;
  91. }
  92. public void forceLoadWorld(String world) {
  93. this.load(world, new NotNullConfiguration(new File(Permissions.instance.getDataFolder().getPath() + File.separator + world + ".yml")));
  94. }
  95. public boolean checkWorld(String world) {
  96. if(this.Worlds.contains(world)) {
  97. return true;
  98. } else {
  99. return false;
  100. }
  101. }
  102. public void load() {
  103. if(this.defaultWorld == null || this.defaultWorld.equals("")) {
  104. return;
  105. }
  106. this.load(this.defaultWorld, this.config);
  107. }
  108. @SuppressWarnings("unused")
  109. public void load(String world, Configuration config) {
  110. if (!(new File(Permissions.instance.getDataFolder().getPath() + File.separator + world + ".yml").exists())) {
  111. FileManager file = new FileManager(Permissions.instance.getDataFolder().getPath() + File.separator, world + ".yml", true);
  112. }
  113. config.load();
  114. this.Worlds.add(world);
  115. this.WorldConfiguration.put(world, config);
  116. if(!world.equals(this.defaultWorld)) {
  117. if(!config.getString("plugin.permissions.copies", "").isEmpty()) {
  118. this.WorldInheritance.put(world, config.getString("plugin.permissions.copies", ""));
  119. return;
  120. }
  121. if (!(new File(Permissions.instance.getDataFolder().getPath() + File.separator + world + ".yml").exists())) {
  122. this.WorldInheritance.put(world, defaultWorld);
  123. }
  124. }
  125. this.WorldBase.put(world, "");
  126. this.WorldCache.put(world, new HashMap<String, Boolean>());
  127. this.WorldUserPermissions.put(world, new HashMap<String, Set<String>>());
  128. this.WorldUserGroups.put(world, new HashMap<String, String>());
  129. this.WorldGroups.put(world, new HashMap<String, Set<String>>());
  130. this.WorldGroupsData.put(world, new HashMap<String, Object[]>());
  131. this.WorldGroupsInheritance.put(world, new HashMap<String, Set<String>>());
  132. // Grab the keys we are going to need
  133. List<String> userKeys = config.getKeys("users");
  134. List<String> groupKeys = config.getKeys("groups");
  135. // Permission set.
  136. Set<String> Permissions = new HashSet<String>();
  137. Set<String> Inheritance = new HashSet<String>();
  138. // Permission list
  139. List<String> permissions;
  140. List<String> inheritance;
  141. // Group
  142. String group;
  143. if (groupKeys != null) {
  144. for (String key : groupKeys) {
  145. Inheritance = new HashSet<String>();
  146. Permissions = new HashSet<String>();
  147. // Configuration
  148. inheritance = config.getStringList("groups." + key + ".inheritance", null);
  149. permissions = config.getStringList("groups." + key + ".permissions", null);
  150. boolean Default = config.getBoolean("groups." + key + ".default", false);
  151. String prefix = config.getString("groups." + key + ".info.prefix", null);
  152. String suffix = config.getString("groups." + key + ".info.suffix", null);
  153. boolean build = config.getBoolean("groups." + key + ".info.build", false);
  154. if (Default && ( (this.WorldBase.get(world)==null)||(this.WorldBase.get(world).isEmpty()) ) ) {
  155. this.WorldBase.put(world, key.toLowerCase());
  156. }
  157. if (inheritance.size() > 0) {
  158. Inheritance.addAll(inheritance);
  159. }
  160. if (permissions.size() > 0) {
  161. Permissions.addAll(permissions);
  162. }
  163. this.WorldGroups.get(world).put(key.toLowerCase(), Permissions);
  164. this.WorldGroupsData.get(world).put(key.toLowerCase(), new Object[]{key, prefix, suffix, build});
  165. if (Inheritance.size() > 0) {
  166. this.WorldGroupsInheritance.get(world).put(key.toLowerCase(), Inheritance);
  167. }
  168. }
  169. }
  170. if (userKeys != null) {
  171. for (String key : userKeys) {
  172. Permissions = new HashSet<String>();
  173. // Configuration
  174. permissions = config.getStringList("users." + key + ".permissions", null);
  175. group = config.getString("users." + key + ".group");
  176. if (group != null) {
  177. if (!group.isEmpty()) {
  178. this.WorldUserGroups.get(world).put(key.toLowerCase(), group);
  179. }
  180. } else {
  181. this.WorldUserGroups.get(world).put(key.toLowerCase(), this.WorldBase.get(world));
  182. }
  183. if (permissions!=null && permissions.size() > 0) {
  184. Permissions.addAll(permissions);
  185. }
  186. this.WorldUserPermissions.get(world).put(key.toLowerCase(), Permissions);
  187. }
  188. }
  189. }
  190. @SuppressWarnings("unused")
  191. private String toArrayListString(Collection<String> variable) {
  192. return new ArrayList<String>(variable).toString();
  193. }
  194. /**
  195. * Simple alias for permission method.
  196. * Easier to understand / recognize what it does and is checking for.
  197. *
  198. * @param player
  199. * @param permission
  200. * @return boolean
  201. */
  202. public boolean has(Player player, String permission) {
  203. return this.permission(player, permission);
  204. }
  205. public boolean has(String world, String playerName, String permission) {
  206. return permission(world, playerName, permission);
  207. }
  208. public boolean permission(Player player, String permission) {
  209. return permission( player.getWorld().getName(), player.getName(), permission);
  210. }
  211. public boolean permission(String world, String playerName, String permission) {
  212. Set<String> Permissions = new HashSet<String>();
  213. Set<String> GroupPermissions = new HashSet<String>();
  214. Set<String> GroupInheritedPermissions = new HashSet<String>();
  215. String group = "";
  216. playerName = playerName.toLowerCase();
  217. world = world.toLowerCase();
  218. // Fix to disable console users getting errors
  219. if (playerName == null && world == null)
  220. {
  221. return true;
  222. }
  223. // Load if it isn't already
  224. this.loadWorld(world);
  225. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  226. world = this.WorldInheritance.get(world);
  227. this.loadWorld(world);
  228. }
  229. if (this.WorldCache.get(world).containsKey(playerName + "," + permission)) {
  230. return this.WorldCache.get(world).get(playerName + "," + permission);
  231. }
  232. Map<String, Set<String>> UserPermissions = this.WorldUserPermissions.get(world);
  233. Map<String, Set<String>> Groups = this.WorldGroups.get(world);
  234. Map<String, Set<String>> GroupsInheritance = this.WorldGroupsInheritance.get(world);
  235. Map<String, Boolean> Cached = this.WorldCache.get(world);
  236. String base = this.WorldBase.get(world);
  237. if (this.WorldUserPermissions.get(world).containsKey(playerName)) {
  238. Permissions = UserPermissions.get(playerName);
  239. group = getGroup(world, playerName).toLowerCase();
  240. if ( Groups != null && !Groups.isEmpty()) {
  241. if (Groups.containsKey(group)) {
  242. GroupPermissions = Groups.get(group);
  243. }
  244. if (GroupsInheritance.containsKey(group)) {
  245. GroupInheritedPermissions = getInheritancePermissions(world, group);
  246. }
  247. } else {
  248. Cached.put(playerName + "," + permission, false);
  249. return false;
  250. }
  251. } else {
  252. if (base == null || base.isEmpty()) {
  253. Cached.put(playerName + "," + permission, false);
  254. return false;
  255. }
  256. group = base.toLowerCase();
  257. if (Groups != null && !Groups.isEmpty()) {
  258. if (Groups.containsKey(group)) {
  259. GroupPermissions = Groups.get(group);
  260. }
  261. if (GroupsInheritance.containsKey(group)) {
  262. GroupInheritedPermissions = getInheritancePermissions(world, group);
  263. }
  264. } else {
  265. Cached.put(playerName + "," + permission, false);
  266. return false;
  267. }
  268. }
  269. String[] nodeHierachy = permission.split("\\.");
  270. if (GroupInheritedPermissions.size() > 0) {
  271. GroupPermissions.addAll(GroupInheritedPermissions);
  272. }
  273. if (Permissions == null || GroupPermissions == null) {
  274. Cached.put(playerName + "," + permission, false);
  275. return false;
  276. }
  277. if(GroupPermissions.contains("-" + permission) || Permissions.contains("-" + permission)) {
  278. Cached.put(playerName + "," + permission, false);
  279. return false;
  280. }
  281. if (GroupPermissions.contains("*") || Permissions.contains("*")) {
  282. Cached.put(playerName + "," + permission, true);
  283. return true;
  284. }
  285. if (GroupPermissions.contains(permission) || Permissions.contains(permission)) {
  286. Cached.put(playerName + "," + permission, true);
  287. return true;
  288. }
  289. if (permission.contains(".")) {
  290. String setting = "";
  291. String node = "";
  292. for (String nextLevel : nodeHierachy) {
  293. setting += nextLevel + ".";
  294. node = setting + "*";
  295. if (GroupPermissions.contains(node) || Permissions.contains(node)) {
  296. Cached.put(playerName + "," + permission, true);
  297. return true;
  298. }
  299. else {
  300. continue;
  301. }
  302. }
  303. }
  304. Cached.put(playerName + "," + permission, false);
  305. return false;
  306. }
  307. private Set<String> getInheritance(String world, String group) {
  308. if (this.WorldGroupsInheritance.containsKey(world)) {
  309. Map<String, Set<String>> WorldGroupInheritance = this.WorldGroupsInheritance.get(world);
  310. if (WorldGroupInheritance.size() > 0) {
  311. if(WorldGroupInheritance.containsKey(group.toLowerCase())) {
  312. return WorldGroupInheritance.get(group.toLowerCase());
  313. }
  314. }
  315. }
  316. return new HashSet<String>();
  317. }
  318. @SuppressWarnings("unchecked")
  319. private Object[] getInheritancePermissions(String world, Set<String> Permissions, Set<String> Inheritance, Set<String> Checked, String group) {
  320. Map<String, Set<String>> Groups = this.WorldGroups.get(world);
  321. group = group.toLowerCase();
  322. if (Inheritance.size() > 0) {
  323. for (String inherited : Inheritance) {
  324. inherited = inherited.toLowerCase();
  325. Set<String> GroupPermissions = Groups.get(inherited.toLowerCase());
  326. Set<String> GottenInheritance = getInheritance(world, inherited);
  327. if (GroupPermissions == null) {
  328. continue;
  329. }
  330. if (GroupPermissions.size() > 0) {
  331. Permissions.addAll(GroupPermissions);
  332. }
  333. if (!Checked.contains(inherited)) {
  334. Checked.add(inherited);
  335. Object[] InheritedPermissions = getInheritancePermissions(world, Permissions, GottenInheritance, Checked, inherited);
  336. if (((Set<String>) InheritedPermissions[0]).size() > 0) {
  337. Permissions.addAll((Set<String>) InheritedPermissions[0]);
  338. }
  339. }
  340. }
  341. } else {
  342. Set<String> GroupPermissions = Groups.get(group);
  343. if (GroupPermissions.size() > 0) {
  344. Permissions.addAll(GroupPermissions);
  345. }
  346. }
  347. return new Object[]{Permissions, Checked};
  348. }
  349. @SuppressWarnings("unchecked")
  350. private Set<String> getInheritancePermissions(String world, String group) {
  351. group = group.toLowerCase();
  352. Map<String, Set<String>> Groups = this.WorldGroups.get(world);
  353. Set<String> Permissions = new HashSet<String>();
  354. Set<String> Inheritance = getInheritance(world, group);
  355. Set<String> Checked = new HashSet<String>();
  356. if (Inheritance.size() > 0 && !Inheritance.isEmpty()) {
  357. for (String inherited : Inheritance) {
  358. inherited = inherited.toLowerCase();
  359. Set<String> GroupPermissions = Groups.get(inherited);
  360. if (GroupPermissions == null) {
  361. continue;
  362. }
  363. if (GroupPermissions.size() > 0) {
  364. Permissions.addAll(GroupPermissions);
  365. }
  366. if (getInheritance(world, inherited).size() > 0 && !Checked.contains(inherited)) {
  367. Checked.add(inherited);
  368. Object[] InheritedPermissions = getInheritancePermissions(world, Permissions, getInheritance(world, inherited), Checked, inherited);
  369. if (((Set<String>) InheritedPermissions[0]).size() > 0) {
  370. Permissions.addAll((Set<String>) InheritedPermissions[0]);
  371. }
  372. }
  373. }
  374. }
  375. return Permissions;
  376. }
  377. public boolean inGroup(String world, String name, String group) {
  378. this.loadWorld(world);
  379. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  380. world = this.WorldInheritance.get(world);
  381. this.loadWorld(world);
  382. }
  383. name = name.toLowerCase();
  384. group = group.toLowerCase();
  385. if (this.WorldUserPermissions.get(world).containsKey(name)) {
  386. String Group = (String) this.WorldUserGroups.get(world).get(name);
  387. Set<String> Inherited = getInheritance(world, Group);
  388. if (Inherited.contains(group) || Group.equalsIgnoreCase(group)) {
  389. return true;
  390. }
  391. }
  392. return false;
  393. }
  394. public boolean inSingleGroup(String world, String name, String group) {
  395. this.loadWorld(world);
  396. name = name.toLowerCase();
  397. group = group.toLowerCase();
  398. if (this.WorldUserPermissions.get(world).containsKey(name)) {
  399. String Group = (String) this.WorldUserGroups.get(world).get(name);
  400. if (Group.equalsIgnoreCase(group)) {
  401. return true;
  402. }
  403. }
  404. return false;
  405. }
  406. public String getGroup(String world, String name) {
  407. this.loadWorld(world);
  408. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  409. world = this.WorldInheritance.get(world);
  410. this.loadWorld(world);
  411. }
  412. name = name.toLowerCase();
  413. if (this.WorldUserPermissions.get(world).containsKey(name) && this.WorldUserGroups.get(world).containsKey(name)) {
  414. String group = (String) ((Object[]) this.WorldGroupsData.get(world).get(this.WorldUserGroups.get(world).get(name).toLowerCase()))[0];
  415. if (group != null) {
  416. return group;
  417. }
  418. }
  419. if (this.WorldBase.get(world).equals("")) {
  420. return null;
  421. } else {
  422. String group = (String) ((Object[]) this.WorldGroupsData.get(world).get(this.WorldBase.get(world)))[0];
  423. return (group == null) ? null : group;
  424. }
  425. }
  426. public String getGroupPrefix(String world, String group) {
  427. this.loadWorld(world);
  428. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  429. world = this.WorldInheritance.get(world);
  430. this.loadWorld(world);
  431. }
  432. group = group.toLowerCase();
  433. if (this.WorldGroups.get(world).containsKey(group)) {
  434. String prefix = (String) ((Object[]) this.WorldGroupsData.get(world).get(group))[1];
  435. return (prefix == null) ? null : Messaging.parse(prefix);
  436. } else {
  437. return null;
  438. }
  439. }
  440. public String getGroupSuffix(String world, String group) {
  441. this.loadWorld(world);
  442. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  443. world = this.WorldInheritance.get(world);
  444. this.loadWorld(world);
  445. }
  446. group = group.toLowerCase();
  447. if (this.WorldGroups.get(world).containsKey(group)) {
  448. String suffix = (String) ((Object[]) this.WorldGroupsData.get(world).get(group))[2];
  449. return (suffix == null) ? null : Messaging.parse(suffix);
  450. } else {
  451. return null;
  452. }
  453. }
  454. public boolean canGroupBuild(String world, String group) {
  455. this.loadWorld(world);
  456. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  457. world = this.WorldInheritance.get(world);
  458. this.loadWorld(world);
  459. }
  460. group = group.toLowerCase();
  461. if (this.WorldGroups.get(world).containsKey(group)) {
  462. return (Boolean) ((Object[]) this.WorldGroupsData.get(world).get(group))[3];
  463. } else {
  464. if (this.WorldBase.get(world).equals("")) {
  465. return false;
  466. } else {
  467. return (Boolean) ((Object[]) this.WorldGroupsData.get(world).get(this.WorldBase.get(world)))[3];
  468. }
  469. }
  470. }
  471. public String[] getGroups(String world, String name) {
  472. this.loadWorld(world);
  473. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  474. world = this.WorldInheritance.get(world);
  475. this.loadWorld(world);
  476. }
  477. String Group = (String) this.WorldUserGroups.get(world).get(name.toLowerCase());
  478. if (Group == null)
  479. {
  480. Group = (String) ((Object[]) this.WorldGroupsData.get(world).get(this.WorldBase.get(world)))[0];
  481. }
  482. Set<String> Inherited = getInheritance(world, Group.toLowerCase());
  483. Inherited.add(Group.toLowerCase());
  484. return Inherited.toArray(new String[0]);
  485. }
  486. public void setCache(String world, Map<String, Boolean> Cache) {
  487. this.loadWorld(world);
  488. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  489. world = this.WorldInheritance.get(world);
  490. this.loadWorld(world);
  491. }
  492. if(this.Worlds.contains(world)) {
  493. this.WorldCache.put(world, Cache);
  494. }
  495. }
  496. public void setCacheItem(String world, String player, String permission, boolean data) {
  497. this.loadWorld(world);
  498. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  499. world = this.WorldInheritance.get(world);
  500. this.loadWorld(world);
  501. }
  502. if(this.Worlds.contains(world)) {
  503. this.WorldCache.get(world).put(player + "," + permission, data);
  504. }
  505. }
  506. public Map<String, Boolean> getCache(String world) {
  507. this.loadWorld(world);
  508. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  509. world = this.WorldInheritance.get(world);
  510. this.loadWorld(world);
  511. }
  512. if(this.Worlds.contains(world)) {
  513. return this.WorldCache.get(world);
  514. }
  515. return new HashMap<String, Boolean>();
  516. }
  517. public boolean getCacheItem(String world, String player, String permission) {
  518. this.loadWorld(world);
  519. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  520. world = this.WorldInheritance.get(world);
  521. this.loadWorld(world);
  522. }
  523. if(this.WorldInheritance.containsKey(world)) {
  524. world = this.WorldInheritance.get(world);
  525. this.loadWorld(world);
  526. }
  527. if(this.Worlds.contains(world)) {
  528. if (this.WorldCache.get(world).containsKey(player + "," + permission)) {
  529. return this.WorldCache.get(world).get(player + "," + permission);
  530. }
  531. }
  532. return false;
  533. }
  534. public void removeCachedItem(String world, String player, String permission) {
  535. this.loadWorld(world);
  536. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  537. world = this.WorldInheritance.get(world);
  538. this.loadWorld(world);
  539. }
  540. if(this.WorldInheritance.containsKey(world)) {
  541. world = this.WorldInheritance.get(world);
  542. this.loadWorld(world);
  543. }
  544. if(this.Worlds.contains(world)) {
  545. if (this.WorldCache.get(world).containsKey(player + "," + permission)) {
  546. this.WorldCache.get(world).remove(player + "," + permission);
  547. }
  548. }
  549. }
  550. public void clearCache() {
  551. this.WorldCache.put(this.defaultWorld, new HashMap<String, Boolean>());
  552. }
  553. public void clearAllCache() {
  554. for(String world : this.WorldCache.keySet()) {
  555. this.WorldCache.put(world, new HashMap<String, Boolean>());
  556. }
  557. }
  558. public void clearCache(String world) {
  559. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  560. world = this.WorldInheritance.get(world);
  561. }
  562. if(this.Worlds.contains(world)) {
  563. this.WorldCache.put(world, new HashMap<String, Boolean>());
  564. }
  565. }
  566. //Fixed functions by rcjrrjcr
  567. public void addGroupPermission(String world, String group, String node) {
  568. this.loadWorld(world);
  569. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  570. world = this.WorldInheritance.get(world);
  571. this.loadWorld(world);
  572. }
  573. List<String> list = this.WorldConfiguration.get(world).getStringList("groups." + group + ".permissions", new LinkedList<String>());
  574. list.add(node);
  575. this.WorldConfiguration.get(world).setProperty("groups." + group + ".permissions", list);
  576. this.WorldConfigurationModification.add(world);
  577. //MODIFICATION START
  578. Set<String> groupPerms = this.WorldGroups.get(world).get(group.toLowerCase());
  579. if(groupPerms==null) groupPerms = new HashSet<String>();
  580. groupPerms.add(node);
  581. this.WorldGroups.get(world).put(group, groupPerms);
  582. this.clearCache(world); //TODO: Use more efficient method to clear cache for affected users
  583. //MODIFICATION END
  584. }
  585. public void removeGroupPermission(String world, String group, String node) {
  586. this.loadWorld(world);
  587. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  588. world = this.WorldInheritance.get(world);
  589. this.loadWorld(world);
  590. }
  591. List<String> list = this.WorldConfiguration.get(world).getStringList("groups." + group + ".permissions", new LinkedList<String>());
  592. if(list.contains(node)) {
  593. list.remove(node);
  594. }
  595. this.WorldConfiguration.get(world).setProperty("groups." + group + ".permissions", list);
  596. this.WorldConfigurationModification.add(world);
  597. //MODIFICATION START
  598. Set<String> groupPerms = this.WorldGroups.get(world).get(group.toLowerCase());
  599. if(groupPerms==null) groupPerms = new HashSet<String>();
  600. groupPerms.remove(node);
  601. this.WorldGroups.get(world).put(group, groupPerms);
  602. this.clearCache(world); //TODO: Use more efficient method to clear cache for affected users
  603. //MODIFICATION END
  604. }
  605. public void addGroupInfo(String world, String group, String node, Object data) {
  606. this.loadWorld(world);
  607. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  608. world = this.WorldInheritance.get(world);
  609. this.loadWorld(world);
  610. }
  611. this.WorldConfiguration.get(world).setProperty("groups." + group + ".info." + node, data);
  612. this.WorldConfigurationModification.add(world);
  613. //MODIFICATION START
  614. Object[] groupData = this.WorldGroupsData.get(world).get(group.toLowerCase());
  615. if(groupData == null) groupData = new Object[]{group,"","",false};
  616. if(data instanceof Boolean && node.equals("build")) groupData[3] = data;
  617. else if (data instanceof String)
  618. {
  619. if(node.equals("prefix")) groupData[1]= data;
  620. else if(node.equals("suffix")) groupData[2]= data;
  621. }
  622. this.WorldGroupsData.get(world).put(group.toLowerCase(), groupData);
  623. //MODIFICATION END
  624. }
  625. public void removeGroupInfo(String world, String group, String node) {
  626. this.loadWorld(world);
  627. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  628. world = this.WorldInheritance.get(world);
  629. this.loadWorld(world);
  630. }
  631. this.WorldConfiguration.get(world).removeProperty("groups." + group + ".info." + node);
  632. this.WorldConfigurationModification.add(world);
  633. //MODIFICATION START
  634. Object[] groupData = this.WorldGroupsData.get(world).get(group.toLowerCase());
  635. if(groupData == null) groupData = new Object[]{group,"","",false};
  636. if(node.equals("build")) groupData[3] = false;
  637. else if(node.equals("prefix")) groupData[1]= "";
  638. else if(node.equals("suffix")) groupData[2]= "";
  639. this.WorldGroupsData.get(world).put(group.toLowerCase(), groupData);
  640. //MODIFICATION END
  641. }
  642. public void addUserPermission(String world, String user, String node) {
  643. this.loadWorld(world);
  644. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  645. world = this.WorldInheritance.get(world);
  646. this.loadWorld(world);
  647. }
  648. List<String> list = this.WorldConfiguration.get(world).getStringList("users." + user + ".permissions", new LinkedList<String>());
  649. list.add(node);
  650. this.WorldConfiguration.get(world).setProperty("users." + user + ".permissions", list);
  651. this.WorldConfigurationModification.add(world);
  652. //MODIFICATION START
  653. Set<String> userPerms = this.WorldUserPermissions.get(world).get(user.toLowerCase());
  654. if(userPerms==null) userPerms = new HashSet<String>();
  655. userPerms.add(node);
  656. this.WorldUserPermissions.get(world).put(user.toLowerCase(), userPerms);
  657. // boolean negated = node.startsWith("-");
  658. // String actualNode = negated ? node.substring(1) : node;
  659. // this.setCacheItem(world, user.toLowerCase(), actualNode, !negated);
  660. this.removeCachedItem(world, user.toLowerCase(), node);
  661. //MODIFICATION END
  662. }
  663. public void removeUserPermission(String world, String user, String node) {
  664. this.loadWorld(world);
  665. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  666. world = this.WorldInheritance.get(world);
  667. this.loadWorld(world);
  668. }
  669. List<String> list = this.WorldConfiguration.get(world).getStringList("users." + user + ".permissions", new LinkedList<String>());
  670. if(list.contains(node)) {
  671. list.remove(node);
  672. }
  673. this.WorldConfiguration.get(world).setProperty("users." + user + ".permissions", list);
  674. this.WorldConfigurationModification.add(world);
  675. //MODIFICATION START
  676. Set<String> userPerms = this.WorldUserPermissions.get(world).get(user.toLowerCase());
  677. if(userPerms==null) userPerms = new HashSet<String>();
  678. userPerms.remove(node);
  679. this.WorldUserPermissions.get(world).put(user.toLowerCase(), userPerms);
  680. // boolean negated = node.startsWith("-");
  681. // String actualNode = negated ? node.substring(1) : node;
  682. // this.setCacheItem(world, user.toLowerCase(), actualNode, negated);
  683. this.removeCachedItem(world, user.toLowerCase(), node);
  684. //MODIFICATION END
  685. }
  686. //End of fixes by rcjrrjcr
  687. public void addUserInfo(String world, String user, String node, Object data) {
  688. this.loadWorld(world);
  689. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  690. world = this.WorldInheritance.get(world);
  691. this.loadWorld(world);
  692. }
  693. this.WorldConfiguration.get(world).setProperty("users." + user + ".info." + node, data);
  694. this.WorldConfigurationModification.add(world);
  695. }
  696. public void removeUserInfo(String world, String user, String node) {
  697. this.loadWorld(world);
  698. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  699. world = this.WorldInheritance.get(world);
  700. this.loadWorld(world);
  701. }
  702. this.WorldConfiguration.get(world).removeProperty("users." + user + ".info." + node);
  703. this.WorldConfigurationModification.add(world);
  704. }
  705. public String getGroupPermissionString(String world, String group, String permission) {
  706. this.loadWorld(world);
  707. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  708. world = this.WorldInheritance.get(world);
  709. this.loadWorld(world);
  710. }
  711. return this.WorldConfiguration.get(world).getString("groups." + group + ".info." + permission, "");
  712. }
  713. public int getGroupPermissionInteger(String world, String group, String permission) {
  714. this.loadWorld(world);
  715. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  716. world = this.WorldInheritance.get(world);
  717. this.loadWorld(world);
  718. }
  719. return this.WorldConfiguration.get(world).getInt("groups." + group + ".info." + permission, -1);
  720. }
  721. public boolean getGroupPermissionBoolean(String world, String group, String permission) {
  722. this.loadWorld(world);
  723. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  724. world = this.WorldInheritance.get(world);
  725. this.loadWorld(world);
  726. }
  727. return this.WorldConfiguration.get(world).getBoolean("groups." + group + ".info." + permission, false);
  728. }
  729. public double getGroupPermissionDouble(String world, String group, String permission) {
  730. this.loadWorld(world);
  731. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  732. world = this.WorldInheritance.get(world);
  733. this.loadWorld(world);
  734. }
  735. return this.WorldConfiguration.get(world).getDouble("groups." + group + ".info." + permission, -1.0);
  736. }
  737. public String getUserPermissionString(String world, String name, String permission) {
  738. this.loadWorld(world);
  739. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  740. world = this.WorldInheritance.get(world);
  741. this.loadWorld(world);
  742. }
  743. return this.WorldConfiguration.get(world).getString("users." + name + ".info." + permission,"");
  744. }
  745. public int getUserPermissionInteger(String world, String name, String permission) {
  746. this.loadWorld(world);
  747. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  748. world = this.WorldInheritance.get(world);
  749. this.loadWorld(world);
  750. }
  751. return this.WorldConfiguration.get(world).getInt("users." + name + ".info." + permission, -1);
  752. }
  753. public boolean getUserPermissionBoolean(String world, String name, String permission) {
  754. this.loadWorld(world);
  755. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  756. world = this.WorldInheritance.get(world);
  757. this.loadWorld(world);
  758. }
  759. return this.WorldConfiguration.get(world).getBoolean("users." + name + ".info." + permission, false);
  760. }
  761. public double getUserPermissionDouble(String world, String name, String permission) {
  762. this.loadWorld(world);
  763. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  764. world = this.WorldInheritance.get(world);
  765. this.loadWorld(world);
  766. }
  767. return this.WorldConfiguration.get(world).getDouble("users." + name + ".info." + permission, -1.0);
  768. }
  769. public String getPermissionString(String world, String name, String permission) {
  770. this.loadWorld(world);
  771. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  772. world = this.WorldInheritance.get(world);
  773. this.loadWorld(world);
  774. }
  775. String group = this.getGroup(world, name);
  776. String userPermission = this.getUserPermissionString(world, name, permission);
  777. String userGroupPermission = "";
  778. if (group != null) {
  779. userGroupPermission = this.getGroupPermissionString(world, group, permission);
  780. }
  781. if (!userPermission.equalsIgnoreCase("")) {
  782. return userPermission;
  783. }
  784. return userGroupPermission;
  785. }
  786. public boolean getPermissionBoolean(String world, String name, String permission) {
  787. this.loadWorld(world);
  788. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  789. world = this.WorldInheritance.get(world);
  790. this.loadWorld(world);
  791. }
  792. String group = this.getGroup(world, name);
  793. boolean userPermission = this.getUserPermissionBoolean(world, name, permission);
  794. boolean userGroupPermission = false;
  795. if (group != null) {
  796. userGroupPermission = this.getGroupPermissionBoolean(world, group, permission);
  797. }
  798. if (userPermission) {
  799. return userPermission;
  800. }
  801. return userGroupPermission;
  802. }
  803. @SuppressWarnings("null")
  804. public int getPermissionInteger(String world, String name, String permission) {
  805. this.loadWorld(world);
  806. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  807. world = this.WorldInheritance.get(world);
  808. this.loadWorld(world);
  809. }
  810. String group = this.getGroup(world, name);
  811. int userPermission = this.getUserPermissionInteger(world, name, permission);
  812. int userGroupPermission = -1;
  813. if (group != null || !group.isEmpty()) {
  814. userGroupPermission = this.getGroupPermissionInteger(world, group, permission);
  815. }
  816. if (userPermission != -1) {
  817. return userPermission;
  818. }
  819. return userGroupPermission;
  820. }
  821. public double getPermissionDouble(String world, String name, String permission) {
  822. this.loadWorld(world);
  823. if(this.WorldInheritance.containsKey(world) && !world.equals(this.defaultWorld)) {
  824. world = this.WorldInheritance.get(world);
  825. this.loadWorld(world);
  826. }
  827. String group = this.getGroup(world, name);
  828. double userPermission = this.getUserPermissionDouble(world, name, permission);
  829. double userGroupPermission = -1.0;
  830. if (group != null) {
  831. userGroupPermission = this.getGroupPermissionDouble(world, group, permission);
  832. }
  833. if (userPermission != -1.0) {
  834. return userPermission;
  835. }
  836. return userGroupPermission;
  837. }
  838. //Addition by rcjrrjcr
  839. @Override
  840. public void save(String world)
  841. {
  842. Configuration worldConfig = this.WorldConfiguration.get(world);
  843. if(worldConfig!=null) worldConfig.save();
  844. }
  845. @Override
  846. public void saveAll() {
  847. for(String world : this.WorldConfigurationModification)
  848. {
  849. if(this.WorldConfiguration.containsKey(world)) this.WorldConfiguration.get(world).save();
  850. }
  851. }
  852. //End of addition by rcjrrjcr
  853. }