Created
July 13, 2022 13:33
-
-
Save Yarillo4/e9a3f2ec7edf94afa499884dfd4e0664 to your computer and use it in GitHub Desktop.
Computercraft mocks for the peripheral API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
fakeperipheral, peripheral mocks github.com/Yarillo4. | |
--]] | |
local newFuncs = {} | |
local oldFuncs = {} | |
local fakeRegistry = {} | |
oldFuncs.wrap = peripheral.wrap | |
oldFuncs.find = peripheral.find | |
oldFuncs.getName = peripheral.getName | |
oldFuncs.getType = peripheral.getType | |
for i,v in pairs(peripheral) do | |
newFuncs[i] = v | |
end | |
local function invemu(base) | |
--[[ | |
invemu, Inventory emulation by Autist69420. | |
# Example: | |
```lua | |
local inv = inventory() | |
inv.addItem("minecraft:barrel", 64) | |
local details = inv.getitemDetail(1) | |
print(details.count, details.displayName) | |
``` | |
]] | |
base = base or {items = {}, slots = 27} | |
local funcs = {} | |
function funcs.list() return base.items end | |
function funcs.size() return base.slots end | |
function funcs.getItemDetail(slot) | |
local item_from_list = base.items[slot] | |
local formatted = item_from_list.name:match(":(.*)") | |
return { | |
count = item_from_list.count, | |
maxCount = 64, -- i know, cope. | |
displayName = formatted, -- not correct but eh, it works for now | |
name = item_from_list.name, | |
tags = {} -- Not gonna implement this, basically impossible AFAIK | |
} | |
end | |
function funcs.getItemLimit(slot) return 64 end | |
function funcs.pushItems(toName, fromSlot, limit, toSlot) end -- TODO | |
function funcs.pullItems(fromName, fromSlot, limit, toSlot) end -- TODO | |
function funcs.addItem(item_name, count) table.insert(base.items, {name = item_name, count = count}) end | |
return funcs | |
end | |
local function monitoremu() | |
local t = {} | |
t.setBackgroundColor = function(colour) return end | |
t.setBackgroundColour = function(colour) return end | |
t.setCursorBlink = function(blink) return end | |
t.setCursorPos = function(x, y) return end | |
t.setPaletteColor = function(...) return end | |
t.setPaletteColour = function(...) return end | |
t.setTextColor = function(colour) return end | |
t.setTextColour = function(colour) return end | |
t.setTextScale = function(scale) return end | |
t.scroll = function(y) return end | |
t.write = function(text) return end | |
t.blit = function(text, textColour, backgroundColour) return end | |
t.clear = function() return end | |
t.clearLine = function() return end | |
t.getBackgroundColor = function() | |
return colors.black | |
end | |
t.getBackgroundColour = function() | |
return colors.black | |
end | |
t.getCursorBlink = function() | |
return true | |
end | |
t.getCursorPos = function() | |
return 1,1 | |
end | |
t.getPaletteColor = function(colour) | |
return 1/15,1/15,1/15 | |
end | |
t.getPaletteColour = function(colour) | |
return 1/15,1/15,1/15 | |
end | |
t.getSize = function() | |
return 51,19 | |
end | |
t.getTextColor = function() | |
return colors.white | |
end | |
t.getTextColour = function() | |
return colors.white | |
end | |
t.getTextScale = function() | |
return 1 | |
end | |
t.isColor = function() | |
return true | |
end | |
t.isColour = function() | |
return true | |
end | |
return t | |
end | |
function newFuncs.fakePeripheral(side, ptype) | |
fakeRegistry[side] = { | |
ptype=ptype, | |
p=nil | |
} | |
if ptype == "inventory" or ptype == "storagedrawers:controller" then | |
fakeRegistry[side].p = invemu() | |
return fakeRegistry[side].p | |
elseif ptype == "monitor" then | |
fakeRegistry[side].p = monitoremu(term.current()) | |
return fakeRegistry[side].p | |
else | |
error("Unsupported peripheral type") | |
end | |
end | |
function newFuncs.wrap(side) | |
if not fakeRegistry[side] then | |
return nil | |
end | |
return fakeRegistry[side].p | |
end | |
function newFuncs.find(ptype) | |
for i,v in pairs(fakeRegistry) do | |
if v.ptype == ptype then | |
return peripheral.wrap(i) | |
end | |
end | |
end | |
function newFuncs.getName(p) | |
for i,v in pairs(fakeRegistry) do | |
if v.p == p then | |
return i | |
end | |
end | |
end | |
function newFuncs.getType(side) | |
if not fakeRegistry[side] then | |
return nil | |
end | |
return fakeRegistry[side].ptype | |
end | |
return newFuncs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment