AdminAnnouncements.java 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.admincommandhandlers;
  16. import java.util.List;
  17. import java.util.StringTokenizer;
  18. import javolution.text.TextBuilder;
  19. import com.l2jserver.Config;
  20. import com.l2jserver.gameserver.Announcements;
  21. import com.l2jserver.gameserver.cache.HtmCache;
  22. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  23. import com.l2jserver.gameserver.model.L2World;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  26. import com.l2jserver.gameserver.taskmanager.AutoAnnounceTaskManager;
  27. import com.l2jserver.gameserver.taskmanager.AutoAnnounceTaskManager.AutoAnnouncement;
  28. import com.l2jserver.util.StringUtil;
  29. /**
  30. * This class handles following admin commands:
  31. * - announce text = announces text to all players
  32. * - list_announcements = show menu
  33. * - reload_announcements = reloads announcements from txt file
  34. * - announce_announcements = announce all stored announcements to all players
  35. * - add_announcement text = adds text to startup announcements
  36. * - del_announcement id = deletes announcement with respective id
  37. *
  38. * @version $Revision: 1.4.4.5 $ $Date: 2005/04/11 10:06:06 $
  39. */
  40. public class AdminAnnouncements implements IAdminCommandHandler
  41. {
  42. private static final String[] ADMIN_COMMANDS =
  43. {
  44. "admin_list_announcements",
  45. "admin_list_critannouncements",
  46. "admin_reload_announcements",
  47. "admin_announce_announcements",
  48. "admin_add_announcement",
  49. "admin_del_announcement",
  50. "admin_add_critannouncement",
  51. "admin_del_critannouncement",
  52. "admin_announce",
  53. "admin_critannounce",
  54. "admin_announce_menu",
  55. "admin_critannounce_menu",
  56. "admin_list_autoann",
  57. "admin_reload_autoann",
  58. "admin_add_autoann",
  59. "admin_del_autoann"
  60. };
  61. @Override
  62. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  63. {
  64. if (command.equals("admin_list_announcements"))
  65. {
  66. Announcements.getInstance().listAnnouncements(activeChar);
  67. }
  68. else if (command.equals("admin_list_critannouncements"))
  69. {
  70. Announcements.getInstance().listCritAnnouncements(activeChar);
  71. }
  72. else if (command.equals("admin_reload_announcements"))
  73. {
  74. Announcements.getInstance().loadAnnouncements();
  75. Announcements.getInstance().listAnnouncements(activeChar);
  76. }
  77. else if (command.startsWith("admin_announce_menu"))
  78. {
  79. if (Config.GM_ANNOUNCER_NAME && command.length() > 20)
  80. command += " ("+activeChar.getName()+")";
  81. Announcements.getInstance().handleAnnounce(command, 20, false);
  82. AdminHelpPage.showHelpPage(activeChar, "gm_menu.htm");
  83. }
  84. else if (command.startsWith("admin_critannounce_menu"))
  85. {
  86. try
  87. {
  88. command = command.substring(24);
  89. if (Config.GM_CRITANNOUNCER_NAME && command.length() > 0)
  90. command = activeChar.getName() + ": " + command;
  91. Announcements.getInstance().handleAnnounce(command, 0, true);
  92. }
  93. catch (StringIndexOutOfBoundsException e)
  94. {
  95. }
  96. AdminHelpPage.showHelpPage(activeChar, "gm_menu.htm");
  97. }
  98. else if (command.equals("admin_announce_announcements"))
  99. {
  100. for (L2PcInstance player : L2World.getInstance().getAllPlayersArray())
  101. Announcements.getInstance().showAnnouncements(player);
  102. Announcements.getInstance().listAnnouncements(activeChar);
  103. }
  104. else if (command.startsWith("admin_add_announcement"))
  105. {
  106. // FIXME the player can send only 16 chars (if you try to send more
  107. // it sends null), remove this function or not?
  108. if (!command.equals("admin_add_announcement"))
  109. {
  110. try
  111. {
  112. String val = command.substring(23);
  113. Announcements.getInstance().addAnnouncement(val);
  114. Announcements.getInstance().listAnnouncements(activeChar);
  115. }
  116. catch (StringIndexOutOfBoundsException e)
  117. {
  118. }// ignore errors
  119. }
  120. }
  121. else if (command.startsWith("admin_add_critannouncement"))
  122. {
  123. // FIXME the player can send only 16 chars (if you try to send more
  124. // it sends null), remove this function or not?
  125. if (!command.equals("admin_add_critannouncement"))
  126. {
  127. try
  128. {
  129. String val = command.substring(27);
  130. Announcements.getInstance().addCritAnnouncement(val);
  131. Announcements.getInstance().listCritAnnouncements(activeChar);
  132. }
  133. catch (StringIndexOutOfBoundsException e)
  134. {
  135. }// ignore errors
  136. }
  137. }
  138. else if (command.startsWith("admin_del_announcement"))
  139. {
  140. try
  141. {
  142. int val = Integer.parseInt(command.substring(23));
  143. Announcements.getInstance().delAnnouncement(val);
  144. Announcements.getInstance().listAnnouncements(activeChar);
  145. }
  146. catch (StringIndexOutOfBoundsException e)
  147. {
  148. }
  149. }
  150. else if (command.startsWith("admin_del_critannouncement"))
  151. {
  152. try
  153. {
  154. int val = Integer.parseInt(command.substring(27));
  155. Announcements.getInstance().delCritAnnouncement(val);
  156. Announcements.getInstance().listCritAnnouncements(activeChar);
  157. }
  158. catch (StringIndexOutOfBoundsException e)
  159. {
  160. }
  161. }
  162. // Command is admin announce
  163. else if (command.startsWith("admin_announce"))
  164. {
  165. if (Config.GM_ANNOUNCER_NAME && command.length() > 15)
  166. command += " ("+activeChar.getName()+")";
  167. // Call method from another class
  168. Announcements.getInstance().handleAnnounce(command, 15, false);
  169. }
  170. else if (command.startsWith("admin_critannounce"))
  171. {
  172. try
  173. {
  174. command = command.substring(19);
  175. if (Config.GM_CRITANNOUNCER_NAME && command.length() > 0)
  176. command = activeChar.getName() + ": " + command;
  177. Announcements.getInstance().handleAnnounce(command, 0, true);
  178. }
  179. catch (StringIndexOutOfBoundsException e)
  180. {
  181. }
  182. }
  183. else if (command.startsWith("admin_list_autoann"))
  184. {
  185. listAutoAnnouncements(activeChar);
  186. }
  187. else if (command.startsWith("admin_reload_autoann"))
  188. {
  189. AutoAnnounceTaskManager.getInstance().restore();
  190. activeChar.sendMessage("AutoAnnouncement Reloaded.");
  191. listAutoAnnouncements(activeChar);
  192. }
  193. else if (command.startsWith("admin_add_autoann"))
  194. {
  195. StringTokenizer st = new StringTokenizer(command);
  196. st.nextToken();
  197. if (!st.hasMoreTokens())
  198. {
  199. activeChar.sendMessage("Not enough parameters for adding autoannounce!");
  200. return false;
  201. }
  202. long initial = Long.parseLong(st.nextToken());
  203. if (!st.hasMoreTokens())
  204. {
  205. activeChar.sendMessage("Not enough parameters for adding autoannounce!");
  206. return false;
  207. }
  208. long delay = Long.parseLong(st.nextToken());
  209. if (!st.hasMoreTokens())
  210. {
  211. activeChar.sendMessage("Not enough parameters for adding autoannounce!");
  212. return false;
  213. }
  214. int repeat = Integer.parseInt(st.nextToken());
  215. if (!st.hasMoreTokens())
  216. {
  217. activeChar.sendMessage("Not enough parameters for adding autoannounce!");
  218. return false;
  219. }
  220. boolean isCritical = Boolean.valueOf(st.nextToken());
  221. if (!st.hasMoreTokens())
  222. {
  223. activeChar.sendMessage("Not enough parameters for adding autoannounce!");
  224. return false;
  225. }
  226. TextBuilder memo = new TextBuilder();
  227. while (st.hasMoreTokens())
  228. {
  229. memo.append(st.nextToken());
  230. memo.append(" ");
  231. }
  232. AutoAnnounceTaskManager.getInstance().addAutoAnnounce(initial*1000, delay*1000, repeat, memo.toString().trim(), isCritical);
  233. listAutoAnnouncements(activeChar);
  234. }
  235. else if (command.startsWith("admin_del_autoann"))
  236. {
  237. StringTokenizer st = new StringTokenizer(command);
  238. st.nextToken();
  239. if (!st.hasMoreTokens())
  240. {
  241. activeChar.sendMessage("Not enough parameters for deleting autoannounce!");
  242. return false;
  243. }
  244. AutoAnnounceTaskManager.getInstance().deleteAutoAnnounce(Integer.parseInt(st.nextToken()));
  245. listAutoAnnouncements(activeChar);
  246. }
  247. return true;
  248. }
  249. private void listAutoAnnouncements(L2PcInstance activeChar)
  250. {
  251. String content = HtmCache.getInstance().getHtmForce(activeChar.getHtmlPrefix(), "data/html/admin/autoannounce.htm");
  252. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  253. adminReply.setHtml(content);
  254. final StringBuilder replyMSG = StringUtil.startAppend(500, "<br>");
  255. List<AutoAnnouncement> autoannouncements = AutoAnnounceTaskManager.getInstance().getAutoAnnouncements();
  256. for (int i = 0; i < autoannouncements.size(); i++)
  257. {
  258. AutoAnnouncement autoann = autoannouncements.get(i);
  259. TextBuilder memo2 = new TextBuilder();
  260. for (String memo0 : autoann.getMemo())
  261. {
  262. memo2.append(memo0);
  263. memo2.append("/n");
  264. }
  265. replyMSG.append("<table width=260><tr><td width=220><font color=\"" + (autoann.isCritical() ? "00FCFC" : "7FFCFC") + "\">");
  266. replyMSG.append(memo2.toString().trim());
  267. replyMSG.append("</font></td><td width=40><button value=\"Delete\" action=\"bypass -h admin_del_autoann ");
  268. replyMSG.append(i);
  269. replyMSG.append("\" width=60 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table>");
  270. }
  271. adminReply.replace("%announces%", replyMSG.toString());
  272. activeChar.sendPacket(adminReply);
  273. }
  274. @Override
  275. public String[] getAdminCommandList()
  276. {
  277. return ADMIN_COMMANDS;
  278. }
  279. }