Create: Radionautics

Create: Radionautics

增加一个模组,为机械动力模组添加无限范围传输的无线电链路。

创建

Create: Radionautics CC:Tweaked 指南

Create: Radionautics 为 CC:Tweaked 计算机添加了无线电外设。这些无线电让计算机能够使用 Create 主题的天线方块进行长距离消息传输。

需求

  • Minecraft 1.21.1
  • NeoForge
  • Create
  • Create Aeronautics
  • CC:Tweaked
  • Create: Radionautics

无线电方块

Radionautics 添加了三种 CC:Tweaked 无线电天线外设:

方块 范围 最大绑定频率数
安山岩无线电天线 2000 方块 1
铜质无线电天线 5000 方块 2
黄铜无线电天线 无限 5

将无线电天线放置在 CC:Tweaked 计算机旁边。

获取外设

使用 peripheral.find:

local radio = peripheral.find("radionautics_radio")

或者包裹已知方向:

local radio = peripheral.wrap("left")

可以检查无线电等级:

print(radio.tier())
print(radio.range())
print(radio.maxBinds())

发送消息

发送端:

local radio = peripheral.find("radionautics_radio")

radio.send("145.500", "chat:hello from base")

第一个参数是频率。第二个参数是消息内容。

接收消息

接收端:

local radio = peripheral.find("radionautics_radio")

radio.udpBind("145.500")

while true do
  local event, frequency, senderId, message = os.pullEvent("radio_message")
  print("Frequency: " .. frequency)
  print("Sender: " .. senderId)
  print("Message: " .. message)
end

接收端必须先绑定频率:

radio.udpBind("145.500")

传入消息会触发 radio_message 事件:

local event, frequency, senderId, message = os.pullEvent("radio_message")

聊天示例

在接收计算机上运行:

local radio = peripheral.find("radionautics_radio")
radio.udpBind("145.500")

print("Listening on 145.500")

while true do
  local event, frequency, senderId, message = os.pullEvent("radio_message")
  print("[" .. frequency .. "] " .. senderId .. ": " .. message)
end

在发送计算机上运行:

local radio = peripheral.find("radionautics_radio")

while true do
  write("Message: ")
  local message = read()
  radio.send("145.500", "chat:" .. message)
end

加密辅助方法

Radionautics 包含用于编码/加密的辅助方法。

可用的加密类型:

  • none
  • xor
  • aes
  • base64

加密发送端:

local radio = peripheral.find("radionautics_radio")

local key = "secret-key"
local message = "chat:encrypted hello"
local encrypted = radio.cryptoEncrypt("aes", message, key)

radio.send("145.500", encrypted)

加密接收端:

local radio = peripheral.find("radionautics_radio")

radio.udpBind("145.500")

local key = "secret-key"

while true do
  local event, frequency, senderId, encrypted = os.pullEvent("radio_message")
  local ok, message = pcall(function()
    return radio.cryptoDecrypt("aes", encrypted, key)
  end)

  if ok then
    print(message)
  end
end

如果使用了错误的密钥,解密可能会失败。如果需要安全地忽略错误数据包,请使用 pcall。

频率

频率是文本形式。可以是数字或名称:

radio.udpBind("145.500")
radio.udpBind("AIRSHIP")
radio.udpBind("main_base")

发送端和接收端必须使用相同的频率。

绑定限制

每种天线等级都有最大活动绑定频率数。

radio.udpBind("145.500")
radio.udpBind("fleet")

如果天线绑定槽已满,绑定会返回 false。

使用完毕后关闭频率:

radio.close("145.500")

列出活动绑定:

for _, frequency in ipairs(radio.boundFrequencies()) do
  print(frequency)
end

队列消息

可以手动轮询消息:

local packet = { radio.receive() }

if packet[1] then
  local frequency = packet[1]
  local senderId = packet[2]
  local message = packet[3]
  print(message)
end

通常,使用 os.pullEvent("radio_message") 更加简单。

检查队列消息数量:

print(radio.queuedMessages())

Sable / 飞船辅助方法

Radionautics 公开了早期 Sable/Create Aeronautics 辅助方法:

radio.sableContinousLoad(true)
radio.sableContinousLoad(false)

检查是否启用了持续加载:

print(radio.sableIsContinousLoading())

获取当前飞船中心位置:

local pos = radio.sableShipCenterPos()
print(pos.x, pos.y, pos.z)

注意:飞船集成功能仍在开发中。当前行为可能回退到方块位置。

可选辅助 API

Radionautics 还提供了一个辅助 API:

local radio = radionautics.find()
radio.udpBind("145.500")
radio.send("145.500", "chat:hello")

加密辅助:

local encrypted = radionautics.crypto.encrypt("aes", "hello", "key")
local clear = radionautics.crypto.decrypt("aes", encrypted, "key")

Sable 辅助:

radionautics.sable.continousLoad(true)

故障排除

No radionautics_radio peripheral attached

确保天线方块紧邻计算机放置。

未收到消息

检查:

  • 接收端是否调用了 radio.udpBind(frequency)。
  • 发送端和接收端是否使用了完全相同的频率。
  • 天线是否处于同一维度。
  • 天线是否在有效范围内。
  • 接收端程序是否仍在运行。

加密消息失败

检查:

  • 两台计算机是否使用了相同的加密类型。
  • 两台计算机是否使用了相同的密钥。
  • 接收端是否需要使用 pcall 来忽略错误数据包。

绑定失败

天线可能已达到活动频率上限。

使用:

print(radio.maxBinds())
print(radio.boundFrequencies())

然后关闭未使用的频率:

radio.close("old_frequency")