Skip to content

Instantly share code, notes, and snippets.

@bananasov
Created October 8, 2022 21:01
Show Gist options
  • Save bananasov/44f18ea2457382b5863163ef97065b79 to your computer and use it in GitHub Desktop.
Save bananasov/44f18ea2457382b5863163ef97065b79 to your computer and use it in GitHub Desktop.
A simple library that adds custom peripheral names to the peripheral library.
local pperipheral = {custom_names = {}}
local oldCall, oldGetType, oldWrap, oldFind = peripheral.call, peripheral.getType, peripheral.wrap, peripheral.find
--- Peripheral call method that supports custom names
---@param name string The name of the peripheral to invoke the method on.
---@param method string The name of the method
---@param ... any Additional arguments
---@return any? The return values of the peripheral method.
function pperipheral.call(name, method, ...)
local name = pperipheral.custom_names[name] or name
return oldCall(name, method, ...)
end
--- Peripheral getType method that supports custom names
---@param peripheral string|table The name of the peripheral to find, or a wrapped peripheral instance.
---@return string[]|nil The peripheral's types, or nil if it is not present.
function pperipheral.getType(peripheral)
local peripheral = pperipheral.custom_names[peripheral] or peripheral
return oldGetType(peripheral)
end
--- Peripheral wrap method that supports custom names
---@param name string The name of the peripheral to wrap.
---@return table|nil The table containing the peripheral's methods, or nil if there is no peripheral present with the given name.
function pperipheral.wrap(name)
local name = pperipheral.custom_names[name] or name
return oldWrap(name)
end
--- Function that adds a new link between peripherals
---@param name string Custom name for peripheral
---@param peripheral string Peripheral name for the custom name to link to
---@return nil
function pperipheral.newLink(name, peripheral)
-- weirdly enough it is this easy, maybe, idk.
-- maybe this wont work but i really have no fucking clue, if it doesnt
-- i probably have to use metatables or something
pperipheral.custom_names[name] = peripheral
end
return pperipheral
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment