- Sky
- Blueberry
- Slate
- Blackcurrant
- Watermelon
- Strawberry
- Orange
- Banana
- Apple
- Emerald
- Chocolate
- Charcoal
Brodur
-
Content Count
7 -
Joined
-
Last visited
Posts posted by Brodur
-
-
1 hour ago, Molinko said:
if db.users[user] then error "Cannot add an existing user!" end
I agree with all of your points, thanks!
One question, in the above quote would this not be undesirable? As I store my users in a table where they are the key and a boolean is the value. So if the user was false, aka trusted but not admin, would the above code not function properly?
{"hasChanges": false,"users": {"Brodur": true,"crassclown": true,"fatso12321": true,"Ouhai_Ruby": true,"Kyle8910": false,"Sacreddeathflame": false,"Emfitty": false},"targets": {"setAttacksMobs": false,"setAttacksNeutrals": false,"setAttacksPlayers": false},"turrets": []} -
Asking honestly, what can I do better in this program? I am not overly familiar with lua and would love some pointers on how I could make this program better.
Also, pointers on getting event listening working would be great!
https://pastebin.com/rChVHKsshttps://github.com/Brodur/Turret-Control--- Turret Control-- Helps manage OpenModularTurrets turret blocks that have serial IO-- @Author: Brodur-- @Version: 2.2-- @Requires:-- https://pastebin.com/WvF00A71 : JSON Utility functions-- https://pastebin.com/pLpe4zPb : Menu functions-- https://pastebin.com/SKwM2UZH : Lua JSON Librarylocal term = require("term")local component = require("component")local event = require("event")local json = require("json")local menu = require("menu")local jsonutil = require("jsonutil")local fs = require("filesystem")local m = {}local tb = "turret_base"local lb = "-------------------------------"local mmopts = {"Summary","Targeting","User Admin","Push Config","Exit"}local db = {}local toRemove = {}local jsondir = "/home/settings.json"--- Trims strings-- Gets rid of trailing white space or special characters.-- @param s The string to trim.-- @return The trimmed string.function trim(s)return (s:gsub("^%s*(.-)%s*$", "%1"))end--- Summary output-- Outputs the summary of a selected turret.-- @param turrets A table containing the hardware addresses of turrets.function m.summary()term.setCursor(1,1)print(mmopts[1] .. "\n")print("Trusted players")print(lb)for k,v in pairs(db.users) doprint(" - " .. k, v and "[Admin]" or "[Trusted]")endprint("\nAttacks")print(lb)print("Mobs:\t\t" .. tostring(db.targets.setAttacksMobs))print("Neutrals:\t" .. tostring(db.targets.setAttacksNeutrals))print("Players:\t" .. tostring(db.targets.setAttacksPlayers))print("\n\nPress enter to continue...")term.read()end--- Set Attack-- Sets the attack setting for all turrets.-- @param turrets A table containing the hardware addresses of turrets.-- @param flag 1 or 2, corresponds to true or false, toggles the attack mode.-- @param option Which option was chosen in the target functionfunction m.setAttack(flag, option)local methods = {"setAttacksMobs", "setAttacksNeutrals", "setAttacksPlayers"}local arguments = {true, false}db.targets[methods[option]] = arguments[flag]db.hasChanges = trueend---Target-- Selects the targeting parameter-- @param turrets A table containing the hardware addresses of turrets.function m.target()local options = {"Attack Mobs", "Attack Neutrals", "Attack Players", "Exit"}local opt = -1while opt ~= #options doopt = menu.list(options, mmopts[2])if opt ~= #options thenlocal flag = menu.dialog(options[opt], "True", "False")m.setAttack(flag, opt)else break endendend--- Add Trusted Player-- Adds a trusted player to all turrets, either with or without admin privileges.-- @param player The player to add.-- @param usrType 1 for trusted, 2 for admin.function m.addTrustedPlayer(player, usrType)local args = {false, true}for k,_ in pairs(db.users) doif player == k then error("Cannot add a user that already exists!") endenddb.users[player] = args[usrType]db.hasChanges = trueend--- Remove Trusted Player-- Removes a user from the trusted players list.-- @param player The user to remove.function m.removeTrustedPlayer(player)toRemove[#toRemove+1] = playerdb.users[player] = nildb.hasChanges = trueend--- Users-- Launches the add or remove user dialogfunction m.users()local options = {"Add a trusted user", "Remove a trused user","Exit"}local opt = -1while opt ~= #options doopt = menu.list(options, mmopts[3])if opt == 1 thenlocal userTypes = {"Trusted", "Admin"}term.write("Add a trusted player: ")local player = trim(term.read())local usrType = menu.dialog("User or Admin?", "User", "Admin")local opt = menu.dialog("Add \'" .. player .. "\' as " .. userTypes[usrType] .." type user?", "Confirm", "Cancel")if opt == 1 thenm.addTrustedPlayer(player, usrType)endendif opt == 2 thenlocal users = {"Cancel"}for k,v in pairs(db.users) do users[#users+1] = k endlocal user = -1while user ~= 1 douser = menu.list(users, "Select a user")if user ~= 1 thenlocal player = users[user]local confirm = menu.dialog("Remove \'" .. player .. "\' from trusted users?", "Confirm", "Cancel")if confirm == 1 thenm.removeTrustedPlayer(player)table.remove(users, user)endendendendendend--- Distribute Json-- Disseminates the settings from the database to all turrets.function m.distribJson()for _,v in pairs(db.turrets) dofor _,player in pairs(toRemove) docomponent.invoke(v, "removeTrustedPlayer", player)endtoRemove= {}for user,priv in pairs(db.users) docomponent.invoke(v, "addTrustedPlayer", user, priv)endfor meth,bool in pairs(db.targets) docomponent.invoke(v, meth, bool)endendend--- Sub menu-- Determines which menu function to call.-- @param index The selected index in the main menu options table.function m.subMenu(index)local turrets = {}for k in pairs(component.list(tb)) do turrets[#turrets+1] = k enddb.turrets = turretsif index == mmopts[1] then m.summary() endif index == mmopts[2] then m.target() endif index == mmopts[3] then m.users() endif index == mmopts[4] then m.distribJson() endend--- On Load-- Initializes the database and grabs the initial turrets.function m.onLoad()if not fs.exists(jsondir) thendb = {hasChanges=false,targets={setAttacksMobs=false,setAttacksNeutrals=false,setAttacksPlayers=false},turrets = {},users = {}}jsonutil.save(db, jsondir)enddb = jsonutil.load(jsondir)for k in pairs(component.list(tb)) do db.turrets[#db.turrets+1] = k endm.main()end--- Main-- Launches the main logic and displays the top level menu.function m.main()local mmoptTitle = "Main Menu"local mmopt = -1while mmopt ~= #mmopts doif db.hasChanges thenjsonutil.save(db, jsondir)db.hasChanges = falsem.distribJson()endif mmopt ~= #mmopts thenmmopt = menu.list(mmopts, mmoptTitle)m.subMenu(mmopts[mmopt])endendend--- On Component Added-- Push the stored config when a new turret is detected.-- @param eventType The type of event, passed by event listener.-- @param address The hardware address of the new component.-- @param componentType The type of the new component.-- @todo figure out why the event listener is not firing all of the time, until that is figured disregard this function-- function onComponentAdded(eventType, address, componentType)-- if componentType == "turret_base" then-- distribJson()-- end-- end-- event.listen("component_added", onComponentAdded)m.onLoad()return m -
I am working on making another program and needed to port some old code from computer craft so I figured I may as well share. This file conaints 2 types of menus, a vertical list which takes an array and optional header text, and a yes/no style menu that can be customized.
local term = require("term") local event = require("event") local methods = {} function methods.list(m, header) header = header or "Menu List" n=1 while(true) do term.clear() term.setCursor(1,1) term.write(header) term.setCursor(1,2) for i=1, #m, 1 do if(i==n) then term.write(" [" .. m[i] .. "]\n") else term.write(" " .. m[i] .. "\n") end end local _,_,_,key = event.pull("key_down") if(key==200 and n>1) then --go up n = n-1 end if(key==208 and n<#m) then --go down n = n+1 end if(key==28) then --exit break end end term.clear() term.setCursor(1,1) return n end function methods.dialog(prompt, lOpt, rOpt) prompt = prompt or "Continue" lOpt = lOpt or "YES" rOpt = rOpt or "NO" local n = 1 term.write(prompt.."\n") while true do local x, y =term.getCursor() term.clearLine() if n==1 then term.setCursor(x, y) term.clearLine() term.write ("["..lOpt.."] "..rOpt) else term.setCursor(x, y) term.clearLine() term.write(" "..lOpt.." ["..rOpt.."]") end term.setCursor(x, y) local _,_,_,key = event.pull("key_down") if key==203 then n=1 end if key==205 then n=2 end if key==28 then term.write("\n") return n end end end return methods
-
NEI Provides IDs, metas, and display names easily enough, however it only dumps a reference to the recipe handlers and no recipes themselves. Might have to look into manual input for that bit... that'd be tedious.
-
2 hours ago, Gorzoid said:
NEI has some resource dumpers that you may want to check out, from what I can remember you can dump all items and all recipes into json/csv files.
That would be amazing, I will look into that.
-
Is there an easy way to obtain Item ids, common names, and material costs?
I am conceptualizing an inventory management program and wondering about the feasability of having robots pick the base components needs to craft an item based on a recursivley generated list (calls all the different material costs until it returns an item with no cost). I have a CSV of vanilla ids and names, but not material costs. Furthermore this would likely be used with some FTB modpack, which would mean I would like to know if there are lists out there for modded items as well. Any resources would be much appreciated.
What can I do better?
in Miscellaneous
Posted
Right on, thanks.