AnnouncementsTable.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.data.sql.impl;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.Collection;
  24. import java.util.Map;
  25. import java.util.concurrent.ConcurrentSkipListMap;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.model.announce.Announcement;
  31. import com.l2jserver.gameserver.model.announce.AnnouncementType;
  32. import com.l2jserver.gameserver.model.announce.AutoAnnouncement;
  33. import com.l2jserver.gameserver.model.announce.IAnnouncement;
  34. import com.l2jserver.gameserver.network.clientpackets.Say2;
  35. import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
  36. /**
  37. * Loads announcements from database.
  38. * @author UnAfraid
  39. */
  40. public final class AnnouncementsTable
  41. {
  42. private static Logger LOGGER = Logger.getLogger(AnnouncementsTable.class.getName());
  43. private final Map<Integer, IAnnouncement> _announcements = new ConcurrentSkipListMap<>();
  44. protected AnnouncementsTable()
  45. {
  46. load();
  47. }
  48. private void load()
  49. {
  50. _announcements.clear();
  51. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  52. Statement st = con.createStatement();
  53. ResultSet rset = st.executeQuery("SELECT * FROM announcements"))
  54. {
  55. while (rset.next())
  56. {
  57. final AnnouncementType type = AnnouncementType.findById(rset.getInt("type"));
  58. final Announcement announce;
  59. switch (type)
  60. {
  61. case NORMAL:
  62. case CRITICAL:
  63. {
  64. announce = new Announcement(rset);
  65. break;
  66. }
  67. case AUTO_NORMAL:
  68. case AUTO_CRITICAL:
  69. {
  70. announce = new AutoAnnouncement(rset);
  71. break;
  72. }
  73. default:
  74. {
  75. continue;
  76. }
  77. }
  78. _announcements.put(announce.getId(), announce);
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. LOGGER.log(Level.WARNING, getClass().getSimpleName() + ": Failed loading announcements:", e);
  84. }
  85. }
  86. /**
  87. * Sending all announcements to the player
  88. * @param player
  89. */
  90. public void showAnnouncements(L2PcInstance player)
  91. {
  92. sendAnnouncements(player, AnnouncementType.NORMAL);
  93. sendAnnouncements(player, AnnouncementType.CRITICAL);
  94. sendAnnouncements(player, AnnouncementType.EVENT);
  95. }
  96. /**
  97. * Sends all announcements to the player by the specified type
  98. * @param player
  99. * @param type
  100. */
  101. public void sendAnnouncements(L2PcInstance player, AnnouncementType type)
  102. {
  103. for (IAnnouncement announce : _announcements.values())
  104. {
  105. if (announce.isValid() && (announce.getType() == type))
  106. {
  107. player.sendPacket(new CreatureSay(0, Say2.ANNOUNCEMENT, player.getName(), announce.getContent()));
  108. }
  109. }
  110. }
  111. /**
  112. * Adds announcement
  113. * @param announce
  114. */
  115. public void addAnnouncement(IAnnouncement announce)
  116. {
  117. if (announce.storeMe())
  118. {
  119. _announcements.put(announce.getId(), announce);
  120. }
  121. }
  122. /**
  123. * Removes announcement by id
  124. * @param id
  125. * @return {@code true} if announcement exists and was deleted successfully, {@code false} otherwise.
  126. */
  127. public boolean deleteAnnouncement(int id)
  128. {
  129. final IAnnouncement announce = _announcements.remove(id);
  130. return (announce != null) && announce.deleteMe();
  131. }
  132. /**
  133. * @param id
  134. * @return {@link IAnnouncement} by id
  135. */
  136. public IAnnouncement getAnnounce(int id)
  137. {
  138. return _announcements.get(id);
  139. }
  140. /**
  141. * @return {@link Collection} containing all announcements
  142. */
  143. public Collection<IAnnouncement> getAllAnnouncements()
  144. {
  145. return _announcements.values();
  146. }
  147. /**
  148. * @return Single instance of {@link AnnouncementsTable}
  149. */
  150. public static AnnouncementsTable getInstance()
  151. {
  152. return SingletonHolder._instance;
  153. }
  154. private static class SingletonHolder
  155. {
  156. protected static final AnnouncementsTable _instance = new AnnouncementsTable();
  157. }
  158. }