AttackStanceTaskManager.java 3.9 KB

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