C_GiveItem.java :  » Game » l1j-tw-99nets » l1j » server » server » clientpackets » Java Open Source

Java Open Source » Game » l1j tw 99nets 
l1j tw 99nets » l1j » server » server » clientpackets » C_GiveItem.java
/**
 *                            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 l1j.server.server.clientpackets;

import l1j.server.server.ClientThread;
import l1j.server.server.datatables.PetTypeTable;
import l1j.server.server.model.L1Inventory;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.L1PcInventory;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.serverpackets.S_ItemName;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.templates.L1Npc;
import l1j.server.server.templates.L1PetType;
import l1j.server.server.utils.Random;

/**
 * 
 */
public class C_GiveItem extends ClientBasePacket {
  private static final String C_GIVE_ITEM = "[C] C_GiveItem";

  public C_GiveItem(byte decrypt[], ClientThread client) {
    super(decrypt);
    int targetId = readD();
    readH();
    readH();
    int itemId = readD();
    int count = readD();

    L1PcInstance pc = client.getActiveChar();
    if (pc.isGhost()) {
      return;
    }

    L1Object object = L1World.getInstance().findObject(targetId);
    if ((object == null) || !(object instanceof L1NpcInstance)) {
      return;
    }
    L1NpcInstance target = (L1NpcInstance) object;
    if (!isNpcItemReceivable(target.getNpcTemplate())) {
      return;
    }
    L1Inventory targetInv = target.getInventory();

    L1Inventory inv = pc.getInventory();
    L1ItemInstance item = inv.getItem(itemId);
    if (item == null) {
      return;
    }
    if (item.isEquipped()) {
      pc.sendPackets(new S_ServerMessage(141)); // \f1
      return;
    }
    if (!item.getItem().isTradable()) {
      pc.sendPackets(new S_ServerMessage(210, item.getItem().getName())); // \f1%0%d
      return;
    }
    if (item.getBless() >= 128) { // 
      // \f1%0%d
      pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
      return;
    }
    for (Object petObject : pc.getPetList().values()) {
      if (petObject instanceof L1PetInstance) {
        L1PetInstance pet = (L1PetInstance) petObject;
        if (item.getId() == pet.getItemObjId()) {
          // \f1%0%d
          pc.sendPackets(new S_ServerMessage(210, item.getItem().getName()));
          return;
        }
      }
    }
    if (targetInv.checkAddItem(item, count) != L1Inventory.OK) {
      pc.sendPackets(new S_ServerMessage(942)); // 
      return;
    }
    item = inv.tradeItem(item, count, targetInv);
    target.onGetItem(item);
    target.turnOnOffLight();
    pc.turnOnOffLight();

    L1PetType petType = PetTypeTable.getInstance().get(target.getNpcTemplate().get_npcId());
    if ((petType == null) || target.isDead()) {
      return;
    }

    // 
    if (item.getItemId() == petType.getItemIdForTaming()) {
      tamePet(pc, target);
    }
    // 
    if ((item.getItemId() == 40070) && petType.canEvolve()) {
      evolvePet(pc, target);
    }
    // 
    if ((item.getItemId() == 41310) && petType.canEvolve()) {
      evolvePet(pc, target);
    }
  }

  private final static String receivableImpls[] = new String[]
  { "L1Npc", // NPC
      "L1Monster", // 
      "L1Guardian", // 
      "L1Teleporter", // 
      "L1Guard" }; // 

  private boolean isNpcItemReceivable(L1Npc npc) {
    for (String impl : receivableImpls) {
      if (npc.getImpl().equals(impl)) {
        return true;
      }
    }
    return false;
  }

  private void tamePet(L1PcInstance pc, L1NpcInstance target) {
    if ((target instanceof L1PetInstance) || (target instanceof L1SummonInstance)) {
      return;
    }

    int petcost = 0;
    Object[] petlist = pc.getPetList().values().toArray();
    for (Object pet : petlist) {
      petcost += ((L1NpcInstance) pet).getPetcost();
    }
    int charisma = pc.getCha();
    if (pc.isCrown()) { // 
      charisma += 6;
    }
    else if (pc.isElf()) { // 
      charisma += 12;
    }
    else if (pc.isWizard()) { // 
      charisma += 6;
    }
    else if (pc.isDarkelf()) { // 
      charisma += 6;
    }
    else if (pc.isDragonKnight()) { // 
      charisma += 6;
    }
    else if (pc.isIllusionist()) { // 
      charisma += 6;
    }
    charisma -= petcost;

    L1PcInventory inv = pc.getInventory();
    if ((charisma >= 6) && (inv.getSize() < 180)) {
      if (isTamePet(target)) {
        L1ItemInstance petamu = inv.storeItem(40314, 1); // 
        if (petamu != null) {
          new L1PetInstance(target, pc, petamu.getId());
          pc.sendPackets(new S_ItemName(petamu));
        }
      }
      else {
        pc.sendPackets(new S_ServerMessage(324)); // 
      }
    }
  }

  private void evolvePet(L1PcInstance pc, L1NpcInstance target) {
    if (!(target instanceof L1PetInstance)) {
      return;
    }
    L1PcInventory inv = pc.getInventory();
    L1PetInstance pet = (L1PetInstance) target;
    L1ItemInstance petamu = inv.getItem(pet.getItemObjId());
    if ((pet.getLevel() >= 30) && // Lv30
        (pc == pet.getMaster()) && // 
        (petamu != null)) {
      L1ItemInstance highpetamu = inv.storeItem(40316, 1);
      if (highpetamu != null) {
        pet.evolvePet( // 
        highpetamu.getId());
        pc.sendPackets(new S_ItemName(highpetamu));
        inv.removeItem(petamu, 1);
      }
    }
  }

  private boolean isTamePet(L1NpcInstance npc) {
    boolean isSuccess = false;
    int npcId = npc.getNpcTemplate().get_npcId();
    if (npcId == 45313) { // 
      if ((npc.getMaxHp() / 3 > npc.getCurrentHp() // HP1/31/16
          )
          && (Random.nextInt(16) == 15)) {
        isSuccess = true;
      }
    }
    else {
      if (npc.getMaxHp() / 3 > npc.getCurrentHp()) {
        isSuccess = true;
      }
    }

    if ((npcId == 45313) || (npcId == 45044) || (npcId == 45711)) { // 
      if (npc.isResurrect()) { // RES
        isSuccess = false;
      }
    }

    return isSuccess;
  }

  @Override
  public String getType() {
    return C_GIVE_ITEM;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.