Advertisement
debiaan

Announcements Npc instance.

Feb 9th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  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 net.sf.l2j.gameserver.model.actor.instance;
  16.  
  17. import java.io.File;
  18. import java.io.FileReader;
  19. import java.io.FileWriter;
  20. import java.io.IOException;
  21. import java.io.LineNumberReader;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.StringTokenizer;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27.  
  28. import net.sf.l2j.gameserver.Announcements;
  29. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  30. import net.sf.l2j.gameserver.templates.chars.L2NpcTemplate;
  31.  
  32. /**
  33. * @author Debian
  34. *
  35. */
  36. public class L2AnnouncementsInstance extends L2NpcInstance
  37. {
  38. public L2AnnouncementsInstance(int objectId, L2NpcTemplate template)
  39. {
  40. super(objectId, template);
  41. }
  42.  
  43. private static Logger _log = Logger.getLogger(Announcements.class.getName());
  44.  
  45. private static Announcements _instance;
  46. private final List<String> _announcements = new ArrayList<>();
  47.  
  48. @SuppressWarnings("unused")
  49. private void Announcements()
  50. {
  51. loadAnnouncements();
  52. }
  53.  
  54. public static Announcements getInstance()
  55. {
  56. if (_instance == null)
  57. _instance = new Announcements();
  58.  
  59. return _instance;
  60. }
  61.  
  62. public void loadAnnouncements()
  63. {
  64. _announcements.clear();
  65. File file = new File("./data/custom/messages.txt");
  66. if (file.exists())
  67. readFromDisk(file);
  68. else
  69. _log.config("The messages file (normally located to 'data/custom/messages.txt') doesn't exist.");
  70. }
  71.  
  72. public void showAnnouncements(L2PcInstance player)
  73. {
  74. for (int i = 0; i < _announcements.size(); i++)
  75. {
  76. NpcHtmlMessage nhm = new NpcHtmlMessage(5);
  77. StringBuilder tb = new StringBuilder("");
  78.  
  79. tb.append("<html><head><title>Announcements</title></head><body>");
  80. tb.append("<center>");
  81. tb.append("<table width=\"250\" cellpadding=\"5\" bgcolor=\"000000\">");
  82. tb.append("<tr>");
  83. tb.append("<td width=\"45\" valign=\"top\" align=\"center\"><img src=\"L2ui_ch3.menubutton4\" width=\"38\" height=\"38\"></td>");
  84. tb.append("<td valign=\"top\"><font color=\"FF6600\">Announcements</font>");
  85. tb.append("<br1><font color=\"00FF00\">"+player.getName()+"</font>, use this interface to check server's announcements.<br1></td>");
  86. tb.append("</tr>");
  87. tb.append("</table>");
  88. tb.append("</center>");
  89. tb.append("<center>");
  90. tb.append("<table width=\"200\" border=\"1\" bgcolor=\"FFFFFF\">");
  91. tb.append("<tr><td align=\"center\"></td><td>"+ _announcements + "</td></tr>");
  92. tb.append("</center>");
  93. tb.append("</body></html>");
  94.  
  95. nhm.setHtml(tb.toString());
  96. player.sendPacket(nhm);
  97. }
  98. }
  99.  
  100. public void addAnnouncement(String text)
  101. {
  102. _announcements.add(text);
  103. saveToDisk();
  104. }
  105.  
  106. public void delAnnouncement(int line)
  107. {
  108. _announcements.remove(line);
  109. saveToDisk();
  110. }
  111.  
  112. private void readFromDisk(File file)
  113. {
  114. try (LineNumberReader lnr = new LineNumberReader(new FileReader(file)))
  115. {
  116. int i = 0;
  117. String line = null;
  118. while ((line = lnr.readLine()) != null)
  119. {
  120. StringTokenizer st = new StringTokenizer(line, "\n\r");
  121. if (st.hasMoreTokens())
  122. {
  123. String announcement = st.nextToken();
  124. _announcements.add(announcement);
  125.  
  126. i++;
  127. }
  128. }
  129. _log.config("Announcements: Loaded " + i + " Announcements.");
  130. }
  131. catch (IOException e1)
  132. {
  133. _log.log(Level.SEVERE, "Error reading announcements", e1);
  134. }
  135. }
  136.  
  137. private void saveToDisk()
  138. {
  139. final File file = new File("data/custom/messages.txt");
  140. try (FileWriter save = new FileWriter(file))
  141. {
  142. for (int i = 0; i < _announcements.size(); i++)
  143. {
  144. save.write(_announcements.get(i));
  145. save.write("\r\n");
  146. }
  147. }
  148. catch (IOException e)
  149. {
  150. _log.warning("saving the announcements file has failed: " + e);
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement