Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Frog Turtle Shop
- -- Version 0.1
- local fileName = "shopsettings.txt"
- local function setColor(color)
- if term.isColor() then
- term.setTextColor(color)
- end
- end
- local function GetDepotContents()
- local slotAmount = turtle.getItemCount()
- while turtle.getItemCount() == slotAmount do
- turtle.suckDown()
- sleep(0.1)
- end
- return turtle.getItemDetail()
- end
- local function searchItem(itemToFind)
- for i = 2, 16, 1 do
- turtle.select(i)
- local itemData = turtle.getItemDetail()
- if itemData ~= nil then
- if itemData.name == itemToFind then
- return true
- end
- end
- end
- return false
- end
- local success, blockDetails = turtle.inspectDown()
- if blockDetails.name ~= "create:depot" then
- setColor(colors.red)
- print("You'll need to put a depot Below me!")
- setColor(colors.white)
- return
- end
- if not turtle.inspectUp() then
- setColor(colors.red)
- print("You should put a storage container above me!")
- setColor(colors.white)
- end
- local function setupShop()
- setColor(colors.green)
- print("Welcome to Frog Turtle Shop!")
- setColor(colors.white)
- print("To set up the shop, put the item (And the amount!) you'd like to sell in the depot below!")
- turtle.select(1)
- local itemToSell = GetDepotContents()
- setColor(colors.green)
- print("I'll be selling " .. itemToSell.count .. " of " .. itemToSell.name .. "!")
- setColor(colors.white)
- print("Next put the price of your item in the depot above!")
- turtle.select(2)
- local itemPrice = GetDepotContents()
- print("I'll be charging customers " .. itemPrice.count .. " of " .. itemPrice.name .. "!")
- turtle.drop()
- setColor(colors.green)
- print("\nPerfect, Let's get started!")
- setColor(colors.white)
- local shopData = { itemToSell, itemPrice }
- local shopFile = fs.open(fileName, "w")
- shopFile.write(textutils.serialise(shopData))
- shopFile.close()
- end
- if not fs.exists(fileName) then
- setupShop()
- end
- local shopFile = fs.open(fileName, "r")
- local shopData = textutils.unserialize(shopFile.readAll())
- shopFile.close()
- local itemToSell = shopData[1]
- local itemPrice = shopData[2]
- print("Selling " ..
- itemToSell.count .. " of " .. itemToSell.name .. " at the price of " .. itemPrice.count .. " " .. itemPrice.name)
- setColor(colors.green)
- print("Put the items you're selling in slots 2-16!")
- setColor(colors.white)
- while true do
- turtle.select(1)
- while turtle.getItemCount() ~= 0 do
- if not turtle.drop() then
- print("Please empty slot 1!")
- return
- end
- end
- local itemInput = GetDepotContents()
- if itemInput.name ~= itemPrice.name then
- turtle.drop()
- else
- local itemsToDispense = math.floor(itemInput.count / itemPrice.count)
- local cashback = itemInput.count - (itemPrice.count * itemsToDispense)
- local notEnough = true
- while notEnough do
- if searchItem(itemToSell.name) then
- if turtle.getItemCount() < itemsToDispense then
- local stackSlot = turtle.getSelectedSlot() + 1
- if stackSlot > 16 then
- print("Out of space!")
- turtle.select(1)
- turtle.drop()
- return
- end
- while not turtle.transferTo(stackSlot) do
- if stackSlot ~= 16 then
- stackSlot = stackSlot + 1
- else
- print("Out of space!")
- turtle.select(1)
- turtle.drop()
- return
- end
- end
- else
- notEnough = false
- turtle.drop(itemsToDispense)
- end
- else
- print("Out of stock!")
- turtle.select(1)
- turtle.drop()
- return
- end
- end
- turtle.select(1)
- turtle.drop(cashback)
- turtle.dropUp()
- end
- end
Add Comment
Please, Sign In to add comment