AutoAnnounceTaskManager.java 3.0 KB

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