com.lineage.server.clientpackets.C_ItemUSe.java Source code

Java tutorial

Introduction

Here is the source code for com.lineage.server.clientpackets.C_ItemUSe.java

Source

/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.clientpackets;

import static com.lineage.server.model.skill.L1SkillId.DECAY_POTION;
import static com.lineage.server.model.skill.L1SkillId.SOLID_CARRIAGE;

import java.sql.Timestamp;
import java.util.Calendar;

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

import com.lineage.server.ClientThread;
import com.lineage.server.model.L1ItemDelay;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.L1PolyMorph;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.CiteItemClass;
import com.lineage.server.model.item.L1TreasureBox;
import com.lineage.server.model.item.etcitem.MagicStone;
import com.lineage.server.serverpackets.S_OwnCharAttrDef;
import com.lineage.server.serverpackets.S_OwnCharStatus;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_SPMR;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.templates.L1EtcItem;

/**
 * ????
 * 
 * @author jrwz
 */
public class C_ItemUSe extends ClientBasePacket {

    private static final String C_ITEM_USE = "[C] C_ItemUSe";
    private static final Log _log = LogFactory.getLog(C_ItemUSe.class);

    /**
     * ??
     * 
     * @param
     */
    public C_ItemUSe(final byte[] abyte0, final ClientThread client) throws Exception {

        // 
        super(abyte0);

        // ?
        final L1PcInstance pc = client.getActiveChar();

        // 
        if (pc == null) {
            return;
        }

        // ??
        if (pc.isGhost()) {
            return;
        }

        // ?
        if (pc.isDead()) {
            return;
        }

        // ??
        if (pc.isTeleport()) {
            return;
        }

        // 
        if (pc.isPrivateShop()) {
            return;
        }

        // ??
        if (!pc.getMap().isUsableItem()) {
            pc.sendPackets(new S_ServerMessage(563)); // \f1
            return;
        }

        // ???
        if (pc.CanNotDoAnything()) {
            return;
        }

        // OBJID
        final int itemObjid = this.readD();

        // ??
        final L1ItemInstance useItem = pc.getInventory().getItem(itemObjid);

        // :?
        if (useItem == null) {
            return;
        }

        // :?
        if (useItem.getCount() <= 0) {
            pc.sendPackets(new S_ServerMessage(329, useItem.getLogName())); // \f1
                                                                            // %0%o
            return;
        }

        // ?Class
        boolean isClass = false;
        final String className = useItem.getItem().getClassName(); // ?

        // Class?0
        if (!className.equals("0")) {
            isClass = true;
        }

        // PC??0
        if (pc.getCurrentHp() > 0) {
            int delay_id = 0; // ID
            if (useItem.getItem().getType2() == 0) { // ??
                delay_id = ((L1EtcItem) useItem.getItem()).get_delayid(); // ?ID
            }
            if (delay_id != 0) { // 
                if (pc.hasItemDelay(delay_id) == true) {
                    return;
                }
            }

            final int min = useItem.getItem().getMinLevel(); // ???
            final int max = useItem.getItem().getMaxLevel(); // ???

            if ((min != 0) && (min > pc.getLevel())) {
                pc.sendPackets(new S_ServerMessage(318, String.valueOf(min))); // 
                                                                               // %0???
                return;
            } else if ((max != 0) && (max < pc.getLevel())) {
                pc.sendPackets(new S_PacketBox(S_PacketBox.MSG_LEVEL_OVER, max)); // 
                                                                                  // %d??
                return;
            }

            // ?
            boolean isDelayEffect = false;
            if (useItem.getItem().getType2() == 0) { // ? (etcitem)
                final int delayEffect = ((L1EtcItem) useItem.getItem()).get_delayEffect(); // 
                if (delayEffect > 0) {
                    isDelayEffect = true;
                    final Timestamp lastUsed = useItem.getLastUsed();
                    if (lastUsed != null) {
                        final Calendar cal = Calendar.getInstance();
                        long UseTime = (cal.getTimeInMillis() - lastUsed.getTime()) / 1000;
                        if (UseTime <= delayEffect) {
                            // ?
                            UseTime = (delayEffect - UseTime) / 60;
                            // ? ( ?)
                            final String UseTimeSurplus = useItem.getLogName() + " " + String.valueOf(UseTime);
                            pc.sendPackets(new S_ServerMessage(1139, UseTimeSurplus)); // %0 
                            return;
                        }
                    }
                }
            }

            // ??
            final int use_type = useItem.getItem().getUseType();
            switch (use_type) {

            case -14: // treasure_box (?)
                // ??ID
                final int item_Id = useItem.getItem().getItemId();
                final L1TreasureBox box = L1TreasureBox.get(item_Id);
                if (box != null) {
                    if (box.open(pc)) {
                        final L1EtcItem temp = (L1EtcItem) useItem.getItem();
                        if (temp.get_delayEffect() > 0) {
                            // ?????
                            if (useItem.isStackable()) {
                                if (useItem.getCount() > 1) {
                                    isDelayEffect = true;
                                }
                                pc.getInventory().removeItem(useItem.getId(), 1);
                            } else {
                                isDelayEffect = true;
                            }
                        } else {
                            pc.getInventory().removeItem(useItem.getId(), 1);
                        }
                    }
                }
                break;

            case -13: // 
                pc.getInventory().setArrow(useItem.getItem().getItemId());
                pc.sendPackets(new S_ServerMessage(452, useItem.getLogName())); // %0%s 
                break;

            case -12: // 
                pc.getInventory().setSting(useItem.getItem().getItemId());
                pc.sendPackets(new S_ServerMessage(452, useItem.getLogName())); // %0%s 
                break;

            case -11: // 9()()(??)()
                if (pc.getInventory().consumeItem(41246, 300)) {
                    final int itemId = useItem.getItem().getItemId();
                    MagicStone.get().useItem(pc, useItem, itemId, 0, 0, 0);
                } else {
                    isDelayEffect = false;
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0?%s
                }
                break;

            case -10: // 1 ~ 4 ()()(??)()
                if (pc.getInventory().consumeItem(41246, 30)) {
                    final int itemId = useItem.getItem().getItemId();
                    MagicStone.get().useItem(pc, useItem, itemId, 0, 0, 0);
                } else {
                    isDelayEffect = false;
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0?%s
                }
                break;

            case -9: // 5 ~ 6()()(??)()
                if (pc.getInventory().consumeItem(41246, 60)) {
                    final int itemId = useItem.getItem().getItemId();
                    MagicStone.get().useItem(pc, useItem, itemId, 0, 0, 0);
                } else {
                    isDelayEffect = false;
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0?%s
                }
                break;

            case -8: // 7()()(??)()
                if (pc.getInventory().consumeItem(41246, 100)) {
                    final int itemId = useItem.getItem().getItemId();
                    MagicStone.get().useItem(pc, useItem, itemId, 0, 0, 0);
                } else {
                    isDelayEffect = false;
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0?%s
                }
                break;

            case -7: // 8()()(??)()
                if (pc.getInventory().consumeItem(41246, 200)) {
                    final int itemId = useItem.getItem().getItemId();
                    MagicStone.get().useItem(pc, useItem, itemId, 0, 0, 0);
                } else {
                    isDelayEffect = false;
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // \f1%0?%s
                }
                break;

            case -6: // ?
                if (isClass) {
                    final int[] newData = new int[2];
                    newData[0] = this.readC();
                    newData[1] = this.readC();
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case -5: // ??
                if (pc.hasSkillEffect(DECAY_POTION)) {
                    pc.sendPackets(new S_ServerMessage(698)); // ??
                    return;
                }
                if (isClass) {
                    CiteItemClass.getInstance().item(null, pc, useItem);
                }
                break;

            case -4: //  (??)
                pc.sendPackets(new S_ServerMessage(74, useItem.getLogName())); // \f1%0%o
                                                                               // 
                break;

            case -3: // ?
            case -2: // 
            case -1: // ?
            case 0: // ?
            case 15: // ?
            case 13: // ()
            case 32: // ?()
            case 34: // ?()
            case 36: // ?()
                if (isClass) {
                    CiteItemClass.getInstance().item(null, pc, useItem);
                }
                break;

            case 1: // 
                    // ?
                if (pc.hasItemDelay(L1ItemDelay.WEAPON) == true) {
                    return;
                }
                if (this.checkEquipped(pc, useItem)) {
                    this.setEquippedWeapon(pc, useItem);
                }
                break;

            case 2: // 
            case 18: // T?
            case 19: // 
            case 20: // 
            case 21: // ?
            case 22: // 
            case 23: // 
            case 24: // 
            case 25: // 
            case 37: // 
            case 40: // 
            case 43: //  (?)
            case 44: //  ()
            case 45: //  ()
                // ?
                if (pc.hasItemDelay(L1ItemDelay.ARMOR) == true) {
                    return;
                }
                if (this.checkEquipped(pc, useItem)) {
                    this.setEquippedArmor(pc, useItem);
                }
                break;

            case 5: // ? (?/??)? / (?)
            case 17: // ? ? (?)
                if (isClass) {
                    final int[] newData = new int[3];
                    newData[0] = this.readD(); // ?OBJID
                    newData[1] = this.readH(); // X??
                    newData[2] = this.readH(); // Y??
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case 6: // ?
            case 29: // ?(??)
                if (isClass) {
                    final int[] newData = new int[2];
                    newData[1] = this.readH(); // ? /
                    newData[0] = this.readD(); // ?OBJID
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case 7: // ?
            case 8: // ??
            case 14: // ? (???) ?///
            case 26: // ?
            case 27: // ?
            case 30: // ? (NPC?Ctrl ? XY??) / ? ()
            case 46: // ??
                if (isClass) {
                    final int[] newData = new int[1];
                    newData[0] = this.readD(); // ?OBJID
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case 12: // 
            case 31: // ?
            case 33: // ?
            case 35: // ?
                if (isClass) {
                    final int[] newData = new int[1];
                    newData[0] = this.readH();
                    final String s = this.readS();
                    pc.setText(s);
                    pc.setTextByte(this.readByte());
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case 16: // ??
                if (isClass) {
                    final String cmd = this.readS();
                    pc.setText(cmd); // ??
                    CiteItemClass.getInstance().item(null, pc, useItem);
                }
                break;

            case 28: // ?
                if (isClass) {
                    final int[] newData = new int[1];
                    newData[0] = this.readC();
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            case 42: // ? ()
                if (isClass) {
                    final int[] newData = new int[2];
                    newData[0] = this.readH(); // X??
                    newData[1] = this.readH(); // Y??
                    CiteItemClass.getInstance().item(newData, pc, useItem);
                }
                break;

            default: // 
                _log.info("??: " + use_type);
                break;
            }

            // 
            if (isDelayEffect) {
                final Timestamp ts = new Timestamp(System.currentTimeMillis());
                useItem.setLastUsed(ts); // 
                pc.getInventory().updateItem(useItem, L1PcInventory.COL_DELAY_EFFECT);
                pc.getInventory().saveItem(useItem, L1PcInventory.COL_DELAY_EFFECT);
            }
            try {
                L1ItemDelay.onItemUse(client, useItem); // 
            } catch (final Exception e) {
                _log.error("?:" + useItem.getItemId(), e);
            }
        }
    }

    /**
     * ??.
     * 
     * @param pc
     *            
     * @param useItem
     *            
     * @return ???true
     */
    private boolean checkEquipped(final L1PcInstance pc, final L1ItemInstance useItem) {

        // ?
        boolean isEquipped = false;

        if (pc.isCrown()) { // ?
            if (useItem.getItem().isUseRoyal()) {
                isEquipped = true;
            }
        } else if (pc.isKnight()) { // 
            if (useItem.getItem().isUseKnight()) {
                isEquipped = true;
            }
        } else if (pc.isElf()) { // ?
            if (useItem.getItem().isUseElf()) {
                isEquipped = true;
            }
        } else if (pc.isWizard()) { // 
            if (useItem.getItem().isUseMage()) {
                isEquipped = true;
            }
        } else if (pc.isDarkelf()) { // ?
            if (useItem.getItem().isUseDarkelf()) {
                isEquipped = true;
            }
        } else if (pc.isDragonKnight()) { // 
            if (useItem.getItem().isUseDragonknight()) {
                isEquipped = true;
            }
        } else if (pc.isIllusionist()) { // 
            if (useItem.getItem().isUseIllusionist()) {
                isEquipped = true;
            }
        }

        // ???
        if (!isEquipped) {
            pc.sendPackets(new S_ServerMessage(264)); // \f1?
        }

        return isEquipped;
    }

    @Override
    public String getType() {
        return C_ITEM_USE;
    }

    /**
     * .
     * 
     * @param pc
     *            
     * @param armor
     *            
     */
    private void setEquippedArmor(final L1PcInstance pc, final L1ItemInstance armor) {

        // ?
        final int type = armor.getItem().getType();

        // ??
        final L1PcInventory pcInventory = pc.getInventory();

        // ???
        boolean equipeSpace;

        // ?2,?1
        if (type == 9) {
            equipeSpace = pcInventory.getTypeEquipped(2, 9) <= 1;
        } else {
            equipeSpace = pcInventory.getTypeEquipped(2, type) <= 0;
        }

        // 
        if (equipeSpace && !armor.isEquipped()) { // ???

            // ???
            final int polyid = pc.getTempCharGfx();

            // ???
            if (!L1PolyMorph.isEquipableArmor(polyid, type)) {
                return;
            }

            // ?
            if (((type == 13) && (pcInventory.getTypeEquipped(2, 7) >= 1))
                    || ((type == 7) && (pcInventory.getTypeEquipped(2, 13) >= 1))) {
                pc.sendPackets(new S_ServerMessage(124)); // ?
                return;
            }

            // ?
            if ((type == 7) && (pc.getWeapon() != null)) {
                if (pc.getWeapon().getItem().isTwohandedWeapon()) { // ?
                    pc.sendPackets(new S_ServerMessage(129)); // \f1?
                    return;
                }
            }

            // ???
            if ((type == 3) && (pcInventory.getTypeEquipped(2, 4) >= 1)) {
                pc.sendPackets(new S_ServerMessage(126, "$224", "$225")); // \f1?%1
                                                                          // 
                                                                          // %0%o
                                                                          // 
                return;
            }

            // ???
            else if ((type == 3) && (pcInventory.getTypeEquipped(2, 2) >= 1)) {
                pc.sendPackets(new S_ServerMessage(126, "$224", "$226")); // \f1?%1
                                                                          // 
                                                                          // %0%o
                                                                          // 
                return;
            }

            // ???
            else if ((type == 2) && (pcInventory.getTypeEquipped(2, 4) >= 1)) {
                pc.sendPackets(new S_ServerMessage(126, "$226", "$225")); // \f1?%1
                                                                          // 
                                                                          // %0%o
                                                                          // 
                return;
            }
            pcInventory.setEquipped(armor, true);
        }

        // 
        else if (armor.isEquipped()) { // 
            if (armor.getItem().getBless() == 2) { // 
                pc.sendPackets(new S_ServerMessage(150)); // \f1???
                return;

            }
            // ??
            if ((type == 3) && (pcInventory.getTypeEquipped(2, 2) >= 1)) {
                pc.sendPackets(new S_ServerMessage(127)); // \f1?
                return;

            }

            // ??
            else if (((type == 2) || (type == 3)) && (pcInventory.getTypeEquipped(2, 4) >= 1)) {
                pc.sendPackets(new S_ServerMessage(127)); // \f1?
                return;
            }

            // ?
            if (type == 7) {
                if (pc.hasSkillEffect(SOLID_CARRIAGE)) {
                    pc.removeSkillEffect(SOLID_CARRIAGE);
                }
            }
            pcInventory.setEquipped(armor, false);
        } else {
            if (armor.getItem().getUseType() == 23) {
                pc.sendPackets(new S_ServerMessage(144)); // \f1??
                return;
            }
            pc.sendPackets(new S_ServerMessage(124)); // \f1?
            return;
        }

        pc.setCurrentHp(pc.getCurrentHp()); // HP
        pc.setCurrentMp(pc.getCurrentMp()); // MP
        pc.sendPackets(new S_OwnCharAttrDef(pc)); // ?
        pc.sendPackets(new S_OwnCharStatus(pc)); // 
        pc.sendPackets(new S_SPMR(pc)); // 
    }

    /**
     * .
     * 
     * @param pc
     *            
     * @param weapon
     *            
     */
    private void setEquippedWeapon(final L1PcInstance pc, final L1ItemInstance weapon) {

        // ??
        final L1PcInventory pcInventory = pc.getInventory();

        // ??
        if ((pc.getWeapon() == null) || !pc.getWeapon().equals(weapon)) {

            // ?
            final int weapon_type = weapon.getItem().getType();

            // ???
            final int polyid = pc.getTempCharGfx();

            // ???
            if (!L1PolyMorph.isEquipableWeapon(polyid, weapon_type)) {
                return;
            }

            // ????
            if (weapon.getItem().isTwohandedWeapon() && (pcInventory.getTypeEquipped(2, 7) >= 1)) {
                pc.sendPackets(new S_ServerMessage(128));
                return;
            }
        }

        // ?
        if (pc.getWeapon() != null) {

            // 
            if (pc.getWeapon().getItem().getBless() == 2) {
                pc.sendPackets(new S_ServerMessage(150)); // \f1???
                return;
            }

            // 
            if (pc.getWeapon().equals(weapon)) {
                pcInventory.setEquipped(pc.getWeapon(), false, false, false);
                return;
            }
            pcInventory.setEquipped(pc.getWeapon(), false, false, true);
        }

        // 
        if (weapon.getItem().getBless() == 2) {
            pc.sendPackets(new S_ServerMessage(149, weapon.getLogName())); // \f1%0%s
                                                                           // ?
        }
        pcInventory.setEquipped(weapon, true, false, false);
    }
}