/* This file is part of the ZeroFusion MapleStory Server Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> Matthias Butz <matze@odinms.de> Jan Christian Meyer <vimes@odinms.de> ZeroFusion organized by "RMZero213" <RMZero213@hotmail.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation. You may not use, modify or distribute this program under any other version of the GNU Affero General Public License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package client.inventory; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.concurrent.atomic.AtomicInteger; import database.DatabaseConnection; import java.io.Serializable; import java.sql.SQLException; public class MapleInventoryIdentifier implements Serializable { private static final long serialVersionUID = 21830921831301L; private final AtomicInteger runningUID = new AtomicInteger(0); private static final MapleInventoryIdentifier instance = new MapleInventoryIdentifier(); public static int getInstance() { return instance.getNextUniqueId(); } public int getNextUniqueId() { if (runningUID.get() <= 0) { runningUID.set(initUID()); } else { runningUID.set(runningUID.get() + 1); } return runningUID.get(); } public int initUID() { int ret = 0; try { int[] ids = new int[5]; Connection con = DatabaseConnection.getConnection(); PreparedStatement ps = con.prepareStatement("SELECT MAX(uniqueid) FROM inventoryitems WHERE uniqueid > 0"); ResultSet rs = ps.executeQuery(); if (rs.next()) { ids[0] = rs.getInt(1) + 1; } rs.close(); ps.close(); ps = con.prepareStatement("SELECT MAX(petid) FROM pets"); rs = ps.executeQuery(); if (rs.next()) { ids[1] = rs.getInt(1) + 1; } rs.close(); ps.close(); ps = con.prepareStatement("SELECT MAX(ringid) FROM rings"); rs = ps.executeQuery(); if (rs.next()) { ids[2] = rs.getInt(1) + 1; } rs.close(); ps.close(); ps = con.prepareStatement("SELECT MAX(partnerringid) FROM rings"); rs = ps.executeQuery(); if (rs.next()) { ids[3] = rs.getInt(1) + 1; //biggest pl0x. but if this happens -> o_O } rs.close(); ps.close(); ps = con.prepareStatement("SELECT MAX(uniqueid) FROM androids"); rs = ps.executeQuery(); if (rs.next()) { ids[4] = rs.getInt(1) + 1; } rs.close(); ps.close(); for (int i = 0; i < ids.length; i++) { if (ids[i] > ret) { ret = ids[i]; } } } catch (SQLException e) { } return ret; } }