PunishmentManager.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.ResultSet;
  22. import java.sql.Statement;
  23. import java.util.Map;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  28. import com.l2jserver.gameserver.model.holders.PunishmentHolder;
  29. import com.l2jserver.gameserver.model.punishment.PunishmentAffect;
  30. import com.l2jserver.gameserver.model.punishment.PunishmentTask;
  31. import com.l2jserver.gameserver.model.punishment.PunishmentType;
  32. /**
  33. * @author UnAfraid
  34. */
  35. public final class PunishmentManager
  36. {
  37. private static final Logger _log = Logger.getLogger(PunishmentManager.class.getName());
  38. private final Map<PunishmentAffect, PunishmentHolder> _tasks = new ConcurrentHashMap<>();
  39. protected PunishmentManager()
  40. {
  41. load();
  42. }
  43. private void load()
  44. {
  45. // Initiate task holders.
  46. for (PunishmentAffect affect : PunishmentAffect.values())
  47. {
  48. _tasks.put(affect, new PunishmentHolder());
  49. }
  50. int initiated = 0;
  51. int expired = 0;
  52. // Load punishments.
  53. try (Connection con = ConnectionFactory.getInstance().getConnection();
  54. Statement st = con.createStatement();
  55. ResultSet rset = st.executeQuery("SELECT * FROM punishments"))
  56. {
  57. while (rset.next())
  58. {
  59. final int id = rset.getInt("id");
  60. final String key = rset.getString("key");
  61. final PunishmentAffect affect = PunishmentAffect.getByName(rset.getString("affect"));
  62. final PunishmentType type = PunishmentType.getByName(rset.getString("type"));
  63. final long expirationTime = rset.getLong("expiration");
  64. final String reason = rset.getString("reason");
  65. final String punishedBy = rset.getString("punishedBy");
  66. if ((type != null) && (affect != null))
  67. {
  68. if ((expirationTime > 0) && (System.currentTimeMillis() > expirationTime)) // expired task.
  69. {
  70. expired++;
  71. }
  72. else
  73. {
  74. initiated++;
  75. _tasks.get(affect).addPunishment(new PunishmentTask(id, key, affect, type, expirationTime, reason, punishedBy, true));
  76. }
  77. }
  78. }
  79. }
  80. catch (Exception e)
  81. {
  82. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error while loading punishments: ", e);
  83. }
  84. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded " + initiated + " active and " + expired + " expired punishments.");
  85. }
  86. public void startPunishment(PunishmentTask task)
  87. {
  88. _tasks.get(task.getAffect()).addPunishment(task);
  89. }
  90. public void stopPunishment(Object key, PunishmentAffect affect, PunishmentType type)
  91. {
  92. final PunishmentTask task = getPunishment(key, affect, type);
  93. if (task != null)
  94. {
  95. _tasks.get(affect).stopPunishment(task);
  96. }
  97. }
  98. public boolean hasPunishment(Object key, PunishmentAffect affect, PunishmentType type)
  99. {
  100. final PunishmentHolder holder = _tasks.get(affect);
  101. return holder.hasPunishment(String.valueOf(key), type);
  102. }
  103. public long getPunishmentExpiration(Object key, PunishmentAffect affect, PunishmentType type)
  104. {
  105. final PunishmentTask p = getPunishment(key, affect, type);
  106. return p != null ? p.getExpirationTime() : 0;
  107. }
  108. private PunishmentTask getPunishment(Object key, PunishmentAffect affect, PunishmentType type)
  109. {
  110. return _tasks.get(affect).getPunishment(String.valueOf(key), type);
  111. }
  112. /**
  113. * Gets the single instance of {@code PunishmentManager}.
  114. * @return single instance of {@code PunishmentManager}
  115. */
  116. public static final PunishmentManager getInstance()
  117. {
  118. return SingletonHolder._instance;
  119. }
  120. private static class SingletonHolder
  121. {
  122. protected static final PunishmentManager _instance = new PunishmentManager();
  123. }
  124. }