Advertisement
Guest User

Pack

a guest
May 26th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.75 KB | None | 0 0
  1. os.loadAPI("/apis/appdata")
  2. os.loadAPI("/apis/json")
  3. os.loadAPI("/apis/tables")
  4. os.loadAPI("utils")
  5.  
  6.  
  7. -- Variable --
  8. local args = {...}
  9.  
  10. local packs = tables.get("packs")
  11.  
  12. -- Fonctions Importante -
  13.  
  14. function getPackByName(name)
  15.     local tmp = json.decode(packs:getData())
  16.  
  17.     for k,v in pairs(tmp) do
  18.         if v.name == name then
  19.             return v
  20.         end
  21.     end
  22.  
  23.     return false
  24. end
  25.  
  26. function addPack(pack)
  27.  
  28.     local tmp = json.decode(packs:getData())
  29.     if tmp == nil or type(tmp) ~= "table" then
  30.         tmp = {}
  31.     end
  32.  
  33.     local tmpLength = utils.tableLength(tmp)
  34.  
  35.  
  36.     if tmpLength > 0 and getPackByName(pack.name) then
  37.         term.setTextColor(colors.red)
  38.         print("Un pack porte deja ce nom")
  39.         return
  40.     else
  41.         pack.id = tmpLength
  42.         tmp[tostring(tmpLength)] = pack
  43.         packs["data"] = json.encode(tmp)
  44.         packs:save()
  45.     end
  46. end
  47.  
  48.  
  49. function requestPack(Name, wrap, direction)
  50.     wrap = wrap or "top"
  51.     direction = direction or "WEST"
  52.  
  53.     local pack = getPackByName(Name)
  54.     local p = peripheral.wrap(wrap)
  55.  
  56.     if p == nil or pack == nil or pack == false then
  57.         return false
  58.     else
  59.         for k,v in pairs(pack.fingerPrints) do
  60.  
  61.             local tmpItem = "{id=\""..v.item.."\",dmg="..v.damage..","..v.quantity.."}"
  62.            
  63.             term.setTextColor(colors.yellow)
  64.             print("Requete effectuee")
  65.             p.exportItem(tmpItem..",".. direction)
  66.         end
  67.     end
  68.  
  69. end
  70.  
  71. if #args == 0 then
  72.     term.setTextColor(colors.yellow)
  73.  
  74.     print("add [Name][fingerPrints]")
  75.     print("get [Name]")
  76.     print("request [Name][Wrap][Direction]")
  77.     return
  78. end
  79.  
  80. if args[1] == "add" then
  81.  
  82.     if utils.commandVerif(args, 3, "add [Name][fingerPrints]") then
  83.  
  84.         local pack = {}
  85.         pack.name = args[2]
  86.         pack.fingerPrints = {}
  87.  
  88.         s = utils.split(args[3]:gsub("[%s{}]", ""), "-")
  89.  
  90.  
  91.         for k,v in pairs(s) do
  92.             pack.fingerPrints[k] = {}
  93.  
  94.             propertyFingPrints = utils.split(v, ",")
  95.  
  96.             pack.fingerPrints[k].item = propertyFingPrints[1]
  97.  
  98.             if #propertyFingPrints == 2 then
  99.                 pack.fingerPrints[k].damage = "0"
  100.                 pack.fingerPrints[k].quantity = propertyFingPrints[2]
  101.             elseif #propertyFingPrints == 3 then
  102.                 pack.fingerPrints[k].damage = propertyFingPrints[2]
  103.                 pack.fingerPrints[k].quantity = propertyFingPrints[3]
  104.             end
  105.  
  106.  
  107.             term.setTextColor(colors.lime)
  108.             print("Item " .. k .. " - " .. v)
  109.         end
  110.  
  111.         addPack(pack)
  112.     end
  113. elseif args[1] == "get" then
  114.  
  115.     if commandVerif(2, "get [Name]") then
  116.         local pack = {}
  117.  
  118.         pack = getPackByName(args[2])
  119.  
  120.         term.setTextColor(colors.lime)
  121.         print("Id: " .. pack.id)
  122.         print("Name: " .. pack.name)
  123.         print("FingerPrints: ")
  124.  
  125.  
  126.         for k,v in pairs(pack.fingerPrints) do
  127.             print(k .. " - " ..v)
  128.         end
  129.        
  130.     end
  131. elseif args[1] == "request" then
  132.  
  133.     if utils.commandVerif(args, 4, "request [Name][Wrap][Direction]") then
  134.         requestPack(args[2], args[3], args[4])
  135.     end
  136.  
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement