Announcements.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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;
  16. import java.io.File;
  17. import java.io.FileReader;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.LineNumberReader;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.StringTokenizer;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import javolution.util.FastList;
  27. import net.sf.l2j.Config;
  28. import net.sf.l2j.gameserver.cache.HtmCache;
  29. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  30. import net.sf.l2j.gameserver.network.SystemMessageId;
  31. import net.sf.l2j.gameserver.network.clientpackets.Say2;
  32. import net.sf.l2j.gameserver.network.serverpackets.CreatureSay;
  33. import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;
  34. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  35. import net.sf.l2j.gameserver.script.DateRange;
  36. import net.sf.l2j.gameserver.util.Broadcast;
  37. import net.sf.l2j.gameserver.util.StringUtil;
  38. /**
  39. * This class ...
  40. *
  41. * @version $Revision: 1.5.2.1.2.7 $ $Date: 2005/03/29 23:15:14 $
  42. */
  43. public class Announcements
  44. {
  45. private static Logger _log = Logger.getLogger(Announcements.class.getName());
  46. private static Announcements _instance;
  47. private List<String> _announcements = new FastList<String>();
  48. private List<List<Object>> _eventAnnouncements = new FastList<List<Object>>();
  49. public Announcements()
  50. {
  51. loadAnnouncements();
  52. }
  53. public static Announcements getInstance()
  54. {
  55. if (_instance == null)
  56. {
  57. _instance = new Announcements();
  58. }
  59. return _instance;
  60. }
  61. public void loadAnnouncements()
  62. {
  63. _announcements.clear();
  64. File file = new File(Config.DATAPACK_ROOT, "data/announcements.txt");
  65. if (file.exists())
  66. {
  67. readFromDisk(file);
  68. }
  69. else
  70. {
  71. _log.config("data/announcements.txt doesn't exist");
  72. }
  73. }
  74. public void showAnnouncements(L2PcInstance activeChar)
  75. {
  76. for (int i = 0; i < _announcements.size(); i++)
  77. {
  78. CreatureSay cs = new CreatureSay(0, Say2.ANNOUNCEMENT, activeChar.getName(), _announcements.get(i));
  79. activeChar.sendPacket(cs);
  80. }
  81. for (int i = 0; i < _eventAnnouncements.size(); i++)
  82. {
  83. List<Object> entry = _eventAnnouncements.get(i);
  84. DateRange validDateRange = (DateRange) entry.get(0);
  85. String[] msg = (String[]) entry.get(1);
  86. Date currentDate = new Date();
  87. if (!validDateRange.isValid() || validDateRange.isWithinRange(currentDate))
  88. {
  89. SystemMessage sm = new SystemMessage(SystemMessageId.S1);
  90. for (int j = 0; j < msg.length; j++)
  91. {
  92. sm.addString(msg[j]);
  93. }
  94. activeChar.sendPacket(sm);
  95. }
  96. }
  97. }
  98. public void addEventAnnouncement(DateRange validDateRange, String[] msg)
  99. {
  100. List<Object> entry = new FastList<Object>();
  101. entry.add(validDateRange);
  102. entry.add(msg);
  103. _eventAnnouncements.add(entry);
  104. }
  105. public void listAnnouncements(L2PcInstance activeChar)
  106. {
  107. String content = HtmCache.getInstance().getHtmForce("data/html/admin/announce.htm");
  108. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  109. adminReply.setHtml(content);
  110. final StringBuilder replyMSG = StringUtil.startAppend(500, "<br>");
  111. for (int i = 0; i < _announcements.size(); i++)
  112. {
  113. StringUtil.append(replyMSG, "<table width=260><tr><td width=220>", _announcements.get(i), "</td><td width=40>"
  114. + "<button value=\"Delete\" action=\"bypass -h admin_del_announcement ", String.valueOf(i),
  115. "\" width=60 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr></table>");
  116. }
  117. adminReply.replace("%announces%", replyMSG.toString());
  118. activeChar.sendPacket(adminReply);
  119. }
  120. public void addAnnouncement(String text)
  121. {
  122. _announcements.add(text);
  123. saveToDisk();
  124. }
  125. public void delAnnouncement(int line)
  126. {
  127. _announcements.remove(line);
  128. saveToDisk();
  129. }
  130. private void readFromDisk(File file)
  131. {
  132. LineNumberReader lnr = null;
  133. try
  134. {
  135. int i = 0;
  136. String line = null;
  137. lnr = new LineNumberReader(new FileReader(file));
  138. while ((line = lnr.readLine()) != null)
  139. {
  140. StringTokenizer st = new StringTokenizer(line, "\n\r");
  141. if (st.hasMoreTokens())
  142. {
  143. String announcement = st.nextToken();
  144. _announcements.add(announcement);
  145. i++;
  146. }
  147. }
  148. if (Config.DEBUG)
  149. _log.config("Announcements: Loaded " + i + " Announcements.");
  150. }
  151. catch (IOException e1)
  152. {
  153. _log.log(Level.SEVERE, "Error reading announcements: ", e1);
  154. }
  155. finally
  156. {
  157. try
  158. {
  159. lnr.close();
  160. }
  161. catch (Exception e2)
  162. {
  163. // nothing
  164. }
  165. }
  166. }
  167. private void saveToDisk()
  168. {
  169. File file = new File("data/announcements.txt");
  170. FileWriter save = null;
  171. try
  172. {
  173. save = new FileWriter(file);
  174. for (int i = 0; i < _announcements.size(); i++)
  175. {
  176. save.write(_announcements.get(i));
  177. save.write("\r\n");
  178. }
  179. }
  180. catch (IOException e)
  181. {
  182. _log.log(Level.SEVERE, "Saving to the announcements file has failed: ", e);
  183. }
  184. finally
  185. {
  186. try
  187. {
  188. save.close();
  189. }
  190. catch (Exception e)
  191. {
  192. }
  193. }
  194. }
  195. public void announceToAll(String text)
  196. {
  197. Broadcast.announceToOnlinePlayers(text);
  198. }
  199. public void announceToAll(SystemMessage sm)
  200. {
  201. Broadcast.toAllOnlinePlayers(sm);
  202. }
  203. public void announceToInstance(SystemMessage sm, int instanceId)
  204. {
  205. Broadcast.toPlayersInInstance(sm, instanceId);
  206. }
  207. // Method for handling announcements from admin
  208. public void handleAnnounce(String command, int lengthToTrim)
  209. {
  210. try
  211. {
  212. // Announce string to everyone on server
  213. String text = command.substring(lengthToTrim);
  214. Announcements.getInstance().announceToAll(text);
  215. }
  216. // No body cares!
  217. catch (StringIndexOutOfBoundsException e)
  218. {
  219. // empty message.. ignore
  220. }
  221. }
  222. }