AttackStanceTaskManager.java 3.7 KB

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