ElijahCrafter

Receiver Test modem 2

Dec 3rd, 2025 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.11 KB | Source Code | 0 0
  1. -- receiver.lua
  2. -- Receives camera/hologram data and voice recordings from sender
  3.  
  4. local hologram = peripheral.find("hologram")
  5. local speaker = peripheral.find("speaker")
  6. local modem = peripheral.find("modem")
  7.  
  8. if not hologram then
  9.     print("No hologram found!")
  10.     return
  11. end
  12.  
  13. if not speaker then
  14.     print("No speaker found!")
  15.     return
  16. end
  17.  
  18. if not modem then
  19.     print("No modem found!")
  20.     return
  21. end
  22.  
  23. -- Configuration
  24. local CHANNEL_LISTEN = 1
  25. local HOLOGRAM_SCALE = 0.1
  26.  
  27. -- Entity filter settings (receive from sender)
  28. local SHOW_PLAYERS = true
  29. local SHOW_MOBS = true
  30. local SHOW_ANIMALS = true
  31. local SHOW_ITEMS = false
  32.  
  33. -- Setup
  34. modem.open(CHANNEL_LISTEN)
  35. hologram.on()
  36. hologram.setScale(HOLOGRAM_SCALE)
  37. hologram.showEntities(true)
  38. hologram.setEntityFilter(SHOW_PLAYERS, SHOW_MOBS, SHOW_ANIMALS, SHOW_ITEMS)
  39.  
  40. print("=== Camera & Voice Receiver ===")
  41. print("Listening on channel: " .. CHANNEL_LISTEN)
  42. print("Hologram scale: " .. HOLOGRAM_SCALE)
  43. print("Entity filters: Players=" .. tostring(SHOW_PLAYERS) ..
  44.       ", Mobs=" .. tostring(SHOW_MOBS) ..
  45.       ", Animals=" .. tostring(SHOW_ANIMALS) ..
  46.       ", Items=" .. tostring(SHOW_ITEMS))
  47. print("Press Ctrl+T to stop")
  48. print("")
  49.  
  50. -- Recording controls
  51. local isRecording = false
  52. local recordingStartTime = 0
  53.  
  54. -- Helper function to format entity count by type
  55. local function countEntitiesByType(entities)
  56.     local counts = {players = 0, mobs = 0, animals = 0, items = 0, other = 0}
  57.     for _, e in ipairs(entities) do
  58.         if e.isPlayer then
  59.             counts.players = counts.players + 1
  60.         elseif e.entityType then
  61.             local t = e.entityType:lower()
  62.             if t:find("zombie") or t:find("skeleton") or t:find("creeper") or
  63.                t:find("spider") or t:find("enderman") or t:find("witch") or
  64.                t:find("blaze") or t:find("ghast") or t:find("slime") then
  65.                 counts.mobs = counts.mobs + 1
  66.             elseif t:find("cow") or t:find("pig") or t:find("sheep") or
  67.                    t:find("chicken") or t:find("horse") or t:find("wolf") or
  68.                    t:find("cat") or t:find("rabbit") or t:find("villager") then
  69.                 counts.animals = counts.animals + 1
  70.             elseif t:find("item") then
  71.                 counts.items = counts.items + 1
  72.             else
  73.                 counts.other = counts.other + 1
  74.             end
  75.         end
  76.     end
  77.     return counts
  78. end
  79.  
  80. local lastEntityCount = 0
  81.  
  82. while true do
  83.     -- Poll for messages instead of using events
  84.     if modem.hasMessage() then
  85.         local msg = modem.receive()
  86.         if msg and msg.message and type(msg.message) == "table" then
  87.             local message = msg.message
  88.            
  89.             if message.type == "hologram" or message.type == "entities" then
  90.                 -- Handle entity data (new API)
  91.                 if message.entities and message.center then
  92.                     hologram.setEntityData(message.entities, message.center)
  93.                    
  94.                     local entityCount = #message.entities
  95.                     if entityCount ~= lastEntityCount then
  96.                         lastEntityCount = entityCount
  97.                         if entityCount > 0 then
  98.                             local counts = countEntitiesByType(message.entities)
  99.                             local parts = {}
  100.                             if counts.players > 0 then table.insert(parts, counts.players .. " player(s)") end
  101.                             if counts.mobs > 0 then table.insert(parts, counts.mobs .. " mob(s)") end
  102.                             if counts.animals > 0 then table.insert(parts, counts.animals .. " animal(s)") end
  103.                             if counts.items > 0 then table.insert(parts, counts.items .. " item(s)") end
  104.                             if counts.other > 0 then table.insert(parts, counts.other .. " other") end
  105.                             print("[HOLO] Displaying: " .. table.concat(parts, ", "))
  106.                         else
  107.                             print("[HOLO] No entities in view")
  108.                         end
  109.                     end
  110.                    
  111.                 -- Backward compatibility: handle old player data format
  112.                 elseif message.players and message.center then
  113.                     -- Convert old player format to new entity format
  114.                     local entities = {}
  115.                     for _, p in ipairs(message.players) do
  116.                         table.insert(entities, {
  117.                             name = p.name or "Unknown",
  118.                             uuid = p.uuid or "",
  119.                             x = p.x or 0,
  120.                             y = p.y or 0,
  121.                             z = p.z or 0,
  122.                             yaw = p.yaw or 0,
  123.                             pitch = p.pitch or 0,
  124.                             entityType = "player",
  125.                             isPlayer = true
  126.                         })
  127.                     end
  128.                     hologram.setEntityData(entities, message.center)
  129.                    
  130.                     if #entities ~= lastEntityCount then
  131.                         lastEntityCount = #entities
  132.                         if lastEntityCount > 0 then
  133.                             print("[HOLO] Displaying " .. lastEntityCount .. " player(s) (legacy format)")
  134.                         else
  135.                             print("[HOLO] No players in view")
  136.                         end
  137.                     end
  138.                 end
  139.                
  140.             elseif message.type == "voice" then
  141.                 if message.packets and #message.packets > 0 then
  142.                     print("[VOICE] Playing " .. #message.packets .. " packets from " .. (message.player or "unknown"))
  143.                     speaker.playRecordingPackets(message.packets)
  144.                 end
  145.                
  146.             elseif message.type == "command" then
  147.                 -- Handle remote commands for recording control
  148.                 if message.command == "start_recording" then
  149.                     if not isRecording then
  150.                         hologram.startRecording()
  151.                         isRecording = true
  152.                         recordingStartTime = os.epoch("utc") / 1000
  153.                         print("[REC] Started recording")
  154.                     end
  155.                 elseif message.command == "stop_recording" then
  156.                     if isRecording then
  157.                         hologram.stopRecording()
  158.                         isRecording = false
  159.                         local duration = (os.epoch("utc") / 1000) - recordingStartTime
  160.                         print("[REC] Stopped recording (" .. string.format("%.1f", duration) .. "s)")
  161.                     end
  162.                 elseif message.command == "play" then
  163.                     hologram.play()
  164.                     print("[REC] Playing recording")
  165.                 elseif message.command == "pause" then
  166.                     hologram.pause()
  167.                     print("[REC] Paused playback")
  168.                 elseif message.command == "stop" then
  169.                     hologram.stop()
  170.                     print("[REC] Stopped playback")
  171.                 elseif message.command == "set_loop" then
  172.                     hologram.setLoop(message.value == true)
  173.                     print("[REC] Loop: " .. tostring(message.value))
  174.                 elseif message.command == "set_speed" then
  175.                     hologram.setPlaybackSpeed(message.value or 1.0)
  176.                     print("[REC] Speed: " .. tostring(message.value) .. "x")
  177.                 elseif message.command == "set_filter" then
  178.                     if message.filter then
  179.                         hologram.setEntityFilter(
  180.                             message.filter.players ~= false,
  181.                             message.filter.mobs ~= false,
  182.                             message.filter.animals ~= false,
  183.                             message.filter.items == true
  184.                         )
  185.                         print("[FILTER] Updated entity filters")
  186.                     end
  187.                 end
  188.             end
  189.         end
  190.     else
  191.         -- Small sleep to avoid busy-waiting
  192.         sleep(0.05)
  193.     end
  194. end
Advertisement
Add Comment
Please, Sign In to add comment