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
|
for color, remote in pairs(remotes) do
|
||||||
if remote.port == channel then
|
if remote.port == channel then
|
||||||
local hashed = encryption.hashKey(remote.password)
|
local hashed = encryption.hashKey(remote.password)
|
||||||
local err, data = pcall(function()
|
local errd, data = pcall(function()
|
||||||
return encryption.decrypt(hashed, message)
|
return encryption.decrypt(hashed, message)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if not data then return end
|
if not data then return end
|
||||||
|
|
||||||
if err == true then
|
if errd == true then
|
||||||
---@type RemoteAccess
|
---@type RemoteAccess
|
||||||
local json, errj = textutils.unserialiseJSON(data)
|
local json, errj = textutils.unserialiseJSON(data)
|
||||||
if json or errj ~= nil then
|
if json or errj ~= nil then
|
||||||
|
|
@ -83,6 +83,32 @@ local function run()
|
||||||
)
|
)
|
||||||
end)
|
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)
|
sendResponse(modem, remote.port, hashed, json.id, ok, nil, err)
|
||||||
elseif json.type == "deposit" then
|
elseif json.type == "deposit" then
|
||||||
local ok, err = pcall(function()
|
local ok, err = pcall(function()
|
||||||
|
|
@ -93,14 +119,14 @@ local function run()
|
||||||
table.insert(move, s)
|
table.insert(move, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
inv.sendItemAwayMultiple(
|
inv:sendItemAwayMultiple(
|
||||||
move,
|
move,
|
||||||
ender_storage,
|
ender_storage,
|
||||||
config.remote.ender_storage,
|
config.remote.ender_storage,
|
||||||
json.data.amount
|
json.data.amount
|
||||||
)
|
)
|
||||||
elseif json.data.slots then
|
elseif json.data.slots then
|
||||||
inv.sendItemAwayMultiple(
|
inv:sendItemAwayMultiple(
|
||||||
json.data.slots,
|
json.data.slots,
|
||||||
ender_storage,
|
ender_storage,
|
||||||
config.remote.ender_storage,
|
config.remote.ender_storage,
|
||||||
|
|
|
||||||
|
|
@ -73,9 +73,9 @@ local function listNames(wait)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param itemName string
|
---@param itemName string
|
||||||
---@param count number|nil
|
---@param amount number|nil
|
||||||
---@param wait boolean|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
|
if not port or not modem or not password then
|
||||||
error("tiny_ra_library: init was never ran")
|
error("tiny_ra_library: init was never ran")
|
||||||
end
|
end
|
||||||
|
|
@ -86,7 +86,7 @@ local function withdraw(itemName, count, wait)
|
||||||
|
|
||||||
local ok, data = sendRequest("withdraw", {
|
local ok, data = sendRequest("withdraw", {
|
||||||
itemName = itemName,
|
itemName = itemName,
|
||||||
count = count
|
amount = amount
|
||||||
}, wait)
|
}, wait)
|
||||||
if wait then
|
if wait then
|
||||||
if not ok then
|
if not ok then
|
||||||
|
|
@ -98,9 +98,9 @@ end
|
||||||
|
|
||||||
|
|
||||||
---@param slots number[]
|
---@param slots number[]
|
||||||
---@param count number|nil
|
---@param amount number|nil
|
||||||
---@param wait boolean|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
|
if not port or not modem or not password then
|
||||||
error("tiny_ra_library: init was never ran")
|
error("tiny_ra_library: init was never ran")
|
||||||
end
|
end
|
||||||
|
|
@ -113,8 +113,8 @@ local function depositBySlots(slots, count, wait)
|
||||||
slots = slots
|
slots = slots
|
||||||
}
|
}
|
||||||
|
|
||||||
if count then
|
if amount then
|
||||||
data["count"] = count
|
data["amount"] = amount
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, response = sendRequest("deposit", data, wait)
|
local ok, response = sendRequest("deposit", data, wait)
|
||||||
|
|
@ -160,9 +160,9 @@ end
|
||||||
|
|
||||||
|
|
||||||
---@param itemName string
|
---@param itemName string
|
||||||
---@param count number|nil
|
---@param amount number|nil
|
||||||
---@param wait boolean|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
|
if not port or not modem or not password then
|
||||||
error("tiny_ra_library: init was never ran")
|
error("tiny_ra_library: init was never ran")
|
||||||
end
|
end
|
||||||
|
|
@ -171,8 +171,8 @@ local function depositByItemName(itemName, count, wait)
|
||||||
itemName = itemName
|
itemName = itemName
|
||||||
}
|
}
|
||||||
|
|
||||||
if count then
|
if amount then
|
||||||
data["count"] = count
|
data["amount"] = amount
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, response = sendRequest("deposit", data, wait)
|
local ok, response = sendRequest("deposit", data, wait)
|
||||||
|
|
@ -185,6 +185,60 @@ local function depositByItemName(itemName, count, wait)
|
||||||
end
|
end
|
||||||
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 {
|
return {
|
||||||
init = init,
|
init = init,
|
||||||
|
|
||||||
|
|
@ -194,5 +248,8 @@ return {
|
||||||
|
|
||||||
withdraw = withdraw,
|
withdraw = withdraw,
|
||||||
depositByItemName = depositByItemName,
|
depositByItemName = depositByItemName,
|
||||||
depositBySlots = depositBySlots
|
depositBySlots = depositBySlots,
|
||||||
|
|
||||||
|
withdrawToManipulator = withdrawToManipulator,
|
||||||
|
depositFromManipulator = depositFromManipulator
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue