RestorationRandom.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Config;
  23. import com.l2jserver.gameserver.model.L2ExtractableProductItem;
  24. import com.l2jserver.gameserver.model.L2ExtractableSkill;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.model.conditions.Condition;
  28. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  29. import com.l2jserver.gameserver.model.holders.ItemHolder;
  30. import com.l2jserver.gameserver.model.skills.BuffInfo;
  31. import com.l2jserver.gameserver.network.SystemMessageId;
  32. import com.l2jserver.util.Rnd;
  33. /**
  34. * Restoration Random effect implementation.<br>
  35. * This effect is present in item skills that "extract" new items upon usage.<br>
  36. * This effect has been unhardcoded in order to work on targets as well.
  37. * @author Zoey76
  38. */
  39. public final class RestorationRandom extends AbstractEffect
  40. {
  41. public RestorationRandom(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  42. {
  43. super(attachCond, applyCond, set, params);
  44. }
  45. @Override
  46. public boolean isInstant()
  47. {
  48. return true;
  49. }
  50. @Override
  51. public void onStart(BuffInfo info)
  52. {
  53. if ((info.getEffector() == null) || (info.getEffected() == null) || !info.getEffector().isPlayer() || !info.getEffected().isPlayer())
  54. {
  55. return;
  56. }
  57. final L2ExtractableSkill exSkill = info.getSkill().getExtractableSkill();
  58. if (exSkill == null)
  59. {
  60. return;
  61. }
  62. if (exSkill.getProductItems().isEmpty())
  63. {
  64. _log.warning("Extractable Skill with no data, probably wrong/empty table in Skill Id: " + info.getSkill().getId());
  65. return;
  66. }
  67. final double rndNum = 100 * Rnd.nextDouble();
  68. double chance = 0;
  69. double chanceFrom = 0;
  70. final List<ItemHolder> creationList = new ArrayList<>();
  71. // Explanation for future changes:
  72. // You get one chance for the current skill, then you can fall into
  73. // one of the "areas" like in a roulette.
  74. // Example: for an item like Id1,A1,30;Id2,A2,50;Id3,A3,20;
  75. // #---#-----#--#
  76. // 0--30----80-100
  77. // If you get chance equal 45% you fall into the second zone 30-80.
  78. // Meaning you get the second production list.
  79. // Calculate extraction
  80. for (L2ExtractableProductItem expi : exSkill.getProductItems())
  81. {
  82. chance = expi.getChance();
  83. if ((rndNum >= chanceFrom) && (rndNum <= (chance + chanceFrom)))
  84. {
  85. creationList.addAll(expi.getItems());
  86. break;
  87. }
  88. chanceFrom += chance;
  89. }
  90. final L2PcInstance player = info.getEffected().getActingPlayer();
  91. if (creationList.isEmpty())
  92. {
  93. player.sendPacket(SystemMessageId.NOTHING_INSIDE_THAT);
  94. return;
  95. }
  96. for (ItemHolder item : creationList)
  97. {
  98. if ((item.getId() <= 0) || (item.getCount() <= 0))
  99. {
  100. continue;
  101. }
  102. player.addItem("Extract", item.getId(), (long) (item.getCount() * Config.RATE_EXTRACTABLE), info.getEffector(), true);
  103. }
  104. }
  105. }