Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local os = require("os")
- local component = require("component")
- local colors = require("colors")
- local computer = require("computer")
- local fs = require("filesystem")
- local shell = require("shell")
- local thread = require("thread")
- local sides = require("sides")
- local internet = require("internet")
- local API
- local wm
- local args = { ... }
- if args[1] == "del" then
- os.execute("del /lib/wm.lua")
- os.execute("del /lib/API.lua")
- end
- if require("filesystem").exists("/lib/wm.lua") then
- wm = require("wm")
- elseif component.isAvailable("internet") then
- os.execute("wget https://raw.githubusercontent.com/THEJean-Kevin/Gui/master/wm.lua /lib/wm.lua")
- wm = require("wm")
- end
- if require("filesystem").exists("/lib/API.lua") then
- API = require("API")
- elseif component.isAvailable("internet") then
- os.execute("wget https://raw.githubusercontent.com/THEJean-Kevin/Gui/master/API.lua /lib/API.lua")
- API = require("API")
- end
- if fs.exists('/lib/json.lua') == false then
- shell.execute('wget -fq "https://pastebin.com/raw/4UWzY19u" "/lib/json.lua"')
- end
- local json = require("json")
- local shrc = io.open("/home/.shrc","w")
- shrc:write("/home/start.lua")
- shrc:close()
- local gpu = component.gpu
- local ScreenWidth, ScreenHeight = gpu.getResolution()
- local buttonadd = 0
- local GeneralList = {}
- local input_address, input_name, input_side, input_nameg,checkbox_offline
- local fnt = {}
- local activeMachine = nil
- local online = false
- component.screen.setTouchModeInverted(true)
- local windowGeneral = wm.newWindow(5, 5, math.floor(ScreenWidth/2) - 5, 20, "General")
- local windowAddGeneral = wm.newWindow(math.floor(ScreenWidth/2) - math.floor(ScreenWidth/4), 5, math.floor(ScreenWidth/2) - 5, 20, "Add Categorie")
- local windowAddMachine = wm.newWindow(math.floor(ScreenWidth/2) + 5, 5, math.floor(ScreenWidth/2) - 5, 20, "Add machine")
- function setRedstoneOutput(disable, windows)
- actif = 0
- if windows.name == "Spawner" then
- for i = #windows.element, 1, -1 do
- if actif == 3 then
- windows.element[i].state = false
- API.setElementState(windows.element[i].button, windows.element[i].state)
- end
- if windows.element[i].state and not disable and online then
- redstone = component.proxy(component.get(windows.element[i].address))
- redstone.setOutput(tonumber(windows.element[i].face), 15)
- actif = actif + 1
- else
- redstone = component.proxy(component.get(windows.element[i].address))
- redstone.setOutput(tonumber(windows.element[i].face), 0)
- end
- end
- fnt.machinedraw(windows)
- wm.redraw()
- else
- for i = #windows.element, 1, -1 do
- needoffline = true
- if windows.element[i].offline and not online then
- needoffline = false
- end
- if windows.element[i].state and needoffline then
- redstone = component.proxy(component.get(windows.element[i].address))
- redstone.setOutput(tonumber(windows.element[i].face), 15)
- else
- redstone = component.proxy(component.get(windows.element[i].address))
- redstone.setOutput(tonumber(windows.element[i].face), 0)
- end
- end
- end
- end
- function moin(self,win)
- if wm.getWindowsValue(win) ~= 1 then
- wm.setWindowsValue(win, wm.getWindowsValue(win) - 1 )
- end
- if win == windowGeneral then
- fnt.Generaldraw()
- else
- fnt.machinedraw(activeMachine)
- end
- wm.redraw()
- end
- function plus(self,win)
- if win == windowGeneral then
- if math.floor(wm.getWindowsValue(win) * 9) <= #GeneralList then
- wm.setWindowsValue(win, wm.getWindowsValue(win) + 1 )
- fnt.Generaldraw()
- end
- else
- if math.floor(wm.getWindowsValue(win) * 9) <= #activeMachine.element then
- wm.setWindowsValue(win, wm.getWindowsValue(win) + 1 )
- fnt.machinedraw(activeMachine)
- end
- end
- wm.redraw()
- end
- function okGeneralCallback(self,win)
- local name = API.getElementText(input_nameg)
- if buttonadd == 1 then
- fnt.Generaladd(name)
- elseif buttonadd == 2 then
- fnt.GeneralRemove(name)
- end
- local file = io.open("/home/General","w")
- for i = #GeneralList, 1, -1 do
- file:write(GeneralList[i].name.."\n")
- end
- file:close()
- fnt.Generaldraw()
- wm.hideWindow(windowAddGeneral, true)
- wm.redraw()
- end
- function cancelGeneralCallback(self,win)
- wm.hideWindow(windowAddGeneral, true)
- end
- function add_general(self,win)
- buttonadd = 1
- for i = #GeneralList, 1, -1 do
- wm.hideWindow(GeneralList[i].windows, true)
- end
- wm.hideWindow(windowAddGeneral, false)
- end
- function remove_general(self,win)
- buttonadd = 2
- for i = #GeneralList, 1, -1 do
- wm.hideWindow(GeneralList[i].windows, true)
- end
- wm.hideWindow(windowAddGeneral, false)
- end
- function okMachineCallback(self,win)
- local text = API.getElementText(input_name)
- local adress = API.getElementText(input_address)
- local side = API.getElementText(input_side)
- local ofline = API.getElementState(checkbox_offline)
- if buttonadd == 1 then
- fnt.machineadd(text,adress,side,ofline)
- elseif buttonadd == 2 then
- fnt.machineRemove(text)
- end
- local file = io.open("/home/"..activeMachine.name,"w")
- for i = #activeMachine.element, 1, -1 do
- file:write(activeMachine.element[i].name .."\n")
- file:write(activeMachine.element[i].address .."\n")
- file:write(activeMachine.element[i].face .."\n")
- file:write(tostring(activeMachine.element[i].offline) .."\n")
- end
- file:close()
- fnt.machinedraw(activeMachine)
- wm.hideWindow(windowAddMachine, true)
- wm.hideWindow(activeMachine.windows, false)
- wm.redraw()
- end
- function cancelMachineCallback(self,win)
- wm.hideWindow(windowAddMachine, true)
- wm.hideWindow(activeMachine.windows, false)
- end
- function add_machine(self,win)
- buttonadd = 1
- for i = #GeneralList, 1, -1 do
- wm.hideWindow(GeneralList[i].windows, true)
- end
- wm.hideWindow(windowAddMachine, false)
- end
- function remove_machine(self,win)
- buttonadd = 2
- for i = #GeneralList, 1, -1 do
- wm.hideWindow(GeneralList[i].windows, true)
- end
- wm.hideWindow(windowAddMachine, false)
- end
- --General Windows
- function general_action(self,win)
- activeMachine = nil
- for i = #GeneralList, 1, -1 do
- if GeneralList[i].name == API.getElementText(self) and API.getElementState(GeneralList[i].button) then
- fnt.machinedraw(GeneralList[i])
- activeMachine = GeneralList[i]
- wm.hideWindow(GeneralList[i].windows, false)
- else
- wm.hideWindow(GeneralList[i].windows, true)
- API.setElementState(GeneralList[i].button)
- end
- end
- end
- function fnt.Generaldraw()
- page = wm.getWindowsValue(windowGeneral)
- wm.clearElement(windowGeneral)
- width = math.floor((math.floor(ScreenWidth/2) - 5) / 4 ) - 1
- btn_moin = API.newButton(1, 1, width, 1, "<", moin, true)
- btn_add_general = API.newButton(width + 2, 1, width, 1, "ADD", add_general, true)
- btn_remove_general = API.newButton(width + width + 3, 1, width, 1, "REMOVE", remove_general, true)
- btn_plus = API.newButton(width + width + width + 4, 1, width, 1, ">", plus, true)
- wm.addElement(windowGeneral, btn_moin)
- wm.addElement(windowGeneral, btn_plus)
- wm.addElement(windowGeneral, btn_add_general)
- wm.addElement(windowGeneral, btn_remove_general)
- for i = 1, 9, 1 do
- local element = math.floor((page - 1) * 9) + i
- if element > #GeneralList then
- break
- end
- GeneralList[element].button = API.newButton(1, 1 + (i * 2), (math.floor(ScreenWidth/2) - 5), 1, GeneralList[element].name, general_action, false)
- wm.addElement(windowGeneral, GeneralList[element].button)
- end
- end
- function fnt.Generaladd(name)
- result = {
- name = name,
- button = nil,
- windows = nil,
- element = {},
- }
- table.insert(GeneralList, result)
- fnt.GeneralsetWindows(result)
- end
- function fnt.GeneralRemove(name)
- for i = #GeneralList, 1, -1 do
- if GeneralList[i].name == name then
- for x = #GeneralList[i].element, 1, -1 do
- GeneralList[i].element[x].state = false
- end
- setRedstoneOutput(false, GeneralList[i])
- table.remove(GeneralList, i)
- end
- end
- os.execute("del "..name)
- end
- function fnt.GeneralsetWindows(result)
- result.windows = wm.newWindow(math.floor(ScreenWidth/2) + 5, 5, math.floor(ScreenWidth/2) - 5, 20, result.name)
- fnt.getMachine(result)
- end
- function fnt.getGeneral()
- if fs.exists('/home/General') == false then
- local file = io.open("/home/General","w")
- file:close()
- end
- for line in io.lines("/home/General") do
- fnt.Generaladd(line)
- end
- end
- --SousWindows
- function action_machine(self,win)
- for i = #activeMachine.element, 1, -1 do
- if activeMachine.element[i].name == API.getElementText(self) then
- activeMachine.element[i].state = not activeMachine.element[i].state
- API.setElementState(activeMachine.element[i].button, activeMachine.element[i].state)
- setRedstoneOutput(false,activeMachine)
- end
- end
- end
- function fnt.machinedraw(tables)
- page = wm.getWindowsValue(tables.windows)
- wm.clearElement(tables.windows)
- width = math.floor((math.floor(ScreenWidth/2) - 5) / 4 ) - 1
- btn_moin = API.newButton(1, 1, width, 1, "<", moin, true)
- btn_add_general = API.newButton(width + 2, 1, width, 1, "ADD", add_machine, true)
- btn_remove_general = API.newButton(width + width + 3, 1, width, 1, "REMOVE", remove_machine, true)
- btn_plus = API.newButton(width + width + width + 4, 1, width, 1, ">", plus, true)
- wm.addElement(tables.windows, btn_moin)
- wm.addElement(tables.windows, btn_plus)
- wm.addElement(tables.windows, btn_add_general)
- wm.addElement(tables.windows, btn_remove_general)
- for i = 1, 9, 1 do
- local elements = math.floor((page - 1) * 9) + i
- if elements > #tables.element then
- break
- end
- tables.element[elements].button = API.newButton(1, 1 + (i * 2), (math.floor(ScreenWidth/2) - 5), 1, tables.element[elements].name, action_machine, false)
- wm.addElement(tables.windows, tables.element[elements].button)
- API.setElementState(tables.element[elements].button, tables.element[elements].state)
- end
- end
- function fnt.machineadd(text,adress,side,ofline)
- result = {
- name = text,
- address = adress,
- face = side,
- state = false,
- button = nil,
- offline = ofline,
- }
- table.insert(activeMachine.element, result)
- end
- function fnt.machineRemove(name)
- for i = #activeMachine.element, 1, -1 do
- if activeMachine.element[i].name == name then
- activeMachine.element[i].state = false
- setRedstoneOutput(false, activeMachine)
- table.remove(activeMachine.element, i)
- end
- end
- end
- function fnt.getMachine(result)
- result.element = {}
- local step = 1
- if fs.exists('/home/'..result.name) == false then
- local file = io.open("/home/"..result.name,"w")
- file:close()
- end
- for line in io.lines("/home/"..result.name) do
- if step == 1 then
- name = line
- step = 2
- elseif step == 2 then
- address = line
- step = 3
- elseif step == 3 then
- face = line
- step = 4
- elseif step == 4 then
- if line == "true" then
- ofline = true
- else
- ofline = false
- end
- step = 1
- bouton = {
- name = name,
- address = address,
- face = face,
- state = false,
- button = nil,
- offline = ofline
- }
- table.insert(result.element,bouton)
- end
- end
- end
- --addgeneral
- function fnt.addGeneraldraw()
- label_name = API.newLabel(2,1, math.floor(ScreenWidth/2) - 5, 1, "Nom de la categorie")
- input_nameg = API.newInput(2,3,math.floor(ScreenWidth/2) - 5, "")
- btn_ok_general = API.newButton(12, 19, 4, 1, "ok", okGeneralCallback)
- btn_cancel_general = API.newButton(26, 19, 6, 1, "cancel", cancelGeneralCallback)
- wm.addElement(windowAddGeneral, label_name)
- wm.addElement(windowAddGeneral, input_nameg)
- wm.addElement(windowAddGeneral, btn_ok_general)
- wm.addElement(windowAddGeneral, btn_cancel_general)
- end
- --addMachine
- function fnt.windowsAddMachine()
- label_name = API.newLabel(2,1, math.floor(ScreenWidth/2) - 5, 1, "Nom de la machine")
- input_name = API.newInput(2,3,math.floor(ScreenWidth/2) - 5, "")
- label_address = API.newLabel(2,5, math.floor(ScreenWidth/2) - 5, 1, "address du redstone opencomputer")
- label_address_help = API.newLabel(2,7, math.floor(ScreenWidth/2) - 5, 1, "les 3 premier chiffre suffit")
- input_address = API.newInput(2,9,math.floor(ScreenWidth/2) - 5, "")
- label_side = API.newLabel(2,11, math.floor(ScreenWidth/2) - 5, 1, "numero de la face du block de redstone")
- label_side_help = API.newLabel(2,13, math.floor(ScreenWidth/2) - 5, 1, "bottom = 0, top = 1,nord = 2, sud = 3, west = 4, east = 5")
- input_side = API.newInput(2,15,math.floor(ScreenWidth/2) - 5, "")
- checkbox_offline = API.newCheckbox(2, 17, "Désactive le bouton si le joueurs est offline")
- btn_ok_spawner = API.newButton(12, 19, 4, 1, "ok", okMachineCallback)
- btn_cancel_spawner = API.newButton(26, 19, 6, 1, "cancel", cancelMachineCallback)
- wm.addElement(windowAddMachine, label_name)
- wm.addElement(windowAddMachine, label_address)
- wm.addElement(windowAddMachine, label_side)
- wm.addElement(windowAddMachine, label_address_help)
- wm.addElement(windowAddMachine, label_side_help)
- wm.addElement(windowAddMachine, input_name)
- wm.addElement(windowAddMachine, input_address)
- wm.addElement(windowAddMachine, input_side)
- wm.addElement(windowAddMachine, checkbox_offline)
- wm.addElement(windowAddMachine, btn_ok_spawner)
- wm.addElement(windowAddMachine, btn_cancel_spawner)
- API.setElementAlignment(checkbox_offline, "right")
- end
- function exitGui()
- local shrc = io.open("/home/.shrc","w")
- shrc:write("")
- shrc:close()
- wm.shutdown(true)
- end
- wm.startGui()
- wm.setTopText("Welcome")
- wm.setSystemMenuPos("right")
- wm.addSystemMenu("Exit", exitGui, nil, 1)
- wm.addSystemMenu("Shutdown", wm.shutdown, nil, 1)
- wm.addSystemMenu("Reboot", wm.shutdown, true, 1)
- fnt.getGeneral()
- fnt.addGeneraldraw()
- fnt.windowsAddMachine()
- fnt.Generaldraw()
- wm.hideWindow(windowGeneral, false)
- for i = #GeneralList, 1, -1 do
- setRedstoneOutput(true,GeneralList[i])
- end
- local main_thread = thread.create(function() os.sleep(5)
- while true do
- local _,_,x,y = require("event").pull("touch")
- if x and y then
- wm.TouchEventListener(_, _, x, y)
- end
- end
- end)
- local function getStringFromResponce(responce)
- local ret = ""
- local resp = responce()
- while(resp~=nil) do
- ret = ret..tostring(resp)
- resp = responce()
- end
- return ret
- end
- local function getHTTPData(url)
- local ret = nil
- local request, responce = pcall(internet.request, url)
- if(request) then
- ret = getStringFromResponce(responce)
- end
- return ret
- end
- local function has_value(tab)
- for index,value in ipairs(tab) do
- if value == "Ao_Shinji" or value == "THEJean_Kevin" then
- return true
- end
- end
- return false
- end
- local second_thread = thread.create(function()
- while true do
- os.sleep(5)
- apiJson = json.decode(getHTTPData("https://api.mineaurion.com/v1/serveurs/revelation.mineaurion.com"))
- if apiJson.joueurs ~= false then
- if has_value(apiJson["joueurs"]) then
- online = true
- for i = #GeneralList, 1, -1 do
- setRedstoneOutput(false, GeneralList[i])
- end
- else
- online = false
- for i = #GeneralList, 1, -1 do
- setRedstoneOutput(false, GeneralList[i])
- end
- end
- end
- end
- end)
- thread.waitForAll({second_thread,main_thread})
- return fnt
Advertisement
Add Comment
Please, Sign In to add comment