diff --git a/doc/script_commands.txt b/doc/script_commands.txt index 601efe901b..31110cfd8a 100644 --- a/doc/script_commands.txt +++ b/doc/script_commands.txt @@ -2903,6 +2903,7 @@ invoking character has in their inventory, including all the data needed to recreate these items perfectly if they are destroyed. Here's what you get: @inventorylist_id[] - array of item ids. +@inventorylist_idx[] - array of item inventory index. @inventorylist_amount[] - their corresponding item amounts. @inventorylist_equip[] - on which position the item is equipped (see EQP_* constants) It will contain 0 if the item is not equipped. @@ -4997,6 +4998,30 @@ See 'getitem2' for an explanation of the expanded parameters. --------------------------------------- +*delitemidx {,{,}} + +This command will remove an item at the given inventory index. + +If is not specified, this will remove all of the items at the specified index. + +The only way to get the inventory index is by using 'getinventorylist()'. After deleting +an item at the given index, that index can remain empty until the player relogs, requiring +'getinventorylist()' to be called again. If an item is deleted with an invalid index, the +script will terminate with an error. + +This command returns true on success and false if the item at the given index could not be deleted or if +not enough items were available at the given index. + +Example: + + // This will remove all Red Potions from player's inventory + getinventorylist(); + for (.@i = 0; .@i < @inventorylist_count; ++.@i) + if (@inventorylist_id[.@i] == 501) + delitemidx @inventorylist_idx[.@i]; + +--------------------------------------- + *cartdelitem2 ,,,,,,,,{,}; *cartdelitem2 "",,,,,,,,{,}; *storagedelitem2 ,,,,,,,,{,}; diff --git a/src/map/script.cpp b/src/map/script.cpp index 0d0b43fdb2..6a4f4e41a6 100644 --- a/src/map/script.cpp +++ b/src/map/script.cpp @@ -8437,6 +8437,43 @@ BUILDIN_FUNC(delitem2) return SCRIPT_CMD_FAILURE; } +/// Deletes items from the target/attached player at given index. +/// delitemidx {,{,}}; +BUILDIN_FUNC(delitemidx) { + struct map_session_data* sd; + + if (!script_charid2sd(4, sd)) { + script_pushint(st, false); + return SCRIPT_CMD_FAILURE; + } + + int idx = script_getnum(st, 2); + if (idx < 0 || idx >= MAX_INVENTORY) { + ShowWarning("buildin_delitemidx: Index %d is out of the range 0-%d.\n", idx, MAX_INVENTORY - 1); + script_pushint(st, false); + return SCRIPT_CMD_FAILURE; + } + + if (sd->inventory_data[idx] == nullptr) { + ShowWarning("buildin_delitemidx: No item can be deleted from index %d of player %s (AID: %u, CID: %u).\n", idx, sd->status.name, sd->status.account_id, sd->status.char_id); + script_pushint(st, false); + return SCRIPT_CMD_FAILURE; + } + + int amount; + if (script_hasdata(st, 3)) + amount = script_getnum(st, 3); + else + amount = sd->inventory.u.items_inventory[idx].amount; + + if (amount > 0) + script_pushint(st, pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_SCRIPT) == 0); + else + script_pushint(st, false); + + return SCRIPT_CMD_SUCCESS; +} + /*========================================== * Enables/Disables use of items while in an NPC [Skotlex] *------------------------------------------*/ @@ -14232,6 +14269,7 @@ BUILDIN_FUNC(getinventorylist) for(i=0;iinventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0){ pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->inventory.u.items_inventory[i].nameid); + pc_setreg(sd,reference_uid(add_str("@inventorylist_idx"), j),i); pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->inventory.u.items_inventory[i].amount); pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->inventory.u.items_inventory[i].equip); pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->inventory.u.items_inventory[i].refine); @@ -25220,6 +25258,7 @@ struct script_function buildin_func[] = { BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"), BUILDIN_DEF2(delitem2,"guildstoragedelitem2","viiiiiiii?"), BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"), + BUILDIN_DEF(delitemidx,"i??"), BUILDIN_DEF2(enableitemuse,"enable_items",""), BUILDIN_DEF2(disableitemuse,"disable_items",""), BUILDIN_DEF(cutin,"si"),