Confusion.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 handlers.effecthandlers;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import javolution.util.FastList;
  19. import com.l2jserver.gameserver.ai.CtrlIntention;
  20. import com.l2jserver.gameserver.model.CharEffectList;
  21. import com.l2jserver.gameserver.model.L2Object;
  22. import com.l2jserver.gameserver.model.actor.L2Character;
  23. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  24. import com.l2jserver.gameserver.model.effects.L2Effect;
  25. import com.l2jserver.gameserver.model.effects.L2EffectType;
  26. import com.l2jserver.gameserver.model.stats.Env;
  27. import com.l2jserver.util.Rnd;
  28. /**
  29. * Implementation of the Confusion Effect
  30. * @author littlecrow
  31. */
  32. public class Confusion extends L2Effect
  33. {
  34. public Confusion(Env env, EffectTemplate template)
  35. {
  36. super(env, template);
  37. }
  38. @Override
  39. public L2EffectType getEffectType()
  40. {
  41. return L2EffectType.CONFUSION;
  42. }
  43. @Override
  44. public boolean onStart()
  45. {
  46. getEffected().startConfused();
  47. onActionTime();
  48. return true;
  49. }
  50. @Override
  51. public void onExit()
  52. {
  53. getEffected().stopConfused(this);
  54. }
  55. @Override
  56. public boolean onActionTime()
  57. {
  58. List<L2Character> targetList = new FastList<>();
  59. // Getting the possible targets
  60. Collection<L2Object> objs = getEffected().getKnownList().getKnownObjects().values();
  61. // synchronized (getEffected().getKnownList().getKnownObjects())
  62. {
  63. for (L2Object obj : objs)
  64. {
  65. if ((obj instanceof L2Character) && (obj != getEffected()))
  66. targetList.add((L2Character) obj);
  67. }
  68. }
  69. // if there is no target, exit function
  70. if (targetList.isEmpty())
  71. return true;
  72. // Choosing randomly a new target
  73. int nextTargetIdx = Rnd.nextInt(targetList.size());
  74. L2Object target = targetList.get(nextTargetIdx);
  75. // Attacking the target
  76. getEffected().setTarget(target);
  77. getEffected().getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
  78. return true;
  79. }
  80. @Override
  81. public int getEffectFlags()
  82. {
  83. return CharEffectList.EFFECT_FLAG_CONFUSED;
  84. }
  85. }