MrHG

fts

Nov 2nd, 2022 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. -- Frog Turtle Shop
  2. -- Version 0.1
  3.  
  4. local fileName = "shopsettings.txt"
  5.  
  6. local function setColor(color)
  7. if term.isColor() then
  8. term.setTextColor(color)
  9. end
  10. end
  11.  
  12. local function GetDepotContents()
  13. local slotAmount = turtle.getItemCount()
  14. while turtle.getItemCount() == slotAmount do
  15. turtle.suckDown()
  16. sleep(0.1)
  17. end
  18. return turtle.getItemDetail()
  19. end
  20.  
  21. local function searchItem(itemToFind)
  22. for i = 2, 16, 1 do
  23. turtle.select(i)
  24. local itemData = turtle.getItemDetail()
  25. if itemData ~= nil then
  26. if itemData.name == itemToFind then
  27. return true
  28. end
  29. end
  30. end
  31. return false
  32. end
  33.  
  34. local success, blockDetails = turtle.inspectDown()
  35. if blockDetails.name ~= "create:depot" then
  36. setColor(colors.red)
  37. print("You'll need to put a depot Below me!")
  38. setColor(colors.white)
  39. return
  40. end
  41.  
  42. if not turtle.inspectUp() then
  43. setColor(colors.red)
  44. print("You should put a storage container above me!")
  45. setColor(colors.white)
  46. end
  47.  
  48. local function setupShop()
  49. setColor(colors.green)
  50. print("Welcome to Frog Turtle Shop!")
  51. setColor(colors.white)
  52. print("To set up the shop, put the item (And the amount!) you'd like to sell in the depot below!")
  53. turtle.select(1)
  54. local itemToSell = GetDepotContents()
  55. setColor(colors.green)
  56. print("I'll be selling " .. itemToSell.count .. " of " .. itemToSell.name .. "!")
  57. setColor(colors.white)
  58. print("Next put the price of your item in the depot above!")
  59. turtle.select(2)
  60. local itemPrice = GetDepotContents()
  61. print("I'll be charging customers " .. itemPrice.count .. " of " .. itemPrice.name .. "!")
  62. turtle.drop()
  63. setColor(colors.green)
  64. print("\nPerfect, Let's get started!")
  65. setColor(colors.white)
  66. local shopData = { itemToSell, itemPrice }
  67. local shopFile = fs.open(fileName, "w")
  68. shopFile.write(textutils.serialise(shopData))
  69. shopFile.close()
  70. end
  71.  
  72. if not fs.exists(fileName) then
  73. setupShop()
  74. end
  75.  
  76. local shopFile = fs.open(fileName, "r")
  77. local shopData = textutils.unserialize(shopFile.readAll())
  78. shopFile.close()
  79. local itemToSell = shopData[1]
  80. local itemPrice = shopData[2]
  81.  
  82. print("Selling " ..
  83. itemToSell.count .. " of " .. itemToSell.name .. " at the price of " .. itemPrice.count .. " " .. itemPrice.name)
  84. setColor(colors.green)
  85. print("Put the items you're selling in slots 2-16!")
  86. setColor(colors.white)
  87.  
  88. while true do
  89. turtle.select(1)
  90. while turtle.getItemCount() ~= 0 do
  91. if not turtle.drop() then
  92. print("Please empty slot 1!")
  93. return
  94. end
  95. end
  96. local itemInput = GetDepotContents()
  97. if itemInput.name ~= itemPrice.name then
  98. turtle.drop()
  99. else
  100. local itemsToDispense = math.floor(itemInput.count / itemPrice.count)
  101. local cashback = itemInput.count - (itemPrice.count * itemsToDispense)
  102. local notEnough = true
  103. while notEnough do
  104. if searchItem(itemToSell.name) then
  105. if turtle.getItemCount() < itemsToDispense then
  106. local stackSlot = turtle.getSelectedSlot() + 1
  107. if stackSlot > 16 then
  108. print("Out of space!")
  109. turtle.select(1)
  110. turtle.drop()
  111. return
  112. end
  113. while not turtle.transferTo(stackSlot) do
  114. if stackSlot ~= 16 then
  115. stackSlot = stackSlot + 1
  116. else
  117. print("Out of space!")
  118. turtle.select(1)
  119. turtle.drop()
  120. return
  121. end
  122. end
  123. else
  124. notEnough = false
  125. turtle.drop(itemsToDispense)
  126. end
  127. else
  128. print("Out of stock!")
  129. turtle.select(1)
  130. turtle.drop()
  131. return
  132. end
  133. end
  134. turtle.select(1)
  135. turtle.drop(cashback)
  136. turtle.dropUp()
  137. end
  138. end
  139.  
Add Comment
Please, Sign In to add comment