Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.sf.l2j.gameserver.data.sql;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- import net.sf.l2j.commons.logging.CLogger;
- import net.sf.l2j.commons.pool.ConnectionPool;
- import net.sf.l2j.gameserver.model.AuctionItem;
- /**
- * @author Anarchy
- *
- */
- public class AuctionTable
- {
- private static final CLogger LOGGER = new CLogger(AuctionTable.class.getName());
- private static final String RESTORE_AUCTION_ITEM = "SELECT * FROM auction_table";
- private static final String ADD_AUCTION_ITEM = "INSERT INTO auction_table VALUES (?,?,?,?,?,?,?)";
- private static final String DELETE_AUCTION_ITEM = "DELETE FROM auction_table WHERE auctionid=?";
- private Set<AuctionItem> _items = ConcurrentHashMap.newKeySet();
- private int _maxId = 0;
- protected AuctionTable()
- {
- try (Connection con = ConnectionPool.getConnection();
- PreparedStatement ps = con.prepareStatement(RESTORE_AUCTION_ITEM);
- ResultSet rs = ps.executeQuery())
- {
- while (rs.next())
- {
- int auctionId = rs.getInt("auctionid");
- _items.add(new AuctionItem(auctionId, rs.getInt("ownerid"), rs.getInt("itemid"), rs.getInt("count"), rs.getInt("enchant"), rs.getInt("costid"), rs.getInt("costcount")));
- if (auctionId > _maxId)
- _maxId = auctionId;
- }
- }
- catch (Exception e)
- {
- LOGGER.error("Couldn't load AuctionTable.", e);
- }
- LOGGER.info("Loaded {} Auction items.", _items.size());
- }
- public void addItem(AuctionItem item)
- {
- _items.add(item);
- try (Connection con = ConnectionPool.getConnection();
- PreparedStatement ps = con.prepareStatement(ADD_AUCTION_ITEM))
- {
- ps.setInt(1, item.getAuctionId());
- ps.setInt(2, item.getOwnerId());
- ps.setInt(3, item.getItemId());
- ps.setInt(4, item.getCount());
- ps.setInt(5, item.getEnchant());
- ps.setInt(6, item.getCostId());
- ps.setInt(7, item.getCostCount());
- ps.executeUpdate();
- }
- catch (Exception e)
- {
- LOGGER.error("Couldn't store auction item.", e);
- }
- }
- public void deleteItem(AuctionItem item)
- {
- _items.remove(item);
- try (Connection con = ConnectionPool.getConnection();
- PreparedStatement ps = con.prepareStatement(DELETE_AUCTION_ITEM))
- {
- ps.setInt(1, item.getAuctionId());
- ps.execute();
- }
- catch (Exception e)
- {
- LOGGER.error("Couldn't delete auction item.", e);
- }
- }
- public AuctionItem getItem(int auctionId)
- {
- return _items.stream().filter(x -> x.getAuctionId() == auctionId).findFirst().orElse(null);
- }
- public Set<AuctionItem> getItems()
- {
- return _items;
- }
- public int getNextAuctionId()
- {
- return _maxId++;
- }
- public static AuctionTable getInstance()
- {
- return SingletonHolder.INSTANCE;
- }
- private static class SingletonHolder
- {
- protected static final AuctionTable INSTANCE = new AuctionTable();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement