AttackStanceTaskManager.java 4.2 KB

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