123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- /*
- * Copyright (C) 2004-2015 L2J DataPack
- *
- * This file is part of L2J DataPack.
- *
- * L2J DataPack is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J DataPack is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package handlers.admincommandhandlers;
- import java.util.StringTokenizer;
- import com.l2jserver.Config;
- import com.l2jserver.gameserver.cache.HtmCache;
- import com.l2jserver.gameserver.data.sql.impl.AnnouncementsTable;
- import com.l2jserver.gameserver.handler.IAdminCommandHandler;
- import com.l2jserver.gameserver.model.PageResult;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.announce.Announcement;
- import com.l2jserver.gameserver.model.announce.AnnouncementType;
- import com.l2jserver.gameserver.model.announce.AutoAnnouncement;
- import com.l2jserver.gameserver.model.announce.IAnnouncement;
- import com.l2jserver.gameserver.util.Broadcast;
- import com.l2jserver.gameserver.util.HtmlUtil;
- import com.l2jserver.gameserver.util.Util;
- /**
- * @author UnAfraid
- */
- public class AdminAnnouncements implements IAdminCommandHandler
- {
- private static final String[] ADMIN_COMMANDS =
- {
- "admin_announce",
- "admin_announce_crit",
- "admin_announce_screen",
- "admin_announces",
- };
-
- @Override
- public boolean useAdminCommand(String command, L2PcInstance activeChar)
- {
- final StringTokenizer st = new StringTokenizer(command);
- final String cmd = st.hasMoreTokens() ? st.nextToken() : "";
- switch (cmd)
- {
- case "admin_announce":
- case "admin_announce_crit":
- case "admin_announce_screen":
- {
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announce <text to announce here>");
- return false;
- }
- String announce = st.nextToken();
- while (st.hasMoreTokens())
- {
- announce += " " + st.nextToken();
- }
- if (cmd.equals("admin_announce_screen"))
- {
- Broadcast.toAllOnlinePlayersOnScreen(announce);
- }
- else
- {
- if (Config.GM_ANNOUNCER_NAME)
- {
- announce = announce + " [" + activeChar.getName() + "]";
- }
- Broadcast.toAllOnlinePlayers(announce, cmd.equals("admin_announce_crit"));
- }
- AdminHtml.showAdminHtml(activeChar, "gm_menu.htm");
- break;
- }
- case "admin_announces":
- {
- final String subCmd = st.hasMoreTokens() ? st.nextToken() : "";
- switch (subCmd)
- {
- case "add":
- {
- if (!st.hasMoreTokens())
- {
- String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-add.htm");
- Util.sendCBHtml(activeChar, content);
- break;
- }
- final String annType = st.nextToken();
- final AnnouncementType type = AnnouncementType.findByName(annType);
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annInitDelay = st.nextToken();
- if (!Util.isDigit(annInitDelay))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int initDelay = Integer.parseInt(annInitDelay) * 1000;
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annDelay = st.nextToken();
- if (!Util.isDigit(annDelay))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int delay = Integer.parseInt(annDelay) * 1000;
- if ((delay < (10 * 1000)) && ((type == AnnouncementType.AUTO_NORMAL) || (type == AnnouncementType.AUTO_CRITICAL)))
- {
- activeChar.sendMessage("Delay cannot be less then 10 seconds!");
- break;
- }
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annRepeat = st.nextToken();
- if (!Util.isDigit(annRepeat))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int repeat = Integer.parseInt(annRepeat);
- if (repeat == 0)
- {
- repeat = -1;
- }
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String content = st.nextToken();
- while (st.hasMoreTokens())
- {
- content += " " + st.nextToken();
- }
- // ************************************
- final IAnnouncement announce;
- if ((type == AnnouncementType.AUTO_CRITICAL) || (type == AnnouncementType.AUTO_NORMAL))
- {
- announce = new AutoAnnouncement(type, content, activeChar.getName(), initDelay, delay, repeat);
- }
- else
- {
- announce = new Announcement(type, content, activeChar.getName());
- }
- AnnouncementsTable.getInstance().addAnnouncement(announce);
- activeChar.sendMessage("Announcement has been successfully added!");
- return useAdminCommand("admin_announces list", activeChar);
- }
- case "edit":
- {
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces edit <id>");
- break;
- }
- String annId = st.nextToken();
- if (!Util.isDigit(annId))
- {
- activeChar.sendMessage("Syntax: //announces edit <id>");
- break;
- }
- int id = Integer.parseInt(annId);
- final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
- if (announce == null)
- {
- activeChar.sendMessage("Announcement doesnt exists!");
- break;
- }
- if (!st.hasMoreTokens())
- {
- String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-edit.htm");
- String announcementId = "" + announce.getId();
- String announcementType = announce.getType().name();
- String announcementInital = "0";
- String announcementDelay = "0";
- String announcementRepeat = "0";
- String announcementAuthor = announce.getAuthor();
- String announcementContent = announce.getContent();
- if (announce instanceof AutoAnnouncement)
- {
- final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
- announcementInital = "" + (autoAnnounce.getInitial() / 1000);
- announcementDelay = "" + (autoAnnounce.getDelay() / 1000);
- announcementRepeat = "" + autoAnnounce.getRepeat();
- }
- content = content.replaceAll("%id%", announcementId);
- content = content.replaceAll("%type%", announcementType);
- content = content.replaceAll("%initial%", announcementInital);
- content = content.replaceAll("%delay%", announcementDelay);
- content = content.replaceAll("%repeat%", announcementRepeat);
- content = content.replaceAll("%author%", announcementAuthor);
- content = content.replaceAll("%content%", announcementContent);
- Util.sendCBHtml(activeChar, content);
- break;
- }
- final String annType = st.nextToken();
- final AnnouncementType type = AnnouncementType.findByName(annType);
- switch (announce.getType())
- {
- case AUTO_CRITICAL:
- case AUTO_NORMAL:
- {
- switch (type)
- {
- case AUTO_CRITICAL:
- case AUTO_NORMAL:
- {
- break;
- }
- default:
- {
- activeChar.sendMessage("Announce type can be changed only to AUTO_NORMAL or AUTO_CRITICAL!");
- return false;
- }
- }
- break;
- }
- case NORMAL:
- case CRITICAL:
- {
- switch (type)
- {
- case NORMAL:
- case CRITICAL:
- {
- break;
- }
- default:
- {
- activeChar.sendMessage("Announce type can be changed only to NORMAL or CRITICAL!");
- return false;
- }
- }
- break;
- }
- }
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annInitDelay = st.nextToken();
- if (!Util.isDigit(annInitDelay))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int initDelay = Integer.parseInt(annInitDelay);
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annDelay = st.nextToken();
- if (!Util.isDigit(annDelay))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int delay = Integer.parseInt(annDelay);
- if ((delay < 10) && ((type == AnnouncementType.AUTO_NORMAL) || (type == AnnouncementType.AUTO_CRITICAL)))
- {
- activeChar.sendMessage("Delay cannot be less then 10 seconds!");
- break;
- }
- // ************************************
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- String annRepeat = st.nextToken();
- if (!Util.isDigit(annRepeat))
- {
- activeChar.sendMessage("Syntax: //announces add <type> <delay> <repeat> <text>");
- break;
- }
- int repeat = Integer.parseInt(annRepeat);
- if (repeat == 0)
- {
- repeat = -1;
- }
- // ************************************
- String content = "";
- if (st.hasMoreTokens())
- {
- content = st.nextToken();
- while (st.hasMoreTokens())
- {
- content += " " + st.nextToken();
- }
- }
- if (content.isEmpty())
- {
- content = announce.getContent();
- }
- // ************************************
- announce.setType(type);
- announce.setContent(content);
- announce.setAuthor(activeChar.getName());
- if (announce instanceof AutoAnnouncement)
- {
- AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
- autoAnnounce.setInitial(initDelay * 1000);
- autoAnnounce.setDelay(delay * 1000);
- autoAnnounce.setRepeat(repeat);
- }
- announce.updateMe();
- activeChar.sendMessage("Announcement has been successfully edited!");
- return useAdminCommand("admin_announces list", activeChar);
- }
- case "remove":
- {
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces remove <announcement id>");
- break;
- }
- String token = st.nextToken();
- if (!Util.isDigit(token))
- {
- activeChar.sendMessage("Syntax: //announces remove <announcement id>");
- break;
- }
- int id = Integer.parseInt(token);
- if (AnnouncementsTable.getInstance().deleteAnnouncement(id))
- {
- activeChar.sendMessage("Announcement has been successfully removed!");
- }
- else
- {
- activeChar.sendMessage("Announcement doesnt exists!");
- }
- return useAdminCommand("admin_announces list", activeChar);
- }
- case "restart":
- {
- if (!st.hasMoreTokens())
- {
- for (IAnnouncement announce : AnnouncementsTable.getInstance().getAllAnnouncements())
- {
- if (announce instanceof AutoAnnouncement)
- {
- final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
- autoAnnounce.restartMe();
- }
- }
- activeChar.sendMessage("Auto announcements has been successfully restarted");
- break;
- }
- String token = st.nextToken();
- if (!Util.isDigit(token))
- {
- activeChar.sendMessage("Syntax: //announces show <announcement id>");
- break;
- }
- int id = Integer.parseInt(token);
- final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
- if (announce != null)
- {
- if (announce instanceof AutoAnnouncement)
- {
- final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
- autoAnnounce.restartMe();
- activeChar.sendMessage("Auto announcement has been successfully restarted");
- }
- else
- {
- activeChar.sendMessage("This option has effect only on auto announcements!");
- }
- }
- else
- {
- activeChar.sendMessage("Announcement doesnt exists!");
- }
- break;
- }
- case "show":
- {
- if (!st.hasMoreTokens())
- {
- activeChar.sendMessage("Syntax: //announces show <announcement id>");
- break;
- }
- String token = st.nextToken();
- if (!Util.isDigit(token))
- {
- activeChar.sendMessage("Syntax: //announces show <announcement id>");
- break;
- }
- int id = Integer.parseInt(token);
- final IAnnouncement announce = AnnouncementsTable.getInstance().getAnnounce(id);
- if (announce != null)
- {
- String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-show.htm");
- String announcementId = "" + announce.getId();
- String announcementType = announce.getType().name();
- String announcementInital = "0";
- String announcementDelay = "0";
- String announcementRepeat = "0";
- String announcementAuthor = announce.getAuthor();
- String announcementContent = announce.getContent();
- if (announce instanceof AutoAnnouncement)
- {
- final AutoAnnouncement autoAnnounce = (AutoAnnouncement) announce;
- announcementInital = "" + (autoAnnounce.getInitial() / 1000);
- announcementDelay = "" + (autoAnnounce.getDelay() / 1000);
- announcementRepeat = "" + autoAnnounce.getRepeat();
- }
- content = content.replaceAll("%id%", announcementId);
- content = content.replaceAll("%type%", announcementType);
- content = content.replaceAll("%initial%", announcementInital);
- content = content.replaceAll("%delay%", announcementDelay);
- content = content.replaceAll("%repeat%", announcementRepeat);
- content = content.replaceAll("%author%", announcementAuthor);
- content = content.replaceAll("%content%", announcementContent);
- Util.sendCBHtml(activeChar, content);
- break;
- }
- activeChar.sendMessage("Announcement doesnt exists!");
- return useAdminCommand("admin_announces list", activeChar);
- }
- case "list":
- {
- int page = 0;
- if (st.hasMoreTokens())
- {
- final String token = st.nextToken();
- if (Util.isDigit(token))
- {
- page = Integer.valueOf(token);
- }
- }
-
- String content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), "data/html/admin/announces-list.htm");
- final PageResult result = HtmlUtil.createPage(AnnouncementsTable.getInstance().getAllAnnouncements(), page, 8, currentPage ->
- {
- 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>";
- }, announcement ->
- {
- final StringBuilder sb = new StringBuilder();
- sb.append("<tr>");
- sb.append("<td width=5></td>");
- sb.append("<td width=80>" + announcement.getId() + "</td>");
- sb.append("<td width=100>" + announcement.getType() + "</td>");
- sb.append("<td width=100>" + announcement.getAuthor() + "</td>");
- if ((announcement.getType() == AnnouncementType.AUTO_NORMAL) || (announcement.getType() == AnnouncementType.AUTO_CRITICAL))
- {
- 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>");
- }
- else
- {
- sb.append("<td width=60><button action=\"\" value=\"\" width=\"60\" height=\"21\" back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
- }
- if (announcement.getType() == AnnouncementType.EVENT)
- {
- 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>");
- sb.append("<td width=60></td>");
- }
- else
- {
- 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>");
- 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>");
- }
- 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>");
- sb.append("<td width=5></td>");
- sb.append("</tr>");
- return sb.toString();
- });
- content = content.replaceAll("%pages%", result.getPagerTemplate().toString());
- content = content.replaceAll("%announcements%", result.getBodyTemplate().toString());
- Util.sendCBHtml(activeChar, content);
- break;
- }
- }
- }
- }
- return false;
- }
-
- @Override
- public String[] getAdminCommandList()
- {
- return ADMIN_COMMANDS;
- }
- }
|