DefenceTrait.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2004-2014 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.HashMap;
  21. import java.util.Map;
  22. import java.util.Map.Entry;
  23. import com.l2jserver.gameserver.model.StatsSet;
  24. import com.l2jserver.gameserver.model.actor.stat.CharStat;
  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.TraitType;
  29. /**
  30. * Defence Trait effect implementation.
  31. * @author NosBit
  32. */
  33. public final class DefenceTrait extends AbstractEffect
  34. {
  35. private final Map<TraitType, Float> _defenceTraits = new HashMap<>();
  36. public DefenceTrait(Condition attachCond, Condition applyCond, StatsSet set, StatsSet params)
  37. {
  38. super(attachCond, applyCond, set, params);
  39. if (params.isEmpty())
  40. {
  41. _log.warning(getClass().getSimpleName() + ": must have parameters.");
  42. return;
  43. }
  44. for (Entry<String, Object> param : params.getSet().entrySet())
  45. {
  46. try
  47. {
  48. final TraitType traitType = TraitType.valueOf(param.getKey());
  49. final float value = Float.parseFloat((String) param.getValue());
  50. if (value == 0)
  51. {
  52. continue;
  53. }
  54. _defenceTraits.put(traitType, (value + 100) / 100);
  55. }
  56. catch (NumberFormatException e)
  57. {
  58. _log.warning(getClass().getSimpleName() + ": value of " + param.getKey() + " must be float value " + param.getValue() + " found.");
  59. }
  60. catch (Exception e)
  61. {
  62. _log.warning(getClass().getSimpleName() + ": value of L2TraitType enum required but found: " + param.getValue());
  63. }
  64. }
  65. }
  66. @Override
  67. public void onExit(BuffInfo info)
  68. {
  69. final CharStat charStat = info.getEffected().getStat();
  70. synchronized (charStat.getDefenceTraits())
  71. {
  72. for (Entry<TraitType, Float> trait : _defenceTraits.entrySet())
  73. {
  74. if (trait.getValue() < 2.0f)
  75. {
  76. charStat.getDefenceTraits()[trait.getKey().getId()] /= trait.getValue();
  77. charStat.getDefenceTraitsCount()[trait.getKey().getId()]--;
  78. }
  79. else
  80. {
  81. charStat.getTraitsInvul()[trait.getKey().getId()]--;
  82. }
  83. }
  84. }
  85. }
  86. @Override
  87. public void onStart(BuffInfo info)
  88. {
  89. final CharStat charStat = info.getEffected().getStat();
  90. synchronized (charStat.getDefenceTraits())
  91. {
  92. for (Entry<TraitType, Float> trait : _defenceTraits.entrySet())
  93. {
  94. if (trait.getValue() < 2.0f)
  95. {
  96. charStat.getDefenceTraits()[trait.getKey().getId()] *= trait.getValue();
  97. charStat.getDefenceTraitsCount()[trait.getKey().getId()]++;
  98. }
  99. else
  100. {
  101. charStat.getTraitsInvul()[trait.getKey().getId()]++;
  102. }
  103. }
  104. }
  105. }
  106. }