Skip to content

Commit

Permalink
📦 add environment/player detector, geo scanner, inventory manager, an…
Browse files Browse the repository at this point in the history
…d redstone integrator adapters (closes #64)
  • Loading branch information
cyberbit committed Jul 18, 2024
1 parent 3c04739 commit e2b076c
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/telem/lib/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ local export = {

advancedPeripherals = {
energyDetector = requireAP('EnergyDetectorInputAdapter'),
environmentDetector = requireAP('EnvironmentDetectorInputAdapter'),
geoScanner = requireAP('GeoScannerInputAdapter'),
inventoryManager = requireAP('InventoryManagerInputAdapter'),
meBridge = requireAP('MEBridgeInputAdapter'),
playerDetector = requireAP('PlayerDetectorInputAdapter'),
redstoneIntegrator = requireAP('RedstoneIntegratorInputAdapter'),
rsBridge = requireAP('RSBridgeInputAdapter'),
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local MetricCollection = require 'telem.lib.MetricCollection'
local BaseAdvancedPeripheralsInputAdapter = o.class(InputAdapter)
BaseAdvancedPeripheralsInputAdapter.type = 'BaseAdvancedPeripheralsInputAdapter'

function BaseAdvancedPeripheralsInputAdapter:constructor (peripheralName, categories)
function BaseAdvancedPeripheralsInputAdapter:constructor (peripheralName, categories, ...)
self:super('constructor')

self.prefix = 'ap:'
Expand All @@ -29,12 +29,12 @@ function BaseAdvancedPeripheralsInputAdapter:constructor (peripheralName, catego
self:addComponentByPeripheralID(peripheralName)
end)()

self:beforeRegister()
self:beforeRegister(peripheralName, categories, ...)

self:register()
end

function BaseAdvancedPeripheralsInputAdapter:beforeRegister ()
function BaseAdvancedPeripheralsInputAdapter:beforeRegister (peripheralName, categories, ...)
-- nothing by default, should be overridden by subclasses
end

Expand Down Expand Up @@ -99,8 +99,8 @@ function BaseAdvancedPeripheralsInputAdapter.mintAdapter (type)
local adapter = o.class(BaseAdvancedPeripheralsInputAdapter)
adapter.type = type

function adapter:constructor (peripheralName, categories)
self:super('constructor', peripheralName, categories)
function adapter:constructor (peripheralName, categories, ...)
self:super('constructor', peripheralName, categories, ...)
end

return adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheral

local EnergyDetectorInputAdapter = base.mintAdapter('EnergyDetectorInputAdapter')

function EnergyDetectorInputAdapter:beforeRegister ()
function EnergyDetectorInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apenergy:'

self.queries = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'

local EnvironmentDetectorInputAdapter = base.mintAdapter('EnvironmentDetectorInputAdapter')

function EnvironmentDetectorInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apenv:'

self.queries = {
basic = {
block_light_level = fn():call('getBlockLightLevel'),
day_light_level = fn():call('getDayLightLevel'),
sky_light_level = fn():call('getSkyLightLevel'),
moon_id = fn():call('getMoonId'),
time = fn():call('getTime'),
radiation = fn():call('getRadiationRaw'):with('unit', 'Sv/h'),
can_sleep = fn():call('canSleepHere'):toFlag(),
raining = fn():call('isRaining'):toFlag(),
sunny = fn():call('isSunny'):toFlag(),
thundering = fn():call('isThunder'):toFlag(),
slime_chunk = fn():call('isSlimeChunk'):toFlag(),
},
}

-- getBiome
-- getDimensionName
-- getDimensionPaN
-- getDimensionProvider
-- getMoonName
-- getOperationCooldown
-- isDimension
-- listDimensions
-- scanCost
-- scanEntities
end

return EnvironmentDetectorInputAdapter
24 changes: 24 additions & 0 deletions src/telem/lib/input/advancedPeripherals/GeoScannerInputAdapter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'
local Metric = require 'telem.lib.Metric'

local GeoScannerInputAdapter = base.mintAdapter('GeoScannerInputAdapter')

function GeoScannerInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apgeo:'

self.queries = {
basic = {},
}

self.storageQueries = {
fn():callElse('chunkAnalyze', {})
:map(function (k, v) return Metric{ name = k, value = v } end)
:values()
}

-- scan
end

return GeoScannerInputAdapter
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'
local Metric = require 'telem.lib.Metric'

local InventoryManagerInputAdapter = base.mintAdapter('InventoryManagerInputAdapter')

function InventoryManagerInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apinv:'

local armor = fn():call('getArmor')

self.queries = {
basic = {
equipped = fn():call('isPlayerEquipped'):toFlag(),
equipped_helmet = armor:firstWhere('slot', 103):toFlag(),
equipped_chestplate = armor:firstWhere('slot', 102):toFlag(),
equipped_leggings = armor:firstWhere('slot', 101):toFlag(),
equipped_boots = armor:firstWhere('slot', 100):toFlag(),
inventory_available = fn():call('isSpaceAvailable'):toFlag(),
inventory_slots_available = fn():call('getEmptySpace'),
},
}

self.storageQueries = {
fn():call('getItems')
:sum('count', 'name')
:map(function (k, v) return Metric{ name = k, value = v, unit = 'item' } end)
:values()
}
end

return InventoryManagerInputAdapter
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local Metric = require 'telem.lib.Metric'

local MEBridgeInputAdapter = base.mintAdapter('MEBridgeInputAdapter')

function MEBridgeInputAdapter:beforeRegister ()
function MEBridgeInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'apmebridge:'

self.queries = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local fn = require 'telem.vendor'.fluent.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'

local PlayerDetectorInputAdapter = base.mintAdapter('PlayerDetectorInputAdapter')

function PlayerDetectorInputAdapter:beforeRegister (peripheralName, categories, playerName)
self.prefix = 'applayer:'

self.queries = {
basic = {
online_player_count = fn():call('getOnlinePlayers'):count(),
},
}

-- getPlayersInRange
-- getPlayersInCoords
-- getPlayersInCubic
-- isPlayerInRange
-- isPlayerInCoords
-- isPlayerInCubic
-- isPlayersInRange
-- isPlayersInCoords
-- isPlayersInCubic

-- TODO improve this
if playerName then
local playerPos = fn():call('getPlayerPos', playerName)

self.queries.player = self.queries.player or {}

self.queries.player.player_eye_height = playerPos:get('eyeHeight'):with('unit', 'm')
self.queries.player.player_pitch = playerPos:get('pitch'):with('unit', '°')
self.queries.player.player_yaw = playerPos:get('yaw'):with('unit', '°')
self.queries.player.player_health = playerPos:get('health')
self.queries.player.player_air_supply = playerPos:get('airSupply')

-- TODO there is a typo fix pending release, so for now there will be a fallback to the typo
self.queries.player.player_max_health = playerPos:transform(function (v)
return v.maxHealth or v.maxHeatlh
end)

-- dimension
-- respawnPosition
-- respawnDimension
-- respawnAngle
-- x
-- y
-- z
end
end

return PlayerDetectorInputAdapter
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local Metric = require 'telem.lib.Metric'

local RSBridgeInputAdapter = base.mintAdapter('RSBridgeInputAdapter')

function RSBridgeInputAdapter:beforeRegister ()
function RSBridgeInputAdapter:beforeRegister (peripheralName, categories)
self.prefix = 'aprsbridge:'

self.queries = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local fl = require 'telem.vendor'.fluent
local fn = fl.fn

local base = require 'telem.lib.input.advancedPeripherals.BaseAdvancedPeripheralsInputAdapter'

local RedstoneIntegratorInputAdapter = base.mintAdapter('RedstoneIntegratorInputAdapter')

function RedstoneIntegratorInputAdapter:beforeRegister (peripheralName, categories, sides)
self.prefix = 'apredstone:'

self.sides = sides or '*'

local allRelativeSides = {
'right', 'left', 'front', 'back', 'top', 'bottom',
}

local allCardinalSides = {
'north', 'south', 'east', 'west', 'up', 'down'
}

local allSides = fl({allRelativeSides, allCardinalSides}):flatten():result()

if self.sides == '*' then
self.sides = allRelativeSides
elseif type(self.sides) == 'table' then
self.sides = fl(self.sides):intersect(allSides):result()
else
error('sides must be a list of sides or "*"')
end

self.queries = {
basic = {}
}

for _, side in ipairs(self.sides) do
print('doign side', side)
self.queries.basic['input_' .. side] = fn():call('getInput', side):toFlag()
self.queries.basic['input_analog_' .. side] = fn():call('getAnalogInput', side)

-- TODO is there any reason to include these?
-- self.queries.basic['output_' .. side] = fn():call('getOutput', side):toFlag()
-- self.queries.basic['output_analog_' .. side] = fn():call('getAnalogOutput', side)
end
end

return RedstoneIntegratorInputAdapter

0 comments on commit e2b076c

Please sign in to comment.