PunishmentManager.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright © 2004-2019 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.util.Map;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import com.l2jserver.commons.database.ConnectionFactory;
  25. import com.l2jserver.gameserver.model.holders.PunishmentHolder;
  26. import com.l2jserver.gameserver.model.punishment.PunishmentAffect;
  27. import com.l2jserver.gameserver.model.punishment.PunishmentTask;
  28. import com.l2jserver.gameserver.model.punishment.PunishmentType;
  29. /**
  30. * Punishment Manager.
  31. * @author UnAfraid
  32. */
  33. public final class PunishmentManager {
  34. private static final Logger LOG = LoggerFactory.getLogger(PunishmentManager.class);
  35. private final Map<PunishmentAffect, PunishmentHolder> _tasks = new ConcurrentHashMap<>();
  36. protected PunishmentManager() {
  37. load();
  38. }
  39. private void load() {
  40. // Initiate task holders.
  41. for (PunishmentAffect affect : PunishmentAffect.values()) {
  42. _tasks.put(affect, new PunishmentHolder());
  43. }
  44. int initiated = 0;
  45. int expired = 0;
  46. try (var con = ConnectionFactory.getInstance().getConnection();
  47. var st = con.createStatement();
  48. var rset = st.executeQuery("SELECT * FROM punishments")) {
  49. while (rset.next()) {
  50. final int id = rset.getInt("id");
  51. final String key = rset.getString("key");
  52. final PunishmentAffect affect = PunishmentAffect.getByName(rset.getString("affect"));
  53. final PunishmentType type = PunishmentType.getByName(rset.getString("type"));
  54. final long expirationTime = rset.getLong("expiration");
  55. final String reason = rset.getString("reason");
  56. final String punishedBy = rset.getString("punishedBy");
  57. if ((type != null) && (affect != null)) {
  58. if ((expirationTime > 0) && (System.currentTimeMillis() > expirationTime)) // expired task.
  59. {
  60. expired++;
  61. } else {
  62. initiated++;
  63. _tasks.get(affect).addPunishment(new PunishmentTask(id, key, affect, type, expirationTime, reason, punishedBy, true));
  64. }
  65. }
  66. }
  67. } catch (Exception ex) {
  68. LOG.warn("There has been an error while loading punishments!", ex);
  69. }
  70. LOG.info("Loaded {} active and {} expired punishments.", initiated, expired);
  71. }
  72. public void startPunishment(PunishmentTask task) {
  73. _tasks.get(task.getAffect()).addPunishment(task);
  74. }
  75. public void stopPunishment(Object key, PunishmentAffect affect, PunishmentType type) {
  76. final PunishmentTask task = getPunishment(key, affect, type);
  77. if (task != null) {
  78. _tasks.get(affect).stopPunishment(task);
  79. }
  80. }
  81. public boolean hasPunishment(Object key, PunishmentAffect affect, PunishmentType type) {
  82. final PunishmentHolder holder = _tasks.get(affect);
  83. return holder.hasPunishment(String.valueOf(key), type);
  84. }
  85. public long getPunishmentExpiration(Object key, PunishmentAffect affect, PunishmentType type) {
  86. final PunishmentTask p = getPunishment(key, affect, type);
  87. return p != null ? p.getExpirationTime() : 0;
  88. }
  89. private PunishmentTask getPunishment(Object key, PunishmentAffect affect, PunishmentType type) {
  90. return _tasks.get(affect).getPunishment(String.valueOf(key), type);
  91. }
  92. /**
  93. * Gets the single instance of {@code PunishmentManager}.
  94. * @return single instance of {@code PunishmentManager}
  95. */
  96. public static final PunishmentManager getInstance() {
  97. return SingletonHolder._instance;
  98. }
  99. private static class SingletonHolder {
  100. protected static final PunishmentManager _instance = new PunishmentManager();
  101. }
  102. }