Advertisement
Guest User

AuctionTable

a guest
Feb 16th, 2021
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. package net.sf.l2j.gameserver.data.sql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.util.Set;
  7. import java.util.concurrent.ConcurrentHashMap;
  8.  
  9. import net.sf.l2j.commons.logging.CLogger;
  10. import net.sf.l2j.commons.pool.ConnectionPool;
  11.  
  12. import net.sf.l2j.gameserver.model.AuctionItem;
  13.  
  14. /**
  15. * @author Anarchy
  16. *
  17. */
  18. public class AuctionTable
  19. {
  20. private static final CLogger LOGGER = new CLogger(AuctionTable.class.getName());
  21.  
  22. private static final String RESTORE_AUCTION_ITEM = "SELECT * FROM auction_table";
  23. private static final String ADD_AUCTION_ITEM = "INSERT INTO auction_table VALUES (?,?,?,?,?,?,?)";
  24. private static final String DELETE_AUCTION_ITEM = "DELETE FROM auction_table WHERE auctionid=?";
  25.  
  26. private Set<AuctionItem> _items = ConcurrentHashMap.newKeySet();
  27. private int _maxId = 0;
  28.  
  29. protected AuctionTable()
  30. {
  31. try (Connection con = ConnectionPool.getConnection();
  32. PreparedStatement ps = con.prepareStatement(RESTORE_AUCTION_ITEM);
  33. ResultSet rs = ps.executeQuery())
  34. {
  35. while (rs.next())
  36. {
  37. int auctionId = rs.getInt("auctionid");
  38.  
  39. _items.add(new AuctionItem(auctionId, rs.getInt("ownerid"), rs.getInt("itemid"), rs.getInt("count"), rs.getInt("enchant"), rs.getInt("costid"), rs.getInt("costcount")));
  40.  
  41. if (auctionId > _maxId)
  42. _maxId = auctionId;
  43. }
  44. }
  45. catch (Exception e)
  46. {
  47. LOGGER.error("Couldn't load AuctionTable.", e);
  48. }
  49.  
  50. LOGGER.info("Loaded {} Auction items.", _items.size());
  51. }
  52.  
  53. public void addItem(AuctionItem item)
  54. {
  55. _items.add(item);
  56.  
  57. try (Connection con = ConnectionPool.getConnection();
  58. PreparedStatement ps = con.prepareStatement(ADD_AUCTION_ITEM))
  59. {
  60. ps.setInt(1, item.getAuctionId());
  61. ps.setInt(2, item.getOwnerId());
  62. ps.setInt(3, item.getItemId());
  63. ps.setInt(4, item.getCount());
  64. ps.setInt(5, item.getEnchant());
  65. ps.setInt(6, item.getCostId());
  66. ps.setInt(7, item.getCostCount());
  67. ps.executeUpdate();
  68. }
  69. catch (Exception e)
  70. {
  71. LOGGER.error("Couldn't store auction item.", e);
  72. }
  73. }
  74.  
  75. public void deleteItem(AuctionItem item)
  76. {
  77. _items.remove(item);
  78.  
  79. try (Connection con = ConnectionPool.getConnection();
  80. PreparedStatement ps = con.prepareStatement(DELETE_AUCTION_ITEM))
  81. {
  82. ps.setInt(1, item.getAuctionId());
  83. ps.execute();
  84. }
  85. catch (Exception e)
  86. {
  87. LOGGER.error("Couldn't delete auction item.", e);
  88. }
  89. }
  90.  
  91. public AuctionItem getItem(int auctionId)
  92. {
  93. return _items.stream().filter(x -> x.getAuctionId() == auctionId).findFirst().orElse(null);
  94. }
  95.  
  96. public Set<AuctionItem> getItems()
  97. {
  98. return _items;
  99. }
  100.  
  101. public int getNextAuctionId()
  102. {
  103. return _maxId++;
  104. }
  105.  
  106. public static AuctionTable getInstance()
  107. {
  108. return SingletonHolder.INSTANCE;
  109. }
  110.  
  111. private static class SingletonHolder
  112. {
  113. protected static final AuctionTable INSTANCE = new AuctionTable();
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement