From 0787cc7913cde5f3050b6be66344ebdf7af2505f Mon Sep 17 00:00:00 2001 From: kepler155c Date: Wed, 21 Nov 2018 17:28:50 -0500 Subject: [PATCH] milo: peripheral improvements --- milo/Milo.lua | 9 +++++++-- milo/plugins/remote.lua | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/milo/Milo.lua b/milo/Milo.lua index 74a5635..84c3e36 100644 --- a/milo/Milo.lua +++ b/milo/Milo.lua @@ -24,6 +24,7 @@ local fs = _G.fs local multishell = _ENV.multishell local os = _G.os local shell = _ENV.shell +local turtle = _G.turtle if multishell then multishell.setTitle(multishell.getCurrent(), 'Milo') @@ -71,11 +72,15 @@ if not modem.getNameLocal() then Syntax('Wired modem is not active') end -local introspection = Peripheral.get('plethora:introspection') or +local introspection = device['plethora:introspection'] or + turtle.equip('left', 'plethora:module:0') and device['plethora:introspection'] or Syntax('Introspection module missing') if not device.workbench then - Syntax('Workbench missing') + turtle.equip('right', 'minecraft:crafting_table:0') + if not device.workbench then + Syntax('Workbench missing') + end end local context = { diff --git a/milo/plugins/remote.lua b/milo/plugins/remote.lua index a9e52ff..ca8f6fa 100644 --- a/milo/plugins/remote.lua +++ b/milo/plugins/remote.lua @@ -122,15 +122,33 @@ local function client(socket) _G._debug('REMOTE: disconnected from ' .. socket.dhost) end -if device.wireless_modem then - Event.addRoutine(function() - _G._debug('REMOTE: listening on port 4242') - while true do - local socket = Socket.server(4242) - Event.addRoutine(function() - client(socket) - socket:close() - end) - end - end) +local handler + +local function listen() + if device.wireless_modem then + handler = Event.addRoutine(function() + _G._debug('REMOTE: listening on port 4242') + while true do + local socket = Socket.server(4242) + Event.addRoutine(function() + client(socket) + socket:close() + end) + end + end) + end end + +Event.on({ 'device_attach', 'device_detach' }, function(_, name) + if name == 'wireless_modem' then + if handler then + handler:terminate() + handler = nil + _debug('REMOTE: wireless modem disconnected') + else + listen() + end + end +end) + +listen()