From 815ccdaab96784c2ea105b5f59c8ade13d863734 Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 29 Jan 2024 01:05:40 +0100 Subject: [PATCH] added equipTool to InvManager --- src/lib/turtle/InvManager.hx | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib/turtle/InvManager.hx b/src/lib/turtle/InvManager.hx index 26d7a77..be10abd 100644 --- a/src/lib/turtle/InvManager.hx +++ b/src/lib/turtle/InvManager.hx @@ -1,5 +1,8 @@ package lib.turtle; +import kernel.turtle.Types.ToolSide; +import lua.Result; +import kernel.peripherals.Side; import kernel.log.Log; import kernel.turtle.Types.TurtleSlot; import kernel.turtle.Types.InteractDirections; @@ -103,6 +106,21 @@ class InvManager { return rtn; } + /** + Returns the first slot that contains this item. + **/ + public static function findItemSlot(item:Item):Null { + var invState = getInvState(); + + for (k => slot in invState) { + if (slot.name == item) { + return k; + } + } + + return null; + } + /** Cleans up turtle inventory. Moves items together. This should be run in a turtle thread. @@ -180,4 +198,22 @@ class InvManager { } } } + + public static function equipTool(item:Item, side:ToolSide):Outcome { + var toolSlot = findItemSlot(item); + + if (toolSlot == null) { + return Failure("Item not found"); + } + + Turtle.selectSlot(toolSlot); + var result = Turtle.equip(side); + + switch result { + case Success(_): + return Success(toolSlot); + case Failure(failure): + return Failure(failure); + } + } }