Skip to content

Instantly share code, notes, and snippets.

@cfpwastaken
Created August 4, 2024 19:45
Show Gist options
  • Save cfpwastaken/c66ecee488bde445d1af2803f03ce859 to your computer and use it in GitHub Desktop.
Save cfpwastaken/c66ecee488bde445d1af2803f03ce859 to your computer and use it in GitHub Desktop.
Item System for CC + Create mod Item Vaults
-- Backend
-- Signals
BACKTOSTORAGE = "front"
TAKEITEM = "right"
SKIPITEM = "left"
GETNEWITEM = "back"
CHEST = peripheral.wrap("minecraft:chest_1")
DELAY = 0.2
RED_DELAY = 0.1
redstone.setOutput(BACKTOSTORAGE, true)
redstone.setOutput(TAKEITEM, true)
redstone.setOutput(SKIPITEM, true)
redstone.setOutput(GETNEWITEM, true)
function takeCurrentItem()
print("Taking item")
redstone.setOutput(TAKEITEM, false)
sleep(RED_DELAY)
redstone.setOutput(TAKEITEM, true)
sleep(DELAY)
end
function skipCurrentItem()
print("Skipping item")
redstone.setOutput(SKIPITEM, false)
sleep(RED_DELAY)
redstone.setOutput(SKIPITEM, true)
sleep(DELAY)
end
function getNewItem()
print("Getting new item")
redstone.setOutput(GETNEWITEM, false)
sleep(RED_DELAY)
redstone.setOutput(GETNEWITEM, true)
sleep(DELAY)
end
function backToStorage()
print("Back to storage")
redstone.setOutput(BACKTOSTORAGE, false)
sleep(5)
redstone.setOutput(BACKTOSTORAGE, true)
sleep(DELAY)
end
function clearCache()
print("Clearing cache")
rednet.send(4, "clear")
end
INDEX = {}
function loadIndex()
if not fs.exists("index") then
return {}
end
local file = fs.open("index", "r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function saveIndex()
local file = fs.open("index", "w")
file.write(textutils.serialize(INDEX))
file.close()
end
function main()
rednet.open("top")
while true do
print("Waiting for item")
id, wantedItem = rednet.receive()
print(wantedItem)
if wantedItem == "back" then
backToStorage()
elseif wantedItem == "index" then
index = indexAllItems()
print("")
print("INDEX:")
for item, count in pairs(index) do
print(item .. " " .. count)
end
rednet.send(id, "done")
elseif wantedItem == "getIndex" then
rednet.send(id, textutils.serialize(INDEX))
else
local found = false
while not found do
getNewItem()
local list = CHEST.list()
if #list == 0 then
print("No items in chest")
rednet.send(id, "notfound")
break
end
for slot, item in pairs(list) do
print(item.name .. " vs " .. wantedItem)
if item.name == wantedItem then
takeCurrentItem()
found = true
break
else
skipCurrentItem()
break
end
end
end
if not found then
-- rednet.send("not found", "front")
clearCache()
else
clearCache()
rednet.send(id, "done")
end
end
end
end
function indexAllItems()
index = {}
while true do
getNewItem()
local list = CHEST.list()
if #list == 0 then
print("No items in chest")
break
end
for slot, item in pairs(list) do
if index[item.name] then
index[item.name] = index[item.name] + item.count
else
index[item.name] = item.count
end
skipCurrentItem()
end
end
clearCache()
INDEX = index
saveIndex()
return index
end
skipCurrentItem()
clearCache()
INDEX = loadIndex()
main()
-- Frontend
completion = require "cc.completion"
BACKEND_ID = 3
MONITOR = peripheral.wrap("top")
rednet.open("back")
width, height = MONITOR.getSize()
function draw()
MONITOR.setBackgroundColor(colors.black)
MONITOR.clear()
-- Add a title
MONITOR.setCursorPos(1, 1)
MONITOR.setBackgroundColor(colors.black)
MONITOR.setTextColor(colors.white)
MONITOR.write("*VAULT STORAGE*")
-- Add a "Storage" button to the screen, bottom right
MONITOR.setCursorPos(width - 8, height - 1)
MONITOR.setBackgroundColor(colors.blue)
MONITOR.write("Storage")
-- Add a "Get item" button to the screen, bottom left
MONITOR.setCursorPos(1, height - 1)
MONITOR.setBackgroundColor(colors.green)
MONITOR.write("Get item")
-- Add a "Index Items" button to the screen, top right
MONITOR.setCursorPos(width - 8, 1)
MONITOR.setBackgroundColor(colors.red)
MONITOR.write("Index")
-- Add a "More" button to the screen, 2 px above the get item button
MONITOR.setCursorPos(1, height - 3)
MONITOR.setBackgroundColor(colors.yellow)
MONITOR.write("More")
end
function waitForTouch()
while true do
event, side, x, y = os.pullEvent("monitor_touch")
if x >= width - 8 and x <= width and y == height - 1 then
return "storage"
elseif x >= 1 and x <= 9 and y == height - 1 then
return "get"
elseif x >= width - 8 and x <= width and y == 1 then
return "index"
elseif x >= 1 and x <= 5 and y == height - 3 then
return "more"
end
end
end
function drawWaitScreen()
MONITOR.setBackgroundColor(colors.black)
MONITOR.clear()
MONITOR.setCursorPos(1, 1)
MONITOR.write("Waiting for backend...")
end
function getIndex()
rednet.send(BACKEND_ID, "getIndex")
id, index = rednet.receive()
return textutils.unserialize(index)
end
function indexCompletions(index)
-- Index format: {itemName = count, ...}
-- We want to return a list of item names
local completions = {}
for item, count in pairs(index) do
table.insert(completions, item)
end
return completions
end
got = ""
function getItem(item)
term.clear()
print("What item do you want?")
-- item = read()
if item == nil then
completions = indexCompletions(getIndex())
item = read(nil, nil, function(text) return completion.choice(text, completions) end)
end
term.clear()
got = item
rednet.send(BACKEND_ID, item)
drawWaitScreen()
id, result = rednet.receive()
if result == "done" then
MONITOR.clear()
MONITOR.setCursorPos(1, 1)
MONITOR.write("Item received!")
sleep(5)
MONITOR.clear()
draw()
else
MONITOR.clear()
MONITOR.setCursorPos(1, 1)
MONITOR.write("Item not found!")
sleep(3)
MONITOR.clear()
draw()
end
end
function main()
while true do
draw()
local action = waitForTouch()
if action == "storage" then
rednet.send(BACKEND_ID, "back")
drawWaitScreen()
sleep(5)
elseif action == "get" then
MONITOR.clear()
getItem()
elseif action == "index" then
rednet.send(BACKEND_ID, "index")
drawWaitScreen()
rednet.receive()
elseif action == "more" then
MONITOR.clear()
getItem(got)
end
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment