From 638d1acbe06bfdeb3d070d9a1636d3fdd46c2fdb Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Mon, 29 Jan 2024 01:24:49 +0100 Subject: [PATCH] TurtleSlot is now guaranteed to be in range --- src/kernel/turtle/Turtle.hx | 18 ++++++------------ src/kernel/turtle/TurtleSlot.hx | 26 ++++++++++++++++++++++++++ src/kernel/turtle/Types.hx | 5 ----- src/lib/turtle/InvManager.hx | 4 +--- 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/kernel/turtle/TurtleSlot.hx diff --git a/src/kernel/turtle/Turtle.hx b/src/kernel/turtle/Turtle.hx index 31ca387..42e8dc8 100644 --- a/src/kernel/turtle/Turtle.hx +++ b/src/kernel/turtle/Turtle.hx @@ -119,23 +119,17 @@ class Turtle { } public static function selectSlot(slot:TurtleSlot):Outcome { - // TODO: slot in bounds? - - var r = cc.Turtle.select(slot + 1); + var r = cc.Turtle.select(slot.toCCSlot()); return (r) ? Outcome.Success(null) : Outcome.Failure("Slot out of bounds"); } public static function getItemCount(?slot:TurtleSlot):Int { - // TODO: slot in bounds? - - return cc.Turtle.getItemCount(slot + 1); + return cc.Turtle.getItemCount(slot.toCCSlot()); } public static function getItemSpace(?slot:TurtleSlot):Int { - // TODO: slot in bounds? - - return cc.Turtle.getItemSpace(slot + 1); + return cc.Turtle.getItemSpace(slot.toCCSlot()); } public static function detect(dir:InteractDirections):Bool { @@ -208,12 +202,12 @@ class Turtle { } public static function compareSlot(otherSlot:TurtleSlot):Bool { - return cc.Turtle.compareTo(otherSlot + 1); + return cc.Turtle.compareTo(otherSlot.toCCSlot()); } public static function transfer(from:TurtleSlot, to:TurtleSlot, ?count:Int):Outcome { selectSlot(from); - var r = cc.Turtle.transferTo(to + 1, count); + var r = cc.Turtle.transferTo(to.toCCSlot(), count); return r ? Outcome.Success(null) : Outcome.Failure(null); } @@ -258,7 +252,7 @@ class Turtle { } public static function getItemDetail(slot:TurtleSlot, ?detailed:Bool = false):Option { - var r = cc.Turtle.getItemDetail(slot + 1); // FIXME: can take detailed as flag. Has to be fixed upstream + var r = cc.Turtle.getItemDetail(slot.toCCSlot()); // FIXME: can take detailed as flag. Has to be fixed upstream if (r == null) { return None; diff --git a/src/kernel/turtle/TurtleSlot.hx b/src/kernel/turtle/TurtleSlot.hx new file mode 100644 index 0000000..9ca72cf --- /dev/null +++ b/src/kernel/turtle/TurtleSlot.hx @@ -0,0 +1,26 @@ +package kernel.turtle; + +using tink.CoreApi; + +/** + The slot nummber for turtle inventory. 0 based. Assured to be in range. +**/ +abstract TurtleSlot(Int) to Int { + public function new(i:Int) { + if (i >= 0 && i < Turtle.MAX_SLOTS) { + this = i; + } else { + throw new Error("Slot not in range: " + i); + } + } + + @:from + public static function fromInt(i:Int) { + return new TurtleSlot(i); + } + + @:allow(kernel.turtle) + private inline function toCCSlot():Int { + return this + 1; + } +} diff --git a/src/kernel/turtle/Types.hx b/src/kernel/turtle/Types.hx index 6029b2d..0d80521 100644 --- a/src/kernel/turtle/Types.hx +++ b/src/kernel/turtle/Types.hx @@ -22,8 +22,3 @@ typedef ItemInspect = { public var damage:Int; public var count:Int; } - -/** - The slot nummber for turtle inventory. 0 based. -**/ -typedef TurtleSlot = Int; diff --git a/src/lib/turtle/InvManager.hx b/src/lib/turtle/InvManager.hx index be10abd..f89bd88 100644 --- a/src/lib/turtle/InvManager.hx +++ b/src/lib/turtle/InvManager.hx @@ -1,10 +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.TurtleSlot; import kernel.turtle.Types.InteractDirections; import kernel.turtle.Turtle;