Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program 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.
- *
- * This program 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 net.sf.l2j.gameserver.model.actor.instance;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.LineNumberReader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import net.sf.l2j.gameserver.Announcements;
- import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
- import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
- /**
- * @author Debian
- *
- */
- public class L2AnnouncementsInstance extends L2NpcInstance
- {
- public L2AnnouncementsInstance(int objectId, L2NpcTemplate template)
- {
- super(objectId, template);
- }
- private static Logger _log = Logger.getLogger(Announcements.class.getName());
- private static Announcements _instance;
- private final List<String> _announcements = new ArrayList<>();
- @SuppressWarnings("unused")
- private void Announcements()
- {
- loadAnnouncements();
- }
- public static Announcements getInstance()
- {
- if (_instance == null)
- _instance = new Announcements();
- return _instance;
- }
- public void loadAnnouncements()
- {
- _announcements.clear();
- File file = new File("./data/custom/messages.txt");
- if (file.exists())
- readFromDisk(file);
- else
- _log.config("The messages file (normally located to 'data/custom/messages.txt') doesn't exist.");
- }
- public void showAnnouncements(L2PcInstance player)
- {
- for (int i = 0; i < _announcements.size(); i++)
- {
- NpcHtmlMessage nhm = new NpcHtmlMessage(5);
- StringBuilder tb = new StringBuilder("");
- tb.append("<html><head><title>Announcements</title></head><body>");
- tb.append("<center>");
- tb.append("<table width=\"250\" cellpadding=\"5\" bgcolor=\"000000\">");
- tb.append("<tr>");
- tb.append("<td width=\"45\" valign=\"top\" align=\"center\"><img src=\"L2ui_ch3.menubutton4\" width=\"38\" height=\"38\"></td>");
- tb.append("<td valign=\"top\"><font color=\"FF6600\">Announcements</font>");
- tb.append("<br1><font color=\"00FF00\">"+player.getName()+"</font>, use this interface to check server's announcements.<br1></td>");
- tb.append("</tr>");
- tb.append("</table>");
- tb.append("</center>");
- tb.append("<center>");
- tb.append("<table width=\"200\" border=\"1\" bgcolor=\"FFFFFF\">");
- tb.append("<tr><td align=\"center\"></td><td>"+ _announcements + "</td></tr>");
- tb.append("</center>");
- tb.append("</body></html>");
- nhm.setHtml(tb.toString());
- player.sendPacket(nhm);
- }
- }
- public void addAnnouncement(String text)
- {
- _announcements.add(text);
- saveToDisk();
- }
- public void delAnnouncement(int line)
- {
- _announcements.remove(line);
- saveToDisk();
- }
- private void readFromDisk(File file)
- {
- try (LineNumberReader lnr = new LineNumberReader(new FileReader(file)))
- {
- int i = 0;
- String line = null;
- while ((line = lnr.readLine()) != null)
- {
- StringTokenizer st = new StringTokenizer(line, "\n\r");
- if (st.hasMoreTokens())
- {
- String announcement = st.nextToken();
- _announcements.add(announcement);
- i++;
- }
- }
- _log.config("Announcements: Loaded " + i + " Announcements.");
- }
- catch (IOException e1)
- {
- _log.log(Level.SEVERE, "Error reading announcements", e1);
- }
- }
- private void saveToDisk()
- {
- final File file = new File("data/custom/messages.txt");
- try (FileWriter save = new FileWriter(file))
- {
- for (int i = 0; i < _announcements.size(); i++)
- {
- save.write(_announcements.get(i));
- save.write("\r\n");
- }
- }
- catch (IOException e)
- {
- _log.warning("saving the announcements file has failed: " + e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement