[mod] Usefull routines for inventory manipulation [invutils]

Post Reply
User avatar
aldobr
Member
Posts: 316
Joined: Sun Nov 25, 2012 05:46

[mod] Usefull routines for inventory manipulation [invutils]

by aldobr » Post

Author: J. Aldo (aldobr)
License: BSD
Description: This mod adds usefull routines for easier inventory manipulation.

Functions:

invutils.fullstacks(count): Returns the number of full stacks that would be used to store the quantity count.

Ex.:

local stackcount = invutils.fullstacks(800)

invutils.remstack(count): Returns the size of the last stack needed to store a certain quantity count (ignoring full stacks).

Ex.:

local remainder = invutils.remstack(800)

invutils.findstackof(inv, name): Will find name in the inventory inv and return its stack or nil.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local stackofmese = invutils.findstackof(inv, "default:mese") -- Get any stack of mese

invutils.getstackofcount(inv, name): Will return the amount of name in all stacks at inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local mesecount = invutils.getstackofcount(inv, "default:mese") -- how much mese do we have here ?

invutils.setstackcount(inv, name, count): Will make n stacks at inventory inv so that they together makes up for count items of the item name. It will take into account already existing items in the inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.setstackcount(inv, "default:mese", 9 * 99) -- guarantee that inv has nine stacks of mese

invutils.getnonemptystackscount(inv): Return the number of non-empty stacks at inventory inv

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local freeslots = invutils.getnonemptystackscount(inv)
.
invutils.clearstacksof(inv, name): Will remove all stacks of name name from inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.clearstacksof(inv, "default:mese") -- inv now does not have any mese at all

invutils.nameofallitems(inv): Returns the name of all items present at stack inv that are not tools

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local names = invutils.nameofallitems(inv)
for key, _ in pairs(names) do
print(key) -- prints the names of all items that are stored at inventory inv (does not include tools and other items that can wear
end

invutils.numberofstacksneeded(count): Returns the number of stacks needed to store a certain quantity

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local freeslots = invutils.getnonemptystackscount(inv)
local numstacks = invutils.numberofstacksneeded(800)
if numstacks > freslots then
print("error, the target inventory cannot hold that many items")
end

invutils.convertstacksof(inv, name1, name2, prop): Will turn all stacks of name1 into stacks of name2, with proportion prop (prop to 1). Eg.: Automatically turns steel ingots into steel blocks).

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.convertstacksof(inv, "default:gold_ingot", "default:gold_block", 9) -- Converts all gold ingots into blocks

invutils.addblockofingot(name1, name2, prop): Add a new block x ingot relationship (name1 = name2 * prop)

Ex.:

invutils.addblockofingot("default:gold_ingot", "default:gold_block", 9) -- nine gold ingots equals one gold block
invutils.addblockofingot("default:steel_ingot", "default:steel_block", 9) -- nine steel ingots equals one steel block

invutils.convertallingotstacks(inv): Automatically converts (at inventory inv) all ingots that match the list found in blocks list.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.convertallingotstacks(inv) -- Now all ingots are compressed into blocks at inventory inv (data taken from previous calls to addblockofingot).

invutils.compressstacksof(inv, name): Automatically compress all stacks of item name at inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.compressstacksof(inv, "default:mese") -- Now all mese will be stored as full stacks (ie, if you have three stacks of 50 mese, it will become a single stack of 99 mese plus a stack containning only 51 mese, occupying only two slots instead of three)

invutils.compressstacks(inv): Automatically compress all stacks at inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
invutils.compressstacks(inv) -- Same as compressstacksof but works on all nome tool stacks

InventoryTable(sourceinv): Returns a InventoryTable (A inventory table that only exists in memmory), if sourceinv is provided, loads data from there.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table

Functions in inventorytableref

loadfrominventory(aThis, inv): Loads an inventory from inv into a table.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(nil)
myinvtable:loadfrominventory(inv) -- loads all items from inventory into a memory table

savetoinventory(aThis, inv): Saves the table into an inventory inv.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(nil)
myinvtable:loadfrominventory(inv) -- loads all items from inventory into a memory table
myinvtable:savetoinventory(inv) -- saves all items back into inventory

getitemcount(aThis, name): Gets the number of items name in this inventory table.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table
print(myinvtable:getitemcount("default:mese")) -- prints how much mese is stored at that table

setitemcount(aThis, name, value): Sets the number of items name in this inventory table.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table
myinvtable:setitemcount("default:mese", 999) -- stores 999 mese at the table

clearitem(aThis, name): Deletes a name item stack from this inventory table.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table
myinvtable:clearitem("default:mese") -- kills all mese that might be contained in the table

containstool(aThis, name): Check if tool exists.

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table
if myinvtable:containstool("default:steel_pickaxe") then
print("inventory contains at least a pickaxe")
end

convertstacksof(aThis, name1, name2, prop): Will turn all stacks of name1 into stacks of name2, with proportion prop (prop to 1). (Eg.: Automatically turns steel ingots into steel blocks).

Ex.:

local meta = minetest.env:get_meta({x=0, y=0, z=0})
local inv = meta:get_inventory()
local myinvtable = InventoryTable(inv) -- loads all items from inventory into a memory table
myinvtable:convertstacksof("default:gold_ingot", "default:gold_block", 9) -- now most gold ingots will become gold blocks

InventoryTable is a table that temporarely holds all that containned in an inventory. Its usefull to move inventories around and do other tricks.
Attachments
invutils.zip
(2.25 KiB) Downloaded 114 times

User avatar
aldobr
Member
Posts: 316
Joined: Sun Nov 25, 2012 05:46

Re: [mod] Usefull routines for inventory manipulation [invut

by aldobr » Post

This mod is broken, pending a complete overhaul due to how minetest deals with tools vs. items.

User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: [mod] Usefull routines for inventory manipulation [invut

by Krock » Post

Code: Select all

minetest.env:get_meta
.. is outdated, please use the following instead:

Code: Select all

minetest.get_meta
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests