Sign in
mod

Tools

by Potluck · version 1 · Checked for all ages

Pickaxes, axes, shovels and hoes for survival, in wood, stone, copper, iron and moonsilver. Hold one and its kind of block breaks faster, and better ones reach harder ores. They wear as they work and a worn out one breaks. Shears cut leaves and plants, a sickle reaps crops and a hammer breaks stone three by three. Make wooden, stone and copper tools, shears, sickles and hammers at a workbench, and iron and moonsilver ones at an anvil, which mends them all and makes iron and moonsilver armor too.

1--!strict
2-- The Tools creation's own tests: `potluck-server --test=tools`.
3
4local test = require("@potluck/test@0.7")
5local world = require("@potluck/world@0.22")
6
7-- a survival player carrying `carrying`, and an anvil beside them
8local function at_an_anvil(carrying: { [string]: number }): (any, vector)
9 local p = test.player("Ada", { survival = true, carrying = carrying })
10 local x, z = p.position.x + 2, p.position.z
11 local at = vector.create(x, world.top(x, z) + 1, z)
12 world.set_block(at, "Anvil")
13 return p, at
14end
15
16-- what the question in front of them says
17local function asked(p): (string, string, { string })
18 local q = test.asked(p)
19 test.check(q ~= nil, "a question")
20 local a = q :: any
21 return a.title, a.text, a.choices
22end
23
24test.case("an anvil makes an iron pickaxe from iron and planks", function()
25 local p, at = at_an_anvil({ ["Iron ingot"] = 3, Planks = 2 })
26 test.hold(p, nil)
27 test.use_block(p, at)
28 local title, _, choices = asked(p)
29 test.same(title, "Anvil")
30 test.same(choices[1], "Make iron tools", "nothing in hand to mend")
31 test.answer(p, 1)
32 title, _, choices = asked(p)
33 test.same(title, "Anvil: Iron tools")
34 test.same(choices[1], "Pickaxe")
35 test.same(choices[#choices], "Back")
36 test.answer(p, 1)
37 local _, text = asked(p)
38 test.check(string.find(text, "Made an iron pickaxe.", 1, true) == 1, text)
39 test.same(world.has(p, "Iron pickaxe"), 1, "the pickaxe")
40 test.same(world.has(p, "Iron ingot") + world.has(p, "Planks"), 0, "the iron and planks it took")
41 test.same(test.heard("clink"), 1, "hammering")
42end)
43
44test.case("without enough, the anvil says what it takes and takes nothing", function()
45 local p, at = at_an_anvil({ ["Moonsilver ingot"] = 2, Planks = 2 })
46 test.use_block(p, at)
47 test.answer(p, 3)
48 local title = asked(p)
49 test.same(title, "Anvil: Moonsilver tools")
50 test.answer(p, 2)
51 local _, text = asked(p)
52 test.check(string.find(text, "You'll need 3 Moonsilver ingot and 2 Planks", 1, true) == 1, text)
53 test.same(world.has(p, "Moonsilver ingot"), 2, "still all there")
54 test.same(world.has(p, "Moonsilver axe"), 0, "no axe")
55end)
56
57test.case("an anvil makes iron armor from iron alone", function()
58 local p, at = at_an_anvil({ ["Iron ingot"] = 15 })
59 test.hold(p, nil)
60 test.use_block(p, at)
61 local _, _, choices = asked(p)
62 test.same(choices[2], "Make iron armor")
63 test.answer(p, 2)
64 local title, text = asked(p)
65 test.same(title, "Anvil: Iron armor")
66 test.check(string.find(text, "Coat: 8 Iron ingot\n", 1, true) ~= nil, text)
67 test.answer(p, 2)
68 _, text = asked(p)
69 test.check(string.find(text, "Made an iron coat.", 1, true) == 1, text)
70 test.answer(p, 3)
71 _, text = asked(p)
72 test.check(string.find(text, "Made iron leggings.", 1, true) == 1, text)
73 test.same(world.has(p, "Iron coat") + world.has(p, "Iron leggings"), 2, "the coat and leggings")
74 test.same(world.has(p, "Iron ingot"), 0, "the iron they took")
75end)
76
77test.case("a worn tool is mended with one more of what it's made of", function()
78 local p, at = at_an_anvil({ ["Wooden pickaxe"] = 1, Planks = 1 })
79 test.hold(p, "Wooden pickaxe")
80 local x, z = p.position.x - 2, p.position.z
81 local stone = vector.create(x, world.top(x, z) + 1, z)
82 for _ = 1, 10 do
83 world.set_block(stone, "Stone")
84 test.dig(p, stone)
85 end
86 test.use_block(p, at)
87 local _, text, choices = asked(p)
88 test.check(string.find(text, "50 of 60 uses left", 1, true) ~= nil, text)
89 test.same(choices[1], "Mend your wooden pickaxe")
90 test.answer(p, 1)
91 _, text = asked(p)
92 test.check(string.find(text, "Mended your wooden pickaxe.", 1, true) == 1, text)
93 test.check(string.find(text, "60 of 60 uses left", 1, true) ~= nil, text)
94 test.same(world.has(p, "Planks"), 0, "the planks it took")
95 test.answer(p, 1)
96 _, text = asked(p)
97 test.check(string.find(text, "as good as new", 1, true) ~= nil, text)
98end)
99
100-- breaks the stone beside them `n` times with what's in hand
101local function dig_stone(p, n: number)
102 local x, z = p.position.x - 2, p.position.z
103 local stone = vector.create(x, world.top(x, z) + 1, z)
104 for _ = 1, n do
105 world.set_block(stone, "Stone")
106 test.dig(p, stone)
107 end
108end
109
110test.case("a worn tool is mended with XP, less the less it's worn", function()
111 local p, at = at_an_anvil({ ["Wooden pickaxe"] = 1 })
112 test.hold(p, "Wooden pickaxe")
113 world.reward(p, 15)
114 dig_stone(p, 30)
115 test.use_block(p, at)
116 local _, text, choices = asked(p)
117 test.check(string.find(text, "30 of 60 uses left. Mending it takes 1 Planks, or 10 XP.", 1, true) ~= nil, text)
118 test.same(choices[2], "Mend it with XP")
119 test.answer(p, 2)
120 _, text = asked(p)
121 test.check(string.find(text, "Mended your wooden pickaxe.", 1, true) == 1, text)
122 test.same(world.xp(p), 5, "the XP it took")
123 dig_stone(p, 30)
124 test.use_block(p, at)
125 test.answer(p, 2)
126 _, text = asked(p)
127 test.check(string.find(text, "Mending your wooden pickaxe takes 10 XP.", 1, true) == 1, text)
128 test.same(world.xp(p), 5, "not enough, so none taken")
129end)
130
131test.case("a Sturdy tool wears about half as fast", function()
132 local p, at = at_an_anvil({ ["Wooden pickaxe"] = 1 })
133 test.hold(p, "Wooden pickaxe")
134 world.charm(p, "Wooden pickaxe", { "Sturdy" })
135 dig_stone(p, 30)
136 test.use_block(p, at)
137 local _, text = asked(p)
138 local left = tonumber(string.match(text, "has (%d+) of 60 uses left")) or 0
139 test.check(left > 30 and left < 60, text)
140end)
141
142test.case("a tool that breaks takes its charms with it", function()
143 local p = test.player("Ada", { survival = true, carrying = { ["Wooden pickaxe"] = 1 } })
144 test.hold(p, "Wooden pickaxe")
145 world.charm(p, "Wooden pickaxe", { "Swift" })
146 dig_stone(p, 60)
147 test.same(world.has(p, "Wooden pickaxe"), 0, "broke")
148 test.same(#world.charms(p, "Wooden pickaxe"), 0, "no charms left on it")
149end)
150
151test.case("a hoe wears as it tills, not as it breaks things", function()
152 local p, at = at_an_anvil({ ["Copper hoe"] = 1 })
153 test.hold(p, "Copper hoe")
154 local x, z = p.position.x - 2, p.position.z
155 local ground = vector.create(x, world.top(x, z) + 1, z)
156 world.set_block(ground, "Grass")
157 world.set_block(ground + vector.create(0, 1, 0), "Air")
158 test.use_block(p, ground)
159 test.use_block(p, ground)
160 world.set_block(ground, "Dirt")
161 test.dig(p, ground)
162 test.use_block(p, at)
163 local _, text = asked(p)
164 test.check(string.find(text, "198 of 200 uses left", 1, true) ~= nil, text)
165end)
166
167test.case("in creative, the anvil says what it's for", function()
168 local p = test.player("Bo")
169 local x, z = p.position.x + 2, p.position.z
170 local at = vector.create(x, world.top(x, z) + 1, z)
171 world.set_block(at, "Anvil")
172 test.use_block(p, at)
173 local _, text = asked(p)
174 test.check(string.find(text, "survival", 1, true) ~= nil, text)
175end)