Advertisement
BruceWplays

FarmGUI V2 Experimental

Feb 13th, 2023 (edited)
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.08 KB | None | 0 0
  1. -- local andy_farm_program_running = 0
  2. function start_farm_gui(args)
  3.     local fgui_exit = 0
  4.     local sel_screen_pos = 6
  5.     farm_prog_progress = "initgui"
  6.     local extra_space = true
  7.     -- andy_farm_program_running = 0
  8.     -- if args[1] == "noset" then
  9.     --  andy_farm_program_running = 1
  10.     -- end
  11.  
  12.  
  13.     --input lists
  14.     con_up = {"keys.up","keys.w"}
  15.     con_down = {"keys.down","keys.s"}
  16.     con_left = {"keys.left","keys.a"}
  17.     con_right = {"keys.right","keys.d"}
  18.     con_select = {"keys.enter","keys.space"}
  19.     con_back = {"keys.leftShift","keys.b"}
  20.     con_quick_quit = {"keys.q"}
  21.  
  22.  
  23.  
  24.     function match_list(var,list)
  25.         for i = 1,#list do
  26.             if var == list[i] then
  27.                 return true
  28.             end
  29.         end
  30.         return false
  31.     end
  32.  
  33.     local function set_menu(menu)
  34.     end
  35.     local cur_menu = "init"
  36.     local prev_menu = "init"
  37.     function menu_init()
  38.  
  39.         function set_menu(menu)
  40.             prev_menu = cur_menu
  41.             cur_menu = menu
  42.             selitem = 1
  43.         end
  44.         set_menu(main_menu)
  45.     end
  46.  
  47.  
  48.  
  49.  
  50.     while fgui_exit == 0 do
  51.  
  52.         local function setmain_start()
  53.             if _G.andy_farm_program_running == 0 then
  54.                 main_menu[1].options = "Start"
  55.             elseif _G.andy_farm_program_running == 1 then
  56.                 main_menu[1].options = "Cancel"
  57.             else
  58.                 main_menu[1].options = "What???"
  59.             end
  60.         end
  61.  
  62.         if fs.exists("AndysPrograms/Farm/settings") then
  63.             settings.load("AndysPrograms/Farm/settings")
  64.         end
  65.         local maxW, maxH = term.getSize()
  66.  
  67.         function menu_back()
  68.             set_menu(prev_menu)
  69.         end
  70.  
  71.         function start_stop_farm()
  72.             if andy_farm_program_running == 1 then
  73.                 andy_farm_program_running = 0
  74.                 _G.andy_farm_program_running = 0
  75.                 set_running(0)
  76.                 setmain_start()
  77.             else
  78.                 andy_farm_program_running = 1
  79.                 _G.andy_farm_program_running = 1
  80.                 set_running(1)
  81.                 setmain_start()
  82.             end
  83.         end
  84.  
  85.         function options_farm()
  86.             extra_space = true
  87.             set_menu(setmenu)
  88.         end
  89.  
  90.         function mainmenu_farm()
  91.             extra_space = true
  92.             set_menu(main_menu)
  93.         end
  94.  
  95.         function exit()
  96.             term.clear()
  97.             term.setCursorPos(1, 1)
  98.             print("EXIT")
  99.             fgui_exit = 1
  100.             farmexit = 1
  101.             stopfarm = 1
  102.  
  103.         end
  104.         function chngset()
  105.             chngsel = 1
  106.         end
  107.  
  108.         function savesets()
  109.             settings.save("AndysPrograms/Farm/settings")
  110.         end
  111.         gpstab = {}
  112.         local stgpsx, stgpsy, stgpsz = gps.locate(5)
  113.         table.insert(gpstab, stgpsx)
  114.         table.insert(gpstab, stgpsy)
  115.         table.insert(gpstab, stgpsz)
  116.         table.insert(gpstab, "n")
  117.         if fs.exists("AndysPrograms/Farm/settings") == false then
  118.             if fs.exists("AndysPrograms/Farm") == false then
  119.             fs.makeDir("AndysPrograms/Farm")
  120.             end
  121.  
  122.  
  123.  
  124.  
  125.             srtbk = {"minecraft:pumpkin", "minecraft:reeds"}
  126.             settings.set("Farm Lenth", 1)
  127.             settings.set("Farm Width", 1)
  128.             settings.set("Mode", 2)
  129.             settings.set("Start Location", gpstab)
  130.             settings.set("Chest Location", gpstab)
  131.             settings.set("Sort Block Names", srtbk)
  132.             settings.set("Sort Blocks", 1)
  133.             settings.set("Chest Direction", 0)
  134.             savesets()
  135.        
  136.            
  137.            
  138.         end
  139.  
  140.         main_menu = {}
  141.         main_menu = {
  142.             {options = "Start", handler = start_stop_farm},
  143.             {options = "Options", handler = options_farm},
  144.             {options = "Exit", handler = exit}
  145.         }
  146.         function resetmenu()
  147.             deftab = {}
  148.  
  149.  
  150.  
  151.             local setmenu = {}
  152.             setmenu = {
  153.                 {options = "Finish editing", handler = mainmenu_farm},
  154.                 {text = "Farm Lenth", setname = "Farm Lenth", options = settings.get("Farm Lenth"), type = "num", handler = chngset},
  155.                 {text = "Farm Width", setname = "Farm Width", options = settings.get("Farm Width"), type = "num", handler = chngset},
  156.                 {text = "Mode, 1 = Nrml, 2 = Pumpkin/Melon/Sugarcane", setname = "Mode", options = settings.get("Mode"), type = "num", handler = chngset},
  157.                 {text = "Start Location", setname = "Start Location", options = settings.get("Start Location"), type = deftab, handler = chngset},
  158.                 {text = "Chest Location", setname = "Chest Location", options = settings.get("Chest Location"), type = deftab, handler = chngset},
  159.                 {text = "Chest Direction 0=fwd 2=up 1=dn", setname = "Chest Direction", options = settings.get("Chest Direction"), type = "num", handler = chngset},
  160.                 {text = "Sort Blocks", setname = "Sort Blocks", options = settings.get("Sort Blocks"), type = "num", handler = chngset},
  161.                 {text = "Sort Block Names", setname = "Sort Block Names", options = settings.get("Sort Block Names"), type = deftab, handler = chngset}
  162.             }
  163.             _G.setmenu = setmenu
  164.             setmenu_set = 1
  165.             setmain_start()
  166.         end
  167.         resetmenu()
  168.  
  169.         function addsettings(num, value)
  170.             settings.set(setmenu[num].setname, value)
  171.             savesets()
  172.         end
  173.  
  174.         openset = 1
  175.  
  176.  
  177.        
  178.  
  179.  
  180.         while fgui_exit == 0 do
  181.  
  182.  
  183.  
  184.  
  185.         if fgui_exit == 0 then
  186.             chngsel = 0
  187.             term.clear()
  188.             setsoffset = 1
  189.             selitem = 1
  190.             --if (maxH) < (#setmenu * 2) then
  191.             --    selitem = maxH / 2
  192.         -- elseif (maxH) > (#setmenu) then
  193.             --selitem = 1
  194.             --end
  195.  
  196.         local function press_input()
  197.             settingsinputtable = {}
  198.             input2 = string.gsub(input, ", ", ",")
  199.             --print(input)
  200.             for i in string.gmatch(input2, '([^,]+)') do
  201.                 table.insert(settingsinputtable, i)
  202.             end
  203.             --print(input)
  204.             addsettings(selitem, settingsinputtable)
  205.         end
  206.  
  207.         local function print_prep(i)
  208.             if i ~= nil then
  209.                 if (type(i) == "table") then
  210.                     return table.concat(i, ", ")
  211.                     -- if #tostring(prntset) / maxW > 1 then
  212.                     --     add_sum(#tostring(prntset) / maxW)
  213.                     -- end
  214.                 else
  215.                     return i
  216.                 end
  217.             end
  218.         end
  219.  
  220.         local function error()
  221.            
  222.         end
  223.         local item_list = {}
  224.         local preped_item_list = {}
  225.         local shifted_list = {}
  226.         local item_list_sel = 1
  227.         local function shift_list(list,up_down,amount)
  228.             local templist = {}
  229.             if amount == nil then
  230.                 amount = 1
  231.             end
  232.             if amount < 0 then
  233.                 amount = amount * -1
  234.             end
  235.             -- print(amount)
  236.             if up_down == nil then
  237.                 return list
  238.             end
  239.             if up_down == "up" then
  240.                 -- print("up")
  241.                 for i=1,#list - amount do
  242.                    
  243.                     table.insert(templist,list[i+amount])
  244.                 end
  245.             elseif up_down == "down" then
  246.                
  247.                 for i=1,#list + amount do
  248.                     -- print("down")
  249.                     if i-amount < 1 then
  250.                         table.insert(templist," ")
  251.                     else
  252.                         table.insert(templist,list[i-amount])
  253.                     end
  254.                    
  255.                 end
  256.             end
  257.             -- print(list[1])
  258.             -- print(templist[1])
  259.             -- print(templist[1+ amount])
  260.             return templist
  261.            
  262.         end
  263.         local stoplistloop = 0
  264.         local dif = 0
  265.         local function prep_list_for_draw(list)
  266.             local shift_dir = ""
  267.             preped_item_list = {}
  268.             dif = item_list_sel - sel_screen_pos
  269.             if dif < 0 then
  270.                 shift_dir = "down"
  271.             elseif dif >= 0 then
  272.                 shift_dir = "up"
  273.             end
  274.             if #list < maxH then
  275.                 return list
  276.             end
  277.             shifted_list = shift_list(list,shift_dir, dif)
  278.             -- print(shifted_list[1])
  279.             -- print(list[1])
  280.             stoplistloop = 0
  281.             -- for ipl=1,#shifted_list do
  282.             --     if #preped_item_list <= maxH then
  283.             --         break
  284.             --     end
  285.             --     table.insert(preped_item_list,shifted_list[ipl])
  286.             -- end
  287.             -- print(preped_item_list[1])
  288.             -- return preped_item_list
  289.             return shifted_list
  290.         end
  291.  
  292.         local function draw_to_screen(ds_list)
  293.             local i = maxH
  294.             while i >= 1 do
  295.                 term.setCursorPos(1,i)
  296.                 --print(ds_list[i])
  297.                 term.write(ds_list[i])
  298.                 i = i - 1
  299.             end
  300.         end
  301.         function set_print()
  302.  
  303.             local function if_overflow(i)
  304.                 local temp_over
  305.                 local break_loop = 0
  306.                 local i2 = 1
  307.                 local over_list = {}
  308.                 while true do
  309.                     local lower = ((maxW * i2) + 1) - maxW
  310.                     local upper = maxW * i2
  311.                     if upper > #i then
  312.                         upper = #i
  313.                         break_loop = 1
  314.                     end
  315.                     temp_over = string.sub(i,lower,upper)
  316.                     table.insert(over_list,temp_over)
  317.                     i2 = i2 + 1
  318.                     if break_loop == 1 then
  319.                         break
  320.                     end
  321.                 end
  322.                 for i3=1,#over_list do
  323.                     table.insert(item_list,over_list[i3])
  324.                 end
  325.             end
  326.             local pos_inc = 1
  327.             item_list = {}
  328.             local inc_ext = 0
  329.             local function add_list(i)
  330.                 -- item_list = item_list .."\n"..i
  331.                 if #i > maxW then
  332.                     if_overflow(i)
  333.                 else
  334.                     table.insert(item_list,i)
  335.                 end
  336.                
  337.             end
  338.             local function inc_extra(i)
  339.                 local inc_ext = i
  340.                 while inc_ext > 0 do
  341.                     pos_inc = pos_inc + 1
  342.                     inc_ext = inc_ext - 1
  343.                 end
  344.             end
  345.             for sets=1,#cur_menu do
  346.                 function add_sum(n)
  347.                     if sets < selitem then
  348.                         -- sum_ext_to_sel = sum_ext_to_sel + n
  349.                     end
  350.                 end
  351.  
  352.                 setsmul = (sets * 2) - 1
  353.                 if (setsmul + setsoffset) <= maxH or true then
  354.                     -- term.setCursorPos(1, ((setsmul + setsoffset) +text_plus)+sum_ext_to_sel - selitem)
  355.                     if cur_menu[sets].text ~= nil then
  356.                         add_list(print_prep(cur_menu[sets].text))
  357.                         if #cur_menu[sets].text / maxW > 1 then
  358.                             inc_extra(#cur_menu[sets].text / maxW)
  359.                             -- add_sum(#cur_menu[sets].text / maxW)
  360.                         end
  361.                         -- text_plus = text_plus + 1
  362.  
  363.                     end
  364.  
  365.                     pos_inc = pos_inc + 1
  366.                     -- term.setCursorPos(1, ((setsmul + setsoffset)+text_plus) + sum_ext_to_sel - selitem)
  367.                     if cur_menu[sets].options ~= nil then
  368.                         -- term.clearLine()
  369.                         prntset = print_prep(cur_menu[sets].options)
  370.                         if #tostring(prntset) / maxW > 1 then
  371.                             -- add_sum(#tostring(prntset) / maxW)
  372.                         end
  373.                         if selitem == sets then
  374.                             if chngsel == 1 then
  375.                                 input = read()
  376.                                 if input then
  377.                                     press_input()
  378.                                 end
  379.  
  380.                             else
  381.                                 add_list(">  "..prntset)
  382.                                 if #tostring(prntset) / maxW > 1 then
  383.                                     -- add_sum(#tostring(prntset) / maxW)
  384.                                 end
  385.                                 item_list_sel = #item_list
  386.                             end
  387.                         else
  388.                             add_list("   "..prntset)
  389.                             if #tostring(prntset) / maxW > 1 then
  390.                                 -- add_sum(#tostring(prntset) / maxW)
  391.                             end
  392.                         end
  393.                         if extra_space == true then
  394.                             if sets <= #cur_menu then
  395.                                 add_list(" ")
  396.                             end
  397.                         end
  398.                     end
  399.                 end
  400.             end
  401.             return item_list
  402.         end
  403.         function printmenu(menu)
  404.             local sum_ext_to_sel = 0
  405.  
  406.             setmain_start()
  407.             local text_plus = 0
  408.  
  409.             local temp_set = set_print()
  410.             local temp_prep = prep_list_for_draw(temp_set)
  411.             print(temp_prep[1])
  412.             draw_to_screen(temp_prep)
  413.             -- draw_to_screen(set_print())
  414.         end
  415.             function onsel(menu, selected)
  416.                 menu[selected].handler()
  417.             end
  418.  
  419.             function onkeypress(key, menu, selected)
  420.                 if key == keys.enter or key == keys.space then
  421.                     if chngsel == 0 then
  422.                         onsel(menu, selected)
  423.                     end
  424.                 -- if chngsel == 1 and (key == keys.enter or key == keys.space) then
  425.                     --   chngsel = 0
  426.                 -- end
  427.                 elseif key == keys.up or key == keys.w then
  428.                     if selitem > 1 then
  429.                         --setsoffset = setsoffset + 2.1
  430.                         setsoffset = math.floor(setsoffset)
  431.                         selitem = selitem - 1
  432.                     end
  433.                 elseif key == keys.down or key == keys.s then
  434.                     if selitem < (#menu) then
  435.                         --setsoffset = setsoffset - 1.9
  436.                         setsoffset = math.floor(setsoffset)
  437.                         selitem = selitem + 1
  438.                     end
  439.                 elseif key == keys.q then
  440.                     exit()
  441.                 end
  442.             end
  443.  
  444.         menu_init()
  445.        
  446.         --os.loadAPI("farm")
  447.         --os.loadAPI("AndysPrograms/Farm/farm")
  448.         while fgui_exit == 0 do
  449.             if setmenu_set == 1 then
  450.                 init_gui = 1
  451.                 _G.init_gui = 1
  452.             end
  453.             term.setCursorPos(1, 1)
  454.             term.clear()
  455.             printmenu(cur_menu)
  456.             event, key = os.pullEvent("key")
  457.             onkeypress(key, cur_menu, selitem)
  458.             setmain_start()
  459.             sleep(0.01)
  460.         end
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.         end
  471.  
  472.  
  473.  
  474.         end
  475.     end
  476. end
  477. function get_running()
  478.     return andy_farm_program_running
  479. end
  480. function set_running(n)
  481.     andy_farm_program_running = n
  482. end
  483. function get_setmenu()
  484.     return setmenu
  485. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement