Advertisement
MagmaLP

Item-Bank_PC Subtrahieren

Nov 12th, 2023 (edited)
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.61 KB | None | 0 0
  1. file = fs.open("local-name", "r")             --read user
  2. name = file.readLine()
  3. file.close()
  4.  
  5. function calc_color(color)
  6.     return math.pow(2, color - 1)
  7. end
  8.  
  9. function msg(gap, line, color, groundb, text)
  10.     term.setBackgroundColor(calc_color(groundb))
  11.     term.setCursorPos(gap,line)
  12.     term.setTextColor(calc_color(color))
  13.     text = term.write(text)
  14. end
  15.  
  16. function RequestItems()
  17.     -- CONSTANTS
  18.     local INTERVAL = 1
  19.     local PIPE_SIDE = "back"
  20.     local REQUEST_IDS = {
  21.  
  22.         -- Beispiel für IDs mit Varianten
  23.         [2491] = {      
  24.             ["2491:7"] = Output_AE_Amount,          --AE Output + Menge
  25.         },
  26.         [20261] = {  
  27.             ["20261:69"] = Output_Shiny_Amount,     --Shiny Output + Menge
  28.                 },
  29.         [254] = {  
  30.             ["254:6"] = Output_Tungsten_Amount,     --Tungsten Output + Menge
  31.         },
  32.     }
  33.  
  34.     -- IMPLEMENTATION
  35.     function convertNBT(nbt)
  36.         local conv = {}
  37.         if (nbt == nil) then
  38.             return nil
  39.         elseif (nbt["type"] == "NBTTagCompound") or (nbt["type"] == "NBTTagList") then
  40.             for key, value in pairs(nbt["value"]) do
  41.                 conv[key] = convertNBT(value)
  42.             end
  43.         else
  44.             conv = nbt["value"]
  45.         end
  46.         return conv
  47.     end
  48.  
  49.     function getItems(pipe)
  50.         pipe.getAvailableItems()
  51.         local event, result = os.pullEvent("available_items_return")
  52.         return result
  53.     end
  54.  
  55.     function inList(item)
  56.         local id = item.id
  57.         local dmg = item.dmg
  58.         local idKey = tostring(id)
  59.         local exists, requestAmount = false, 0
  60.  
  61.         if REQUEST_IDS[id] then
  62.             if type(REQUEST_IDS[id]) == "table" then
  63.                 local variantKey = idKey .. ":" .. tostring(dmg)
  64.                 if REQUEST_IDS[id][variantKey] then
  65.                     exists = true
  66.                     requestAmount = REQUEST_IDS[id][variantKey]
  67.                 end
  68.             else
  69.                 exists = true
  70.                 requestAmount = REQUEST_IDS[id]
  71.             end
  72.         end
  73.  
  74.         return exists, requestAmount
  75.     end
  76.  
  77.     -- MAIN
  78.     local pipe = peripheral.wrap(PIPE_SIDE)
  79.     while true do
  80.         for i, result in pairs(getItems(pipe)) do
  81.             local iid, amount = unpack(result)
  82.             local item = {
  83.                 id = pipe.getItemID(iid),
  84.                 dmg = pipe.getItemDamage(iid),
  85.                 nbt = convertNBT(pipe.getNBTTagCompound(iid))
  86.             }
  87.             local exists, requestAmount = inList(item)
  88.             if exists then
  89.                 pipe.makeRequest(iid, requestAmount)
  90.                 sleep(1)
  91.             end
  92.         end
  93.         sleep(5)
  94.         os.reboot()
  95.     end
  96. end
  97.  
  98. local function createFormatter()
  99.     return function(input)
  100.         local str = tostring(input)
  101.         return (str .. string.rep(' ', 5 - #str)):sub(1, 5)
  102.     end
  103. end
  104.  
  105. function Output()  
  106.     local event, button, X, Y = os.pullEvent("mouse_click")
  107.     if X >= 02 and X <= 06 and Y == 14 and (Output_AE_Amount+16) < AEs_Amount then                  --AE +16
  108.         Output_AE_Amount = Output_AE_Amount + 16
  109.     elseif X >= 02 and X <= 06 and Y == 15 and (Output_Shiny_Amount+16) < Shiny_Amount then                 --Shiny +16
  110.         Output_Shiny_Amount = Output_Shiny_Amount + 16
  111.     elseif X >= 02 and X <= 06 and Y == 16 and (Output_Tungsten_Amount+16) < Tungsten_Amount then               --Tungsten +16
  112.         Output_Tungsten_Amount = Output_Tungsten_Amount + 16
  113.     elseif X >= 08 and X <= 11 and Y == 14 and Output_AE_Amount < AEs_Amount then               --AE +1
  114.         Output_AE_Amount = Output_AE_Amount + 1
  115.     elseif X >= 08 and X <= 11 and Y == 15 and Output_Shiny_Amount < Shiny_Amount then              --Shiny +1
  116.         Output_Shiny_Amount = Output_Shiny_Amount + 1
  117.     elseif X >= 08 and X <= 11 and Y == 16 and Output_Tungsten_Amount < Tungsten_Amount then                --Tungsten +1
  118.         Output_Tungsten_Amount = Output_Tungsten_Amount + 1
  119.        
  120.     elseif X >= 19 and X <= 22 and Y == 14 and Output_AE_Amount >= 1 then               --AE -1
  121.         Output_AE_Amount = Output_AE_Amount - 1
  122.     elseif X >= 19 and X <= 22 and Y == 15 and Output_Shiny_Amount >= 1 then                --Shiny -1
  123.         Output_Shiny_Amount = Output_Shiny_Amount - 1
  124.     elseif X >= 19 and X <= 22 and Y == 16 and Output_Tungsten_Amount >= 1 then                 --Tungsten -1
  125.         Output_Tungsten_Amount = Output_Tungsten_Amount - 1
  126.     elseif X >= 24 and X <= 28 and Y == 14 and Output_AE_Amount >= 16 then      --AE -16
  127.         Output_AE_Amount = Output_AE_Amount - 16
  128.     elseif X >= 24 and X <= 28 and Y == 15 and Output_Shiny_Amount >= 16 then       --Shiny -16
  129.         Output_Shiny_Amount = Output_Shiny_Amount - 16
  130.     elseif X >= 24 and X <= 28 and Y == 16 and Output_Tungsten_Amount >= 16 then        --Tungsten +16
  131.         Output_Tungsten_Amount = Output_Tungsten_Amount - 16      
  132.     elseif X >= 47 and X <= 52 and Y == 19 then     --Back Button
  133.         os.reboot()
  134.     elseif X >= 02 and X <= 24 and Y == 19 then     --Payout Button
  135.        
  136.         print(AEs_Amount)
  137.         print(Output_AE_Amount)
  138.         print(Shiny_Amount)
  139.         print(Output_Shiny_Amount)
  140.         print(Tungsten_Amount)
  141.         print(Output_Tungsten_Amount)
  142.        
  143.         New_AEs_Amount = AEs_Amount - Output_AE_Amount
  144.         New_Shiny_Amount = Shiny_Amount - Output_Shiny_Amount
  145.         New_Tungsten_Amount = Tungsten_Amount - Output_Tungsten_Amount
  146.        
  147.         fs.delete("Users/" .. name)
  148.         file = fs.open("Users/" .. name, "w")
  149.         file.close()
  150.        
  151.         local file = fs.open("Users/" .. name, "a")
  152.         file.write("Adjustable Emitter= " .. New_AEs_Amount .. "\n")
  153.         file.write("Shiny Ingot= " .. New_Shiny_Amount .. "\n")
  154.         file.write("Tungsten Ore= " .. New_Tungsten_Amount .. "\n")
  155.         file.close()
  156.  
  157.         print(" ")
  158.         print("addierte Werte:")
  159.         print("AEs:", New_AEs_Amount)
  160.         print("Shiny:", New_Shiny_Amount)
  161.         print("Tungsten:", New_Tungsten_Amount)
  162.         RequestItems()
  163.     end
  164.  
  165.     updateNumber = createFormatter()
  166.     msg(13,14,04,16, updateNumber(Output_AE_Amount))
  167.     msg(13,15,04,16, updateNumber(Output_Shiny_Amount))
  168.     msg(13,16,04,16, updateNumber(Output_Tungsten_Amount))
  169.     Output()
  170. end
  171.  
  172. function ladeItems()
  173.     local datei = fs.open("Users/" .. name, "r")  -- Datei im Lesemodus öffnen
  174.     if not datei then
  175.         print("Fehler: Datei konnte nicht geöffnet werden.")
  176.         return
  177.     end
  178.     local items = {}    
  179.     -- Schleife durch jede Zeile der Datei
  180.     repeat
  181.         local zeile = datei.readLine()
  182.         if zeile then
  183.             -- Trenne den Itemnamen und die Menge anhand des Gleichheitszeichens
  184.             local itemName, menge = zeile:match("([^=]+)%s*=%s*(%d+)")
  185.             if itemName and menge then
  186.                 items[itemName] = tonumber(menge)
  187.             else
  188.                 print("Fehlerhafte Zeile in der Datei: " .. zeile)
  189.             end
  190.         end
  191.     until not zeile
  192.     datei.close()  -- Datei schließen
  193.     return items
  194. end
  195.  
  196.  
  197. --Program Start--
  198. local itemTabelle = ladeItems()
  199.  
  200. -- Beispiel: Verwendung der abgerufenen Informationen
  201. if itemTabelle then
  202.     AEs_Amount = itemTabelle["Adjustable Emitter"]
  203.     Shiny_Amount = itemTabelle["Shiny Ingot"]
  204.     Tungsten_Amount = itemTabelle["Tungsten Ore"]
  205.    
  206.     msg(01,01,16,16," ")
  207.     term.clear()
  208.     msg(01,01,02,16," Account: "..name)                             --überschrift
  209.     msg(01,02,08,16,"---------------------------------------------------")
  210.     msg(14,04,06,16,"$$$                   $$$")
  211.     msg(18,04,05,16,"Trading-City Bank")
  212.     msg(18,05,01,16,"~~~~~~~~~~~~~~~~~")
  213.     msg(02,08,01,16,"Your Items:")
  214.     msg(02,09,01,16,"~~~~~~~~~~~")
  215.     msg(19,08,09,16," " .. AEs_Amount .. "x")
  216.     msg(19,09,09,16," " .. Shiny_Amount .. "x")
  217.     msg(19,10,09,16," " .. Tungsten_Amount .. "x")
  218.     msg(01,18,08,16,"---------------------------------------------------")
  219.     msg(47,19,16,01," back")
  220.     msg(02,19,01,15," click here for payout ")
  221.    
  222.     msg(30,08,09,16,"Adjustable Emitters")
  223.     msg(30,09,09,16,"Shiny Ingot")
  224.     msg(30,10,09,16,"Tungste Ore")
  225.     msg(30,14,09,16,"Adjustable Emitters")
  226.     msg(30,15,09,16,"Shiny Ingot")
  227.     msg(30,16,09,16,"Tungste Ore")
  228.     msg(02,12,01,16,"Select Your Output:")
  229.     msg(02,13,01,16,"~~~~~~~~~~~~~~~~~~~")
  230.    
  231.     customMaxLength = 5
  232.     Output_AE_Amount = 0
  233.     Output_Shiny_Amount = 0
  234.     Output_Tungsten_Amount = 0
  235.    
  236.     for yPos = 14,16 do
  237.         msg(02,yPos,01,15," +16 ")
  238.         msg(08,yPos,01,15," +1 ")
  239.         msg(13,yPos,04,16,"0    ")
  240.         msg(19,yPos,01,15," -1 ")
  241.         msg(24,yPos,01,15," -16 ")
  242.     end  
  243.     Output()
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement