2
0

AttackStanceTaskManager.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.taskmanager;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import java.util.concurrent.ConcurrentHashMap;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.instance.L2CubicInstance;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  31. /**
  32. * Attack stance task manager.
  33. * @author Luca Baldi, Zoey76
  34. */
  35. public class AttackStanceTaskManager
  36. {
  37. protected static final Logger _log = Logger.getLogger(AttackStanceTaskManager.class.getName());
  38. protected static final Map<L2Character, Long> _attackStanceTasks = new ConcurrentHashMap<>();
  39. /**
  40. * Instantiates a new attack stance task manager.
  41. */
  42. protected AttackStanceTaskManager()
  43. {
  44. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
  45. }
  46. /**
  47. * Adds the attack stance task.
  48. * @param actor the actor
  49. */
  50. public void addAttackStanceTask(L2Character actor)
  51. {
  52. if (actor != null)
  53. {
  54. if (actor.isPlayable())
  55. {
  56. final L2PcInstance player = actor.getActingPlayer();
  57. for (L2CubicInstance cubic : player.getCubics().values())
  58. {
  59. if (cubic.getId() != L2CubicInstance.LIFE_CUBIC)
  60. {
  61. cubic.doAction();
  62. }
  63. }
  64. }
  65. _attackStanceTasks.put(actor, System.currentTimeMillis());
  66. }
  67. }
  68. /**
  69. * Removes the attack stance task.
  70. * @param actor the actor
  71. */
  72. public void removeAttackStanceTask(L2Character actor)
  73. {
  74. if (actor != null)
  75. {
  76. if (actor.isSummon())
  77. {
  78. actor = actor.getActingPlayer();
  79. }
  80. _attackStanceTasks.remove(actor);
  81. }
  82. }
  83. /**
  84. * Checks for attack stance task.<br>
  85. * @param actor the actor
  86. * @return {@code true} if the character has an attack stance task, {@code false} otherwise
  87. */
  88. public boolean hasAttackStanceTask(L2Character actor)
  89. {
  90. if (actor != null)
  91. {
  92. if (actor.isSummon())
  93. {
  94. actor = actor.getActingPlayer();
  95. }
  96. return _attackStanceTasks.containsKey(actor);
  97. }
  98. return false;
  99. }
  100. protected class FightModeScheduler implements Runnable
  101. {
  102. @Override
  103. public void run()
  104. {
  105. long current = System.currentTimeMillis();
  106. try
  107. {
  108. final Iterator<Entry<L2Character, Long>> iter = _attackStanceTasks.entrySet().iterator();
  109. Entry<L2Character, Long> e;
  110. L2Character actor;
  111. while (iter.hasNext())
  112. {
  113. e = iter.next();
  114. if ((current - e.getValue()) > 15000)
  115. {
  116. actor = e.getKey();
  117. if (actor != null)
  118. {
  119. actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
  120. actor.getAI().setAutoAttacking(false);
  121. if (actor.isPlayer() && actor.hasSummon())
  122. {
  123. actor.getSummon().broadcastPacket(new AutoAttackStop(actor.getSummon().getObjectId()));
  124. }
  125. }
  126. iter.remove();
  127. }
  128. }
  129. }
  130. catch (Exception e)
  131. {
  132. // Unless caught here, players remain in attack positions.
  133. _log.log(Level.WARNING, "Error in FightModeScheduler: " + e.getMessage(), e);
  134. }
  135. }
  136. }
  137. /**
  138. * Gets the single instance of AttackStanceTaskManager.
  139. * @return single instance of AttackStanceTaskManager
  140. */
  141. public static AttackStanceTaskManager getInstance()
  142. {
  143. return SingletonHolder._instance;
  144. }
  145. private static class SingletonHolder
  146. {
  147. protected static final AttackStanceTaskManager _instance = new AttackStanceTaskManager();
  148. }
  149. }