Storage
A script forgets everything when it ends. Python variables are gone with the process,
and the Lua names from run() are gone with the connection. lt.storage
is the exception: what you write here is stored in the world and is still there the next
time you run your script, and after the server was restarted.
It is a dictionary, so there is nothing new to learn - in, len(), for,
.get(), .pop(), .update() and del behave the way you expect.
- Example:
>>> lt.storage["home"] = "10,20,30" >>> lt.storage["home"] '10,20,30' >>> "home" in lt.storage True >>> del lt.storage["home"]
Keys and values are always strings, because that is all Luanti stores. Miney does not
hide that: a number raises a TypeError rather than coming back as text you did
not write. Use str for single values and json for lists and dictionaries.
Important
There is one store per world, shared by every Miney script connected to it - it is a noticeboard, not a private drawer. Give your keys a prefix if more than one project uses the same world.
The keys belong to Miney alone: Luanti files them under the mod that wrote them, so no other mod on the server sees them, and you never collide with one. That is a namespace and not a secret - the store is a plain file in the world directory.
- class miney.Storage(luanti: Luanti)[source]
The world’s key-value store, used like a dictionary.
Everything a script does is gone when it ends: variables live in Python, and even the Lua names from
run()disappear with the connection. This is the exception. What you put here is written into the world and is still there for the next run, and after a server restart:>>> lt.storage["home"] = "10,20,30" >>> lt.storage["home"] '10,20,30'
It behaves like a
dict-in,len(),for,.get(),.pop(),.update()anddelall work:>>> "home" in lt.storage True >>> for key, value in lt.storage.items(): ... print(key, value) home 10,20,30 >>> del lt.storage["home"]
Keys and values are strings, both of them. Luanti writes text and nothing else - its
set_intandset_floatonly convert to text on the way in, and what comes back out is a string either way. So Miney does not pretend otherwise: assigning a number raises aTypeErrorinstead of silently handing back"7"where you wrote7. Convert on the way in and out:>>> lt.storage["visits"] = str(41 + 1) >>> int(lt.storage["visits"]) 42
For anything with a shape, use
json:>>> import json >>> lt.storage["seen"] = json.dumps({"players": ["Steve", "Alex"]}) >>> json.loads(lt.storage["seen"])["players"] ['Steve', 'Alex']
There is one store per world, not one per player, and it is not private: every Miney script connecting to that world reads and writes the same keys. Prefix what belongs to one project. No other mod on the server sees them though - Luanti files every key under the mod that wrote it, so nothing here can collide with a game’s own data.
You do not create this class yourself, it is reached through
storage.- __delitem__(key: str) None[source]
- Parameters:
key – The key to remove.
- Raises:
KeyError – If the key is not in the store.
- __getitem__(key: str) str[source]
- Parameters:
key – The key to look up.
- Returns:
The stored text.
- Raises:
KeyError – If the key is not in the store.
- __setitem__(key: str, value: str) None[source]
- Parameters:
key – The key to store under.
value – The text to store.
- Raises:
TypeError – If the key or the value is not a string.
ValueError – If the key or the value is empty.
- clear() None[source]
Remove everything from the store.
Affects every script using this world, not only yours.
- items() ItemsView[str, str][source]
- Returns:
All key-value pairs, read in a single call to the server.
- values() ValuesView[str][source]
- Returns:
All values, read in a single call to the server.