AutoAnnounceTaskManager.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.PreparedStatement;
  15. import java.sql.ResultSet;
  16. import java.util.List;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javolution.util.FastList;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.ThreadPoolManager;
  22. import net.sf.l2j.gameserver.util.Broadcast;
  23. /**
  24. *
  25. * @author nBd
  26. */
  27. public class AutoAnnounceTaskManager
  28. {
  29. protected static final Logger _log = Logger.getLogger(AutoAnnounceTaskManager.class.getName());
  30. private static AutoAnnounceTaskManager _instance;
  31. protected List<AutoAnnouncement> _announces = new FastList<AutoAnnouncement>();
  32. public static AutoAnnounceTaskManager getInstance()
  33. {
  34. if (_instance == null)
  35. _instance = new AutoAnnounceTaskManager();
  36. return _instance;
  37. }
  38. public AutoAnnounceTaskManager()
  39. {
  40. restore();
  41. }
  42. public void restore()
  43. {
  44. if (!_announces.isEmpty())
  45. {
  46. for (AutoAnnouncement a : _announces)
  47. a.stopAnnounce();
  48. _announces.clear();
  49. }
  50. java.sql.Connection conn = null;
  51. int count = 0;
  52. try
  53. {
  54. conn = L2DatabaseFactory.getInstance().getConnection();
  55. PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
  56. ResultSet data = statement.executeQuery();
  57. while(data.next())
  58. {
  59. int id = data.getInt("id");
  60. long initial = data.getLong("initial");
  61. long delay = data.getLong("delay");
  62. int repeat = data.getInt("cycle");
  63. String memo = data.getString("memo");
  64. String[] text = memo.split("/n");
  65. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text), initial);
  66. count++;
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. _log.log(Level.SEVERE, "AutoAnnoucements: Fail to load announcements data.", e);
  72. }
  73. _log.log(Level.SEVERE, "AutoAnnoucements: Load "+count+" Auto Annoucement Data.");
  74. }
  75. private class AutoAnnouncement implements Runnable
  76. {
  77. private int _id;
  78. private long _delay;
  79. private int _repeat = -1;
  80. private String[] _memo;
  81. private boolean _stopped = false;
  82. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  83. {
  84. _id = id;
  85. _delay = delay;
  86. _repeat = repeat;
  87. _memo = memo;
  88. if (!_announces.contains(this))
  89. _announces.add(this);
  90. }
  91. public void stopAnnounce()
  92. {
  93. _stopped = true;
  94. }
  95. public void run()
  96. {
  97. for (String text : _memo)
  98. {
  99. announce(text);
  100. }
  101. if (!_stopped && _repeat > 0)
  102. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_id, _delay, _repeat--, _memo), _delay);
  103. }
  104. }
  105. public void announce(String text)
  106. {
  107. Broadcast.announceToOnlinePlayers(text);
  108. _log.warning("AutoAnnounce: " + text);
  109. }
  110. }