add depositFromManipulator and withdrawToManipulator
This commit is contained in:
parent
00c612c005
commit
2dac4c10fd
2 changed files with 99 additions and 16 deletions
|
|
@ -54,13 +54,13 @@ local function run()
|
|||
for color, remote in pairs(remotes) do
|
||||
if remote.port == channel then
|
||||
local hashed = encryption.hashKey(remote.password)
|
||||
local err, data = pcall(function()
|
||||
local errd, data = pcall(function()
|
||||
return encryption.decrypt(hashed, message)
|
||||
end)
|
||||
|
||||
if not data then return end
|
||||
|
||||
if err == true then
|
||||
if errd == true then
|
||||
---@type RemoteAccess
|
||||
local json, errj = textutils.unserialiseJSON(data)
|
||||
if json or errj ~= nil then
|
||||
|
|
@ -83,6 +83,32 @@ local function run()
|
|||
)
|
||||
end)
|
||||
|
||||
sendResponse(modem, remote.port, hashed, json.id, ok, nil, err)
|
||||
elseif json.type == "withdraw_to_manipulator" then
|
||||
local ok, err = pcall(function()
|
||||
inv:sendItemToSelf(json.data.itemName, peripheral.wrap(json.data.manipulator).getInventory(),
|
||||
json.data.amount,
|
||||
json.data.manipulator)
|
||||
end)
|
||||
|
||||
sendResponse(modem, remote.port, hashed, json.id, ok, nil, err)
|
||||
elseif json.type == "deposit_from_manipulator" then
|
||||
local ok, err = pcall(function()
|
||||
local manip = peripheral.wrap(json.data.manipulator)
|
||||
local move = {}
|
||||
for s, i in pairs(manip.getInventory().list()) do
|
||||
if i.name == json.data.itemName then
|
||||
table.insert(move, s)
|
||||
end
|
||||
end
|
||||
inv:sendItemAwayMultiple(
|
||||
move,
|
||||
manip.getInventory(),
|
||||
json.data.manipulator,
|
||||
json.data.amount
|
||||
)
|
||||
end)
|
||||
|
||||
sendResponse(modem, remote.port, hashed, json.id, ok, nil, err)
|
||||
elseif json.type == "deposit" then
|
||||
local ok, err = pcall(function()
|
||||
|
|
@ -93,14 +119,14 @@ local function run()
|
|||
table.insert(move, s)
|
||||
end
|
||||
end
|
||||
inv.sendItemAwayMultiple(
|
||||
inv:sendItemAwayMultiple(
|
||||
move,
|
||||
ender_storage,
|
||||
config.remote.ender_storage,
|
||||
json.data.amount
|
||||
)
|
||||
elseif json.data.slots then
|
||||
inv.sendItemAwayMultiple(
|
||||
inv:sendItemAwayMultiple(
|
||||
json.data.slots,
|
||||
ender_storage,
|
||||
config.remote.ender_storage,
|
||||
|
|
|
|||
|
|
@ -73,9 +73,9 @@ local function listNames(wait)
|
|||
end
|
||||
|
||||
---@param itemName string
|
||||
---@param count number|nil
|
||||
---@param amount number|nil
|
||||
---@param wait boolean|nil
|
||||
local function withdraw(itemName, count, wait)
|
||||
local function withdraw(itemName, amount, wait)
|
||||
if not port or not modem or not password then
|
||||
error("tiny_ra_library: init was never ran")
|
||||
end
|
||||
|
|
@ -86,7 +86,7 @@ local function withdraw(itemName, count, wait)
|
|||
|
||||
local ok, data = sendRequest("withdraw", {
|
||||
itemName = itemName,
|
||||
count = count
|
||||
amount = amount
|
||||
}, wait)
|
||||
if wait then
|
||||
if not ok then
|
||||
|
|
@ -98,9 +98,9 @@ end
|
|||
|
||||
|
||||
---@param slots number[]
|
||||
---@param count number|nil
|
||||
---@param amount number|nil
|
||||
---@param wait boolean|nil
|
||||
local function depositBySlots(slots, count, wait)
|
||||
local function depositBySlots(slots, amount, wait)
|
||||
if not port or not modem or not password then
|
||||
error("tiny_ra_library: init was never ran")
|
||||
end
|
||||
|
|
@ -113,8 +113,8 @@ local function depositBySlots(slots, count, wait)
|
|||
slots = slots
|
||||
}
|
||||
|
||||
if count then
|
||||
data["count"] = count
|
||||
if amount then
|
||||
data["amount"] = amount
|
||||
end
|
||||
|
||||
local ok, response = sendRequest("deposit", data, wait)
|
||||
|
|
@ -160,9 +160,9 @@ end
|
|||
|
||||
|
||||
---@param itemName string
|
||||
---@param count number|nil
|
||||
---@param amount number|nil
|
||||
---@param wait boolean|nil
|
||||
local function depositByItemName(itemName, count, wait)
|
||||
local function depositByItemName(itemName, amount, wait)
|
||||
if not port or not modem or not password then
|
||||
error("tiny_ra_library: init was never ran")
|
||||
end
|
||||
|
|
@ -171,8 +171,8 @@ local function depositByItemName(itemName, count, wait)
|
|||
itemName = itemName
|
||||
}
|
||||
|
||||
if count then
|
||||
data["count"] = count
|
||||
if amount then
|
||||
data["amount"] = amount
|
||||
end
|
||||
|
||||
local ok, response = sendRequest("deposit", data, wait)
|
||||
|
|
@ -185,6 +185,60 @@ local function depositByItemName(itemName, count, wait)
|
|||
end
|
||||
end
|
||||
|
||||
---@param itemName string
|
||||
---@param manipulator string
|
||||
---@param count number|nil
|
||||
---@param wait boolean|nil
|
||||
local function depositFromManipulator(manipulator, itemName, amount, wait)
|
||||
if not port or not modem or not password then
|
||||
error("tiny_ra_library: init was never ran")
|
||||
end
|
||||
|
||||
local data = {
|
||||
itemName = itemName,
|
||||
manipulator = manipulator
|
||||
}
|
||||
|
||||
if amount then
|
||||
data["amount"] = amount
|
||||
end
|
||||
|
||||
local ok, response = sendRequest("deposit_from_manipulator", data, wait)
|
||||
|
||||
if wait then
|
||||
if not ok then
|
||||
error("Failed to list names: " .. tostring(response))
|
||||
end
|
||||
return response
|
||||
end
|
||||
end
|
||||
|
||||
---@param itemName string
|
||||
---@param manipulator string
|
||||
---@param amount number|nil
|
||||
---@param wait boolean|nil
|
||||
local function withdrawToManipulator(manipulator, itemName, amount, wait)
|
||||
if not port or not modem or not password then
|
||||
error("tiny_ra_library: init was never ran")
|
||||
end
|
||||
|
||||
if wait == nil then
|
||||
wait = true
|
||||
end
|
||||
|
||||
local ok, data = sendRequest("withdraw_to_manipulator", {
|
||||
manipulator = manipulator,
|
||||
itemName = itemName,
|
||||
amount = amount
|
||||
}, wait)
|
||||
if wait then
|
||||
if not ok then
|
||||
error("Failed to withdraw: " .. tostring(data))
|
||||
end
|
||||
return data
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
init = init,
|
||||
|
||||
|
|
@ -194,5 +248,8 @@ return {
|
|||
|
||||
withdraw = withdraw,
|
||||
depositByItemName = depositByItemName,
|
||||
depositBySlots = depositBySlots
|
||||
depositBySlots = depositBySlots,
|
||||
|
||||
withdrawToManipulator = withdrawToManipulator,
|
||||
depositFromManipulator = depositFromManipulator
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue