Advertisement
zwenboy

StorageManager

Oct 3rd, 2023 (edited)
1,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local manager = peripheral.find("inventoryManager")
  2. local chatBox = peripheral.find("chatBox")
  3. local rsBridge = peripheral.find("rsBridge")
  4.  
  5. if manager == nil then
  6.     error("inventoryManager not found")
  7. end
  8. if chatBox == nil then
  9.     error("chatBox not found")
  10. end
  11.  
  12. local message = {
  13.     {text = "Moved all items!", color = "red"}
  14. }
  15. local json = textutils.serialiseJSON(message)
  16.  
  17. function handleGiveCommand(username, message)
  18.     if string.sub(message, 1, 5) == "!give" then
  19.         local _, _, command, itemName, quantity = string.find(message, "(!%w+)%s+(%w+)%s*(%d*)")
  20.         if command and itemName then
  21.             if quantity == "" then
  22.                 quantity = "1"
  23.             end
  24.             local requestedQuantity = tonumber(quantity)
  25.             if requestedQuantity and requestedQuantity >= 1 and requestedQuantity <= 64 then
  26.                 local requestedItem = {name = itemName, amount = requestedQuantity}
  27.                 local itemInfo = rsBridge.getItem(requestedItem)
  28.                 if itemInfo then
  29.                     if itemInfo.amount >= requestedQuantity then
  30.                         -- Perform the desired action with the extracted information
  31.                         itemInfo.amount = requestedQuantity
  32.                         local exportedAmount = rsBridge.exportItemToPeripheral(requestedItem, "minecraft:chest_1")
  33.                         if exportedAmount >= 0 then
  34.                             print("Successfully exported " .. exportedAmount .. " items.")
  35.                         else
  36.                             print("Failed to export item. Error code: " .. exportedAmount)
  37.                         end
  38.                         local successMessage = "Successfully gave " .. requestedQuantity .. " " .. itemName .. " to " .. username
  39.                         chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = successMessage, color = "green"}}))
  40.                     else
  41.                         local insufficientMessage = "Insufficient " .. itemName .. " available. There are only " .. itemInfo.amount .. " items."
  42.                         chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = insufficientMessage, color = "red"}}))
  43.                     end
  44.                 else
  45.                     chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "We don't have that item.", color = "red"}}))
  46.                 end
  47.             else
  48.                 chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "Invalid quantity. Quantity must be a number between 1 and 64.", color = "red"}}))
  49.             end
  50.         else
  51.             chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "Invalid command format. Use '!give item_name [quantity]'.", color = "red"}}))
  52.         end
  53.     end
  54. end
  55.  
  56.  
  57.  
  58. function waitForCommand()
  59.     local event, username, message, uuid, isHidden = os.pullEvent("chat")
  60.     if username == manager.getOwner() then
  61.         handleGiveCommand(username, message)
  62.     end
  63.     waitForCommand()
  64. end
  65.  
  66. waitForCommand()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement