Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local manager = peripheral.find("inventoryManager")
- local chatBox = peripheral.find("chatBox")
- local rsBridge = peripheral.find("rsBridge")
- if manager == nil then
- error("inventoryManager not found")
- end
- if chatBox == nil then
- error("chatBox not found")
- end
- local message = {
- {text = "Moved all items!", color = "red"}
- }
- local json = textutils.serialiseJSON(message)
- function handleGiveCommand(username, message)
- if string.sub(message, 1, 5) == "!give" then
- local _, _, command, itemName, quantity = string.find(message, "(!%w+)%s+(%w+)%s*(%d*)")
- if command and itemName then
- if quantity == "" then
- quantity = "1"
- end
- local requestedQuantity = tonumber(quantity)
- if requestedQuantity and requestedQuantity >= 1 and requestedQuantity <= 64 then
- local requestedItem = {name = itemName, amount = requestedQuantity}
- local itemInfo = rsBridge.getItem(requestedItem)
- if itemInfo then
- if itemInfo.amount >= requestedQuantity then
- -- Perform the desired action with the extracted information
- itemInfo.amount = requestedQuantity
- local exportedAmount = rsBridge.exportItemToPeripheral(requestedItem, "minecraft:chest_1")
- if exportedAmount >= 0 then
- print("Successfully exported " .. exportedAmount .. " items.")
- else
- print("Failed to export item. Error code: " .. exportedAmount)
- end
- local successMessage = "Successfully gave " .. requestedQuantity .. " " .. itemName .. " to " .. username
- chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = successMessage, color = "green"}}))
- else
- local insufficientMessage = "Insufficient " .. itemName .. " available. There are only " .. itemInfo.amount .. " items."
- chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = insufficientMessage, color = "red"}}))
- end
- else
- chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "We don't have that item.", color = "red"}}))
- end
- else
- chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "Invalid quantity. Quantity must be a number between 1 and 64.", color = "red"}}))
- end
- else
- chatBox.sendFormattedMessage(textutils.serialiseJSON({{text = "Invalid command format. Use '!give item_name [quantity]'.", color = "red"}}))
- end
- end
- end
- function waitForCommand()
- local event, username, message, uuid, isHidden = os.pullEvent("chat")
- if username == manager.getOwner() then
- handleGiveCommand(username, message)
- end
- waitForCommand()
- end
- waitForCommand()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement