AttackStanceTaskManager.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 net.sf.l2j.gameserver.taskmanager;
  21. import java.util.Map;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastMap;
  24. import net.sf.l2j.gameserver.ThreadPoolManager;
  25. import net.sf.l2j.gameserver.model.L2Character;
  26. import net.sf.l2j.gameserver.model.L2Summon;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2CubicInstance;
  28. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  29. import net.sf.l2j.gameserver.network.serverpackets.AutoAttackStop;
  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>().setShared(true);
  40. private static AttackStanceTaskManager _instance;
  41. public AttackStanceTaskManager()
  42. {
  43. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(new FightModeScheduler(), 0, 1000);
  44. }
  45. public static AttackStanceTaskManager getInstance()
  46. {
  47. if (_instance == null)
  48. _instance = new AttackStanceTaskManager();
  49. return _instance;
  50. }
  51. public void addAttackStanceTask(L2Character actor)
  52. {
  53. _attackStanceTasks.put(actor, System.currentTimeMillis());
  54. if (actor instanceof L2Summon)
  55. {
  56. L2Summon summon = (L2Summon) actor;
  57. actor = summon.getOwner();
  58. }
  59. if (actor instanceof L2PcInstance)
  60. {
  61. L2PcInstance player = (L2PcInstance) actor;
  62. for (L2CubicInstance cubic : player.getCubics().values())
  63. if (cubic.getId() != L2CubicInstance.LIFE_CUBIC)
  64. cubic.doAction();
  65. }
  66. }
  67. public void removeAttackStanceTask(L2Character actor)
  68. {
  69. _attackStanceTasks.remove(actor);
  70. }
  71. public boolean getAttackStanceTask(L2Character actor)
  72. {
  73. return _attackStanceTasks.containsKey(actor);
  74. }
  75. private class FightModeScheduler implements Runnable
  76. {
  77. protected FightModeScheduler()
  78. {
  79. // Do nothing
  80. }
  81. public void run()
  82. {
  83. Long current = System.currentTimeMillis();
  84. try
  85. {
  86. if (_attackStanceTasks != null)
  87. synchronized (this)
  88. {
  89. for (L2Character actor : _attackStanceTasks.keySet())
  90. {
  91. if ((current - _attackStanceTasks.get(actor)) > 15000)
  92. {
  93. actor.broadcastPacket(new AutoAttackStop(actor.getObjectId()));
  94. actor.getAI().setAutoAttacking(false);
  95. _attackStanceTasks.remove(actor);
  96. }
  97. }
  98. }
  99. }
  100. catch (Throwable e)
  101. {
  102. // TODO: Find out the reason for exception. Unless caught here,
  103. // players remain in attack positions.
  104. _log.warning(e.toString());
  105. }
  106. }
  107. }
  108. }