public void moveItem( Player player, int itemObjId, int sourceStorageType, int destinationStorageType, int slot) { Storage sourceStorage = player.getStorage(sourceStorageType); Item item = player.getStorage(sourceStorageType).getItemByObjId(itemObjId); if (item == null) return; item.setEquipmentSlot(slot); if (sourceStorageType == destinationStorageType) return; Storage destinationStorage = player.getStorage(destinationStorageType); List<Item> existingItems = destinationStorage.getItemsByItemId(item.getItemTemplate().getTemplateId()); int count = item.getItemCount(); int maxStackCount = item.getItemTemplate().getMaxStackCount(); for (Item existingItem : existingItems) { if (count == 0) break; int freeCount = maxStackCount - existingItem.getItemCount(); if (count <= freeCount) { existingItem.increaseItemCount(count); count = 0; sendDeleteItemPacket(player, sourceStorageType, item.getObjectId()); sourceStorage.removeFromBag(item, true); } else { existingItem.increaseItemCount(freeCount); count -= freeCount; } sendStorageUpdatePacket(player, destinationStorageType, existingItem); } while (!destinationStorage.isFull() && count > 0) { // item count still more than maxStack value if (count > maxStackCount) { count -= maxStackCount; Item newitem = newItem(item.getItemTemplate().getTemplateId(), maxStackCount); newitem = destinationStorage.putToBag(newitem); sendStorageUpdatePacket(player, destinationStorageType, newitem); } else { item.setItemCount(count); sourceStorage.removeFromBag(item, false); sendDeleteItemPacket(player, sourceStorageType, item.getObjectId()); Item newitem = destinationStorage.putToBag(item); sendStorageUpdatePacket(player, destinationStorageType, newitem); count = 0; } } if (count > 0) // if storage is full and some items left { item.setItemCount(count); sendUpdateItemPacket(player, sourceStorageType, item); } }
/** * Adds item count to player inventory I moved this method to service cause right implementation * of it is critical to server operation and could cause starvation of object ids. * * <p>This packet will send necessary packets to client (initialize used only from quest engine * * @param player * @param itemId * @param count - amount of item that were not added to player's inventory */ public int addItem(Player player, int itemId, int count, boolean isQuestItem) { Storage inventory = player.getInventory(); ItemTemplate itemTemplate = DataManager.ITEM_DATA.getItemTemplate(itemId); if (itemTemplate == null) return count; int maxStackCount = itemTemplate.getMaxStackCount(); if (itemId == ItemId.KINAH.value()) { inventory.increaseKinah(count); return 0; } else { /** Increase count of existing items */ List<Item> existingItems = inventory.getAllItemsByItemId( itemId); // look for existing in equipment. need for power shards. for (Item existingItem : existingItems) { if (count == 0) break; int freeCount = maxStackCount - existingItem.getItemCount(); if (count <= freeCount) { existingItem.increaseItemCount(count); count = 0; } else { existingItem.increaseItemCount(freeCount); count -= freeCount; } updateItem(player, existingItem, false); } /** Create new stacks */ while (!inventory.isFull() && count > 0) { // item count still more than maxStack value if (count > maxStackCount) { Item item = newItem(itemId, maxStackCount); item.setQuest(isQuestItem); count -= maxStackCount; inventory.putToBag(item); updateItem(player, item, true); } else { Item item = newItem(itemId, count); item.setQuest(isQuestItem); inventory.putToBag(item); updateItem(player, item, true); count = 0; } } return count; } }
/** * Used to merge 2 items in inventory * * @param player * @param sourceItemObjId * @param itemAmount * @param destinationObjId */ public void mergeItems( Player player, int sourceItemObjId, int itemAmount, int destinationObjId, int sourceStorageType, int destinationStorageType) { if (itemAmount == 0) return; Storage sourceStorage = player.getStorage(sourceStorageType); Storage destinationStorage = player.getStorage(destinationStorageType); Item sourceItem = sourceStorage.getItemByObjId(sourceItemObjId); Item destinationItem = destinationStorage.getItemByObjId(destinationObjId); if (sourceItem == null || destinationItem == null) return; // Invalid object id provided if (sourceItem.getItemTemplate().getTemplateId() != destinationItem.getItemTemplate().getTemplateId()) return; // Invalid item type if (sourceItem.getItemCount() < itemAmount) return; // Invalid item amount if (sourceItem.getItemCount() == itemAmount) { destinationItem.increaseItemCount(itemAmount); sourceStorage.removeFromBag(sourceItem, true); sendDeleteItemPacket(player, sourceStorageType, sourceItem.getObjectId()); sendUpdateItemPacket(player, destinationStorageType, destinationItem); } else if (sourceItem.getItemCount() > itemAmount) { sourceItem.decreaseItemCount(itemAmount); destinationItem.increaseItemCount(itemAmount); sendUpdateItemPacket(player, sourceStorageType, sourceItem); sendUpdateItemPacket(player, destinationStorageType, destinationItem); } else return; // cant happen in theory, but... }