AttackStanceTaskManager.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.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.L2Summon;
  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. * This class ...
  33. *
  34. * @version $Revision: $ $Date: $
  35. * @author Luca Baldi
  36. */
  37. public class AttackStanceTaskManager
  38. {
  39. protected static final Logger _log = Logger.getLogger(AttackStanceTaskManager.class.getName());
  40. protected Map<L2Character, Long> _attackStanceTasks = new FastMap<L2Character, Long>().shared();
  41. private AttackStanceTaskManager()
  42. {
  43. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
  44. }
  45. public static AttackStanceTaskManager getInstance()
  46. {
  47. return SingletonHolder._instance;
  48. }
  49. public void addAttackStanceTask(L2Character actor)
  50. {
  51. if (actor instanceof L2Summon)
  52. {
  53. L2Summon summon = (L2Summon) actor;
  54. actor = summon.getOwner();
  55. }
  56. if (actor instanceof L2PcInstance)
  57. {
  58. L2PcInstance player = (L2PcInstance) actor;
  59. for (L2CubicInstance cubic : player.getCubics().values(new L2CubicInstance[player.getCubics().size()]))
  60. if (cubic.getId() != L2CubicInstance.LIFE_CUBIC)
  61. cubic.doAction();
  62. }
  63. _attackStanceTasks.put(actor, System.currentTimeMillis());
  64. }
  65. public void removeAttackStanceTask(L2Character actor)
  66. {
  67. if (actor instanceof L2Summon)
  68. {
  69. L2Summon summon = (L2Summon) actor;
  70. actor = summon.getOwner();
  71. }
  72. _attackStanceTasks.remove(actor);
  73. }
  74. public boolean getAttackStanceTask(L2Character actor)
  75. {
  76. if (actor instanceof L2Summon)
  77. {
  78. L2Summon summon = (L2Summon) actor;
  79. actor = summon.getOwner();
  80. }
  81. return _attackStanceTasks.containsKey(actor);
  82. }
  83. private class FightModeScheduler implements Runnable
  84. {
  85. protected FightModeScheduler()
  86. {
  87. // Do nothing
  88. }
  89. @Override
  90. public void run()
  91. {
  92. Long current = System.currentTimeMillis();
  93. try
  94. {
  95. if (_attackStanceTasks != null)
  96. synchronized (this)
  97. {
  98. for (L2Character actor : _attackStanceTasks.keySet())
  99. {
  100. if ((current - _attackStanceTasks.get(actor)) > 15000)
  101. {
  102. actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
  103. if (actor instanceof L2PcInstance && ((L2PcInstance) actor).getPet() != null)
  104. ((L2PcInstance) actor).getPet().broadcastPacket(new AutoAttackStop(((L2PcInstance) actor).getPet().getObjectId()));
  105. actor.getAI().setAutoAttacking(false);
  106. _attackStanceTasks.remove(actor);
  107. }
  108. }
  109. }
  110. }
  111. catch (Exception e)
  112. {
  113. // TODO: Find out the reason for exception. Unless caught here,
  114. // players remain in attack positions.
  115. _log.log(Level.WARNING, "Error in FightModeScheduler: " + e.getMessage(), e);
  116. }
  117. }
  118. }
  119. @SuppressWarnings("synthetic-access")
  120. private static class SingletonHolder
  121. {
  122. protected static final AttackStanceTaskManager _instance = new AttackStanceTaskManager();
  123. }
  124. }