Пример #1
0
 @Override
 protected void writeImpl() {
   writeC(0xFE);
   writeH(0x6C);
   writeD(_item.getObjectId());
   writeD(_item.getDisplayId());
   writeD(_item.getLocationSlot());
   writeQ(_item.getCount());
   writeH(_item.getItem().getType2());
   writeH(_item.getCustomType1());
   writeH(_item.isEquipped() ? 0x01 : 0x00);
   writeD(_item.getItem().getBodyPart());
   writeH(_item.getEnchantLevel());
   writeH(_item.getCustomType2());
   if (_item.isAugmented()) {
     writeD(_item.getAugmentation().getAugmentationId());
   } else {
     writeD(0x00);
   }
   writeD(_item.getMana());
   writeD(_item.isTimeLimitedItem() ? (int) (_item.getRemainingTime() / 1000) : -9999);
   writeH(_item.getAttackElementType());
   writeH(_item.getAttackElementPower());
   for (byte i = 0; i < 6; i++) {
     writeH(_item.getElementDefAttr(i));
   }
   // Enchant Effects
   for (int op : _item.getEnchantOptions()) {
     writeH(op);
   }
 }
  @Override
  public String onAdvEvent(String event, L2Npc npc, L2PcInstance player) {
    final QuestState qs = getQuestState(player, false);
    if (qs == null) {
      return null;
    }

    String htmltext = null;
    switch (event) {
      case "ACCEPT":
        {
          qs.startQuest();
          if ((player.getLevel() > 20) && (player.getLevel() < 31)) {
            playSound(player, QuestSound.ITEMSOUND_QUEST_ACCEPT);
            qs.setMemoState(2);
            qs.setCond(3);
            htmltext = "30981-04.htm";
          } else if (player.getLevel() < 21) {
            qs.setMemoState(1);
            qs.setMemoStateEx(1, 0);
            qs.setCond(2);
            htmltext = "30981-03.htm";
          } else if ((player.getLevel() > 30) && (player.getLevel() < 41)) {
            qs.setMemoState(3);
            qs.setCond(4);
            htmltext = "30981-05.htm";
          } else {
            qs.setMemoState(4);
            qs.setCond(5);
            htmltext = "30981-06.htm";
          }
          break;
        }
      case "30981-11.html":
        {
          if ((qs.getMemoState() >= 9) && (qs.getMemoState() <= 12)) {
            if (hasAtLeastOneQuestItem(player, MANACLES_OF_PENITENT, PENITENTS_MANACLES1)) {
              if (hasQuestItems(player, PENITENTS_MANACLES1)) {
                takeItems(player, PENITENTS_MANACLES1, 1);
              }

              if (hasQuestItems(player, MANACLES_OF_PENITENT)) {
                takeItems(player, MANACLES_OF_PENITENT, 1);
              }
              qs.setMemoStateEx(1, player.getLevel());
              giveItems(player, PENITENTS_MANACLES, 1);
              qs.setCond(16);
              htmltext = event;
            }
          }
          break;
        }
      case "30981-14.html":
      case "30981-17.html":
        {
          if ((qs.getMemoState() >= 9) && (qs.getMemoState() <= 12)) {
            htmltext = event;
          }
          break;
        }
      case "30981-15t.html":
        {
          final L2ItemInstance petItem = player.getInventory().getItemByItemId(PENITENTS_MANACLES);
          final int petLevel = (petItem == null) ? 0 : petItem.getEnchantLevel();
          if ((qs.getMemoState() >= 9)
              && (qs.getMemoState() <= 12)
              && (petLevel > qs.getMemoStateEx(1))) {
            final L2Summon summon = player.getSummon();
            if (summon != null) {
              htmltext = event;
            } else {
              int i1 = 0;
              if (player.getLevel() > qs.getMemoStateEx(1)) {
                i1 = petLevel - qs.getMemoStateEx(1) - (player.getLevel() - qs.getMemoStateEx(1));
              } else {
                i1 = petLevel - qs.getMemoStateEx(1);
              }

              if (i1 < 0) {
                i1 = 0;
              }

              int i0 = getRandom(i1) + 1;
              if (player.getPkKills() <= i0) {
                giveItems(player, MANACLES_OF_PENITENT, 1);
                if (petItem != null) {
                  takeItems(player, PENITENTS_MANACLES, -1);
                }
                htmltext = "30981-15.html";

                player.setPkKills(0);
                qs.exitQuest(true, true);
              } else {
                giveItems(player, MANACLES_OF_PENITENT, 1);
                if (petItem != null) {
                  takeItems(player, PENITENTS_MANACLES, -1);
                }
                htmltext = "30981-16.html";

                player.setPkKills(player.getPkKills() - i0);
                qs.setMemoStateEx(1, 0);
              }
            }
          }
          break;
        }
      case "30981-18.html":
        {
          if ((qs.getMemoState() >= 9) && (qs.getMemoState() <= 12)) {
            qs.exitQuest(true, true);
            htmltext = event;
          }
          break;
        }
    }
    return htmltext;
  }
 @Override
 public String onTalk(L2Npc npc, L2PcInstance player) {
   final QuestState qs = getQuestState(player, true);
   final int memoState = qs.getMemoState();
   String htmltext = getNoQuestMsg(player);
   if (qs.isCreated()) {
     if (npc.getId() == BLACK_JUDGE) {
       if (player.getPkKills() == 0) {
         htmltext = "30981-01.htm";
       } else {
         htmltext = "30981-02.htm";
       }
     }
   } else if (qs.isStarted()) {
     switch (npc.getId()) {
       case BLACK_JUDGE:
         {
           if (memoState == 1000) {
             takeItems(player, PENITENTS_MANACLES, 1);
           } else if (memoState < 9) {
             htmltext = "30981-07.html";
           } else if ((memoState >= 9) && (memoState <= 12)) {
             if (!hasAtLeastOneQuestItem(
                 player,
                 MANUAL_OF_MANACLES,
                 MANACLES_OF_PENITENT,
                 PENITENTS_MANACLES1,
                 PENITENTS_MANACLES)) {
               giveItems(player, MANUAL_OF_MANACLES, 1);
               qs.setCond(14, true);
               htmltext = "30981-08.html";
             } else if (hasQuestItems(player, MANUAL_OF_MANACLES)
                 && !hasAtLeastOneQuestItem(
                     player, MANACLES_OF_PENITENT, PENITENTS_MANACLES1, PENITENTS_MANACLES)) {
               htmltext = "30981-09.html";
             } else if (hasQuestItems(player, PENITENTS_MANACLES1)
                 && !hasAtLeastOneQuestItem(
                     player, MANUAL_OF_MANACLES, MANACLES_OF_PENITENT, PENITENTS_MANACLES)) {
               htmltext = "30981-10.html";
             } else if (hasQuestItems(player, PENITENTS_MANACLES)) {
               final L2ItemInstance petItem =
                   player.getInventory().getItemByItemId(PENITENTS_MANACLES);
               final int petLevel = (petItem == null) ? 0 : petItem.getEnchantLevel();
               if (petLevel < (qs.getMemoStateEx(1) + 1)) {
                 htmltext = "30981-12.html";
               } else {
                 htmltext = "30981-13.html";
               }
             } else if (hasQuestItems(player, MANACLES_OF_PENITENT)
                 && !hasQuestItems(player, PENITENTS_MANACLES)) {
               htmltext = "30981-16t.html";
             }
           }
           break;
         }
       case BLACKSMITH_PUSHKIN:
         {
           if ((memoState >= 9) && (memoState <= 12)) {
             if (!hasAtLeastOneQuestItem(
                     player, PENITENTS_MANACLES1, PENITENTS_MANACLES, MANACLES_OF_PENITENT)
                 && hasQuestItems(player, MANUAL_OF_MANACLES)) {
               if ((getQuestItemsCount(player, BLACKSMITHS_FRAME) > 0)
                   && (getQuestItemsCount(player, STEEL) >= 5)
                   && (getQuestItemsCount(player, ADAMANTITE_NUGGET) >= 2)
                   && (getQuestItemsCount(player, SILVER_NUGGET) >= 10)
                   && (getQuestItemsCount(player, COKES) >= 10)) {
                 takeItems(player, SILVER_NUGGET, 10);
                 takeItems(player, ADAMANTITE_NUGGET, 2);
                 takeItems(player, COKES, 10);
                 takeItems(player, STEEL, 5);
                 takeItems(player, BLACKSMITHS_FRAME, 1);
                 giveItems(player, PENITENTS_MANACLES1, 1);
                 takeItems(player, MANUAL_OF_MANACLES, 1);
                 qs.setCond(15, true);
                 htmltext = "30300-01.html";
               } else {
                 htmltext = "30300-02.html";
               }
             } else if (hasAtLeastOneQuestItem(
                 player, PENITENTS_MANACLES1, PENITENTS_MANACLES, MANACLES_OF_PENITENT)) {
               htmltext = "30300-03.html";
             }
           }
           break;
         }
       case PIOTUR:
         {
           if (memoState == 2) {
             qs.setMemoState(6);
             qs.setCond(7, true);
             htmltext = "30597-01.html";
           } else if (memoState == 6) {
             if (getQuestItemsCount(player, TUREK_WAR_HOUNDS_TAIL) < 10) {
               htmltext = "30597-02.html";
             } else {
               takeItems(player, TUREK_WAR_HOUNDS_TAIL, -1);
               qs.setMemoState(10);
               qs.setCond(11, true);
               htmltext = "30597-03.html";
             }
           } else if (memoState == 10) {
             htmltext = "30597-04.html";
           }
           break;
         }
       case ELDER_CASIAN:
         {
           if (memoState == 3) {
             qs.setMemoState(7);
             qs.setCond(8, true);
             htmltext = "30612-01.html";
           } else if (memoState == 7) {
             if (!hasQuestItems(player, TYRANT_KINGPINS_HEART)) {
               htmltext = "30612-02.html";
             } else {
               takeItems(player, TYRANT_KINGPINS_HEART, -1);
               qs.setMemoState(11);
               qs.setCond(12, true);
               htmltext = "30612-03.html";
             }
           } else if (memoState == 11) {
             htmltext = "30612-04.html";
           }
           break;
         }
       case KATARI:
         {
           if (memoState == 1) {
             qs.setMemoState(5);
             qs.setCond(6, true);
             htmltext = "30668-01.html";
           } else if (memoState == 5) {
             if (getQuestItemsCount(player, RATMAN_SCAVENGERS_SKULL) < 10) {
               htmltext = "30668-02.html";
             } else {
               takeItems(player, RATMAN_SCAVENGERS_SKULL, -1);
               qs.setMemoState(9);
               qs.setCond(10, true);
               htmltext = "30668-03.html";
             }
           } else if (memoState == 9) {
             htmltext = "30668-04.html";
           }
           break;
         }
       case MAGISTER_JOAN:
         {
           if (memoState == 4) {
             qs.setMemoState(8);
             qs.setCond(9, true);
             htmltext = "30718-01.html";
           } else if (memoState == 8) {
             if (getQuestItemsCount(player, TRISALIM_TARANTULAS_VENOM_SAC) < 3) {
               htmltext = "30718-02.html";
             } else {
               takeItems(player, TRISALIM_TARANTULAS_VENOM_SAC, -1);
               qs.setMemoState(12);
               qs.setCond(13, true);
               htmltext = "30718-03.html";
             }
           } else if (memoState == 12) {
             htmltext = "30718-04.html";
           }
           break;
         }
     }
   }
   return htmltext;
 }
  @Override
  protected void readImpl() {
    L2PcInstance player = getClient().getActiveChar();
    if (player == null) {
      return;
    }
    int count = readD();
    if ((count < 1)
        || (count > Config.MAX_ITEM_IN_PACKET)
        || ((count * BATCH_LENGTH) != _buf.remaining())) {
      return;
    }

    _items = new Item[count];
    for (int i = 0; i < count; i++) {
      int itemId = readD();
      int enchantLevel = readD();

      long cnt = readQ();
      long price = readQ();

      if ((itemId < 1) || (cnt < 1) || (price < 0)) {
        _items = null;
        return;
      }
      int attackAttribute = readH(); // Attack Attribute Type
      int attackAttributeValue = readH(); // Attack Attribute Value
      int defenseAttributes[] = new int[6];
      for (int h = 0; h < 6; h++) {
        defenseAttributes[i] = readH(); // Defense attributes
      }
      int appearanceId = readD(); // Appearance ID
      boolean canUse = false;
      for (L2ItemInstance item : player.getInventory().getItemsByItemId(itemId)) {
        if ((enchantLevel == item.getEnchantLevel())
            && (attackAttribute == item.getAttackElementType())
            && (attackAttributeValue == item.getAttackElementPower())
            && (appearanceId == item.getVisualId())
            && (item.getElementDefAttr((byte) 0) == defenseAttributes[0])
            && (item.getElementDefAttr((byte) 1) == defenseAttributes[1])
            && (item.getElementDefAttr((byte) 2) == defenseAttributes[2])
            && (item.getElementDefAttr((byte) 3) == defenseAttributes[3])
            && (item.getElementDefAttr((byte) 4) == defenseAttributes[4])
            && (item.getElementDefAttr((byte) 5) == defenseAttributes[5])) {
          canUse = true;
          break;
        }
      }
      if (!canUse) {
        enchantLevel = 0;
        attackAttribute = -1;
        attackAttributeValue = 0;
        defenseAttributes[0] = 0;
        defenseAttributes[1] = 0;
        defenseAttributes[2] = 0;
        defenseAttributes[3] = 0;
        defenseAttributes[4] = 0;
        defenseAttributes[5] = 0;
        appearanceId = 0;
      }
      _items[i] =
          new Item(
              itemId,
              cnt,
              price,
              enchantLevel,
              attackAttribute,
              attackAttributeValue,
              defenseAttributes,
              appearanceId);
    }
  }
