Peripheral API
peripheral provides convenient access to nearby blocks using relative sides, plus absolute lookups by position.
Typical usage
local topBlock = peripheral.getRelative("top")
if topBlock ~= nil then
print("Top block id:", topBlock.getId())
end
local frontBlock = peripheral.getRelative("front")
if frontBlock ~= nil and frontBlock.isDisplayModule() then
frontBlock.setDisplayText("Connected")
end
local wrappedDisplay = peripheral.wrap(frontBlock, "display")
if wrappedDisplay ~= nil then
wrappedDisplay.setText("Wrapped display")
end
Side names
Accepted side names:
frontbackleftrighttop(alias:up)bottom(alias:down)
Reference
getCurrentBlock()Returns the current computerBlock.wrap(block<Block>, asType<String>)Returns a typed wrapper forblockbased onasType, ornilwhen incompatible.wrapCurrent(asType<String>)Typed wrapper for the current computer block.getAt(position<LuaVec3i>)Returns aBlockat an absolute position, ornil.getRelative(side<String>)Returns adjacentBlockon that side, ornil.wrapRelative(side<String>, asType<String>)Equivalent towrap(getRelative(side), asType).wrapAt(position<LuaVec3i>, asType<String>)Equivalent towrap(getAt(position), asType).hasRelative(side<String>)Returnstruewhen a block exists on that side.getSides()Returns supported side names.
Notes
Relative sides are resolved from the current computer block position.
front/back/left/right/top/bottomare resolved relative to the computer block’s facing/orientation.If no block exists at a location/side, methods return
nil(orfalseforhasRelative).wrap(..., asType)supports:display,inventory,diskdrive,accesspoint,networkmodule,datastore,networkeddatastore,block/base, andauto. Other mods may register additional type names.displayexposes display helpers likesetText()/getText().inventoryexposes helpers likegetItems()andgetInventoryName().diskdriveexposes disk methods likesaveProgram(),installProgram(), andlistPrograms().networkmoduleexposes networking and FTP access — see Network Module wrapper below.accesspointexposes remote access methods described below.
Network Module wrapper
When a Network Module block is wrapped, it provides access to the networking and FTP APIs:
getNet()Returns aNetworkInterfacehandle for this module. See networking for the full API reference.getFtp()Returns anFtpApihandle bound to this module’s hostname and the calling computer’s filesystem. See FTP for the full API reference.
local nm = peripheral.wrapRelative("front", "networkmodule")
local net = nm.getNet()
local ftp = nm.getFtp()
net.setHostname("myserver")
net.send("target", "proto", "hello")
ftp.listen("secret")
Each Network Module block has its own identity (hostname) on the network. The hostname is persisted across server restarts.
Remote access point wrapper
When a remote access point block is wrapped from a computer script, it can be bound to that computer:
connect()Binds the access point to the current computer. Returnstrueon success.disconnect()Removes the computer binding from the access point.isConnected()Returnstruewhen the access point is currently bound to a computer.getLinkedComputerUUID()Returns the UUID of the linked computer, ornilwhen unbound.getLinkedComputerName()Returns the linked computer display name, ornilwhen unavailable.isSessionActive()Returnstruewhen this access point currently has an active remote control session.
local access = peripheral.wrapRelative("front", "accesspoint")
if access ~= nil then
access:connect()
end
local topBlock = peripheral.getRelative("top")
local display = peripheral.wrap(topBlock, "display")
if display ~= nil then
display.setText("Status: OK")
end
local inv = peripheral.wrap(topBlock, "inventory")
if inv ~= nil then
print("Inventory volume:", inv.getInventoryVolume())
end