AttackStanceTaskManager.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * $HeadURL: $
  3. *
  4. * $Author: $ $Date: $ $Revision: $
  5. *
  6. *
  7. * This program is free software: you can redistribute it and/or modify it under
  8. * the terms of the GNU General Public License as published by the Free Software
  9. * Foundation, either version 3 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.l2jserver.gameserver.taskmanager;
  21. import java.util.Map;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.ThreadPoolManager;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.L2Summon;
  26. import com.l2jserver.gameserver.model.actor.instance.L2CubicInstance;
  27. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  28. import com.l2jserver.gameserver.network.serverpackets.AutoAttackStop;
  29. import javolution.util.FastMap;
  30. /**
  31. * This class ...
  32. *
  33. * @version $Revision: $ $Date: $
  34. * @author Luca Baldi
  35. */
  36. public class AttackStanceTaskManager
  37. {
  38. protected static final Logger _log = Logger.getLogger(AttackStanceTaskManager.class.getName());
  39. protected Map<L2Character, Long> _attackStanceTasks = new FastMap<L2Character, Long>().shared();
  40. private AttackStanceTaskManager()
  41. {
  42. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
  43. }
  44. public static AttackStanceTaskManager getInstance()
  45. {
  46. return SingletonHolder._instance;
  47. }
  48. public void addAttackStanceTask(L2Character actor)
  49. {
  50. if (actor instanceof L2Summon)
  51. {
  52. L2Summon summon = (L2Summon) actor;
  53. actor = summon.getOwner();
  54. }
  55. if (actor instanceof L2PcInstance)
  56. {
  57. L2PcInstance player = (L2PcInstance) actor;
  58. for (L2CubicInstance cubic : player.getCubics().values())
  59. if (cubic.getId() != L2CubicInstance.LIFE_CUBIC)
  60. cubic.doAction();
  61. }
  62. _attackStanceTasks.put(actor, System.currentTimeMillis());
  63. }
  64. public void removeAttackStanceTask(L2Character actor)
  65. {
  66. if (actor instanceof L2Summon)
  67. {
  68. L2Summon summon = (L2Summon) actor;
  69. actor = summon.getOwner();
  70. }
  71. _attackStanceTasks.remove(actor);
  72. }
  73. public boolean getAttackStanceTask(L2Character actor)
  74. {
  75. if (actor instanceof L2Summon)
  76. {
  77. L2Summon summon = (L2Summon) actor;
  78. actor = summon.getOwner();
  79. }
  80. return _attackStanceTasks.containsKey(actor);
  81. }
  82. private class FightModeScheduler implements Runnable
  83. {
  84. protected FightModeScheduler()
  85. {
  86. // Do nothing
  87. }
  88. public void run()
  89. {
  90. Long current = System.currentTimeMillis();
  91. try
  92. {
  93. if (_attackStanceTasks != null)
  94. synchronized (this)
  95. {
  96. for (L2Character actor : _attackStanceTasks.keySet())
  97. {
  98. if ((current - _attackStanceTasks.get(actor)) > 15000)
  99. {
  100. actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
  101. if (actor instanceof L2PcInstance && ((L2PcInstance) actor).getPet() != null)
  102. ((L2PcInstance) actor).getPet().broadcastPacket(new AutoAttackStop(((L2PcInstance) actor).getPet().getObjectId()));
  103. actor.getAI().setAutoAttacking(false);
  104. _attackStanceTasks.remove(actor);
  105. }
  106. }
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. // TODO: Find out the reason for exception. Unless caught here,
  112. // players remain in attack positions.
  113. _log.warning(e.toString());
  114. }
  115. }
  116. }
  117. @SuppressWarnings("synthetic-access")
  118. private static class SingletonHolder
  119. {
  120. protected static final AttackStanceTaskManager _instance = new AttackStanceTaskManager();
  121. }
  122. }