Skip to content

Commit 3f4c7d6

Browse files
committed
feat: 🗃️ Added inventorymanagement
1 parent dc44c20 commit 3f4c7d6

File tree

3 files changed

+363
-21
lines changed

3 files changed

+363
-21
lines changed

Progress.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
turtle.getFuelLevel()
55
turtle.turnLeft()
66
turtle.turnRight()
7+
turtle.getSelectedSlot()
8+
turtle.select(slotNum)
9+
turtle.getItemCount(slotNum)
10+
turtle.getItemSpace(slotNum)
711
```
812

913
## Implemented but not finished
@@ -22,9 +26,6 @@ turtle.down()
2226
## Haven't started
2327

2428
```lua
25-
turtle.select(slotNum)
26-
turtle.getItemCount(slotNum)
27-
turtle.getItemSpace(slotNum)
2829
turtle.detect()
2930
turtle.detectUp()
3031
turtle.detectDown()
@@ -40,7 +41,7 @@ turtle.suckUp(count)
4041
turtle.suckDown(count)
4142
turtle.refuel(count)
4243
turtle.getFuelLimit()
43-
turtle.transferTo(slotNum, count)
44+
turtle.transferTo(slotNum, count)--now
4445
turtle.equipLeft()
4546
turtle.equipRight()
4647
turtle.dig()
@@ -56,6 +57,4 @@ turtle.attack()
5657
turtle.attackUp()
5758
turtle.attackDown()
5859
turtle.craft(count)
59-
turtle.getSelectedSlot()
60-
turtle.getSlotCount()
6160
```

tests/test_spec.lua

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,120 @@ describe("ProxyTests", function()
290290
assert.are.equal(2, gotCalled, "asserts did not run as expected")
291291
end)
292292
end)
293+
describe("InventoryTests", function()
294+
---@type TurtleProxy
295+
local turtle
296+
before_each(function()
297+
end)
298+
describe("Adding Items for Testing", function()
299+
before_each(function()
300+
turtle = turtleEmulator:createTurtle()
301+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 64 }, 1)
302+
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
303+
end)
304+
it("Throw Error when adding Item to full slot", function()
305+
assert.has_error(function()
306+
turtle:addItemToInventory(
307+
{ name = "minecraft:Stone", count = 64, maxcount = 64 },
308+
1
309+
)
310+
end)
311+
end)
312+
it("Throw Error when adding Item to invalid slot", function()
313+
assert.has_error(function()
314+
turtle:addItemToInventory(
315+
{ name = "minecraft:Stone", count = 64, maxcount = 64 },
316+
0)
317+
end)
318+
end)
319+
it("Adding two different items to the same slot", function()
320+
local succ = turtle:addItemToInventory(
321+
{ name = "minecraft:Stone", count = 32, maxcount = 64 },
322+
3)
323+
assert.is_true(succ)
324+
assert.has_error(function()
325+
turtle:addItemToInventory(
326+
{ name = "minecraft:Woods", count = 32, maxcount = 64 },
327+
3)
328+
end)
329+
end)
330+
it("Adding item to Slot 3", function()
331+
local succ = turtle:addItemToInventory(
332+
{ name = "minecraft:Wood", count = 64, maxcount = 64, },
333+
3)
334+
assert.is_true(succ)
335+
assert.are.equal(64, turtle.inventory[3].count)
336+
end)
337+
it("Adding items to slot 4 twice", function()
338+
local succ = turtle:addItemToInventory(
339+
{ name = "minecraft:Wood", count = 32, maxcount = 64 },
340+
4)
341+
assert.is_true(succ)
342+
succ = turtle:addItemToInventory(
343+
{ name = "minecraft:Wood", count = 32, maxcount = 64 },
344+
4)
345+
assert.is_true(succ)
346+
assert.are.equal(64, turtle.inventory[4].count)
347+
end)
348+
it("Adding more items than maxcount without a specific slot", function()
349+
turtle:addItemToInventory(
350+
{ name = "minecraft:Stone", count = 1, maxcount = 64 }, 4)
351+
local succ = turtle:addItemToInventory(
352+
{ name = "minecraft:Wood", count = 128, maxcount = 64 })
353+
assert.is_true(succ)
354+
assert.are.equal(64, turtle.inventory[3].count)
355+
assert.are.equal(64, turtle.inventory[5].count)
356+
end)
357+
end)
358+
describe("Remaining Inventory Tests", function()
359+
before_each(function()
360+
turtle = turtleEmulator:createTurtle()
361+
turtle:addItemToInventory({ name = "minecraft:coal", count = 64, maxcount = 64, fuelgain = 64 }, 1)
362+
turtle:addItemToInventory({ name = "minecraft:wood_plank", count = 64, maxcount = 64, fuelgain = 8 }, 2)
363+
turtle:getSocket()
364+
end)
365+
it("transferTo Slot 2", function()
366+
turtle:select(2)
367+
local succ = turtle:transferTo(4, 16)
368+
assert.is_true(succ)
369+
assert.are.equal(16, turtle.inventory[4].count)
370+
assert.are.equal(48, turtle.inventory[2].count)
371+
assert.are.equal("minecraft:wood_plank", turtle.inventory[4].name)
372+
succ = turtle:transferTo(4, 48)
373+
assert.is_true(succ)
374+
assert.are.equal(64, turtle.inventory[4].count)
375+
assert.are.equal(nil, turtle.inventory[2])
376+
end)
377+
it("getItemInfos", function()
378+
local items = turtle:getItemDetail()
379+
assert.are_not.equal(nil, items)
380+
assert.are.equal("minecraft:coal", items.name)
381+
assert.are.equal(64, items.count)
382+
items = turtle:getItemDetail(2)
383+
assert.are_not.equal(nil, items)
384+
assert.are.equal("minecraft:wood_plank", items.name)
385+
assert.are.equal(64, items.count)
386+
end)
387+
it("getItemCount", function()
388+
local count = turtle.getItemCount()
389+
assert.are.equal(64, count)
390+
count = turtle.getItemCount(2)
391+
assert.are.equal(64, count)
392+
end)
393+
it("getitemSpace", function()
394+
local space = turtle.getItemSpace()
395+
assert.are.equal(0, space)
396+
space = turtle.getItemSpace(2)
397+
assert.are.equal(0, space)
398+
space = turtle.getItemSpace(3)
399+
assert.are.equal(64, space)
400+
end)
401+
it("getSelectedSlot", function()
402+
local slot = turtle.getSelectedSlot()
403+
assert.are.equal(1, slot)
404+
turtle:select(2)
405+
slot = turtle.getSelectedSlot()
406+
assert.are.equal(2, slot)
407+
end)
408+
end)
409+
end)

0 commit comments

Comments
 (0)