PlayerVariables.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (C) 2004-2013 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 com.l2jserver.gameserver.model;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.util.Map.Entry;
  25. import java.util.concurrent.atomic.AtomicBoolean;
  26. import java.util.logging.Level;
  27. import java.util.logging.Logger;
  28. import com.l2jserver.L2DatabaseFactory;
  29. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  30. /**
  31. * @author UnAfraid
  32. */
  33. public class PlayerVariables extends StatsSet
  34. {
  35. private static final Logger _log = Logger.getLogger(PlayerVariables.class.getName());
  36. // SQL Queries.
  37. private static final String SELECT_QUERY = "SELECT * FROM character_variables WHERE charId = ?";
  38. private static final String DELETE_QUERY = "DELETE FROM character_variables WHERE charId = ?";
  39. private static final String INSERT_QUERY = "INSERT INTO character_variables (charId, var, val) VALUES (?, ?, ?)";
  40. private final int _objectId;
  41. private final AtomicBoolean _hasChanges = new AtomicBoolean(false);
  42. public PlayerVariables(int objectId)
  43. {
  44. _objectId = objectId;
  45. load();
  46. }
  47. private void load()
  48. {
  49. // Restore previous variables.
  50. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  51. PreparedStatement st = con.prepareStatement(SELECT_QUERY))
  52. {
  53. st.setInt(1, _objectId);
  54. try (ResultSet rset = st.executeQuery())
  55. {
  56. while (rset.next())
  57. {
  58. super.set(rset.getString("var"), rset.getString("val"));
  59. }
  60. }
  61. }
  62. catch (SQLException e)
  63. {
  64. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't restore variables for: " + getPlayer(), e);
  65. }
  66. }
  67. public void store()
  68. {
  69. // No changes, nothing to store.
  70. if (!_hasChanges.get())
  71. {
  72. return;
  73. }
  74. try (Connection con = L2DatabaseFactory.getInstance().getConnection())
  75. {
  76. // Clear previous entries.
  77. try (PreparedStatement st = con.prepareStatement(DELETE_QUERY))
  78. {
  79. st.setInt(1, _objectId);
  80. st.execute();
  81. }
  82. // Insert all variables.
  83. try (PreparedStatement st = con.prepareStatement(INSERT_QUERY))
  84. {
  85. st.setInt(1, _objectId);
  86. for (Entry<String, Object> entry : getSet().entrySet())
  87. {
  88. st.setString(2, entry.getKey());
  89. st.setObject(3, entry.getValue());
  90. st.execute();
  91. }
  92. }
  93. }
  94. catch (SQLException e)
  95. {
  96. _log.log(Level.WARNING, getClass().getSimpleName() + ": Couldn't update variables for: " + getPlayer(), e);
  97. }
  98. finally
  99. {
  100. _hasChanges.compareAndSet(true, false);
  101. }
  102. }
  103. /**
  104. * Overriding following methods to prevent from doing useless database operations if there is no changes since player's login.
  105. */
  106. @Override
  107. public void set(String name, boolean value)
  108. {
  109. _hasChanges.compareAndSet(false, true);
  110. super.set(name, value);
  111. }
  112. @Override
  113. public void set(String name, double value)
  114. {
  115. _hasChanges.compareAndSet(false, true);
  116. super.set(name, value);
  117. }
  118. @Override
  119. public void set(String name, Enum<?> value)
  120. {
  121. _hasChanges.compareAndSet(false, true);
  122. super.set(name, value);
  123. }
  124. @Override
  125. public void set(String name, int value)
  126. {
  127. _hasChanges.compareAndSet(false, true);
  128. super.set(name, value);
  129. }
  130. @Override
  131. public void set(String name, long value)
  132. {
  133. _hasChanges.compareAndSet(false, true);
  134. super.set(name, value);
  135. }
  136. @Override
  137. public void set(String name, String value)
  138. {
  139. _hasChanges.compareAndSet(false, true);
  140. super.set(name, value);
  141. }
  142. public L2PcInstance getPlayer()
  143. {
  144. return L2World.getInstance().getPlayer(_objectId);
  145. }
  146. }