L2Augmentation.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 net.sf.l2j.gameserver.model;
  16. import java.sql.PreparedStatement;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19. import javolution.util.FastList;
  20. import net.sf.l2j.L2DatabaseFactory;
  21. import net.sf.l2j.gameserver.datatables.AugmentationData;
  22. import net.sf.l2j.gameserver.datatables.SkillTable;
  23. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  24. import net.sf.l2j.gameserver.skills.Stats;
  25. import net.sf.l2j.gameserver.skills.funcs.FuncAdd;
  26. import net.sf.l2j.gameserver.skills.funcs.LambdaConst;
  27. /**
  28. * Used to store an augmentation and its boni
  29. *
  30. * @author durgus
  31. */
  32. public final class L2Augmentation
  33. {
  34. private static final Logger _log = Logger.getLogger(L2Augmentation.class.getName());
  35. private L2ItemInstance _item;
  36. private int _effectsId = 0;
  37. private augmentationStatBoni _boni = null;
  38. private L2Skill _skill = null;
  39. public L2Augmentation(L2ItemInstance item, int effects, L2Skill skill, boolean save)
  40. {
  41. _item = item;
  42. _effectsId = effects;
  43. _boni = new augmentationStatBoni(_effectsId);
  44. _skill = skill;
  45. // write to DB if save is true
  46. if (save) saveAugmentationData();
  47. }
  48. public L2Augmentation(L2ItemInstance item, int effects, int skill, int skillLevel, boolean save)
  49. {
  50. this(item, effects, SkillTable.getInstance().getInfo(skill, skillLevel), save);
  51. }
  52. // =========================================================
  53. // Nested Class
  54. public class augmentationStatBoni
  55. {
  56. private Stats _stats[];
  57. private float _values[];
  58. private boolean _active;
  59. public augmentationStatBoni(int augmentationId)
  60. {
  61. _active = false;
  62. FastList <AugmentationData.AugStat> as = AugmentationData.getInstance().getAugStatsById(augmentationId);
  63. _stats = new Stats[as.size()];
  64. _values = new float[as.size()];
  65. int i=0;
  66. for (AugmentationData.AugStat aStat : as)
  67. {
  68. _stats[i] = aStat.getStat();
  69. _values[i] = aStat.getValue();
  70. i++;
  71. }
  72. }
  73. public void applyBonus(L2PcInstance player)
  74. {
  75. // make sure the bonuses are not applied twice..
  76. if (_active) return;
  77. for (int i=0; i < _stats.length; i++)
  78. ((L2Character)player).addStatFunc(new FuncAdd(_stats[i], 0x40, this, new LambdaConst(_values[i])));
  79. _active = true;
  80. }
  81. public void removeBonus(L2PcInstance player)
  82. {
  83. // make sure the bonuses are not removed twice
  84. if (!_active) return;
  85. ((L2Character)player).removeStatsOwner(this);
  86. _active = false;
  87. }
  88. }
  89. private void saveAugmentationData()
  90. {
  91. java.sql.Connection con = null;
  92. try
  93. {
  94. con = L2DatabaseFactory.getInstance().getConnection();
  95. PreparedStatement statement = con.prepareStatement("INSERT INTO augmentations (item_id,attributes,skill,level) VALUES (?,?,?,?)");
  96. statement.setInt(1, _item.getObjectId());
  97. statement.setInt(2, _effectsId);
  98. if (_skill != null)
  99. {
  100. statement.setInt(3, _skill.getId());
  101. statement.setInt(4, _skill.getLevel());
  102. } else
  103. {
  104. statement.setInt(3, 0);
  105. statement.setInt(4, 0);
  106. }
  107. statement.executeUpdate();
  108. statement.close();
  109. } catch (Exception e) {
  110. _log.log(Level.SEVERE, "Could not save augmentation for item: "+_item.getObjectId()+" from DB:", e);
  111. } finally {
  112. try { con.close(); } catch (Exception e) {}
  113. }
  114. }
  115. public void deleteAugmentationData()
  116. {
  117. if (!_item.isAugmented()) return;
  118. // delete the augmentation from the database
  119. java.sql.Connection con = null;
  120. try
  121. {
  122. con = L2DatabaseFactory.getInstance().getConnection();
  123. PreparedStatement statement = con.prepareStatement("DELETE FROM augmentations WHERE item_id=?");
  124. statement.setInt(1, _item.getObjectId());
  125. statement.executeUpdate();
  126. statement.close();
  127. } catch (Exception e) {
  128. _log.log(Level.SEVERE, "Could not delete augmentation for item: "+_item.getObjectId()+" from DB:", e);
  129. } finally {
  130. try { con.close(); } catch (Exception e) {}
  131. }
  132. }
  133. /**
  134. * Get the augmentation "id" used in serverpackets.
  135. * @return augmentationId
  136. */
  137. public int getAugmentationId()
  138. {
  139. return _effectsId;
  140. }
  141. public L2Skill getSkill()
  142. {
  143. return _skill;
  144. }
  145. /**
  146. * Applies the bonuses to the player.
  147. * @param player
  148. */
  149. public void applyBonus(L2PcInstance player)
  150. {
  151. _boni.applyBonus(player);
  152. // add the skill if any
  153. if (_skill != null)
  154. {
  155. player.addSkill(_skill);
  156. player.sendSkillList();
  157. }
  158. }
  159. /**
  160. * Removes the augmentation bonuses from the player.
  161. * @param player
  162. */
  163. public void removeBonus(L2PcInstance player)
  164. {
  165. _boni.removeBonus(player);
  166. // remove the skill if any
  167. if (_skill != null)
  168. {
  169. player.removeSkill(_skill);
  170. player.sendSkillList();
  171. }
  172. }
  173. }