AttackStanceTaskManager.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.taskmanager;
  16. import java.util.Iterator;
  17. import java.util.Map.Entry;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastMap;
  21. import com.l2jserver.gameserver.ThreadPoolManager;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.actor.instance.L2CubicInstance;
  24. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  25. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  26. /**
  27. * @author Luca Baldi, Zoey76
  28. */
  29. public class AttackStanceTaskManager
  30. {
  31. protected static final Logger _log = Logger.getLogger(AttackStanceTaskManager.class.getName());
  32. protected static final FastMap<L2Character, Long> _attackStanceTasks = new FastMap<>();
  33. /**
  34. * Instantiates a new attack stance task manager.
  35. */
  36. protected AttackStanceTaskManager()
  37. {
  38. _attackStanceTasks.shared();
  39. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
  40. }
  41. /**
  42. * Adds the attack stance task.
  43. * @param actor the actor
  44. */
  45. public void addAttackStanceTask(L2Character actor)
  46. {
  47. if ((actor != null) && actor.isPlayable())
  48. {
  49. final L2PcInstance player = actor.getActingPlayer();
  50. for (L2CubicInstance cubic : player.getCubics().values())
  51. {
  52. if (cubic.getId() != L2CubicInstance.LIFE_CUBIC)
  53. {
  54. cubic.doAction();
  55. }
  56. }
  57. }
  58. _attackStanceTasks.put(actor, System.currentTimeMillis());
  59. }
  60. /**
  61. * Removes the attack stance task.
  62. * @param actor the actor
  63. */
  64. public void removeAttackStanceTask(L2Character actor)
  65. {
  66. if ((actor != null) && actor.isSummon())
  67. {
  68. actor = actor.getActingPlayer();
  69. }
  70. _attackStanceTasks.remove(actor);
  71. }
  72. /**
  73. * Checks for attack stance task.<br>
  74. * @param actor the actor
  75. * @return {@code true} if the character has an attack stance task, {@code false} otherwise
  76. */
  77. public boolean hasAttackStanceTask(L2Character actor)
  78. {
  79. if ((actor != null) && actor.isSummon())
  80. {
  81. actor = actor.getActingPlayer();
  82. }
  83. return _attackStanceTasks.containsKey(actor);
  84. }
  85. protected class FightModeScheduler implements Runnable
  86. {
  87. @Override
  88. public void run()
  89. {
  90. long current = System.currentTimeMillis();
  91. try
  92. {
  93. final Iterator<Entry<L2Character, Long>> iter = _attackStanceTasks.entrySet().iterator();
  94. Entry<L2Character, Long> e;
  95. L2Character actor;
  96. while (iter.hasNext())
  97. {
  98. e = iter.next();
  99. if ((current - e.getValue()) > 15000)
  100. {
  101. actor = e.getKey();
  102. if (actor != null)
  103. {
  104. actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
  105. actor.getAI().setAutoAttacking(false);
  106. if (actor.isPlayer() && actor.getActingPlayer().hasSummon())
  107. {
  108. actor.getSummon().broadcastPacket(new AutoAttackStop(actor.getSummon().getObjectId()));
  109. }
  110. }
  111. iter.remove();
  112. }
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. // Unless caught here, players remain in attack positions.
  118. _log.log(Level.WARNING, "Error in FightModeScheduler: " + e.getMessage(), e);
  119. }
  120. }
  121. }
  122. /**
  123. * Gets the single instance of AttackStanceTaskManager.
  124. * @return single instance of AttackStanceTaskManager
  125. */
  126. public static AttackStanceTaskManager getInstance()
  127. {
  128. return SingletonHolder._instance;
  129. }
  130. private static class SingletonHolder
  131. {
  132. protected static final AttackStanceTaskManager _instance = new AttackStanceTaskManager();
  133. }
  134. }