AdminAnnouncements.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack 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 DataPack 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 handlers.admincommandhandlers;
  20. import java.util.StringTokenizer;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.cache.HtmCache;
  23. import com.l2jserver.gameserver.data.sql.impl.AnnouncementsTable;
  24. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  25. import com.l2jserver.gameserver.model.PageResult;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.announce.Announcement;
  28. import com.l2jserver.gameserver.model.announce.AnnouncementType;
  29. import com.l2jserver.gameserver.model.announce.AutoAnnouncement;
  30. import com.l2jserver.gameserver.model.announce.IAnnouncement;
  31. import com.l2jserver.gameserver.util.Broadcast;
  32. import com.l2jserver.gameserver.util.HtmlUtil;
  33. import com.l2jserver.gameserver.util.Util;
  34. /**
  35. * @author UnAfraid
  36. */
  37. public class AdminAnnouncements implements IAdminCommandHandler
  38. {
  39. private static final String[] ADMIN_COMMANDS =
  40. {
  41. "admin_announce",
  42. "admin_announce_crit",
  43. "admin_announce_screen",
  44. "admin_announces",
  45. };
  46. @Override
  47. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  48. {
  49. final StringTokenizer st = new StringTokenizer(command);
  50. final String cmd = st.hasMoreTokens() ? st.nextToken() : "";
  51. switch (cmd)
  52. {
  53. case "admin_announce":
  54. case "admin_announce_crit":
  55. case "admin_announce_screen":
  56. {
  57. if (!st.hasMoreTokens())
  58. {
  59. activeChar.sendMessage("Syntax: //announce <text to announce here>");
  60. return false;
  61. }
  62. String announce = st.nextToken();
  63. while (st.hasMoreTokens())
  64. {
  65. announce += " " + st.nextToken();
  66. }
  67. if (cmd.equals("admin_announce_screen"))
  68. {
  69. Broadcast.toAllOnlinePlayersOnScreen(announce);
  70. }
  71. else
  72. {
  73. if (Config.GM_ANNOUNCER_NAME)
  74. {
  75. announce = announce + " [" + activeChar.getName() + "]";
  76. }
  77. Broadcast.toAllOnlinePlayers(announce, cmd.equals("admin_announce_crit"));
  78. }
  79. AdminHtml.showAdminHtml(activeChar, "gm_menu.htm");
  80. break;
  81. }
  82. case "admin_announces":
  83. {
  84. final String subCmd = st.hasMoreTokens() ? st.nextToken() : "";
  85. switch (subCmd)
  86. {
  87. case "add":
  88. {
  89. if (!st.hasMoreTokens())
  90. {
  91. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-add.htm");
  92. Util.sendCBHtml(activeChar, content);
  93. break;
  94. }
  95. final String annType = st.nextToken();
  96. final AnnouncementType type = AnnouncementType.findByName(annType);
  97. // ************************************
  98. if (!st.hasMoreTokens())
  99. {
  100. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  101. break;
  102. }
  103. String annInitDelay = st.nextToken();
  104. if (!Util.isDigit(annInitDelay))
  105. {
  106. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  107. break;
  108. }
  109. int initDelay = Integer.parseInt(annInitDelay) * 1000;
  110. // ************************************
  111. if (!st.hasMoreTokens())
  112. {
  113. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  114. break;
  115. }
  116. String annDelay = st.nextToken();
  117. if (!Util.isDigit(annDelay))
  118. {
  119. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  120. break;
  121. }
  122. int delay = Integer.parseInt(annDelay) * 1000;
  123. if ((delay < (10 * 1000)) && ((type == AnnouncementType.AUTO_NORMAL) || (type == AnnouncementType.AUTO_CRITICAL)))
  124. {
  125. activeChar.sendMessage("Delay cannot be less then 10 seconds!");
  126. break;
  127. }
  128. // ************************************
  129. if (!st.hasMoreTokens())
  130. {
  131. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  132. break;
  133. }
  134. String annRepeat = st.nextToken();
  135. if (!Util.isDigit(annRepeat))
  136. {
  137. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  138. break;
  139. }
  140. int repeat = Integer.parseInt(annRepeat);
  141. if (repeat == 0)
  142. {
  143. repeat = -1;
  144. }
  145. // ************************************
  146. if (!st.hasMoreTokens())
  147. {
  148. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  149. break;
  150. }
  151. String content = st.nextToken();
  152. while (st.hasMoreTokens())
  153. {
  154. content += " " + st.nextToken();
  155. }
  156. // ************************************
  157. final IAnnouncement announce;
  158. if ((type == AnnouncementType.AUTO_CRITICAL) || (type == AnnouncementType.AUTO_NORMAL))
  159. {
  160. announce = new AutoAnnouncement(type, content, activeChar.getName(), initDelay, delay, repeat);
  161. }
  162. else
  163. {
  164. announce = new Announcement(type, content, activeChar.getName());
  165. }
  166. AnnouncementsTable.getInstance().addAnnouncement(announce);
  167. activeChar.sendMessage("Announcement has been successfully added!");
  168. return useAdminCommand("admin_announces list", activeChar);
  169. }
  170. case "edit":
  171. {
  172. if (!st.hasMoreTokens())
  173. {
  174. activeChar.sendMessage("Syntax: //announces edit <id>");
  175. break;
  176. }
  177. String annId = st.nextToken();
  178. if (!Util.isDigit(annId))
  179. {
  180. activeChar.sendMessage("Syntax: //announces edit <id>");
  181. break;
  182. }
  183. int id = Integer.parseInt(annId);
  184. final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
  185. if (announce == null)
  186. {
  187. activeChar.sendMessage("Announcement doesnt exists!");
  188. break;
  189. }
  190. if (!st.hasMoreTokens())
  191. {
  192. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-edit.htm");
  193. String announcementId = "" + announce.getId();
  194. String announcementType = announce.getType().name();
  195. String announcementInital = "0";
  196. String announcementDelay = "0";
  197. String announcementRepeat = "0";
  198. String announcementAuthor = announce.getAuthor();
  199. String announcementContent = announce.getContent();
  200. if (announce instanceof AutoAnnouncement)
  201. {
  202. final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
  203. announcementInital = "" + (autoAnnounce.getInitial() / 1000);
  204. announcementDelay = "" + (autoAnnounce.getDelay() / 1000);
  205. announcementRepeat = "" + autoAnnounce.getRepeat();
  206. }
  207. content = content.replaceAll("%id%", announcementId);
  208. content = content.replaceAll("%type%", announcementType);
  209. content = content.replaceAll("%initial%", announcementInital);
  210. content = content.replaceAll("%delay%", announcementDelay);
  211. content = content.replaceAll("%repeat%", announcementRepeat);
  212. content = content.replaceAll("%author%", announcementAuthor);
  213. content = content.replaceAll("%content%", announcementContent);
  214. Util.sendCBHtml(activeChar, content);
  215. break;
  216. }
  217. final String annType = st.nextToken();
  218. final AnnouncementType type = AnnouncementType.findByName(annType);
  219. switch (announce.getType())
  220. {
  221. case AUTO_CRITICAL:
  222. case AUTO_NORMAL:
  223. {
  224. switch (type)
  225. {
  226. case AUTO_CRITICAL:
  227. case AUTO_NORMAL:
  228. {
  229. break;
  230. }
  231. default:
  232. {
  233. activeChar.sendMessage("Announce type can be changed only to AUTO_NORMAL or AUTO_CRITICAL!");
  234. return false;
  235. }
  236. }
  237. break;
  238. }
  239. case NORMAL:
  240. case CRITICAL:
  241. {
  242. switch (type)
  243. {
  244. case NORMAL:
  245. case CRITICAL:
  246. {
  247. break;
  248. }
  249. default:
  250. {
  251. activeChar.sendMessage("Announce type can be changed only to NORMAL or CRITICAL!");
  252. return false;
  253. }
  254. }
  255. break;
  256. }
  257. }
  258. // ************************************
  259. if (!st.hasMoreTokens())
  260. {
  261. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  262. break;
  263. }
  264. String annInitDelay = st.nextToken();
  265. if (!Util.isDigit(annInitDelay))
  266. {
  267. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  268. break;
  269. }
  270. int initDelay = Integer.parseInt(annInitDelay);
  271. // ************************************
  272. if (!st.hasMoreTokens())
  273. {
  274. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  275. break;
  276. }
  277. String annDelay = st.nextToken();
  278. if (!Util.isDigit(annDelay))
  279. {
  280. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  281. break;
  282. }
  283. int delay = Integer.parseInt(annDelay);
  284. if ((delay < 10) && ((type == AnnouncementType.AUTO_NORMAL) || (type == AnnouncementType.AUTO_CRITICAL)))
  285. {
  286. activeChar.sendMessage("Delay cannot be less then 10 seconds!");
  287. break;
  288. }
  289. // ************************************
  290. if (!st.hasMoreTokens())
  291. {
  292. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  293. break;
  294. }
  295. String annRepeat = st.nextToken();
  296. if (!Util.isDigit(annRepeat))
  297. {
  298. activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
  299. break;
  300. }
  301. int repeat = Integer.parseInt(annRepeat);
  302. if (repeat == 0)
  303. {
  304. repeat = -1;
  305. }
  306. // ************************************
  307. String content = "";
  308. if (st.hasMoreTokens())
  309. {
  310. content = st.nextToken();
  311. while (st.hasMoreTokens())
  312. {
  313. content += " " + st.nextToken();
  314. }
  315. }
  316. if (content.isEmpty())
  317. {
  318. content = announce.getContent();
  319. }
  320. // ************************************
  321. announce.setType(type);
  322. announce.setContent(content);
  323. announce.setAuthor(activeChar.getName());
  324. if (announce instanceof AutoAnnouncement)
  325. {
  326. AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
  327. autoAnnounce.setInitial(initDelay * 1000);
  328. autoAnnounce.setDelay(delay * 1000);
  329. autoAnnounce.setRepeat(repeat);
  330. }
  331. announce.updateMe();
  332. activeChar.sendMessage("Announcement has been successfully edited!");
  333. return useAdminCommand("admin_announces list", activeChar);
  334. }
  335. case "remove":
  336. {
  337. if (!st.hasMoreTokens())
  338. {
  339. activeChar.sendMessage("Syntax: //announces remove <announcement id>");
  340. break;
  341. }
  342. String token = st.nextToken();
  343. if (!Util.isDigit(token))
  344. {
  345. activeChar.sendMessage("Syntax: //announces remove <announcement id>");
  346. break;
  347. }
  348. int id = Integer.parseInt(token);
  349. if (AnnouncementsTable.getInstance().deleteAnnouncement(id))
  350. {
  351. activeChar.sendMessage("Announcement has been successfully removed!");
  352. }
  353. else
  354. {
  355. activeChar.sendMessage("Announcement doesnt exists!");
  356. }
  357. return useAdminCommand("admin_announces list", activeChar);
  358. }
  359. case "restart":
  360. {
  361. if (!st.hasMoreTokens())
  362. {
  363. for (IAnnouncement announce : AnnouncementsTable.getInstance().getAllAnnouncements())
  364. {
  365. if (announce instanceof AutoAnnouncement)
  366. {
  367. final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
  368. autoAnnounce.restartMe();
  369. }
  370. }
  371. activeChar.sendMessage("Auto announcements has been successfully restarted");
  372. break;
  373. }
  374. String token = st.nextToken();
  375. if (!Util.isDigit(token))
  376. {
  377. activeChar.sendMessage("Syntax: //announces show <announcement id>");
  378. break;
  379. }
  380. int id = Integer.parseInt(token);
  381. final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
  382. if (announce != null)
  383. {
  384. if (announce instanceof AutoAnnouncement)
  385. {
  386. final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
  387. autoAnnounce.restartMe();
  388. activeChar.sendMessage("Auto announcement has been successfully restarted");
  389. }
  390. else
  391. {
  392. activeChar.sendMessage("This option has effect only on auto announcements!");
  393. }
  394. }
  395. else
  396. {
  397. activeChar.sendMessage("Announcement doesnt exists!");
  398. }
  399. break;
  400. }
  401. case "show":
  402. {
  403. if (!st.hasMoreTokens())
  404. {
  405. activeChar.sendMessage("Syntax: //announces show <announcement id>");
  406. break;
  407. }
  408. String token = st.nextToken();
  409. if (!Util.isDigit(token))
  410. {
  411. activeChar.sendMessage("Syntax: //announces show <announcement id>");
  412. break;
  413. }
  414. int id = Integer.parseInt(token);
  415. final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
  416. if (announce != null)
  417. {
  418. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-show.htm");
  419. String announcementId = "" + announce.getId();
  420. String announcementType = announce.getType().name();
  421. String announcementInital = "0";
  422. String announcementDelay = "0";
  423. String announcementRepeat = "0";
  424. String announcementAuthor = announce.getAuthor();
  425. String announcementContent = announce.getContent();
  426. if (announce instanceof AutoAnnouncement)
  427. {
  428. final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
  429. announcementInital = "" + (autoAnnounce.getInitial() / 1000);
  430. announcementDelay = "" + (autoAnnounce.getDelay() / 1000);
  431. announcementRepeat = "" + autoAnnounce.getRepeat();
  432. }
  433. content = content.replaceAll("%id%", announcementId);
  434. content = content.replaceAll("%type%", announcementType);
  435. content = content.replaceAll("%initial%", announcementInital);
  436. content = content.replaceAll("%delay%", announcementDelay);
  437. content = content.replaceAll("%repeat%", announcementRepeat);
  438. content = content.replaceAll("%author%", announcementAuthor);
  439. content = content.replaceAll("%content%", announcementContent);
  440. Util.sendCBHtml(activeChar, content);
  441. break;
  442. }
  443. activeChar.sendMessage("Announcement doesnt exists!");
  444. return useAdminCommand("admin_announces list", activeChar);
  445. }
  446. case "list":
  447. {
  448. int page = 0;
  449. if (st.hasMoreTokens())
  450. {
  451. final String token = st.nextToken();
  452. if (Util.isDigit(token))
  453. {
  454. page = Integer.valueOf(token);
  455. }
  456. }
  457. String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-list.htm");
  458. final PageResult result = HtmlUtil.createPage(AnnouncementsTable.getInstance().getAllAnnouncements(), page, 8, currentPage ->
  459. {
  460. return "<td align=center><button action=\"bypass admin_announces list " + currentPage + "\" value=\"" + (currentPage + 1) + "\" width=35 height=20 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>";
  461. }, announcement ->
  462. {
  463. final StringBuilder sb = new StringBuilder();
  464. sb.append("<tr>");
  465. sb.append("<td width=5></td>");
  466. sb.append("<td width=80>" + announcement.getId() + "</td>");
  467. sb.append("<td width=100>" + announcement.getType() + "</td>");
  468. sb.append("<td width=100>" + announcement.getAuthor() + "</td>");
  469. if ((announcement.getType() == AnnouncementType.AUTO_NORMAL) || (announcement.getType() == AnnouncementType.AUTO_CRITICAL))
  470. {
  471. sb.append("<td width=60><button action=\"bypass -h admin_announces restart " + announcement.getId() + "\" value=\"Restart\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  472. }
  473. else
  474. {
  475. sb.append("<td width=60><button action=\"\" value=\"\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  476. }
  477. if (announcement.getType() == AnnouncementType.EVENT)
  478. {
  479. sb.append("<td width=60><button action=\"bypass -h admin_announces show " + announcement.getId() + "\" value=\"Show\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  480. sb.append("<td width=60></td>");
  481. }
  482. else
  483. {
  484. sb.append("<td width=60><button action=\"bypass -h admin_announces show " + announcement.getId() + "\" value=\"Show\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  485. sb.append("<td width=60><button action=\"bypass -h admin_announces edit " + announcement.getId() + "\" value=\"Edit\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  486. }
  487. sb.append("<td width=60><button action=\"bypass -h admin_announces remove " + announcement.getId() + "\" value=\"Remove\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
  488. sb.append("<td width=5></td>");
  489. sb.append("</tr>");
  490. return sb.toString();
  491. });
  492. content = content.replaceAll("%pages%", result.getPagerTemplate().toString());
  493. content = content.replaceAll("%announcements%", result.getBodyTemplate().toString());
  494. Util.sendCBHtml(activeChar, content);
  495. break;
  496. }
  497. }
  498. }
  499. }
  500. return false;
  501. }
  502. @Override
  503. public String[] getAdminCommandList()
  504. {
  505. return ADMIN_COMMANDS;
  506. }
  507. }