Repository navigation
Expand file tree
/
Copy pathinventory_util.lua
More file actions
134 lines (114 loc) · 4.71 KB
/
Copy pathinventory_util.lua
File metadata and controls
134 lines (114 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
-- http://raw.githubusercontent.com/cartographer1/ComputerCraft/main/inventory_util.lua
local function update(name, url)
shell.run(string.format("rm %s.lua", name))
local result = shell.run(string.format("wget %s", url))
assert(result, "Download Failed!")
end
update("functional", "http://raw.githubusercontent.com/cartographer1/ComputerCraft/main/functional.lua")
local expect_mod = require "cc.expect"
local expect, field = expect_mod.expect, expect_mod.field
local functional = require("functional")
local map = functional.map
local function moveItems(options)
local function _moveItems(srcName, destName, fromSlots, slotFilter, itemFilter, limit, toSlot, pullMode)
local src = peripheral.wrap(srcName)
local dest = peripheral.wrap(destName)
if type(fromSlots) == "number" then
fromSlots = { fromSlots }
end
local function getSlots()
if type(fromSlots) == "table" then
return map(function (_, slot)
if slotFilter and not slotFilter(slot) then
return
end
local name = src.list()[slot]
if itemFilter and not itemFilter(name) then
return
end
return slot
end, ipairs(fromSlots))
else -- type(fromSlots) == nil
return map(function (slot, item)
if slotFilter and not slotFilter(slot) then
return
end
local name = item.name
if itemFilter and not itemFilter(name) then
return
end
return slot
end, pairs(src.list()))
end
end
for slot in getSlots() do
if pullMode then
dest.pullItems(srcName, slot, limit, toSlot)
else
src.pushItems(destName, slot, limit, toSlot)
end
end
end
field(options, "srcName", "string")
field(options, "destName", "string")
field(options, "fromSlots", "number", "table", "nil")
field(options, "slotFilter", "function", "nil")
field(options, "itemFilter", "function", "nil")
field(options, "limit", "number", "nil")
field(options, "toSlot", "number", "nil")
field(options, "pullMode", "boolean", "nil")
_moveItems(options.srcName, options.destName, options.fromSlots, options.slotFilter, options.itemFilter, options.limit, options.toSlot, options.pullMode)
end
local function moveFluid(options)
local function _moveFluid(srcName, destName, slotOrFluidNames, filter, limit, pullMode)
local src = peripheral.wrap(srcName)
local dest = peripheral.wrap(destName)
if type(slotOrFluidNames) == "string" or type(slotOrFluidNames) == "number" then
slotOrFluidNames = { slotOrFluidNames }
end
local function getFluidNames()
if type(slotOrFluidNames) == "table" then
return map(function (index, slotOrFluidName)
local fluidName
if type(slotOrFluidName) == "string" then
fluidName = slotOrFluidName
elseif type(slotOrFluidName) == "number" then
local fluid = src.tanks()[slotOrFluidName]
if fluid ~= nil then
fluidName = fluid.name
end
end
if filter and not filter(fluidName) then
return
end
return fluidName
end, ipairs(slotOrFluidNames))
else -- nil
return map(function (slot, fluid)
if filter and not filter(fluid.name) then
return
end
return fluid.name
end, pairs(src.tanks()))
end
end
for fluidName in getFluidNames() do
if pullMode then
dest.pullFluid(srcName, limit, fluidName)
else
src.pushFluid(destName, limit, fluidName)
end
end
end
field(options, "srcName", "string")
field(options, "destName", "string")
field(options, "slotsOrFluidNames", "string", "number", "table", "nil")
field(options, "filter", "function", "nil")
field(options, "limit", "number", "nil")
field(options, "pullMode", "boolean", "nil")
_moveFluid(options.srcName, options.destName, options.slotsOrFluidNames, options.filter, options.limit, options.pullMode)
end
return {
moveItems=moveItems,
moveFluid=moveFluid,
}