EnchantItem.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server 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 Server 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 com.l2jserver.gameserver.model.enchant;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.logging.Logger;
  23. import com.l2jserver.gameserver.datatables.ItemTable;
  24. import com.l2jserver.gameserver.model.StatsSet;
  25. import com.l2jserver.gameserver.model.items.L2Item;
  26. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  27. /**
  28. * @author UnAfraid
  29. */
  30. public class EnchantItem
  31. {
  32. protected static final Logger _log = Logger.getLogger(EnchantItem.class.getName());
  33. private final int _id;
  34. private final boolean _isWeapon;
  35. private final int _grade;
  36. private final int _maxEnchantLevel;
  37. private final double _bonusRate;
  38. private List<Integer> _itemIds;
  39. public EnchantItem(StatsSet set)
  40. {
  41. _id = set.getInteger("id");
  42. _isWeapon = set.getBool("isWeapon", true);
  43. _grade = ItemTable._crystalTypes.get(set.getString("targetGrade", "none"));
  44. _maxEnchantLevel = set.getInteger("maxEnchant", 65535);
  45. _bonusRate = set.getDouble("bonusRate", 0);
  46. }
  47. /**
  48. * @return id of current item.
  49. */
  50. public final int getId()
  51. {
  52. return _id;
  53. }
  54. /**
  55. * @return bonus chance that would be added.
  56. */
  57. public final double getBonusRate()
  58. {
  59. return _bonusRate;
  60. }
  61. public void addItem(int id)
  62. {
  63. if (_itemIds == null)
  64. {
  65. _itemIds = new ArrayList<>();
  66. }
  67. _itemIds.add(id);
  68. }
  69. public boolean verifyItemId(int itemId)
  70. {
  71. return _itemIds != null ? _itemIds.contains(itemId) : true;
  72. }
  73. /**
  74. * @param enchantItem
  75. * @return {@code true} if current item is valid to be enchanted, {@code false} otherwise.
  76. */
  77. public final boolean isValid(L2ItemInstance enchantItem)
  78. {
  79. if (enchantItem == null)
  80. {
  81. return false;
  82. }
  83. else if (enchantItem.isEnchantable() == 0)
  84. {
  85. return false;
  86. }
  87. else if (!isValidItemType(enchantItem.getItem().getType2()))
  88. {
  89. return false;
  90. }
  91. else if ((_maxEnchantLevel != 0) && (enchantItem.getEnchantLevel() >= _maxEnchantLevel))
  92. {
  93. return false;
  94. }
  95. else if (_grade != enchantItem.getItem().getItemGradeSPlus())
  96. {
  97. return false;
  98. }
  99. else if ((enchantItem.isEnchantable() > 1) && !verifyItemId(enchantItem.getId()))
  100. {
  101. return false;
  102. }
  103. return true;
  104. }
  105. /**
  106. * @param type2
  107. * @return {@code true} if current type2 is valid to be enchanted, {@code false} otherwise.
  108. */
  109. private boolean isValidItemType(int type2)
  110. {
  111. if (type2 == L2Item.TYPE2_WEAPON)
  112. {
  113. return _isWeapon;
  114. }
  115. else if ((type2 == L2Item.TYPE2_SHIELD_ARMOR) || (type2 == L2Item.TYPE2_ACCESSORY))
  116. {
  117. return !_isWeapon;
  118. }
  119. return false;
  120. }
  121. }