Advertisement
rollton

Запись и считывания предметов с uuid

Jun 22nd, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.28 KB | None | 0 0
  1. local component = require("component")
  2. local fs = require("filesystem")
  3. local shell = require("shell")
  4. local term = require("term")
  5. local serialization = require("serialization")
  6.  
  7. if not component.isAvailable("openperipheral_sensor") then print("Установите сенсор (через адаптер)") return end
  8. if not component.isAvailable("os_entdetector") then print("Установите entity detector") return end
  9. local sensor = component.openperipheral_sensor
  10.  
  11. local function xyz(g)
  12.     local x,y = term.getCursor()
  13.     term.setCursor(otstup,y-2)
  14.     otstup = otstup + #tostring(g) + 1
  15.     term.write(g)
  16.     term.setCursor(x,y)
  17. end
  18.  
  19. local function clear(g)
  20.     local x,y = term.getCursor()
  21.     for i = 0, g do
  22.         term.setCursor(x,y-i)
  23.         term.clearLine()
  24.     end
  25. end
  26.  
  27. local function read(g,t)
  28.     while true do
  29.         local x,y = term.getCursor()
  30.         term.write(g)
  31.         local a = tonumber(term.read())
  32.         if a ~= nil then
  33.             clear(t)
  34.             return a
  35.         end
  36.         clear(2)
  37.         print("*** Повторите попытку ***")
  38.     end
  39. end
  40.  
  41. local args = shell.parse(...)
  42.  
  43. if args[1] == nil then
  44.     print([[
  45. --- Программа для записи/обнаружения предметов с uuid и их записи/сравнения с базой данных ---
  46. Доступные команды:
  47.     [программа] new [Ваш ник]    - добавить новоый предмет из вашего инвентаря в базу данных
  48.     [программа] del                - удалить предмета из базы данных
  49.     [программа] point              - изменить точку проверки ключей (дверь)
  50.     [программа] run                - запуск программы]])
  51.     return
  52. end
  53.  
  54. if args[1] == "new" then
  55.     if not fs.exists("/home/bditem") then
  56.         fs.makeDirectory("/home/bditem")
  57.     end
  58.  
  59.     if args[2] == nil then print(" После 'new' Вы забыли ввести Ваш ник, повторите попытку.") return end
  60.     local players = sensor.getPlayers()
  61.     local flag = true
  62.     for _,v in pairs(players) do
  63.         if v.name == args[2] then
  64.             flag = false
  65.         end
  66.     end
  67.  
  68.     if flag then print(" Игрок не найден") return end
  69.  
  70.     local buf = {}
  71.     local name = {}
  72.     local size = 0
  73.  
  74.     for i = 1, 36 do
  75.         local inv = sensor.getPlayerByName(args[2]).all().player.inventory[i]
  76.         if inv ~= nil then
  77.              inv = inv.all()
  78.              if inv.nbt_hash ~= nil then
  79.                 size = size + 1
  80.                 buf[size] = inv.nbt_hash
  81.                 name[size] = inv.display_name
  82.                 print(size..") "..name[size])
  83.              end
  84.         end
  85.     end
  86.  
  87.     if size == 0 then
  88.         print(" Предметы, имеющие uuid, в вашем инвентаре не найдено, повторите попытку.")
  89.         return
  90.     end
  91.  
  92.     print("-==========================-")
  93.  
  94.     local item = {["addname"] = args[2]}
  95.     local file
  96.  
  97.     while true do
  98.         print(" Выберите номер предмета:")
  99.         t = read(">> ",2)
  100.         if t>=1 and t<=size then
  101.             file = "/home/bditem/"..name[t]
  102.             if fs.exists(file) then
  103.                 print(" Данный предмет уже записан, хотите удалить? [Y/N]")
  104.                 term.write(">> ")
  105.                 local st = io.read()
  106.                 clear(2)
  107.                 if (st == "y") or (st == "Y") then
  108.                     fs.remove(file)
  109.                 end
  110.             else
  111.                 break
  112.             end
  113.         end
  114.     end
  115.     clear(size+1)
  116.     item["uuid"] = buf[t]
  117.    
  118.     otstup = 13
  119.  
  120.     print("Координаты:")
  121.     print(" Введите:")
  122.     item["x"] = read("X >> ",1)
  123.     xyz(item["x"])
  124.     item["y"] = read("Y >> ",1)
  125.     xyz(item["y"])
  126.     item["z"] = read("Z >> ",1)
  127.     xyz(item["z"])
  128.     clear(1)
  129.  
  130.     file=io.open(file,"w")
  131.         file:write(serialization.serialize(item))
  132.     file:close()
  133.  
  134.     print("*** Предмет успешно занесен в базу данных ***")
  135.  
  136.     return
  137. end
  138.  
  139. if args[1] == "del" then
  140.     local inc = 0
  141.     local name = {}
  142.     for file in fs.list("/home/bditem") do
  143.         inc = inc + 1
  144.         print(inc..") "..file)
  145.         name[inc] = file
  146.     end
  147.     if inc == 0 then print(" Предметов не найдено.") return end
  148.  
  149.     print(" Выберите номер предмета:")
  150.     fs.remove("/home/bditem/"..name[read(">> ",2)])
  151.     clear(inc)
  152.     print("*** Предмет успешно удален ***")
  153. end
  154.  
  155. if args[1] == "point" then
  156.     local item = {}
  157.     otstup = 13
  158.     print("Координаты:")
  159.     print(" Введите:")
  160.     item["x"] = read("X >> ",1)
  161.     xyz(item["x"])
  162.     item["y"] = read("Y >> ",1)
  163.     xyz(item["y"])
  164.     item["z"] = read("Z >> ",1)
  165.     xyz(item["z"])
  166.     clear(1)
  167.  
  168.     local file = io.open("point.txt", "w")
  169.         file:write(serialization.serialize(item))
  170.     file:close()
  171.     print("*** Точка проверки ключей обновлена ***")
  172. end
  173.  
  174. if args[1] == "run" then
  175.     if not fs.exists("/home/point.txt") then print(" Точка еще не создана, воспользуйтесь gg piont для ее создания") return end
  176.    
  177.     local file = io.open("point.txt", "r")
  178.     local point = serialization.unserialize(file:read())
  179.     file:close()
  180.  
  181.     while true do
  182.         os.sleep(1)
  183.         local scan = component.os_entdetector.scanPlayers(16)
  184.         for _,k in pairs(scan) do
  185.             local flag = true
  186.             for n,_ in pairs(point) do
  187.                 if not (math.floor(k[n]) == point[n]) then
  188.                     flag = false
  189.                     break
  190.                 end
  191.             end
  192.  
  193.             if flag then
  194.                 local ff = {}
  195.  
  196.                 local inc = 0
  197.                 for file in fs.list("/home/bditem") do
  198.                     inc = inc + 1
  199.                     local file = io.open("/home/bditem/"..file, "r")
  200.                     ff[inc] = serialization.unserialize(file:read())
  201.                     file:close()
  202.                 end
  203.  
  204.                 if inc == 0 then print(" Файлы не найдены") return end
  205.  
  206.                 for i = 1, 36 do
  207.                     local inv = sensor.getPlayerByName(k.name).all().player.inventory[i]
  208.                     if inv ~= nil then
  209.                         inv = inv.all()
  210.                         if inv.nbt_hash ~= nil then
  211.                             for _,v in pairs(ff) do
  212.                                 if v.uuid == inv.nbt_hash then
  213.                                     if component.isAvailable("opencb") then
  214.                                         component.opencb.execute("tppos "..k.name.." "..v.x.." "..v.y.." "..v.z)
  215.                                     else
  216.                                         if component.isAvailable("debug") then
  217.                                             component.debug.getPlayer(k.name).setPosition(v.x,v.y,v.z)
  218.                                         else
  219.                                             print("Отсутствует дебаг карта или командный блок")
  220.                                             return
  221.                                         end
  222.                                     end
  223.                                     os.sleep(1)
  224.                                     break
  225.                                 end
  226.                             end
  227.                         end
  228.                     end
  229.                 end
  230.             end
  231.         end
  232.     end
  233. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement