AttackStanceTaskManager.java 3.8 KB

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