Sign in
mod

Storage

by Potluck · version 1 · Checked for all ages

Chests, crates and barrels keep what you put in them, in survival. A chest holds more kinds of thing, and two chests side by side are one double chest with twice the room. A dispenser gives out what it holds each time power reaches it: it puts a block down, pours water, fills a bucket or hands the thing to someone near. A chute moves things down from what is over it into what is under it, unless it has power. Make them at a workbench.

1--!strict
2-- Storage's own tests: `potluck-server --test=storage`.
3
4local test = require("@potluck/test@0.7")
5local world = require("@potluck/world@0.15")
6local store = require("@potluck/store@0.1")
7
8-- a survival player beside a crate they've put down
9local function crate(carrying: { [string]: number }): (any, vector)
10 carrying.Crate = (carrying.Crate or 0) + 1
11 local p = test.player("Ada", { survival = true, carrying = carrying })
12 local x, z = p.position.x + 2, p.position.z
13 local at = vector.create(x, world.top(x, z) + 1, z)
14 test.place(p, at, "Crate")
15 return p, at
16end
17
18test.case("put in what's in your hand, and take it out again", function()
19 local p, at = crate({ Planks = 10, Stone = 3 })
20 test.same(world.block(at), "Crate", "the crate")
21 test.hold(p, "Planks")
22 test.use_block(p, at)
23 local q = test.asked(p)
24 test.check(q ~= nil and string.find(q.text, "empty") ~= nil, "it starts empty")
25 test.same(q.choices[1], "Put in your Planks")
26 test.answer(p, 1)
27 test.same(world.has(p, "Planks"), 0, "planks left in the hotbar")
28 q = test.asked(p)
29 test.check(string.find(q.text, "It holds 10 Planks", 1, true) ~= nil, q.text)
30 test.hold(p, "Stone")
31 test.use_block(p, at)
32 test.answer(p, 1)
33 q = test.asked(p)
34 test.check(string.find(q.text, "10 Planks and 3 Stone", 1, true) ~= nil, q.text)
35 -- nothing left in the hand to put in, so only what it holds to take out
36 test.same(q.choices[1], "Take out Planks")
37 test.same(q.choices[2], "Take out Stone")
38 test.answer(p, 1)
39 test.same(world.has(p, "Planks"), 10, "planks back")
40 -- kept in the store by where it is
41 local kept = store.get("boxes") or {}
42 local box = kept[math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)]
43 test.check(box ~= nil and box.Stone == 3 and box.Planks == nil, "kept in the store")
44end)
45
46test.case("broken, a crate gives back what it held", function()
47 local p, at = crate({ Planks = 5 })
48 test.hold(p, "Planks")
49 test.use_block(p, at)
50 test.answer(p, 1)
51 test.same(world.has(p, "Planks"), 0)
52 test.dig(p, at)
53 test.same(world.has(p, "Planks"), 5, "the planks came back")
54 test.same(next(store.get("boxes") or {}), nil, "nothing's left in the store")
55end)
56
57test.case("in creative, it says what it's for", function()
58 local p = test.player("Bo")
59 local x, z = p.position.x + 2, p.position.z
60 local at = vector.create(x, world.top(x, z) + 1, z)
61 test.place(p, at, "Barrel")
62 test.use_block(p, at)
63 local q = test.asked(p)
64 test.check(q ~= nil and string.find(q.text, "In creative", 1, true) ~= nil, "told")
65end)
66
67local UP = vector.create(0, 1, 0)
68
69local function held(at: vector): { [string]: number }
70 local kept = store.get("boxes") or {}
71 return kept[math.floor(at.x) .. "," .. math.floor(at.y) .. "," .. math.floor(at.z)] or {}
72end
73
74-- puts what's in their hand into the holder at `at`
75local function put(p, at: vector, thing: string)
76 test.hold(p, thing)
77 test.use_block(p, at)
78 test.answer(p, 1)
79end
80
81test.case("a chute moves things down from one crate into another", function()
82 local p, low = crate({ Crate = 1, Chute = 1, Stone = 3 })
83 local chute, high = low + UP, low + UP * 2
84 world.set_block(chute, "Air")
85 world.set_block(high, "Air")
86 test.place(p, chute, "Chute")
87 test.place(p, high, "Crate")
88 put(p, high, "Stone")
89 test.same(held(high).Stone, 3, "three in the top crate")
90 -- two a second, and kept in the store every two seconds
91 test.wait(5)
92 test.same(held(low).Stone, 3, "all three moved down")
93 test.same(next(held(high)), nil, "the top crate's empty")
94 test.same(next(held(chute)), nil, "and the chute")
95end)
96
97test.case("a chute holds one kind of thing", function()
98 local p, at = crate({ Chute = 1, Stone = 2, Planks = 2 })
99 local chute = at + vector.create(0, 0, 2)
100 world.set_block(chute, "Air")
101 world.set_block(chute - UP, "Stone")
102 test.place(p, chute, "Chute")
103 put(p, chute, "Stone")
104 test.same(world.has(p, "Stone"), 0, "the stone's in")
105 put(p, chute, "Planks")
106 local q = test.asked(p)
107 test.check(q ~= nil and string.find(q.text, "one kind of thing", 1, true) ~= nil, q and q.text or "asked")
108 test.same(world.has(p, "Planks"), 2, "the planks stay in hand")
109end)
110
111test.case("a dispenser faces away from whoever put it down, and keeps things", function()
112 local p, at = crate({ Dispenser = 1, Planks = 4 })
113 local d = at + vector.create(0, 0, 2)
114 world.set_block(d, "Air")
115 test.place(p, d, "Dispenser", "east")
116 test.same(world.facing(d), "east")
117 put(p, d, "Planks")
118 test.same(held(d).Planks, 4, "it holds them")
119 test.wait(1)
120 test.same(world.block(d + vector.create(1, 0, 0)), "Air", "and gives nothing out without power")
121end)
122
123-- survival Ada with two chests put down side by side
124local function chests(carrying: { [string]: number }): (any, vector, vector)
125 carrying.Chest = 2
126 local p = test.player("Ada", { survival = true, carrying = carrying })
127 local x, z = p.position.x + 2, p.position.z
128 local a = vector.create(x, world.top(x, z) + 1, z)
129 local b = a + vector.create(1, 0, 0)
130 test.place(p, a, "Chest")
131 return p, a, b
132end
133
134test.case("a chest holds more kinds of thing than a crate", function()
135 local p, at = chests({ Planks = 1, Stone = 1, Dirt = 1, Sand = 1, Gravel = 1, Coal = 1, Hay = 1, Glass = 1 })
136 for _, name in { "Planks", "Stone", "Dirt", "Sand", "Gravel", "Coal", "Hay", "Glass" } do
137 test.hold(p, name)
138 test.use_block(p, at)
139 test.answer(p, 1)
140 end
141 local q = test.asked(p)
142 test.check(q ~= nil and string.find(q.text, "8 kinds") == nil, "still room")
143 test.same(world.has(p, "Glass"), 0, "the eighth kind went in, which a crate would have refused")
144end)
145
146test.case("two chests side by side are one double chest", function()
147 local p, a, b = chests({ Planks = 10, Stone = 4 })
148 test.hold(p, "Planks")
149 test.use_block(p, a)
150 test.answer(p, 1)
151 test.place(p, b, "Chest")
152 -- the new one shows what the first holds, and is called a double
153 test.use_block(p, b)
154 local q = test.asked(p)
155 test.same(q.title, "Double chest")
156 test.check(string.find(q.text, "10 Planks", 1, true) ~= nil, q.text)
157 test.hold(p, "Stone")
158 test.use_block(p, b)
159 test.answer(p, 1)
160 test.use_block(p, a)
161 q = test.asked(p)
162 test.check(string.find(q.text, "4 Stone", 1, true) ~= nil, "both halves share what's in it: " .. q.text)
163 -- breaking one half gives back what that half kept
164 test.dig(p, b)
165 test.use_block(p, a)
166 q = test.asked(p)
167 test.same(q.title, "Chest", "alone again")
168end)