Posted 15 December 2015 - 12:44 PM
As part of a miner script in the infinity mod pack I've been making use of enderchests as form of mobile storage just dialing in the frequency I wanted, but I was a little bothered by how precarious that was, since any unexpected item in the chest would basically ruin everything. turtle.suck() lacks any kind of awareness of what it's looking for, it it will always grab the first stack it sees, even if that stack is too small, or if it contains the wrong items. I noticed that I could mount enderchests as peripherals, letting me look carefully through their inventories and at the items contained within. (Though that might be because of other mods) , but the peripheral method .pushItem() requires a direction to be given in terms of the world. I.e. "NORTH", "EAST", or the number which those translate to. I rewrote my program to simply use .swapStack() to put the right item into the first slot before trying to take it, but from this I did come up with an incredibly cheaty way to determine my world direction after placing a chest with turtle.place().
Basically I attempt to transfer a stack of zero items from the chest into an inventory in every direction. If the turtle is the only block around with an inventory, then when the transfer completes without throwing an error you know you're facing the right direction.
It's really sketchy but it does work.
Basically I attempt to transfer a stack of zero items from the chest into an inventory in every direction. If the turtle is the only block around with an inventory, then when the transfer completes without throwing an error you know you're facing the right direction.
local function enderGetWorldDirection()
--This is a string compare, hence the weird layout
if not (peripheral.getType("front") == "ender_chest") then error("ERROR: Enderchest could not be found") end
local failed
local errReturn
local chest = peripheral.wrap("front")
local retVal = nil
-- directions are reversed from standard values because your facing an opposite direction to the chests direction toward you
local directionTable = {
[1] = "WEST",
[2] = "EAST",
[3] = "SOUTH",
[4] = "NORTH"
}
for i = 1, 4, 1 do
failed, errReturn = pcall(chest.pushItem, i, 0)
if (err == 0) then
assert(retVal)
if retVal == nil then
retVal = i
end
end
end
assert(not returnVal)
return directionTable[retVal]
end
It's really sketchy but it does work.
Edited on 15 December 2015 - 11:10 PM