Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- chestNames = {"chest", "copper", "iron", "silver", "gold", "diamond", "crystal", "obsidian", "mcp_mobius_betterbarrel"}
- enderChestDir = "down"
- modemDir = "back"
- chests = {}
- local Args = {...}
- if Args[1] == "update" then
- print("Updating...")
- program = shell.getRunningProgram()
- fs.delete(program)
- shell.run("pastebin", "get", "WFB4TcYb", program)
- print("Finished updating.")
- error()
- end
- for i = 0, 100 do
- for s, k in pairs(chestNames) do
- p = peripheral.wrap(k .. "_" .. i)
- if p ~= nil then
- table.insert(chests, p)
- end
- end
- end
- items = {}
- function isSameData(data1, data2)
- if data1.display_name ~= data2.display_name then return false end
- if data1.raw_name ~= data2.raw_name then return false end
- if data1.dmg ~= data2.dmg then return false end
- if data1.maxdmg ~= data2.maxdmg then return false end
- if data1.max_size ~= data2.max_size then return false end
- if data1.id ~= data2.id then return false end
- -- if data1.ore_dict ~= data2.ore_dict then return false end
- if data1.name ~= data2.name then return false end
- if data1.mod_id ~= data2.mod_id then return false end
- return true
- end
- function getItemID(stackData)
- return tostring(stackData.id) .. ":" .. tostring(stackData.dmg)
- end
- function append(data1, data2)
- data1.qty = data1.qty + data2.qty
- for i, k in pairs(data2.chests) do
- table.insert(data1.chests, k)
- end
- end
- function safeString(input)
- text = tostring(input)
- local newText = {}
- for i = 1, #text do
- local val = text:byte(i)
- newText[i] = (val > 31 and val < 127) and val or 63
- end
- return string.char(unpack(newText))
- end
- function spairs(t, order)
- local keys = {}
- for k in pairs(t) do keys[#keys+1] = k end
- if order then
- table.sort(keys, function(a,b) return order(t, a, b) end)
- else
- table.sort(keys)
- end
- local i = 0
- return function()
- i = i + 1
- if keys[i] then
- return keys[i], t[keys[i]]
- end
- end
- end
- function itemNameSort()
- return function(t, a, b) return t[a].display_name < t[b].display_name end
- end
- function itemQtySort()
- return function(t, a, b) return t[a].qty > t[b].qty end
- end
- function extractItemsFromChest(chest, stringID, qty, outDir)
- local out = 0
- for slot, stack in pairs(chest.getAllStacks()) do
- if getItemID(stack.all()) == stringID then
- out = out + chest.pushItem(outDir, slot, qty - out)
- if (qty - out) <= 0 then return out end
- end
- end
- return out
- end
- function extractItemsFromChests(stringID, qty, outDir)
- local out = 0
- local item = items[stringID]
- if item ~= nil then
- for i = 1, #item.chests do
- local chest = item.chests[i].chest
- local slot = item.chests[i].slot
- --print("From " .. tostring(chest) .. " slot " .. slot)
- out = out + chest.pushItem(outDir, slot, qty - out)
- --out = out + extractItemsFromChest(item.chests[i], stringID, qty - out, outDir)
- if (qty - out) <= 0 then return out end
- end
- end
- return out
- end
- function getItemIDFromSearch(searchString)
- for id, item in pairs(items) do
- if string.find(tostring(id), searchString) ~= nil then
- return id
- end
- end
- return nil
- end
- function updateStock()
- items = {}
- for i, chest in pairs(chests) do
- for j, stack in pairs(chest.getAllStacks()) do
- stackData = stack.basic()
- chestData = {}
- chestData.chest = chest
- chestData.slot = j
- stackData.chests = {chestData}
- id = getItemID(stackData)
- if items[id] == nil then
- items[id] = stackData
- else
- append(items[id], stackData)
- end
- end
- end
- end
- updateStock()
- rednet.open(modemDir)
- maxSearchResults = 13
- function getSearchResults(searchText, sortingFunction)
- local searchResults = {}
- for id, item in spairs(items, sortingFunction) do
- if string.find(string.lower(tostring(id)), string.lower(searchText)) ~= nil or string.find(string.lower(tostring(item.display_name)), string.lower(searchText)) ~= nil then
- resultData = item
- resultData.searchID = id
- table.insert(searchResults, resultData)
- if #searchResults >= maxSearchResults then return searchResults end
- end
- end
- return searchResults;
- end
- function getPacket(type)
- local packet = {}
- packet[1] = "ccaec"
- packet[2] = type
- return packet
- end
- function sendStatusPacket(client, statusMessage, searchData)
- local packet = getPacket(1)
- packet[3] = statusMessage
- if searchData ~= nil then
- packet[4] = searchData
- end
- rednet.send(client, packet)
- print(client .. ": " .. statusMessage)
- end
- -- Packet structures:
- -- 1 = packet authentication, should always be ccaes to the server for valid one.
- -- 2 = packet type.
- -- Rest is data.
- -- Search packet = 0
- -- 3 = sorting function (0 = qty, 1 = name)
- -- 4 = search text.
- -- Status packet = 1
- -- 3 = string message
- -- Request packet = 2
- -- 3 = amount
- -- 4 = ID
- -- 5 = sortingFunction
- -- 6 = searchText
- function processPacket(client, packet)
- if packet[1] ~= "ccaes" then return end -- Not valid packet
- -- print("Got packet ID: " .. packet[2] .. " from client: " .. client)
- if packet[2] == 0 then -- Update search packet
- print(client .. " searched for " .. packet[4])
- updateStock()
- local sortingFunction = itemQtySort()
- if packet[3] == 1 then sortingFunction = itemNameSort() end
- local respondPacket = getPacket(0)
- respondPacket[3] = getSearchResults(packet[4], sortingFunction)
- rednet.send(client, respondPacket)
- elseif packet[2] == 2 then
- local extractAmount = packet[3]
- local extractID = packet[4]
- sendStatusPacket(client, "Extracting " .. extractID)
- local out = extractItemsFromChests(extractID, extractAmount, enderChestDir)
- local sortingFunction = itemQtySort()
- if packet[5] == 1 then sortingFunction = itemNameSort() end
- updateStock()
- sendStatusPacket(client, out .. " items extracted", getSearchResults(packet[6], sortingFunction))
- end
- end
- print("Running CC AE Server...")
- while true do
- event, client, message = os.pullEvent()
- if event == "rednet_message" then
- processPacket(client, message)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment