RandomizeHate.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.effecthandlers;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import com.l2jserver.gameserver.model.StatsSet;
  23. import com.l2jserver.gameserver.model.actor.L2Attackable;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.conditions.Condition;
  26. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  27. import com.l2jserver.gameserver.model.skills.BuffInfo;
  28. import com.l2jserver.gameserver.model.stats.Formulas;
  29. import com.l2jserver.util.Rnd;
  30. /**
  31. * Randomize Hate effect implementation.
  32. */
  33. public final class RandomizeHate extends AbstractEffect
  34. {
  35. private final int _chance;
  36. public RandomizeHate(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  37. {
  38. super(attachCond, applyCond, set, params);
  39. _chance = params.getInt("chance", 100);
  40. }
  41. @Override
  42. public boolean calcSuccess(BuffInfo info)
  43. {
  44. return Formulas.calcProbability(_chance, info.getEffector(), info.getEffected(), info.getSkill());
  45. }
  46. @Override
  47. public boolean isInstant()
  48. {
  49. return true;
  50. }
  51. @Override
  52. public void onStart(BuffInfo info)
  53. {
  54. if ((info.getEffected() == null) || (info.getEffected() == info.getEffector()) || !info.getEffected().isAttackable())
  55. {
  56. return;
  57. }
  58. L2Attackable effectedMob = (L2Attackable) info.getEffected();
  59. final List<L2Character> targetList = new ArrayList<>();
  60. for (L2Character cha : info.getEffected().getKnownList().getKnownCharacters())
  61. {
  62. if ((cha != null) && (cha != effectedMob) && (cha != info.getEffector()))
  63. {
  64. // Aggro cannot be transfered to a mob of the same faction.
  65. if (cha.isAttackable() && ((L2Attackable) cha).isInMyClan(effectedMob))
  66. {
  67. continue;
  68. }
  69. targetList.add(cha);
  70. }
  71. }
  72. // if there is no target, exit function
  73. if (targetList.isEmpty())
  74. {
  75. return;
  76. }
  77. // Choosing randomly a new target
  78. final L2Character target = targetList.get(Rnd.get(targetList.size()));
  79. final int hate = effectedMob.getHating(info.getEffector());
  80. effectedMob.stopHating(info.getEffector());
  81. effectedMob.addDamageHate(target, 0, hate);
  82. }
  83. }