Advertisement
Clem105

Workshop piloute

Oct 4th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.02 KB | None | 0 0
  1. --TRANSCRIPTION TABLE
  2. convTab = {
  3. dye = {"Ink Sac","Rose Red","Cactus Green","Cocoa Beans","Lapis Lazuli","Purlple Dye","Cyan Dye","Light Gray Dye","Gray Dye","Pink Dye","Lime Dye","Dandelion Yellow","Light Blue Dye","Magenta Dye","Orange Dye","Bonemeal"},
  4. wool = {"White wool","Orange wool","Magenta wool","Light blue wool","Yellow wool","Light green wool","Pink wool","Gray wool","Light gray wool","Cyan wool","Purple wool","Brown wool","Green wool","Red wool","Black wool"},
  5. stone = {"Stone","Granite","Polished granite","Diorite","Polished diorite","Andesite","Polished andesite"},
  6. planks = {"Oak plank","Spruce plank","Birch plank","Jungle plank","Acacia plank","Dark oak plank"},
  7. log1 = {"Oak log","Spruce log","Birch log","Jungle log"},
  8. log2 = {"Acacia log","Dark oak log"},
  9. stained_hardened = {"White clay","Orange clay","Magenta clay","Light blue clay","Yellow clay","Light green clay","Pink clay","Gray clay","Light gray clay","Cyan clay","Purple clay","Brown clay","Green clay","Red clay","Black clay"}
  10. }
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. modem = peripheral.wrap("right")
  18. modem.open(57)
  19. Chest = peripheral.wrap("InventoryInterface_31")
  20. Disp = peripheral.wrap("InventoryInterface_33")
  21. Storage = peripheral.wrap("InventoryInterface_34")
  22. maxLimit = 1024
  23.  
  24. PagePlus = function()
  25.     page = page + 1
  26.     if page>LastPage then
  27.         page = LastPage
  28.     end
  29. end
  30.    
  31. PageMoins = function()
  32.     page = page - 1
  33.     if page<1 then
  34.         page = 1
  35.     end
  36. end
  37.  
  38. Count = function(mode,num)
  39.     if mode == "+" then
  40.             currCount = currCount + num
  41.             if currCount > maxLimit then
  42.                 currCount = maxLimit
  43.             elseif currCount > qtTab[product]["amount"] and isBuying then
  44.                 currCount = qtTab[product]["amount"]
  45.                 end
  46.     elseif mode == "-" then
  47.             currCount = currCount - num
  48.             if currCount < 1 then
  49.                 currCount = 1
  50.             end
  51.     end
  52. end
  53.  
  54.  
  55. Cancel = function()
  56.     PanelMode = true
  57. end
  58.  
  59. Confirm = function()
  60.     PanelMode = true
  61.     monitor.clear()
  62.     if isBuying then
  63.         modem.transmit(57,1,textutils.serialize({qtTab[product]["raw"],currCount}))
  64.         monitor.setBackgroundColor(colors.black)
  65.         monitor.clear()
  66.         monitor.setCursorPos(6,10)
  67.         monitor.write("Pulled 0 "..product.." out of ".. currCount)
  68.         while true do
  69.             os.startTimer(2)
  70.             event = os.pullEvent("timer")
  71.             if event == "timer" then
  72.                 chest = {}
  73.                 count = 0
  74.                 for i = 1,Storage.getInventorySize() do
  75.                     temp = Storage.getItemInSlot(i)
  76.                     count = count + (temp.stackSize or 0)
  77.                 end
  78.                 monitor.setCursorPos(6,10)
  79.                 monitor.write("Pulled "..count.." "..product.." out of ".. currCount)
  80.                 if count == currCount then
  81.                     for i = 1,Storage.getInventorySize() do
  82.                         Storage.transferToII(Disp.uuid(),1,1,64)
  83.                     end
  84.                     os.reboot()
  85.                 end
  86.             end
  87.         end
  88.     else
  89.         Command = touch.new("back")
  90.         Command:add("Cancel",nil,11,18,32,20,colors.red,colors.lime)
  91.         Command:draw()
  92.         local chest = {}
  93.         while true do
  94.             os.startTimer(2)
  95.             event,p1 = Command:handleEvents(os.pullEvent())
  96.             if event == "button_click" then
  97.                 break
  98.             elseif event == "timer" then
  99.                 chest = {}
  100.                 for i = 1,Chest.getInventorySize() do
  101.                     temp = Chest.getItemInSlot(i)
  102.                     if temp["name"] then
  103.                         chest[temp.name:gsub("minecraft:","")..":"..temp.metadata] = (chest[temp.name:gsub("minecraft:","")..":"..temp.metadata] or 0) + temp.stackSize
  104.                     end
  105.                 end
  106.                 monitor.setBackgroundColor(colors.black)
  107.                 monitor.setCursorPos(12,10)
  108.                 if chest[qtTab[product]["raw"]] then
  109.                     monitor.write(chest[qtTab[product]["raw"]].." "..product.." out of ".. currCount)
  110.                     if chest[qtTab[product]["raw"]] >= currCount then
  111.                         amount = currCount
  112.                         for i = 1,Chest.getInventorySize() do
  113.                             data = Chest.getItemInSlot(i)
  114.                             if data.name:gsub("minecraft:","")..":"..data.metadata == qtTab[product]["raw"] then
  115.                                 Chest.transferToII(Storage.uuid(),i,i,amount)
  116.                                 amount = amount - data.stackSize
  117.                                 if amount <= 0 then
  118.                                     break
  119.                                 end
  120.                             end
  121.                         end
  122.                         sleep(5)
  123.                         for i = 1,Chest.getInventorySize() do
  124.                             Chest.transferToII(Disp.uuid(),i,i,64)
  125.                         end
  126.                    
  127.                         os.reboot()
  128.                     end
  129.                 else
  130.                     monitor.write("0 "..product.." out of ".. currCount)
  131.                 end
  132.             end
  133.         end
  134.     end
  135. end
  136.  
  137.  
  138. function split(str,pat)
  139.     local t = {}
  140.     local fpat = "(.-)" .. pat
  141.     local last_end = 1
  142.     local s, e, cap = str:find(fpat, 1)
  143.     while s do
  144.         if s ~= 1 or cap ~= "" then
  145.             table.insert(t,cap)
  146.         end
  147.         last_end = e+1
  148.         s, e, cap = str:find(fpat, last_end)
  149.     end
  150.     if last_end <= #str then
  151.         cap = str:sub(last_end)
  152.         table.insert(t, cap)
  153.     end
  154.     return t
  155. end
  156.  
  157.  
  158. function Panel()
  159.    
  160.     local s = false
  161.     PanelMode = true
  162.     product = ""
  163.     qtTab = {}
  164.     isBuying = true
  165.     os.loadAPI("touch")
  166.     monitor = peripheral.wrap("back")
  167.     monitor.clear()
  168.     xMin = {22,3}
  169.     xMax = {37,18}
  170.     yMin = {2,6,10,14}
  171.     yMax = {4,8,12,16}
  172.     modem.transmit(57,1,"Request")
  173.     tableau = {}
  174.     while true do
  175.         event,side,_,chanBis,msg = os.pullEvent("modem_message")
  176.         if chanBis == 1 then
  177.             tab = textutils.unserialize(msg)
  178.             if type(tab) == "table" then
  179.                 for k,v in pairs(tab) do
  180.                     fullName = split(k,":")
  181.                     for a,b in pairs(convTab) do
  182.                         if k:find(a) then
  183.                             metadata = tonumber(fullName[2])   
  184.                             name = b[metadata+1]
  185.                             table.insert(tableau,name)
  186.                             qtTab[name] = {}
  187.                             qtTab[name]["amount"] = v
  188.                             qtTab[name]["raw"] = k
  189.                             s = true
  190.                             break
  191.                         end
  192.                     end
  193.                     if not s then
  194.                         name = fullName[1]
  195.                         name = name:gsub("^%l", string.upper)
  196.                         name = name:gsub("%_"," ")
  197.                         table.insert(tableau,name)
  198.                         qtTab[name] = {}
  199.                         qtTab[name]["amount"] = v
  200.                         qtTab[name]["raw"] = k
  201.                     end
  202.                     s = false
  203.                 end
  204.             end
  205.             break
  206.         end
  207.         sleep(1)
  208.     end
  209.     if tableau == nil then
  210.         tableau = {}
  211.     end
  212.  
  213.     lastNumber = 0
  214.     page = 1
  215.     screen = {}
  216.     currCount = 1
  217.     GUI = touch.new("back")
  218.     GUI:add("-100",function() Count("-",100) end,2,12,7,14,colors.red,colors.lime)
  219.     GUI:add("-10",function() Count("-",10) end,9,12,13,14,colors.red,colors.lime)
  220.     GUI:add("-1",function() Count("-",1) end,15,12,19,14,colors.red,colors.lime)
  221.     GUI:add("+1",function() Count("+",1) end,21,12,25,14,colors.red,colors.lime)
  222.     GUI:add("+10",function() Count("+",10) end,27,12,31,14,colors.red,colors.lime)
  223.     GUI:add("+100",function() Count("+",100) end,33,12,38,14,colors.red,colors.lime)
  224.     GUI:add("Cancel",function()  Cancel() end,7,23,18,25,colors.red,colors.lime)
  225.     GUI:add("Confirm",function()  Confirm() end,23,23,34,25,colors.red,colors.lime)
  226.  
  227.     labelSwitch = {
  228.     "                   ",
  229.     "        Buy        ",
  230.     "                   ",
  231.     label = "switch"
  232.     }
  233.     buyLabel = {
  234.     "                   ",
  235.     "        Buy        ",
  236.     "                   "
  237.     }
  238.     sellLabel = {
  239.     "                   ",
  240.     "        Sell       ",
  241.     "                   "
  242.     }
  243.  
  244.     for i=1,(math.ceil(#tableau/8)) do
  245.         screen[i] = touch.new("back")
  246.     end
  247.  
  248.     LastPage = #screen
  249.  
  250.     if LastPage ~= 1  then
  251.         for a=1,(LastPage-1) do
  252.             for b=1,8 do
  253.                 xMinCord = xMin[b%2+1]
  254.                 xMaxCord = xMax[b%2+1]
  255.                 yMinCord = yMin[math.ceil(b/2)]
  256.                 yMaxCord = yMax[math.ceil(b/2)]
  257.                 screen[a]:add(tableau[(a-1)*8+b],nil,xMinCord,yMinCord,xMaxCord,yMaxCord,colors.red,colors.lime)
  258.             end
  259.             screen[a]:add(labelSwitch,0,11,18,32,20,colors.blue,colors.cyan)
  260.             screen[a]:add("<<Page",function()  PageMoins() end,6,23,17,25,colors.red,colors.lime)
  261.             screen[a]:add("Page>>",function()  PagePlus() end,22,23,33,25,colors.red,colors.lime)
  262.         end
  263.     end
  264.  
  265.     o = #tableau%8
  266.    
  267.     lastNumber = o%2
  268.  
  269.     screen[LastPage]:add(labelSwitch,0,11,18,32,20,colors.blue,colors.cyan)
  270.     screen[LastPage]:add("<<Page",function()  PageMoins() end,6,23,17,25,colors.red,colors.lime)
  271.     screen[LastPage]:add("Page>>",function()  PagePlus() end,22,23,33,25,colors.red,colors.lime)
  272.     if o>1 then
  273.         for i=1,(o-lastNumber) do
  274.             xMinCord = xMin[i%2+1]
  275.             xMaxCord = xMax[i%2+1]
  276.             yMinCord = yMin[math.ceil(i/2)]
  277.             yMaxCord = yMax[math.ceil(i/2)]
  278.             screen[LastPage]:add(tableau[(LastPage-1)*8+i],nil,xMinCord,yMinCord,xMaxCord,yMaxCord,colors.red,colors.lime)
  279.         end
  280.     end
  281.     if lastNumber == 1 then
  282.         screen[LastPage]:add(tableau[#tableau],nil,13,yMin[math.ceil(o/2)],27,yMax[math.ceil(o/2)],colors.red,colors.lime)
  283.     end
  284.     while true do
  285.         if PanelMode then
  286.           screen[page]:draw()
  287.           monitor.setCursorPos(15,22)
  288.           monitor.setBackgroundColor(colors.black)
  289.           monitor.write("Page "..page.." of "..LastPage)
  290.           local event, p1,p2,p3,p4 = screen[page]:handleEvents(os.pullEvent())
  291.           if event == "button_click" then
  292.             if type(screen[page].buttonList[p1].func) == "function" then
  293.                 screen[page]:flash(p1,0.2)
  294.                 screen[page].buttonList[p1].func()
  295.             elseif type(screen[page].buttonList[p1].func) == "number" then
  296.                 isBuying = not isBuying
  297.                 for i=1,LastPage do
  298.                     if isBuying then
  299.                         screen[i].buttonList["switch"].label = buyLabel
  300.                     else
  301.                         screen[i].buttonList["switch"].label = sellLabel
  302.                     end
  303.                     screen[i]:toggleButton("switch")
  304.                 end
  305.             else
  306.                 product = p1
  307.                 screen[page]:flash(p1,0.5)
  308.                 PanelMode = false
  309.                 currCount = 1
  310.            
  311.             end
  312.         end
  313.       elseif not PanelMode then
  314.           GUI:draw()
  315.           monitor.setCursorPos(12,10)
  316.           monitor.setBackgroundColor(colors.black)
  317.           if isBuying then
  318.              monitor.write("Buy "..currCount.." of "..qtTab[product]["amount"].." "..product)
  319.           else
  320.             monitor.write("Sell "..currCount.." "..product)
  321.           end
  322.            local event, p1,p2,p3,p4 = GUI:handleEvents(os.pullEvent())
  323.           if event == "button_click" then
  324.             if type(GUI.buttonList[p1].func) == "function" then
  325.                 GUI:flash(p1,0.3)
  326.                 GUI.buttonList[p1].func()
  327.             end
  328.           end
  329.       end
  330.     end
  331. end
  332.        
  333. Panel()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement