AutoAnnounceTaskManager.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }
  49. java.sql.Connection conn = null;
  50. int count = 0;
  51. try
  52. {
  53. conn = L2DatabaseFactory.getInstance().getConnection();
  54. PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
  55. ResultSet data = statement.executeQuery();
  56. while(data.next())
  57. {
  58. int id = data.getInt("id");
  59. long initial = data.getLong("initial");
  60. long delay = data.getLong("delay");
  61. int repeat = data.getInt("cycle");
  62. String memo = data.getString("memo");
  63. String[] text = memo.split("/n");
  64. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text), initial);
  65. count++;
  66. }
  67. }
  68. catch (Exception e)
  69. {
  70. _log.log(Level.SEVERE, "AutoAnnoucements: Fail to load announcements data.", e);
  71. }
  72. _log.log(Level.SEVERE, "AutoAnnoucements: Load "+count+" Auto Annoucement Data.");
  73. }
  74. private class AutoAnnouncement implements Runnable
  75. {
  76. private int _id;
  77. private long _delay;
  78. private int _repeat = -1;
  79. private String[] _memo;
  80. private boolean _stopped = false;
  81. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  82. {
  83. _id = id;
  84. _delay = delay;
  85. _repeat = repeat;
  86. _memo = memo;
  87. if (!_announces.contains(this))
  88. _announces.add(this);
  89. }
  90. public void stopAnnounce()
  91. {
  92. _stopped = true;
  93. }
  94. public void run()
  95. {
  96. for (String text : _memo)
  97. {
  98. announce(text);
  99. }
  100. if (!_stopped && _repeat > 0)
  101. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_id, _delay, _repeat--, _memo), _delay);
  102. }
  103. }
  104. public void announce(String text)
  105. {
  106. Broadcast.announceToOnlinePlayers(text);
  107. _log.warning("AutoAnnounce: " + text);
  108. }
  109. }