public void switchStoragesItems( Player player, int sourceStorageType, int sourceItemObjId, int replaceStorageType, int replaceItemObjId) { Storage sourceStorage = player.getStorage(sourceStorageType); Storage replaceStorage = player.getStorage(replaceStorageType); Item sourceItem = sourceStorage.getItemByObjId(sourceItemObjId); if (sourceItem == null) return; Item replaceItem = replaceStorage.getItemByObjId(replaceItemObjId); if (replaceItem == null) return; int sourceSlot = sourceItem.getEquipmentSlot(); int replaceSlot = replaceItem.getEquipmentSlot(); sourceItem.setEquipmentSlot(replaceSlot); replaceItem.setEquipmentSlot(sourceSlot); sourceStorage.removeFromBag(sourceItem, false); replaceStorage.removeFromBag(replaceItem, false); Item newSourceItem = sourceStorage.putToBag(replaceItem); Item newReplaceItem = replaceStorage.putToBag(sourceItem); sendDeleteItemPacket(player, sourceStorageType, sourceItemObjId); sendStorageUpdatePacket(player, sourceStorageType, newSourceItem); sendDeleteItemPacket(player, replaceStorageType, replaceItemObjId); sendStorageUpdatePacket(player, replaceStorageType, newReplaceItem); }
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); } }
/** * This method is used for creating new players * * @param playerCommonData * @param playerAppearance * @return Player */ public static Player newPlayer( PlayerCommonData playerCommonData, PlayerAppearance playerAppearance) { PlayerInitialData playerInitialData = DataManager.PLAYER_INITIAL_DATA; LocationData ld = playerInitialData.getSpawnLocation(playerCommonData.getRace()); WorldPosition position = World.getInstance() .createPosition(ld.getMapId(), ld.getX(), ld.getY(), ld.getZ(), ld.getHeading()); playerCommonData.setPosition(position); Player newPlayer = new Player(new PlayerController(), playerCommonData, playerAppearance); // Starting skills SkillLearnService.addNewSkills(newPlayer, true); // Starting items PlayerCreationData playerCreationData = playerInitialData.getPlayerCreationData(playerCommonData.getPlayerClass()); List<ItemType> items = playerCreationData.getItems(); Storage playerInventory = new Storage(StorageType.CUBE); Storage regularWarehouse = new Storage(StorageType.REGULAR_WAREHOUSE); Storage accountWarehouse = new Storage(StorageType.ACCOUNT_WAREHOUSE); Equipment equipment = new Equipment(newPlayer); newPlayer.setStorage(playerInventory, StorageType.CUBE); newPlayer.setStorage(regularWarehouse, StorageType.REGULAR_WAREHOUSE); newPlayer.setStorage(accountWarehouse, StorageType.ACCOUNT_WAREHOUSE); newPlayer.setEquipment(equipment); newPlayer.setMailbox(new Mailbox()); for (ItemType itemType : items) { int itemId = itemType.getTemplate().getTemplateId(); Item item = ItemService.newItem(itemId, itemType.getCount()); if (item == null) continue; // When creating new player - all equipment that has slot values will be equipped // Make sure you will not put into xml file more items than possible to equip. ItemTemplate itemTemplate = item.getItemTemplate(); item.setOwnerId(newPlayer.getObjectId()); if (itemTemplate.isArmor() || itemTemplate.isWeapon()) { item.setEquipped(true); List<ItemSlot> itemSlots = ItemSlot.getSlotsFor(itemTemplate.getItemSlot()); item.setEquipmentSlot(itemSlots.get(0).getSlotIdMask()); equipment.onLoadHandler(item); } else ItemService.onLoadHandler(newPlayer, newPlayer.getInventory(), item); } equipment.onLoadApplyEquipmentStats(); /** Mark inventory and equipment as UPDATE_REQUIRED to be saved during character creation */ playerInventory.setPersistentState(PersistentState.UPDATE_REQUIRED); equipment.setPersistentState(PersistentState.UPDATE_REQUIRED); return newPlayer; }
/** * Used to split item into 2 items * * @param player * @param itemObjId * @param splitAmount * @param slotNum * @param sourceStorageType * @param desetinationStorageType */ public void splitItem( Player player, int itemObjId, int splitAmount, int slotNum, int sourceStorageType, int destinationStorageType) { Storage sourceStorage = player.getStorage(sourceStorageType); Storage destinationStorage = player.getStorage(destinationStorageType); Item itemToSplit = sourceStorage.getItemByObjId(itemObjId); if (itemToSplit == null) { itemToSplit = sourceStorage.getKinahItem(); if (itemToSplit.getObjectId() != itemObjId || itemToSplit == null) { log.warn( String.format( "CHECKPOINT: attempt to split null item %d %d %d", itemObjId, splitAmount, slotNum)); return; } } // To move kinah from inventory to warehouse and vise versa client using split item packet if (itemToSplit.getItemTemplate().isKinah()) { moveKinah(player, sourceStorage, splitAmount); return; } int oldItemCount = itemToSplit.getItemCount() - splitAmount; if (itemToSplit.getItemCount() < splitAmount || oldItemCount == 0) return; Item newItem = newItem(itemToSplit.getItemTemplate().getTemplateId(), splitAmount); newItem.setEquipmentSlot(slotNum); if (destinationStorage.putToBag(newItem) != null) { itemToSplit.decreaseItemCount(splitAmount); List<Item> itemsToUpdate = new ArrayList<Item>(); itemsToUpdate.add(newItem); sendStorageUpdatePacket(player, destinationStorageType, itemsToUpdate.get(0)); sendUpdateItemPacket(player, sourceStorageType, itemToSplit); } else { releaseItemId(newItem); } }