Пример #5
0
  @Override
  public void runImpl() {
    final L2PcInstance player = getClient().getActiveChar();
    if (player == null) {
      return;
    }

    if (!getClient().getFloodProtectors().getMultiSell().tryPerformAction("multisell choose")) {
      player.setMultiSell(null);
      return;
    }

    if ((_amount < 1) || (_amount > 5000)) {
      player.setMultiSell(null);
      return;
    }

    PreparedListContainer list = player.getMultiSell();
    if ((list == null) || (list.getListId() != _listId)) {
      player.setMultiSell(null);
      return;
    }

    final L2Npc npc = player.getLastFolkNPC();
    if (((npc != null) && !list.isNpcAllowed(npc.getId())) || ((npc == null) && list.isNpcOnly())) {
      player.setMultiSell(null);
      return;
    }

    if (!player.isGM() && (npc != null)) {
      if (!player.isInsideRadius(npc, INTERACTION_DISTANCE, true, false)
          || (player.getInstanceId() != npc.getInstanceId())) {
        player.setMultiSell(null);
        return;
      }
    }

    for (Entry entry : list.getEntries()) {
      if (entry.getEntryId() == _entryId) {
        if (!entry.isStackable() && (_amount > 1)) {
          _log.severe(
              "Character: "
                  + player.getName()
                  + " is trying to set amount > 1 on non-stackable multisell, id:"
                  + _listId
                  + ":"
                  + _entryId);
          player.setMultiSell(null);
          return;
        }

        final PcInventory inv = player.getInventory();

        int slots = 0;
        int weight = 0;
        for (Ingredient e : entry.getProducts()) {
          if (e.getItemId() < 0) {
            continue;
          }

          if (!e.isStackable()) {
            slots += e.getItemCount() * _amount;
          } else if (player.getInventory().getItemByItemId(e.getItemId()) == null) {
            slots++;
          }
          weight += e.getItemCount() * _amount * e.getWeight();
        }

        if (!inv.validateWeight(weight)) {
          player.sendPacket(SystemMessageId.WEIGHT_LIMIT_EXCEEDED);
          return;
        }

        if (!inv.validateCapacity(slots)) {
          player.sendPacket(SystemMessageId.SLOTS_FULL);
          return;
        }

        ArrayList<Ingredient> ingredientsList = new ArrayList<>(entry.getIngredients().size());
        // Generate a list of distinct ingredients and counts in order to check if the correct
        // item-counts
        // are possessed by the player
        boolean newIng;
        for (Ingredient e : entry.getIngredients()) {
          newIng = true;
          // at this point, the template has already been modified so that enchantments are properly
          // included
          // whenever they need to be applied. Uniqueness of items is thus judged by item id AND
          // enchantment level
          for (int i = ingredientsList.size(); --i >= 0; ) {
            Ingredient ex = ingredientsList.get(i);
            // if the item was already added in the list, merely increment the count
            // this happens if 1 list entry has the same ingredient twice (example 2 swords = 1
            // dual)
            if ((ex.getItemId() == e.getItemId())
                && (ex.getEnchantLevel() == e.getEnchantLevel())) {
              if ((ex.getItemCount() + e.getItemCount()) > Integer.MAX_VALUE) {
                player.sendPacket(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
                return;
              }
              // two same ingredients, merge into one and replace old
              final Ingredient ing = ex.getCopy();
              ing.setItemCount(ex.getItemCount() + e.getItemCount());
              ingredientsList.set(i, ing);
              newIng = false;
              break;
            }
          }
          if (newIng) {
            // if it's a new ingredient, just store its info directly (item id, count, enchantment)
            ingredientsList.add(e);
          }
        }

        // now check if the player has sufficient items in the inventory to cover the ingredients'
        // expences
        for (Ingredient e : ingredientsList) {
          if ((e.getItemCount() * _amount) > Integer.MAX_VALUE) {
            player.sendPacket(SystemMessageId.YOU_HAVE_EXCEEDED_QUANTITY_THAT_CAN_BE_INPUTTED);
            return;
          }
          if (e.getItemId() < 0) {
            if (!MultisellData.hasSpecialIngredient(
                e.getItemId(), e.getItemCount() * _amount, player)) {
              return;
            }
          } else {
            // if this is not a list that maintains enchantment, check the count of all items that
            // have the given id.
            // otherwise, check only the count of items with exactly the needed enchantment level
            final long required =
                ((Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMaintainIngredient())
                    ? (e.getItemCount() * _amount)
                    : e.getItemCount());
            if (inv.getInventoryItemCount(
                    e.getItemId(), list.getMaintainEnchantment() ? e.getEnchantLevel() : -1, false)
                < required) {
              SystemMessage sm =
                  SystemMessage.getSystemMessage(SystemMessageId.S2_UNIT_OF_THE_ITEM_S1_REQUIRED);
              sm.addItemName(e.getTemplate());
              sm.addLong(required);
              player.sendPacket(sm);
              return;
            }
          }
        }

        List<L2Augmentation> augmentation = new ArrayList<>();
        Elementals[] elemental = null;
        /** All ok, remove items and add final product */
        for (Ingredient e : entry.getIngredients()) {
          if (e.getItemId() < 0) {
            if (!MultisellData.takeSpecialIngredient(
                e.getItemId(), e.getItemCount() * _amount, player)) {
              return;
            }
          } else {
            L2ItemInstance itemToTake =
                inv.getItemByItemId(
                    e.getItemId()); // initialize and initial guess for the item to take.
            if (itemToTake
                == null) { // this is a cheat, transaction will be aborted and if any items already
                           // taken will not be returned back to inventory!
              _log.severe(
                  "Character: "
                      + player.getName()
                      + " is trying to cheat in multisell, id:"
                      + _listId
                      + ":"
                      + _entryId);
              player.setMultiSell(null);
              return;
            }

            // if (itemToTake.isEquipped())
            // {
            // this is a cheat, transaction will be aborted and if any items already taken will not
            // be returned back to inventory!
            // _log.severe("Character: " + player.getName() + " is trying to cheat in multisell,
            // exchanging equipped item, merchatnt id:" + merchant.getNpcId());
            // player.setMultiSell(null);
            // return;
            // }

            if (Config.ALT_BLACKSMITH_USE_RECIPES || !e.getMaintainIngredient()) {
              // if it's a stackable item, just reduce the amount from the first (only) instance
              // that is found in the inventory
              if (itemToTake.isStackable()) {
                if (!player.destroyItem(
                    "Multisell",
                    itemToTake.getObjectId(),
                    (e.getItemCount() * _amount),
                    player.getTarget(),
                    true)) {
                  player.setMultiSell(null);
                  return;
                }
              } else {
                // for non-stackable items, one of two scenaria are possible:
                // a) list maintains enchantment: get the instances that exactly match the requested
                // enchantment level
                // b) list does not maintain enchantment: get the instances with the LOWEST
                // enchantment level

                // a) if enchantment is maintained, then get a list of items that exactly match this
                // enchantment
                if (list.getMaintainEnchantment()) {
                  // loop through this list and remove (one by one) each item until the required
                  // amount is taken.
                  L2ItemInstance[] inventoryContents =
                      inv.getAllItemsByItemId(e.getItemId(), e.getEnchantLevel(), false);
                  for (int i = 0; i < (e.getItemCount() * _amount); i++) {
                    if (inventoryContents[i].isAugmented()) {
                      augmentation.add(inventoryContents[i].getAugmentation());
                    }
                    if (inventoryContents[i].getElementals() != null) {
                      elemental = inventoryContents[i].getElementals();
                    }
                    if (!player.destroyItem(
                        "Multisell",
                        inventoryContents[i].getObjectId(),
                        1,
                        player.getTarget(),
                        true)) {
                      player.setMultiSell(null);
                      return;
                    }
                  }
                } else
                // b) enchantment is not maintained. Get the instances with the LOWEST enchantment
                // level
                {
                  // NOTE: There are 2 ways to achieve the above goal.
                  // 1) Get all items that have the correct itemId, loop through them until the
                  // lowest enchantment
                  // level is found. Repeat all this for the next item until proper count of items
                  // is reached.
                  // 2) Get all items that have the correct itemId, sort them once based on
                  // enchantment level,
                  // and get the range of items that is necessary.
                  // Method 1 is faster for a small number of items to be exchanged.
                  // Method 2 is faster for large amounts.
                  //
                  // EXPLANATION:
                  // Worst case scenario for algorithm 1 will make it run in a number of cycles
                  // given by:
                  // m*(2n-m+1)/2 where m is the number of items to be exchanged and n is the total
                  // number of inventory items that have a matching id.
                  // With algorithm 2 (sort), sorting takes n*log(n) time and the choice is done in
                  // a single cycle
                  // for case b (just grab the m first items) or in linear time for case a (find the
                  // beginning of items
                  // with correct enchantment, index x, and take all items from x to x+m).
                  // Basically, whenever m > log(n) we have: m*(2n-m+1)/2 = (2nm-m*m+m)/2 >
                  // (2nlogn-logn*logn+logn)/2 = nlog(n) - log(n*n) + log(n) = nlog(n) + log(n/n*n)
                  // =
                  // nlog(n) + log(1/n) = nlog(n) - log(n) = (n-1)log(n)
                  // So for m < log(n) then m*(2n-m+1)/2 > (n-1)log(n) and m*(2n-m+1)/2 > nlog(n)
                  //
                  // IDEALLY:
                  // In order to best optimize the performance, choose which algorithm to run, based
                  // on whether 2^m > n
                  // if ( (2<<(e.getItemCount()// _amount)) < inventoryContents.length )
                  // // do Algorithm 1, no sorting
                  // else
                  // // do Algorithm 2, sorting
                  //
                  // CURRENT IMPLEMENTATION:
                  // In general, it is going to be very rare for a person to do a massive exchange
                  // of non-stackable items
                  // For this reason, we assume that algorithm 1 will always suffice and we keep
                  // things simple.
                  // If, in the future, it becomes necessary that we optimize, the above discussion
                  // should make it clear
                  // what optimization exactly is necessary (based on the comments under "IDEALLY").
                  //

                  // choice 1. Small number of items exchanged. No sorting.
                  for (int i = 1; i <= (e.getItemCount() * _amount); i++) {
                    L2ItemInstance[] inventoryContents =
                        inv.getAllItemsByItemId(e.getItemId(), false);

                    itemToTake = inventoryContents[0];
                    // get item with the LOWEST enchantment level from the inventory...
                    // +0 is lowest by default...
                    if (itemToTake.getEnchantLevel() > 0) {
                      for (L2ItemInstance item : inventoryContents) {
                        if (item.getEnchantLevel() < itemToTake.getEnchantLevel()) {
                          itemToTake = item;
                          // nothing will have enchantment less than 0. If a zero-enchanted
                          // item is found, just take it
                          if (itemToTake.getEnchantLevel() == 0) {
                            break;
                          }
                        }
                      }
                    }
                    if (!player.destroyItem(
                        "Multisell", itemToTake.getObjectId(), 1, player.getTarget(), true)) {
                      player.setMultiSell(null);
                      return;
                    }
                  }
                }
              }
            }
          }
        }

        // Generate the appropriate items
        for (Ingredient e : entry.getProducts()) {
          if (e.getItemId() < 0) {
            MultisellData.giveSpecialProduct(e.getItemId(), e.getItemCount() * _amount, player);
          } else {
            if (e.isStackable()) {
              inv.addItem(
                  "Multisell",
                  e.getItemId(),
                  e.getItemCount() * _amount,
                  player,
                  player.getTarget());
            } else {
              L2ItemInstance product = null;
              for (int i = 0; i < (e.getItemCount() * _amount); i++) {
                product = inv.addItem("Multisell", e.getItemId(), 1, player, player.getTarget());
                if ((product != null) && list.getMaintainEnchantment()) {
                  if (i < augmentation.size()) {
                    product.setAugmentation(
                        new L2Augmentation(augmentation.get(i).getAugmentationId()));
                  }
                  if (elemental != null) {
                    for (Elementals elm : elemental) {
                      product.setElementAttr(elm.getElement(), elm.getValue());
                    }
                  }
                  product.setEnchantLevel(e.getEnchantLevel());
                  product.updateDatabase();
                }
              }
            }
            // msg part
            SystemMessage sm;

            if ((e.getItemCount() * _amount) > 1) {
              sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
              sm.addItemName(e.getItemId());
              sm.addLong(e.getItemCount() * _amount);
              player.sendPacket(sm);
            } else {
              if (list.getMaintainEnchantment() && (e.getEnchantLevel() > 0)) {
                sm = SystemMessage.getSystemMessage(SystemMessageId.ACQUIRED_S1_S2);
                sm.addLong(e.getEnchantLevel());
                sm.addItemName(e.getItemId());
              } else {
                sm = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
                sm.addItemName(e.getItemId());
              }
              player.sendPacket(sm);
            }
          }
        }
        player.sendPacket(new ItemList(player, false));

        StatusUpdate su = new StatusUpdate(player);
        su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
        player.sendPacket(su);

        // finally, give the tax to the castle...
        if ((npc != null) && (entry.getTaxAmount() > 0)) {
          npc.getCastle().addToTreasury(entry.getTaxAmount() * _amount);
        }
        break;
      }
    }
  }
  private void setEnchant(L2PcInstance activeChar, int ench, int armorType) {
    // get the target
    L2Object target = activeChar.getTarget();
    if (target == null) {
      target = activeChar;
    }
    L2PcInstance player = null;
    if (target instanceof L2PcInstance) {
      player = (L2PcInstance) target;
    } else {
      activeChar.sendPacket(SystemMessageId.INCORRECT_TARGET);
      return;
    }

    // now we need to find the equipped weapon of the targeted character...
    int curEnchant = 0; // display purposes only
    L2ItemInstance itemInstance = null;

    // only attempt to enchant if there is a weapon equipped
    L2ItemInstance parmorInstance = player.getInventory().getPaperdollItem(armorType);
    if ((parmorInstance != null) && (parmorInstance.getLocationSlot() == armorType)) {
      itemInstance = parmorInstance;
    }

    if (itemInstance != null) {
      curEnchant = itemInstance.getEnchantLevel();

      // set enchant value
      player.getInventory().unEquipItemInSlot(armorType);
      itemInstance.setEnchantLevel(ench);
      player.getInventory().equipItem(itemInstance);

      // send packets
      InventoryUpdate iu = new InventoryUpdate();
      iu.addModifiedItem(itemInstance);
      player.sendPacket(iu);
      player.broadcastPacket(new CharInfo(player));
      player.sendPacket(new UserInfo(player));
      player.broadcastPacket(new ExBrExtraUserInfo(player));

      // informations
      activeChar.sendMessage(
          "Changed enchantment of "
              + player.getName()
              + "'s "
              + itemInstance.getItem().getName()
              + " from "
              + curEnchant
              + " to "
              + ench
              + ".");
      player.sendMessage(
          "Admin has changed the enchantment of your "
              + itemInstance.getItem().getName()
              + " from "
              + curEnchant
              + " to "
              + ench
              + ".");
    }
  }
  @Override
  protected void runImpl() {
    L2PcInstance activeChar = getClient().getActiveChar();

    if (activeChar == null) {
      _log.fine("RequestCrystalizeItem: activeChar was null");
      return;
    }

    if (!getClient().getFloodProtectors().getTransaction().tryPerformAction("crystallize")) {
      activeChar.sendMessage("You are crystallizing too fast.");
      return;
    }

    if (_count <= 0) {
      Util.handleIllegalPlayerAction(
          activeChar,
          "[RequestCrystallizeItem] count <= 0! ban! oid: "
              + _objectId
              + " owner: "
              + activeChar.getName(),
          Config.DEFAULT_PUNISH);
      return;
    }

    if ((activeChar.getPrivateStoreType() != PrivateStoreType.NONE)
        || !activeChar.isInCrystallize()) {
      activeChar.sendPacket(
          SystemMessageId
              .WHILE_OPERATING_A_PRIVATE_STORE_OR_WORKSHOP_YOU_CANNOT_DISCARD_DESTROY_OR_TRADE_AN_ITEM);
      return;
    }

    int skillLevel = activeChar.getSkillLevel(CommonSkill.CRYSTALLIZE.getId());
    if (skillLevel <= 0) {
      activeChar.sendPacket(
          SystemMessageId
              .YOU_MAY_NOT_CRYSTALLIZE_THIS_ITEM_YOUR_CRYSTALLIZATION_SKILL_LEVEL_IS_TOO_LOW);
      activeChar.sendPacket(ActionFailed.STATIC_PACKET);
      if ((activeChar.getRace() != Race.DWARF)
          && (activeChar.getClassId().ordinal() != 117)
          && (activeChar.getClassId().ordinal() != 55)) {
        _log.info(
            "Player "
                + activeChar.getClient()
                + " used crystalize with classid: "
                + activeChar.getClassId().ordinal());
      }
      return;
    }

    PcInventory inventory = activeChar.getInventory();
    if (inventory != null) {
      L2ItemInstance item = inventory.getItemByObjectId(_objectId);
      if (item == null) {
        activeChar.sendPacket(ActionFailed.STATIC_PACKET);
        return;
      }

      if (item.isHeroItem()) {
        return;
      }

      if (_count > item.getCount()) {
        _count = activeChar.getInventory().getItemByObjectId(_objectId).getCount();
      }
    }

    final L2ItemInstance itemToRemove = activeChar.getInventory().getItemByObjectId(_objectId);
    if ((itemToRemove == null) || itemToRemove.isShadowItem() || itemToRemove.isTimeLimitedItem()) {
      return;
    }

    if (!itemToRemove.getItem().isCrystallizable()
        || (itemToRemove.getItem().getCrystalCount() <= 0)
        || (itemToRemove.getItem().getCrystalType() == CrystalType.NONE)) {
      _log.warning(
          activeChar.getName()
              + " ("
              + activeChar.getObjectId()
              + ") tried to crystallize "
              + itemToRemove.getItem().getId());
      return;
    }

    if (!activeChar.getInventory().canManipulateWithItemId(itemToRemove.getId())) {
      activeChar.sendMessage("You cannot use this item.");
      return;
    }

    // Check if the char can crystallize items and return if false;
    boolean canCrystallize = true;

    switch (itemToRemove.getItem().getCrystalTypePlus()) {
      case D:
        {
          if (skillLevel < 1) {
            canCrystallize = false;
          }
          break;
        }
      case C:
        {
          if (skillLevel < 2) {
            canCrystallize = false;
          }
          break;
        }
      case B:
        {
          if (skillLevel < 3) {
            canCrystallize = false;
          }
          break;
        }
      case A:
        {
          if (skillLevel < 4) {
            canCrystallize = false;
          }
          break;
        }
      case S:
        {
          if (skillLevel < 5) {
            canCrystallize = false;
          }
          break;
        }
      case R:
        {
          if (skillLevel < 6) {
            canCrystallize = false;
          }
          break;
        }
    }

    if (!canCrystallize) {
      activeChar.sendPacket(
          SystemMessageId
              .YOU_MAY_NOT_CRYSTALLIZE_THIS_ITEM_YOUR_CRYSTALLIZATION_SKILL_LEVEL_IS_TOO_LOW);
      activeChar.sendPacket(ActionFailed.STATIC_PACKET);
      return;
    }

    // activeChar.setInCrystallize(true);

    // unequip if needed
    SystemMessage sm;
    if (itemToRemove.isEquipped()) {
      L2ItemInstance[] unequiped =
          activeChar.getInventory().unEquipItemInSlotAndRecord(itemToRemove.getLocationSlot());
      InventoryUpdate iu = new InventoryUpdate();
      for (L2ItemInstance item : unequiped) {
        iu.addModifiedItem(item);
      }
      activeChar.sendPacket(iu);

      if (itemToRemove.getEnchantLevel() > 0) {
        sm = SystemMessage.getSystemMessage(SystemMessageId.THE_EQUIPMENT_S1_S2_HAS_BEEN_REMOVED);
        sm.addInt(itemToRemove.getEnchantLevel());
        sm.addItemName(itemToRemove);
      } else {
        sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_BEEN_UNEQUIPPED);
        sm.addItemName(itemToRemove);
      }
      activeChar.sendPacket(sm);
    }

    // remove from inventory
    final L2ItemInstance removedItem =
        activeChar.getInventory().destroyItem("Crystalize", _objectId, _count, activeChar, null);

    final InventoryUpdate iu = new InventoryUpdate();
    iu.addRemovedItem(removedItem);
    activeChar.sendPacket(iu);

    final int crystalId = itemToRemove.getItem().getCrystalItemId();
    final int crystalAmount = itemToRemove.getCrystalCount();

    final List<ItemChanceHolder> items = new ArrayList<>();
    items.add(new ItemChanceHolder(crystalId, 100, crystalAmount));

    final CrystalizationData data =
        ItemCrystalizationData.getInstance().getCrystalization(itemToRemove.getId());
    if (data != null) {
      data.getItems().stream().filter(holder -> (holder.getId() != crystalId)).forEach(items::add);
    }

    for (ItemChanceHolder holder : items) {
      final double rand = Rnd.nextDouble() * 100;
      if (rand < holder.getChance()) {
        // add crystals
        final L2ItemInstance createdItem =
            activeChar
                .getInventory()
                .addItem("Crystalize", holder.getId(), holder.getCount(), activeChar, activeChar);

        sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_HAVE_EARNED_S2_S1_S);
        sm.addItemName(createdItem);
        sm.addLong(holder.getCount());
        activeChar.sendPacket(sm);
      }
    }

    sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_BEEN_CRYSTALLIZED);
    sm.addItemName(removedItem);
    activeChar.sendPacket(sm);

    activeChar.broadcastUserInfo();

    L2World.getInstance().removeObject(removedItem);

    activeChar.setInCrystallize(false);
  }