LK005

Inventory manager

Jun 13th, 2022 (edited)
465
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.32 KB | None | 0 0
  1. local CB
  2. local IM
  3.  
  4. function splitString(str)
  5.   local fields = {}
  6.   str:gsub("([^ ]+)", function(c) table.insert(fields, c) end)
  7.   return fields
  8. end
  9.  
  10. function init()
  11.   IM = peripheral.find("inventoryManager")
  12.   if IM == nil then error("inventoryManager not found") end
  13.   CB = peripheral.find("chatBox")
  14.   if CB == nil then error("chatBox not found") end
  15. end
  16.  
  17. function mainLoop()
  18.   while true do
  19.     event, username, message = os.pullEvent("chat")
  20.  
  21.     if username == "_LK005_" then
  22.       splitMessage = splitString(message)
  23.       local identifier = splitMessage[1]
  24.       local command = splitMessage[2]
  25.       local itemName = splitMessage[3]
  26.       local itemAmount = splitMessage[4]
  27.  
  28.       if identifier == "-" then
  29.         print("Command: "..command)
  30.         if command == "get" then
  31.           print("giving "..itemAmount.." "..itemName.." to "..username)
  32.           IM.addItemToPlayer("LEFT", tonumber(itemAmount), -1, itemName)
  33.         elseif command == "put" then
  34.           print("putting "..itemAmount.." "..itemName.." into the system from "..username)
  35.           IM.removeItemFromPlayer("LEFT", tonumber(itemAmount), -1, itemName)
  36.         elseif command == "select" then
  37.           for key,value in pairs(IM.getItemInHand()) do print(key,value) end
  38.         end
  39.       end
  40.     end
  41.   end
  42. end
  43.  
  44. init()
  45. mainLoop()
  46.  
Add Comment
Please, Sign In to add comment