Inventory: sealed items with server records
Partly liveThe inventory capability: give, take and list your own sealed items.
#Inventory: sealed items with server records
Capability inventory. Typings: Inventory, ItemSpec, ItemRef (packages/script/v1/inventory.d.ts).
import { Inventory } from "@x1/script";
await Inventory.GiveAsync(player, { base: "cod", name: "Lago pike", lore: ["4.2 kg"], tag: { species: "pike", kg: 4.2 } });
const mine = (await Inventory.ListAsync(player)).filter((i) => i.mine && i.tag.species === "pike");
const sold = await Inventory.TakeAsync(player, (i) => i.tag.species === "pike", 10);
Inventory.ItemUsed.Connect((p, item) => { /* a right click with one of your items */ });#What you can give
basemust be in your grant'sbases.- Operator items are never given, whatever the grant says: command blocks, barriers, spawners, spawn eggs, debug sticks, totems, elytra and the like.
- Weapons (swords, axes, bows, TNT, lava buckets, ...) are never given in v1.
countis 1-64 and at most the grant'smax_count.gives_per_min(default 120) limits gives per minute.- Names and lore are plain text. Every item also carries a last lore line naming your experience.
#Why a forged item doesn't count
Every item a script gives is sealed. The item carries x1:<experience> = {id, tag, mac}:
idnames a record the server keeps (how many units it gave, how many came back);macis an HMAC over the world, the experience, that id, the base and the tag, under a key only the server holds.
ItemRef.mine is true only when both hold:
- the seal checks, so an item made in creative mode, by a hacked client or by another plugin with an invented or edited tag never counts;
- the player holds no more units of that id than the record has outstanding, so a copy of a real item doesn't count while the original is still out.
TakeAsync takes only items that are mine and consumes the record. A record never gives back more units than it
handed out. So if you pay for items you take back, you pay at most once per unit you gave, however many copies exist.
In creative mode, a sealed stack can only be moved: taken out of a slot and put back within 2 seconds. The creative inventory packet can't clone or forge one. Operators are exempt.
The console shows the records per experience and base: given, taken back, outstanding.
#TakeAsync
- With a filter: it runs in your script over
ListAsync's list, and the host takes only slots that still hold the same sealed id. - With slot numbers: only the slots that hold your sealed items.
In both forms max limits the total units taken.
