Persistent variables
persist coins = {}
on block.break:
coins[player.uuid] = (coins[player.uuid] or 0) + 1
A persist variable is loaded when the script starts and saved automatically (every minute by default) and on shutdown. The value after = is only used the very first time. Changes inside lists and maps are noticed too.
Shared storage
persist variables belong to one script. The storage module is a key/value store shared by all scripts:
import storage
storage.set("maintenance", true)
if storage.get("maintenance", false):
print("maintenance mode")
storage.add("total_joins", 1)
print(storage.keys("shop."))
By default data is written to plugins/Flint/data/*.json. Set storage.type=database in config.properties to keep it in a flint_data table instead. Point several servers and the proxy at the same database to share it across the network; use storage.fetch(key) to read the latest value another server wrote.
Files
The files module reads and writes text and JSON in plugins/Flint/files/. It never blocks the server.
import files
files.append("logs/chat.txt", "{player.name}: {message}")
reports = files.read_json("reports.json", [])
reports.add({by: sender.name, reason: reason})
files.write_json("reports.json", reports)
Scripts cannot reach files outside that folder.
SQL databases
Configure a database in plugins/Flint/config.properties:
# type: mysql, mariadb, postgresql or sqlite
db.main.type=mariadb
db.main.host=localhost
db.main.port=3306
db.main.database=minecraft
db.main.username=root
db.main.password=secret
db.main.pool-size=8
import db
stats = none
on load:
stats = db.get("main")
stats.execute("CREATE TABLE IF NOT EXISTS kills (uuid VARCHAR(36) PRIMARY KEY, n INT NOT NULL)")
command kills:
row = stats.first("SELECT n FROM kills WHERE uuid = ?", [sender.uuid])
send "Kills: {row.n if row else 0}" to sender
| Call | Returns |
|---|---|
db.get(name) |
a database from the config |
db.connect(url, [user], [password]) |
mysql://user:pass@host:3306/db, postgresql://..., sqlite:file.db |
d.query(sql, [params]) |
a list of rows; each row is a map of column to value |
d.first(sql, [params]) |
the first row, or none |
d.execute(sql, [params]) |
the number of changed rows |
d.batch(sql, rows) |
runs one statement per parameter list |
d.begin() |
a transaction with query, first, execute, commit(), rollback() |
Every call pauses the script, runs on a pooled background connection and resumes the script on the right thread with the result. Always pass values through ? placeholders, never by putting them into the SQL text. Errors can be caught with try / catch.
SQLite works on Paper (which ships the driver) but not on Velocity.