AdminData.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.xml.impl;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import java.util.concurrent.ConcurrentHashMap;
  26. import org.w3c.dom.Document;
  27. import org.w3c.dom.NamedNodeMap;
  28. import org.w3c.dom.Node;
  29. import com.l2jserver.gameserver.model.L2AccessLevel;
  30. import com.l2jserver.gameserver.model.L2AdminCommandAccessRight;
  31. import com.l2jserver.gameserver.model.StatsSet;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.network.SystemMessageId;
  34. import com.l2jserver.gameserver.network.serverpackets.L2GameServerPacket;
  35. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  36. import com.l2jserver.util.data.xml.IXmlReader;
  37. /**
  38. * Loads administrator access levels and commands.
  39. * @author UnAfraid
  40. */
  41. public final class AdminData implements IXmlReader
  42. {
  43. private final Map<Integer, L2AccessLevel> _accessLevels = new HashMap<>();
  44. private final Map<String, L2AdminCommandAccessRight> _adminCommandAccessRights = new HashMap<>();
  45. private final Map<L2PcInstance, Boolean> _gmList = new ConcurrentHashMap<>();
  46. private int _highestLevel = 0;
  47. protected AdminData()
  48. {
  49. load();
  50. }
  51. @Override
  52. public synchronized void load()
  53. {
  54. _accessLevels.clear();
  55. _adminCommandAccessRights.clear();
  56. parseDatapackFile("config/accessLevels.xml");
  57. LOGGER.info("{}: Loaded: {} Access Levels.", getClass().getSimpleName(), _accessLevels.size());
  58. parseDatapackFile("config/adminCommands.xml");
  59. LOGGER.info("{}: Loaded: {} Access Commands.", getClass().getSimpleName(), _adminCommandAccessRights.size());
  60. }
  61. @Override
  62. public void parseDocument(Document doc)
  63. {
  64. NamedNodeMap attrs;
  65. Node attr;
  66. StatsSet set;
  67. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  68. {
  69. if ("list".equalsIgnoreCase(n.getNodeName()))
  70. {
  71. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  72. {
  73. if ("access".equalsIgnoreCase(d.getNodeName()))
  74. {
  75. set = new StatsSet();
  76. attrs = d.getAttributes();
  77. for (int i = 0; i < attrs.getLength(); i++)
  78. {
  79. attr = attrs.item(i);
  80. set.set(attr.getNodeName(), attr.getNodeValue());
  81. }
  82. final L2AccessLevel level = new L2AccessLevel(set);
  83. if (level.getLevel() > _highestLevel)
  84. {
  85. _highestLevel = level.getLevel();
  86. }
  87. _accessLevels.put(level.getLevel(), level);
  88. }
  89. else if ("admin".equalsIgnoreCase(d.getNodeName()))
  90. {
  91. set = new StatsSet();
  92. attrs = d.getAttributes();
  93. for (int i = 0; i < attrs.getLength(); i++)
  94. {
  95. attr = attrs.item(i);
  96. set.set(attr.getNodeName(), attr.getNodeValue());
  97. }
  98. final L2AdminCommandAccessRight command = new L2AdminCommandAccessRight(set);
  99. _adminCommandAccessRights.put(command.getAdminCommand(), command);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Returns the access level by characterAccessLevel.
  107. * @param accessLevelNum as int
  108. * @return the access level instance by char access level
  109. */
  110. public L2AccessLevel getAccessLevel(int accessLevelNum)
  111. {
  112. if (accessLevelNum < 0)
  113. {
  114. return _accessLevels.get(-1);
  115. }
  116. else if (!_accessLevels.containsKey(accessLevelNum))
  117. {
  118. _accessLevels.put(accessLevelNum, new L2AccessLevel());
  119. }
  120. return _accessLevels.get(accessLevelNum);
  121. }
  122. /**
  123. * Gets the master access level.
  124. * @return the master access level
  125. */
  126. public L2AccessLevel getMasterAccessLevel()
  127. {
  128. return _accessLevels.get(_highestLevel);
  129. }
  130. /**
  131. * Checks for access level.
  132. * @param id the id
  133. * @return {@code true}, if successful, {@code false} otherwise
  134. */
  135. public boolean hasAccessLevel(int id)
  136. {
  137. return _accessLevels.containsKey(id);
  138. }
  139. /**
  140. * Checks for access.
  141. * @param adminCommand the admin command
  142. * @param accessLevel the access level
  143. * @return {@code true}, if successful, {@code false} otherwise
  144. */
  145. public boolean hasAccess(String adminCommand, L2AccessLevel accessLevel)
  146. {
  147. L2AdminCommandAccessRight acar = _adminCommandAccessRights.get(adminCommand);
  148. if (acar == null)
  149. {
  150. // Trying to avoid the spam for next time when the gm would try to use the same command
  151. if ((accessLevel.getLevel() > 0) && (accessLevel.getLevel() == _highestLevel))
  152. {
  153. acar = new L2AdminCommandAccessRight(adminCommand, true, accessLevel.getLevel());
  154. _adminCommandAccessRights.put(adminCommand, acar);
  155. LOGGER.info("{}: No rights defined for admin command {} auto setting accesslevel: {}!", getClass().getSimpleName(), adminCommand, accessLevel.getLevel());
  156. }
  157. else
  158. {
  159. LOGGER.info("{}: No rights defined for admin command {}!", getClass().getSimpleName(), adminCommand);
  160. return false;
  161. }
  162. }
  163. return acar.hasAccess(accessLevel);
  164. }
  165. /**
  166. * Require confirm.
  167. * @param command the command
  168. * @return {@code true}, if the command require confirmation, {@code false} otherwise
  169. */
  170. public boolean requireConfirm(String command)
  171. {
  172. final L2AdminCommandAccessRight acar = _adminCommandAccessRights.get(command);
  173. if (acar == null)
  174. {
  175. LOGGER.info("{}: No rights defined for admin command {}.", getClass().getSimpleName(), command);
  176. return false;
  177. }
  178. return acar.getRequireConfirm();
  179. }
  180. /**
  181. * Gets the all GMs.
  182. * @param includeHidden the include hidden
  183. * @return the all GMs
  184. */
  185. public List<L2PcInstance> getAllGms(boolean includeHidden)
  186. {
  187. final List<L2PcInstance> tmpGmList = new ArrayList<>();
  188. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  189. {
  190. if (includeHidden || !entry.getValue())
  191. {
  192. tmpGmList.add(entry.getKey());
  193. }
  194. }
  195. return tmpGmList;
  196. }
  197. /**
  198. * Gets the all GM names.
  199. * @param includeHidden the include hidden
  200. * @return the all GM names
  201. */
  202. public List<String> getAllGmNames(boolean includeHidden)
  203. {
  204. final List<String> tmpGmList = new ArrayList<>();
  205. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  206. {
  207. if (!entry.getValue())
  208. {
  209. tmpGmList.add(entry.getKey().getName());
  210. }
  211. else if (includeHidden)
  212. {
  213. tmpGmList.add(entry.getKey().getName() + " (invis)");
  214. }
  215. }
  216. return tmpGmList;
  217. }
  218. /**
  219. * Add a L2PcInstance player to the Set _gmList.
  220. * @param player the player
  221. * @param hidden the hidden
  222. */
  223. public void addGm(L2PcInstance player, boolean hidden)
  224. {
  225. _gmList.put(player, hidden);
  226. }
  227. /**
  228. * Delete a GM.
  229. * @param player the player
  230. */
  231. public void deleteGm(L2PcInstance player)
  232. {
  233. _gmList.remove(player);
  234. }
  235. /**
  236. * GM will be displayed on clients GM list.
  237. * @param player the player
  238. */
  239. public void showGm(L2PcInstance player)
  240. {
  241. if (_gmList.containsKey(player))
  242. {
  243. _gmList.put(player, false);
  244. }
  245. }
  246. /**
  247. * GM will no longer be displayed on clients GM list.
  248. * @param player the player
  249. */
  250. public void hideGm(L2PcInstance player)
  251. {
  252. if (_gmList.containsKey(player))
  253. {
  254. _gmList.put(player, true);
  255. }
  256. }
  257. /**
  258. * Checks if is GM online.
  259. * @param includeHidden the include hidden
  260. * @return true, if is GM online
  261. */
  262. public boolean isGmOnline(boolean includeHidden)
  263. {
  264. for (Entry<L2PcInstance, Boolean> entry : _gmList.entrySet())
  265. {
  266. if (includeHidden || !entry.getValue())
  267. {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * Send list to player.
  275. * @param player the player
  276. */
  277. public void sendListToPlayer(L2PcInstance player)
  278. {
  279. if (isGmOnline(player.isGM()))
  280. {
  281. player.sendPacket(SystemMessageId.GM_LIST);
  282. for (String name : getAllGmNames(player.isGM()))
  283. {
  284. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.GM_C1);
  285. sm.addString(name);
  286. player.sendPacket(sm);
  287. }
  288. }
  289. else
  290. {
  291. player.sendPacket(SystemMessageId.NO_GM_PROVIDING_SERVICE_NOW);
  292. }
  293. }
  294. /**
  295. * Broadcast to GMs.
  296. * @param packet the packet
  297. */
  298. public void broadcastToGMs(L2GameServerPacket packet)
  299. {
  300. for (L2PcInstance gm : getAllGms(true))
  301. {
  302. gm.sendPacket(packet);
  303. }
  304. }
  305. /**
  306. * Broadcast message to GMs.
  307. * @param message the message
  308. */
  309. public void broadcastMessageToGMs(String message)
  310. {
  311. for (L2PcInstance gm : getAllGms(true))
  312. {
  313. gm.sendMessage(message);
  314. }
  315. }
  316. /**
  317. * Gets the single instance of AdminTable.
  318. * @return AccessLevels: the one and only instance of this class<br>
  319. */
  320. public static AdminData getInstance()
  321. {
  322. return SingletonHolder._instance;
  323. }
  324. private static class SingletonHolder
  325. {
  326. protected static final AdminData _instance = new AdminData();
  327. }
  328. }