AutoAnnounceTaskManager.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package net.sf.l2j.gameserver.taskmanager;
  14. import java.sql.Connection;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.util.List;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import net.sf.l2j.L2DatabaseFactory;
  22. import net.sf.l2j.gameserver.ThreadPoolManager;
  23. import net.sf.l2j.gameserver.util.Broadcast;
  24. /**
  25. *
  26. * @author nBd
  27. */
  28. public class AutoAnnounceTaskManager
  29. {
  30. protected static final Logger _log = Logger.getLogger(AutoAnnounceTaskManager.class.getName());
  31. private static AutoAnnounceTaskManager _instance;
  32. protected List<AutoAnnouncement> _announces = new FastList<AutoAnnouncement>();
  33. public static AutoAnnounceTaskManager getInstance()
  34. {
  35. if (_instance == null)
  36. _instance = new AutoAnnounceTaskManager();
  37. return _instance;
  38. }
  39. public AutoAnnounceTaskManager()
  40. {
  41. restore();
  42. }
  43. public void restore()
  44. {
  45. if (!_announces.isEmpty())
  46. {
  47. for (AutoAnnouncement a : _announces)
  48. a.stopAnnounce();
  49. _announces.clear();
  50. }
  51. Connection conn = null;
  52. int count = 0;
  53. try
  54. {
  55. conn = L2DatabaseFactory.getInstance().getConnection();
  56. PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
  57. ResultSet data = statement.executeQuery();
  58. while(data.next())
  59. {
  60. int id = data.getInt("id");
  61. long initial = data.getLong("initial");
  62. long delay = data.getLong("delay");
  63. int repeat = data.getInt("cycle");
  64. String memo = data.getString("memo");
  65. String[] text = memo.split("/n");
  66. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text), initial);
  67. count++;
  68. }
  69. data.close();
  70. statement.close();
  71. }
  72. catch (Exception e)
  73. {
  74. _log.log(Level.SEVERE, "AutoAnnoucements: Fail to load announcements data.", e);
  75. }
  76. finally
  77. {
  78. try { conn.close(); } catch (Exception e) {}
  79. }
  80. _log.log(Level.SEVERE, "AutoAnnoucements: Load "+count+" Auto Annoucement Data.");
  81. }
  82. private class AutoAnnouncement implements Runnable
  83. {
  84. private int _id;
  85. private long _delay;
  86. private int _repeat = -1;
  87. private String[] _memo;
  88. private boolean _stopped = false;
  89. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  90. {
  91. _id = id;
  92. _delay = delay;
  93. _repeat = repeat;
  94. _memo = memo;
  95. if (!_announces.contains(this))
  96. _announces.add(this);
  97. }
  98. public void stopAnnounce()
  99. {
  100. _stopped = true;
  101. }
  102. public void run()
  103. {
  104. for (String text : _memo)
  105. {
  106. announce(text);
  107. }
  108. if (!_stopped && _repeat > 0)
  109. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_id, _delay, _repeat--, _memo), _delay);
  110. }
  111. }
  112. public void announce(String text)
  113. {
  114. Broadcast.announceToOnlinePlayers(text);
  115. _log.warning("AutoAnnounce: " + text);
  116. }
  117. }