RandomizeHate.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.model.actor.L2Attackable;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.model.effects.EffectTemplate;
  22. import com.l2jserver.gameserver.model.effects.L2Effect;
  23. import com.l2jserver.gameserver.model.effects.L2EffectType;
  24. import com.l2jserver.gameserver.model.stats.Env;
  25. import com.l2jserver.util.Rnd;
  26. public class RandomizeHate extends L2Effect
  27. {
  28. public RandomizeHate(Env env, EffectTemplate template)
  29. {
  30. super(env, template);
  31. }
  32. @Override
  33. public L2EffectType getEffectType()
  34. {
  35. return L2EffectType.RANDOMIZE_HATE;
  36. }
  37. @Override
  38. public boolean onStart()
  39. {
  40. if (getEffected() == null || getEffected() == getEffector())
  41. return false;
  42. // Effect is for mobs only.
  43. if (!(getEffected() instanceof L2Attackable))
  44. return false;
  45. L2Attackable effectedMob = (L2Attackable) getEffected();
  46. List<L2Character> targetList = new FastList<L2Character>();
  47. // Getting the possible targets
  48. Collection<L2Character> chars = getEffected().getKnownList().getKnownCharacters();
  49. for (L2Character cha : chars)
  50. {
  51. if (cha != null && (cha != effectedMob) && (cha != getEffector()))
  52. {
  53. // Aggro cannot be transfared to a mob of the same faction.
  54. if (cha instanceof L2Attackable && ((L2Attackable) cha).getFactionId() != null && ((L2Attackable) cha).getFactionId().equals(effectedMob.getFactionId()))
  55. continue;
  56. targetList.add(cha);
  57. }
  58. }
  59. // if there is no target, exit function
  60. if (targetList.isEmpty())
  61. return true;
  62. // Choosing randomly a new target
  63. final L2Character target = targetList.get(Rnd.get(targetList.size()));
  64. final int hate = effectedMob.getHating(getEffector());
  65. effectedMob.stopHating(getEffector());
  66. effectedMob.addDamageHate(target, 0, hate);
  67. return true;
  68. }
  69. @Override
  70. public boolean onActionTime()
  71. {
  72. return false;
  73. }
  74. }