Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("turtleChest.lua")
- username = nil
- password = nil
- serverID = 0
- atmID = 37
- maxitems = 1024
- -- This code DRASTICALLY needs an update, as of now only 1024 of each item can be accessed at a time.
- -- Items MUST only have 1 chest for this to work. (I think) At least the subtraction method of item count needs modification to
- -- change this
- function isStorageBlock(direction)
- local storageBlocks = {
- "minecraft:chest",
- "minecraft:trapped_chest",
- "minecraft:barrel",
- "minecraft:shulker_box",
- "minecraft:white_shulker_box",
- "minecraft:orange_shulker_box",
- "minecraft:magenta_shulker_box",
- "minecraft:light_blue_shulker_box",
- "minecraft:yellow_shulker_box",
- "minecraft:lime_shulker_box",
- "minecraft:pink_shulker_box",
- "minecraft:gray_shulker_box",
- "minecraft:light_gray_shulker_box",
- "minecraft:cyan_shulker_box",
- "minecraft:purple_shulker_box",
- "minecraft:blue_shulker_box",
- "minecraft:brown_shulker_box",
- "minecraft:green_shulker_box",
- "minecraft:red_shulker_box",
- "minecraft:black_shulker_box",
- "minecraft:hopper",
- "computercraft:turtle_normal"
- }
- if direction == "top" then
- success, data = turtle.inspectUp()
- elseif direction == "bottom" then
- success, data = turtle.inspectDown()
- else
- success, data = turtle.inspect()
- end
- if success then
- -- Check if the block's name is in the storageBlocks list
- for _, block in ipairs(storageBlocks) do
- if data.name == block then
- return true -- The block is a storage block
- end
- end
- end
- return false -- The block is not a storage block or no block detected
- end
- function twoInputStrip(str)
- first, second = str:match("(%S+)%s+(%S+)")
- return first, second
- end
- function fiveInputStrip(str)
- first, second, third, fourth, five = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
- return first, second, third, fourth, five
- end
- function send(id, message)
- rednet.open("left")
- rednet.send(id, message)
- print("send: " .. message)
- end
- function getItemLocations()
- local directions = {
- {name = "front", takeAll = turtleChest.takeAll, insertAll = turtleChest.insertAll, detect = turtle.detect},
- {name = "left",
- takeAll = function()
- turtle.turnLeft()
- local success = turtleChest.takeAll()
- turtle.turnRight()
- return success
- end,
- insertAll = function()
- turtle.turnLeft()
- local success = turtleChest.insertAll()
- turtle.turnRight()
- return success
- end,
- detect = function()
- turtle.turnLeft()
- local success = turtle.detect()
- turtle.turnRight()
- return success
- end
- },
- {name = "right",
- takeAll = function()
- turtle.turnRight()
- local success = turtleChest.takeAll()
- turtle.turnLeft()
- return success
- end,
- insertAll = function()
- turtle.turnRight()
- local success = turtleChest.insertAll()
- turtle.turnLeft()
- return success
- end,
- detect = function()
- turtle.turnRight()
- local success = turtle.detect()
- turtle.turnLeft()
- return success
- end
- },
- {name = "back",
- takeAll = function()
- turtle.turnLeft()
- turtle.turnLeft()
- local success = turtleChest.takeAll()
- turtle.turnRight()
- turtle.turnRight()
- return success
- end,
- insertAll = function()
- turtle.turnLeft()
- turtle.turnLeft()
- local success = turtleChest.insertAll()
- turtle.turnRight()
- turtle.turnRight()
- return success
- end,
- detect = function()
- turtle.turnLeft()
- turtle.turnLeft()
- local success = turtle.detect()
- turtle.turnRight()
- turtle.turnRight()
- return success
- end
- },
- {name = "top", takeAll = turtleChest.takeAllUp, insertAll = turtleChest.insertAllUp, detect = turtle.detectUp},
- {name = "bottom", takeAll = turtleChest.takeAllDown, insertAll = turtleChest.insertAllDown, detect = turtle.detectDown}
- }
- local items = {} -- Array to store items
- for _, dir in ipairs(directions) do
- -- Check if there is a chest in the specified direction
- if dir.detect() and isStorageBlock(dir.name) then
- -- Take all items from the chest
- dir.takeAll()
- -- Get the count of items
- local count = turtleChest.countCheck()
- turtle.select(1)
- local itemDetail = turtle.getItemDetail()
- -- Insert all items back into the chest
- dir.insertAll()
- -- Add the information to the items array
- if itemDetail then
- table.insert(items, {direction = dir.name, itemName = itemDetail.name, count = count})
- else
- table.insert(items, {direction = dir.name, itemName = "Empty", count = 0})
- end
- end
- end
- -- Return the array with items
- return items
- end
- function suckOnce(amount, direction)
- if direction == "top" then
- return turtle.suckUp(amount)
- elseif direction == "bottom" then
- return turtle.suckDown(amount)
- else
- return turtle.suck(amount)
- end
- end
- function suckChest(amount, direction)
- success = false
- slots = math.ceil(amount / 64)
- remains = amount % 64
- if slots > 16 then
- print("Too Many Items Requested!")
- return false
- elseif slots == 1 then
- return suckOnce(amount, direction)
- end
- for slot = 1, slots do
- turtle.select(slot)
- if slot == slots then
- return suckOnce(remains, direction)
- end
- success = suckOnce(64, direction)
- if not success then
- return success
- end
- end
- end
- function getItem(amount, direction)
- if direction == "left" then
- turtle.turnLeft()
- if not suckChest(amount, direction) then
- turtleChest.insertAll()
- turtle.turnRight()
- return false
- end
- turtle.turnRight()
- turtleChest.insertAll()
- return true
- elseif direction == "right" then
- turtle.turnRight()
- if not suckChest(amount, direction) then
- turtleChest.insertAll()
- turtle.turnLeft()
- return false
- end
- turtle.turnLeft()
- turtleChest.insertAll()
- return true
- elseif direction == "back" then
- turtle.turnLeft()
- turtle.turnLeft()
- if not suckChest(amount, direction) then
- turtleChest.insertAll()
- turtle.turnRight()
- turtle.turnRight()
- return false
- end
- turtle.turnRight()
- turtle.turnRight()
- turtleChest.insertAll()
- return true
- elseif direction == "top" then
- if not suckChest(amount, direction) then
- turtleChest.insertAllUp()
- return false
- end
- turtleChest.insertAll()
- return true
- elseif direction == "bottom" then
- if not suckChest(amount, direction) then
- turtleChest.insertAllDown()
- return false
- end
- turtleChest.insertAll()
- return true
- end
- end
- function getItems(itemname, count, array)
- for _, item in ipairs(array) do
- if item.itemName == itemname then
- direction = item.direction
- end
- end
- if direction then
- return getItem(count, direction)
- else
- return false
- end
- end
- function recieve(serverID)
- rednet.open("left")
- senderID, message, protocol = rednet.receive()
- print(message)
- print(tonumber(serverID) == tonumber(senderID))
- if tonumber(serverID) == tonumber(senderID) then
- return message
- else
- return nil
- end
- end
- function setItemlist(filename, itemlist)
- local file = fs.open(filename, "w")
- for _, line in pairs(itemlist) do
- file.write(line .. "\n")
- end
- file.close()
- end
- function searchItemlist(item)
- local file = fs.open("itemlist.txt", "r")
- local line = file.readLine()
- while line do
- name, cost = twoInputStrip(line)
- if name == item then
- return cost
- end
- line = file.readLine()
- end
- file.close()
- return nil
- end
- function conductTransaction(username, password, cost)
- message = "buy " .. username .. " " .. password .. " " .. cost
- rednet.send(serverID, message)
- id, value = rednet.receive(nil, 3)
- os.sleep(1)
- return value
- end
- function handleConnection(itemLocations)
- rednet.open("left")
- senderID, message, protocol = rednet.receive()
- print(message)
- command = twoInputStrip(message)
- if command == "itemlist" then
- senderID, titemlist = recieve()
- if titemlist and tonumber(senderID) == tonumber(serverID) then
- itemlist = titemlist
- setItemlist(filename, itemlist)
- end
- elseif command == "vmitems" then
- rednet.send(senderID, itemLocations)
- elseif command == "buy" and tonumber(senderID) == tonumber(atmID) then
- --change this strip to include an amount, username, and password. variables should be count, username and password
- _, username, password, nitem, count = fiveInputStrip(message)
- print("username: ." .. username .. ".")
- print("nitem: ." .. nitem .. ".")
- print("count: ." .. count .. ".")
- count = tonumber(count)
- cost = searchItemlist(nitem)
- if not cost then
- print("No Item Sold")
- --error code
- return "No Item Sold"
- end
- check = false
- for _, item in ipairs(itemLocations) do
- if item.itemName == nitem then
- check = true
- end
- end
- if not check then
- print("No Item Found")
- --error code
- return "No Item Found"
- end
- lcount = 0
- for _, item in pairs(itemLocations) do
- if item.itemName == nitem then
- lcount = item.count + lcount
- end
- end
- if count > maxitems or count > lcount then
- print("Not Enough Items")
- print("Only " .. lcount .. " found")
- --error code
- return "Not Enough Items"
- end
- --check if user has enough money
- login = "search " .. username .. " " .. password
- send(serverID, login)
- balance = recieve(serverID)
- if balance == "false" or tonumber(balance) < tonumber(cost) then
- print("Not Enough Money")
- --error code
- return "Not Enough Money"
- end
- if not getItems(nitem, count, itemLocations) then
- print("Item Retrieval Failed")
- --error code
- return "Item Retrieval Failed"
- end
- --buy code
- conductTransaction(username, password, cost*count)
- for _, item in pairs(itemLocations) do
- if item.itemName == nitem then
- item.count = item.count - count
- end
- end
- return itemLocations
- end
- end
- local itemLocations = getItemLocations()
- print("Array")
- for _, item in ipairs(itemLocations) do
- print(item.direction .. ": " .. item.itemName .. " " .. item.count)
- end
- rednet.open("left")
- filename = "itemlist.txt"
- rednet.send(serverID, "vmitems please")
- itemlist = recieve(serverID)
- setItemlist(filename, itemlist)
- while true do
- nitemLocations = handleConnection(itemLocations)
- if type(nitemLocations) == "table" then
- itemLocations = nitemLocations
- rednet.send(atmID, "Success")
- elseif type(nitemLocations) == "string" then
- rednet.send(atmID, "Failure due to " .. nitemLocations)
- else
- rednet.send(atmID, "Unknown Failure")
- end
- end
Add Comment
Please, Sign In to add comment