AutoAnnounceTaskManager.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. protected List<AutoAnnouncement> _announces = new FastList<AutoAnnouncement>();
  32. public static AutoAnnounceTaskManager getInstance()
  33. {
  34. return SingletonHolder._instance;
  35. }
  36. private AutoAnnounceTaskManager()
  37. {
  38. restore();
  39. }
  40. public void restore()
  41. {
  42. if (!_announces.isEmpty())
  43. {
  44. for (AutoAnnouncement a : _announces)
  45. a.stopAnnounce();
  46. _announces.clear();
  47. }
  48. Connection conn = null;
  49. int count = 0;
  50. try
  51. {
  52. conn = L2DatabaseFactory.getInstance().getConnection();
  53. PreparedStatement statement = conn.prepareStatement("SELECT id, initial, delay, cycle, memo FROM auto_announcements");
  54. ResultSet data = statement.executeQuery();
  55. while (data.next())
  56. {
  57. int id = data.getInt("id");
  58. long initial = data.getLong("initial");
  59. long delay = data.getLong("delay");
  60. int repeat = data.getInt("cycle");
  61. String memo = data.getString("memo");
  62. String[] text = memo.split("/n");
  63. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(id, delay, repeat, text), initial);
  64. count++;
  65. }
  66. data.close();
  67. statement.close();
  68. }
  69. catch (Exception e)
  70. {
  71. _log.log(Level.SEVERE, "AutoAnnoucements: Failed to load announcements data.", e);
  72. }
  73. finally
  74. {
  75. try
  76. {
  77. conn.close();
  78. }
  79. catch (Exception e)
  80. {
  81. }
  82. }
  83. _log.log(Level.INFO, "AutoAnnoucements: Loaded " + count + " Auto Annoucement Data.");
  84. }
  85. private class AutoAnnouncement implements Runnable
  86. {
  87. private int _id;
  88. private long _delay;
  89. private int _repeat = -1;
  90. private String[] _memo;
  91. private boolean _stopped = false;
  92. public AutoAnnouncement(int id, long delay, int repeat, String[] memo)
  93. {
  94. _id = id;
  95. _delay = delay;
  96. _repeat = repeat;
  97. _memo = memo;
  98. if (!_announces.contains(this))
  99. _announces.add(this);
  100. }
  101. public void stopAnnounce()
  102. {
  103. _stopped = true;
  104. }
  105. public void run()
  106. {
  107. for (String text : _memo)
  108. {
  109. announce(text);
  110. }
  111. if (!_stopped && _repeat > 0)
  112. ThreadPoolManager.getInstance().scheduleGeneral(new AutoAnnouncement(_id, _delay, _repeat--, _memo), _delay);
  113. }
  114. }
  115. public void announce(String text)
  116. {
  117. Broadcast.announceToOnlinePlayers(text);
  118. _log.warning("AutoAnnounce: " + text);
  119. }
  120. @SuppressWarnings("synthetic-access")
  121. private static class SingletonHolder
  122. {
  123. protected static final AutoAnnounceTaskManager _instance = new AutoAnnounceTaskManager();
  124. }
  125. }