com.l2jfree.gameserver.model.ShortCuts.java Source code

Java tutorial

Introduction

Here is the source code for com.l2jfree.gameserver.model.ShortCuts.java

Source

/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jfree.gameserver.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;

import javolution.util.FastMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.l2jfree.L2DatabaseFactory;
import com.l2jfree.gameserver.gameobjects.L2Player;
import com.l2jfree.gameserver.model.items.L2ItemInstance;
import com.l2jfree.gameserver.model.items.templates.L2EtcItemType;
import com.l2jfree.gameserver.network.packets.server.ExAutoSoulShot;
import com.l2jfree.gameserver.network.packets.server.ShortCutInit;

public final class ShortCuts {
    private static final Log _log = LogFactory.getLog(ShortCuts.class);

    private final Map<Integer, L2ShortCut> _shortCuts = new FastMap<Integer, L2ShortCut>().setShared(true);
    private final L2Player _owner;

    public ShortCuts(L2Player owner) {
        _owner = owner;
    }

    public L2ShortCut[] getAllShortCuts() {
        return _shortCuts.values().toArray(new L2ShortCut[_shortCuts.size()]);
    }

    public synchronized void registerShortCut(L2ShortCut shortcut) {
        _shortCuts.put(shortcut.getSlot() + 12 * shortcut.getPage(), shortcut);

        Connection con = null;
        try {
            con = L2DatabaseFactory.getInstance().getConnection(con);

            PreparedStatement statement = con.prepareStatement(
                    "REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)");
            statement.setInt(1, _owner.getObjectId());
            statement.setInt(2, shortcut.getSlot());
            statement.setInt(3, shortcut.getPage());
            statement.setInt(4, shortcut.getType());
            statement.setInt(5, shortcut.getId());
            statement.setInt(6, shortcut.getLevel());
            statement.setInt(7, _owner.getClassIndex());
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.warn("", e);
        } finally {
            L2DatabaseFactory.close(con);
        }
    }

    public synchronized void deleteShortCut(int slot, int page) {
        L2ShortCut old = _shortCuts.remove(slot + page * 12);
        if (old == null)
            return;

        Connection con = null;
        try {
            con = L2DatabaseFactory.getInstance().getConnection(con);

            PreparedStatement statement = con.prepareStatement(
                    "DELETE FROM character_shortcuts WHERE charId=? AND slot=? AND page=? AND class_index=?");
            statement.setInt(1, _owner.getObjectId());
            statement.setInt(2, old.getSlot());
            statement.setInt(3, old.getPage());
            statement.setInt(4, _owner.getClassIndex());
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.warn("", e);
        } finally {
            L2DatabaseFactory.close(con);
        }

        if (old.getType() == L2ShortCut.TYPE_ITEM) {
            L2ItemInstance item = _owner.getInventory().getItemByObjectId(old.getId());

            if (item != null && item.getItemType() == L2EtcItemType.SHOT)
                _owner.getShots().removeAutoSoulShot(item.getItemId());
        }

        _owner.sendPacket(new ShortCutInit(_owner));

        for (int shotId : _owner.getShots().getAutoSoulShots())
            _owner.sendPacket(new ExAutoSoulShot(shotId, 1));
    }

    public synchronized void deleteShortCutByObjectId(int objectId) {
        for (L2ShortCut sc : _shortCuts.values())
            if (sc.getType() == L2ShortCut.TYPE_ITEM)
                if (sc.getId() == objectId)
                    deleteShortCut(sc.getSlot(), sc.getPage());
    }

    public synchronized void restore() {
        _shortCuts.clear();

        Connection con = null;
        try {
            con = L2DatabaseFactory.getInstance().getConnection(con);
            PreparedStatement statement = con.prepareStatement(
                    "SELECT slot, page, type, shortcut_id, level FROM character_shortcuts WHERE charId=? AND class_index=?");
            statement.setInt(1, _owner.getObjectId());
            statement.setInt(2, _owner.getClassIndex());

            ResultSet rset = statement.executeQuery();

            while (rset.next()) {
                int slot = rset.getInt("slot");
                int page = rset.getInt("page");
                int type = rset.getInt("type");
                int id = rset.getInt("shortcut_id");
                int level = rset.getInt("level");

                _shortCuts.put(slot + page * 12, new L2ShortCut(slot, page, type, id, level, 1));
            }

            rset.close();
            statement.close();
        } catch (Exception e) {
            _log.warn("", e);
        } finally {
            L2DatabaseFactory.close(con);
        }

        for (L2ShortCut sc : _shortCuts.values())
            if (sc.getType() == L2ShortCut.TYPE_ITEM)
                if (_owner.getInventory().getItemByObjectId(sc.getId()) == null)
                    deleteShortCut(sc.getSlot(), sc.getPage());
    }
}