AdminData.java 9.4 KB

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