PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/net/milkbowl/vault/chat/plugins/Chat_GroupManager.java

https://gitlab.com/MineYourMind/Vault
Java | 367 lines | 309 code | 42 blank | 16 comment | 89 complexity | 00e08d95474f8d2cd4d0b834db8bedf4 MD5 | raw file
  1. /* This file is part of Vault.
  2. Vault is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU Lesser General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. Vault is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with Vault. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package net.milkbowl.vault.chat.plugins;
  14. import java.util.logging.Logger;
  15. import net.milkbowl.vault.chat.Chat;
  16. import net.milkbowl.vault.permission.Permission;
  17. import org.anjocaido.groupmanager.GroupManager;
  18. import org.anjocaido.groupmanager.data.Group;
  19. import org.anjocaido.groupmanager.data.User;
  20. import org.anjocaido.groupmanager.dataholder.OverloadedWorldHolder;
  21. import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler;
  22. import org.bukkit.Bukkit;
  23. import org.bukkit.event.EventHandler;
  24. import org.bukkit.event.EventPriority;
  25. import org.bukkit.event.Listener;
  26. import org.bukkit.event.server.PluginDisableEvent;
  27. import org.bukkit.event.server.PluginEnableEvent;
  28. import org.bukkit.plugin.Plugin;
  29. public class Chat_GroupManager extends Chat {
  30. private static final Logger log = Logger.getLogger("Minecraft");
  31. private final String name = "GroupManager - Chat";
  32. private Plugin plugin = null;
  33. private GroupManager groupManager;
  34. public Chat_GroupManager(Plugin plugin, Permission perms) {
  35. super(perms);
  36. this.plugin = plugin;
  37. Bukkit.getServer().getPluginManager().registerEvents(new PermissionServerListener(this), plugin);
  38. // Load Plugin in case it was loaded before
  39. if (groupManager == null) {
  40. Plugin chat = plugin.getServer().getPluginManager().getPlugin("GroupManager");
  41. if (chat != null) {
  42. if (chat.isEnabled()) {
  43. groupManager = (GroupManager) chat;
  44. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), name));
  45. }
  46. }
  47. }
  48. }
  49. public class PermissionServerListener implements Listener {
  50. Chat_GroupManager chat = null;
  51. public PermissionServerListener(Chat_GroupManager chat) {
  52. this.chat = chat;
  53. }
  54. @EventHandler(priority = EventPriority.MONITOR)
  55. public void onPluginEnable(PluginEnableEvent event) {
  56. if (chat.groupManager == null) {
  57. Plugin perms = event.getPlugin();
  58. if (perms.getDescription().getName().equals("GroupManager")) {
  59. chat.groupManager = (GroupManager) perms;
  60. log.info(String.format("[%s][Chat] %s hooked.", plugin.getDescription().getName(), chat.name));
  61. }
  62. }
  63. }
  64. @EventHandler(priority = EventPriority.MONITOR)
  65. public void onPluginDisable(PluginDisableEvent event) {
  66. if (chat.groupManager != null) {
  67. if (event.getPlugin().getDescription().getName().equals("GroupManager")) {
  68. chat.groupManager = null;
  69. log.info(String.format("[%s][Chat] %s un-hooked.", plugin.getDescription().getName(), chat.name));
  70. }
  71. }
  72. }
  73. }
  74. @Override
  75. public String getName() {
  76. return this.name;
  77. }
  78. @Override
  79. public boolean isEnabled() {
  80. if (groupManager == null) {
  81. return false;
  82. } else {
  83. return groupManager.isEnabled();
  84. }
  85. }
  86. @Override
  87. public int getPlayerInfoInteger(String worldName, String playerName, String node, int defaultValue) {
  88. AnjoPermissionsHandler handler;
  89. if (worldName == null) {
  90. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  91. } else {
  92. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  93. }
  94. if (handler == null) {
  95. return defaultValue;
  96. }
  97. Integer val = handler.getUserPermissionInteger(playerName, node);
  98. return val != null ? val : defaultValue;
  99. }
  100. @Override
  101. public void setPlayerInfoInteger(String worldName, String playerName, String node, int value) {
  102. setPlayerValue(worldName, playerName, node, value);
  103. }
  104. @Override
  105. public int getGroupInfoInteger(String worldName, String groupName, String node, int defaultValue) {
  106. AnjoPermissionsHandler handler;
  107. if (worldName == null) {
  108. handler = groupManager.getWorldsHolder().getDefaultWorld().getPermissionsHandler();
  109. } else {
  110. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  111. }
  112. if (handler == null) {
  113. return defaultValue;
  114. }
  115. Integer val = handler.getGroupPermissionInteger(groupName, node);
  116. return val != null ? val : defaultValue;
  117. }
  118. @Override
  119. public void setGroupInfoInteger(String worldName, String groupName, String node, int value) {
  120. setGroupValue(worldName, groupName, node, value);
  121. }
  122. @Override
  123. public double getPlayerInfoDouble(String worldName, String playerName, String node, double defaultValue) {
  124. AnjoPermissionsHandler handler;
  125. if (worldName == null) {
  126. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  127. } else {
  128. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  129. }
  130. if (handler == null) {
  131. return defaultValue;
  132. }
  133. Double val = handler.getUserPermissionDouble(playerName, node);
  134. return val != null ? val : defaultValue;
  135. }
  136. @Override
  137. public void setPlayerInfoDouble(String worldName, String playerName, String node, double value) {
  138. setPlayerValue(worldName, playerName, node, value);
  139. }
  140. @Override
  141. public double getGroupInfoDouble(String worldName, String groupName, String node, double defaultValue) {
  142. AnjoPermissionsHandler handler;
  143. if (worldName == null) {
  144. handler = groupManager.getWorldsHolder().getDefaultWorld().getPermissionsHandler();
  145. } else {
  146. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  147. }
  148. if (handler == null) {
  149. return defaultValue;
  150. }
  151. Double val = handler.getGroupPermissionDouble(groupName, node);
  152. return val != null ? val : defaultValue;
  153. }
  154. @Override
  155. public void setGroupInfoDouble(String worldName, String groupName, String node, double value) {
  156. setGroupValue(worldName, groupName, node, value);
  157. }
  158. @Override
  159. public boolean getPlayerInfoBoolean(String worldName, String playerName, String node, boolean defaultValue) {
  160. AnjoPermissionsHandler handler;
  161. if (worldName == null) {
  162. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  163. } else {
  164. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  165. }
  166. if (handler == null) {
  167. return defaultValue;
  168. }
  169. Boolean val = handler.getUserPermissionBoolean(playerName, node);
  170. return val != null ? val : defaultValue;
  171. }
  172. @Override
  173. public void setPlayerInfoBoolean(String worldName, String playerName, String node, boolean value) {
  174. setPlayerValue(worldName, playerName, node, value);
  175. }
  176. @Override
  177. public boolean getGroupInfoBoolean(String worldName, String groupName, String node, boolean defaultValue) {
  178. AnjoPermissionsHandler handler;
  179. if (worldName == null) {
  180. handler = groupManager.getWorldsHolder().getDefaultWorld().getPermissionsHandler();
  181. } else {
  182. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  183. }
  184. if (handler == null) {
  185. return defaultValue;
  186. }
  187. Boolean val = handler.getGroupPermissionBoolean(groupName, node);
  188. return val != null ? val : defaultValue;
  189. }
  190. @Override
  191. public void setGroupInfoBoolean(String worldName, String groupName, String node, boolean value) {
  192. setGroupValue(worldName, groupName, node, value);
  193. }
  194. @Override
  195. public String getPlayerInfoString(String worldName, String playerName, String node, String defaultValue) {
  196. AnjoPermissionsHandler handler;
  197. if (worldName == null) {
  198. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  199. } else {
  200. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  201. }
  202. if (handler == null) {
  203. return defaultValue;
  204. }
  205. String val = handler.getUserPermissionString(playerName, node);
  206. return val != null ? val : defaultValue;
  207. }
  208. @Override
  209. public void setPlayerInfoString(String worldName, String playerName, String node, String value) {
  210. setPlayerValue(worldName, playerName, node, value);
  211. }
  212. @Override
  213. public String getGroupInfoString(String worldName, String groupName, String node, String defaultValue) {
  214. AnjoPermissionsHandler handler;
  215. if (worldName == null) {
  216. handler = groupManager.getWorldsHolder().getDefaultWorld().getPermissionsHandler();
  217. } else {
  218. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  219. }
  220. if (handler == null) {
  221. return defaultValue;
  222. }
  223. String val = handler.getGroupPermissionString(groupName, node);
  224. return val != null ? val : defaultValue;
  225. }
  226. @Override
  227. public void setGroupInfoString(String worldName, String groupName, String node, String value) {
  228. setGroupValue(worldName, groupName, node, value);
  229. }
  230. @Override
  231. public String getPlayerPrefix(String worldName, String playerName) {
  232. AnjoPermissionsHandler handler;
  233. if (worldName == null) {
  234. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  235. } else {
  236. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  237. }
  238. if (handler == null) {
  239. return "";
  240. }
  241. return handler.getUserPrefix(playerName);
  242. }
  243. @Override
  244. public String getPlayerSuffix(String worldName, String playerName) {
  245. AnjoPermissionsHandler handler;
  246. if (worldName == null) {
  247. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  248. } else {
  249. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  250. }
  251. if (handler == null) {
  252. return "";
  253. }
  254. return handler.getUserSuffix(playerName);
  255. }
  256. @Override
  257. public void setPlayerSuffix(String worldName, String player, String suffix) {
  258. setPlayerInfoString(worldName, player, "suffix", suffix);
  259. }
  260. @Override
  261. public void setPlayerPrefix(String worldName, String player, String prefix) {
  262. setPlayerInfoString(worldName, player, "prefix", prefix);
  263. }
  264. @Override
  265. public String getGroupPrefix(String worldName, String group) {
  266. return getGroupInfoString(worldName, group, "prefix", "");
  267. }
  268. @Override
  269. public void setGroupPrefix(String worldName, String group, String prefix) {
  270. setGroupInfoString(worldName, group, "prefix", prefix);
  271. }
  272. @Override
  273. public String getGroupSuffix(String worldName, String group) {
  274. return getGroupInfoString(worldName, group, "suffix", "");
  275. }
  276. @Override
  277. public void setGroupSuffix(String worldName, String group, String suffix) {
  278. setGroupInfoString(worldName, group, "suffix", suffix);
  279. }
  280. @Override
  281. public String getPrimaryGroup(String worldName, String playerName) {
  282. AnjoPermissionsHandler handler;
  283. if (worldName == null) {
  284. handler = groupManager.getWorldsHolder().getWorldPermissionsByPlayerName(playerName);
  285. } else {
  286. handler = groupManager.getWorldsHolder().getWorldPermissions(worldName);
  287. }
  288. return handler.getGroup(playerName);
  289. }
  290. private void setPlayerValue(String worldName, String playerName, String node, Object value) {
  291. OverloadedWorldHolder owh;
  292. if (worldName == null) {
  293. owh = groupManager.getWorldsHolder().getWorldDataByPlayerName(playerName);
  294. } else {
  295. owh = groupManager.getWorldsHolder().getWorldData(worldName);
  296. }
  297. if (owh == null) {
  298. return;
  299. }
  300. User user = owh.getUser(playerName);
  301. if (user == null) {
  302. return;
  303. }
  304. user.getVariables().addVar(node, value);
  305. }
  306. private void setGroupValue(String worldName, String groupName, String node, Object value) {
  307. OverloadedWorldHolder owh;
  308. if (worldName == null) {
  309. owh = groupManager.getWorldsHolder().getDefaultWorld();
  310. } else {
  311. owh = groupManager.getWorldsHolder().getWorldData(worldName);
  312. }
  313. if (owh == null) {
  314. return;
  315. }
  316. Group group = owh.getGroup(groupName);
  317. if (group == null) {
  318. return;
  319. }
  320. group.getVariables().addVar(node, value);
  321. }
  322. }