Advertisement
Jishh_

CC Item metadata

Feb 19th, 2025 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. ------------------------------------------------------------
  2. -- Item Metadata Watcher
  3. --
  4. -- This program waits for a rednet message with protocol "items".
  5. -- The message must contain a list of item IDs.
  6. -- After receiving the list, the program enters an infinite loop.
  7. --
  8. -- In each iteration, it goes through each item, looks up its
  9. -- display name (via getName) and stock status (via isInStock).
  10. -- It caches that information in-memory. If the metadata for an
  11. -- item has changed (or if this is the first time), it sends an
  12. -- "itemMetadata" event back to the original sender with the updated
  13. -- metadata.
  14. ------------------------------------------------------------
  15.  
  16. ------------------------------------------------------------
  17. --- Load the meUtils library for item lookups.
  18. ------------------------------------------------------------
  19. local meUtils = require("lib/meUtils")
  20.  
  21. -- Open rednet on a modem (adjust side as necessary)
  22. rednet.open("top") -- Change "back" to the appropriate side if needed
  23.  
  24. -- Wait for a rednet message with protocol "items"
  25. print("Waiting for 'items' event...")
  26. local senderId, message, protocol = rednet.receive("items")
  27. if type(message) ~= "table" then
  28. error("Expected a table of item IDs in the 'items' event")
  29. end
  30. local itemList = message -- List of item IDs
  31. local origin = senderId -- Remember the sender for future replies
  32. print("Received item list from computer:", origin)
  33. -- Uncomment the following line for debugging:
  34. -- print(textutils.serialize(itemList))
  35.  
  36. -- In-memory cache for item metadata.
  37. -- cache[itemID] = { displayName = ..., inStock = ... }
  38. local cache = {}
  39.  
  40. ------------------------------------------------------------
  41. -- Return whether the given item is in stock.
  42. ------------------------------------------------------------
  43. local function isInStock(itemID)
  44. local count = meUtils.getItemCount(itemID)
  45. return count > 0
  46. end
  47.  
  48. ------------------------------------------------------------
  49. --- Return the name of the given item.
  50. ------------------------------------------------------------
  51. local function getItemName(itemID)
  52. return meUtils.getItemName(itemID)
  53. end
  54.  
  55. -- Function to update an item's metadata.
  56. local function updateItem(itemID)
  57. local name = getItemName(itemID)
  58. local inStock = isInStock(itemID)
  59. return { name = name, inStock = inStock }
  60. end
  61.  
  62. -- Enter infinite loop; each iteration update each item's metadata.
  63. while true do
  64. for i, itemID in ipairs(itemList) do
  65. local newMeta = updateItem(itemID)
  66. local oldMeta = cache[itemID]
  67. -- If this is the first time or if anything has changed:
  68. if not oldMeta or newMeta.displayName ~= oldMeta.displayName or newMeta.inStock ~= oldMeta.inStock then
  69. cache[itemID] = newMeta
  70. rednet.send(origin, { item = itemID, metadata = newMeta }, "itemMetadata")
  71.  
  72. -- Only print if ID begins with "createcafe:" (for debugging)
  73. if string.find(itemID, "^createcafe:") then
  74. print("Sent metadata for item:", itemID)
  75. print(" Old metadata:", textutils.serialize(oldMeta))
  76. print(" New metadata:", textutils.serialize(newMeta))
  77. end
  78. --print("Updated metadata for item:", itemID)
  79. end
  80. end
  81. print("Waiting for next iteration...")
  82. sleep(1) -- Adjust the interval as needed.
  83. end
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement