TurtleSlot is now guaranteed to be in range

This commit is contained in:
Niklas Kapelle 2024-01-29 01:24:49 +01:00
parent 815ccdaab9
commit 638d1acbe0
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
4 changed files with 33 additions and 20 deletions

View File

@ -119,23 +119,17 @@ class Turtle {
}
public static function selectSlot(slot:TurtleSlot):Outcome<Noise, Noise> {
// 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<Noise, Noise> {
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<ItemInspect> {
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;

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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;