generator.insert no longer throws if slot was empty (never should have); cleaned and patched up refuel.lua a little

This commit is contained in:
Florian Nücke 2014-02-28 21:21:22 +01:00
parent 7c6837c7ae
commit b73f39c443
2 changed files with 31 additions and 26 deletions

View File

@ -29,7 +29,7 @@ class UpgradeGenerator(val owner: TileEntity) extends ManagedComponent {
val count = if (args.count > 0) args.checkInteger(0) else 64 val count = if (args.count > 0) args.checkInteger(0) else 64
val player = context.player val player = context.player
val stack = player.inventory.getStackInSlot(context.selectedSlot) val stack = player.inventory.getStackInSlot(context.selectedSlot)
if (stack == null) throw new IllegalArgumentException("selected slot is empty") if (stack == null) return result(Unit, "selected slot is empty")
if (!TileEntityFurnace.isItemFuel(stack)) { if (!TileEntityFurnace.isItemFuel(stack)) {
return result(Unit, "selected slot does not contain fuel") return result(Unit, "selected slot does not contain fuel")
} }

View File

@ -4,49 +4,54 @@ local component = require("component")
local robot = require("robot") local robot = require("robot")
local shell = require("shell") local shell = require("shell")
local args = shell.parse(...) -- Not using shell.parse to allow `refuel 1 -10`, the -10 would be
-- parsed as an option otherwise.
local args = {...}
local function printUsage() local function printUsage()
print("Usages:") print("Usages:")
print("'refuel' to get the current fuel count") print("refuel")
print("'refuel <slot> [amount]' to refuel [amount] from that specific slot,\n or try to completely fill it") print(" Gets the current fuel count.")
print("'refuel all' to refuel from all slots") print("refuel <slot> [amount]")
print(" Refuel the specified number of items (default")
print(" as many as possible) from the specified slot.")
print("refuel all")
print(" Refuel from all slots.")
end end
if component.isAvailable("generator") then if component.isAvailable("generator") then
local g = component.generator local g = component.generator
if #args == 0 then if #args == 0 then
print("Current Number of items in generator: "..g.count()) print("Current number of items in generator: "..g.count())
elseif tonumber(args[1]) ~= nil then elseif tonumber(args[1]) ~= nil then
print("Refuelling from slot"..args[1].."...") local slot = tonumber(args[1])
robot.select(tonumber(args[1])) local count = tonumber(args[2]) or 64
local success, msg robot.select(slot)
if tonumber(args[2]) ~= nil then if count > 0 then
if tonumber(args[2]) > 0 then io.write("Refueling from slot "..slot.."... ")
success, msg = g.insert(tonumber(args[2])) local success, msg = g.insert(count)
elseif tonumber(args[2]) < 0 then
success = g.remove(math.abs(tonumber(args[2])))
if not (success == true) then msg = "Could not remove item, generator is empty" end
else
msg = "You can't insert 0 of an item!"
end
else
success, msg = g.insert()
end
if success then if success then
print("Success.") print("success.")
else else
print("Error: "..msg) print("failed: "..msg)
end end
elseif count < 0 then
io.write("Ejecting into slot "..slot.."... ")
if g.remove(-count) then
print("success.")
else
print("failed.")
end
end -- else: ignore zero
robot.select(1) robot.select(1)
elseif string.lower(args[1]) == "all" then elseif string.lower(args[1]) == "all" then
io.write("Refuelling from all slots...") io.write("Refueling from all slots... ")
for i = 1, 16 do for i = 1, 16 do
robot.select(i) robot.select(i)
g.insert() g.insert()
end end
robot.select(1) robot.select(1)
print("Done.") print("done.")
else else
printUsage() printUsage()
end end