local path_finder = require("pathfinder_core") local json = require("json") local player = require("player") local threads = require("threads") path_finder.jumpHeight = 1 path_finder.maxNodes = 50000 path_finder.fallDepth = 3 local homes = {} local function getFile(flag) return io.open("config/neoscripts/homes.json", flag) end local file = getFile("r") if file then local content = file:read("*a") if content then homes = json.decode(content) end file:close() end if homes == nil then homes = {} end registerCommand("sethome", function(commandName, args) local name = args[1] or "home" homes[name] = { name = name, pos = { x = player.entity.x, y = player.entity.y, z = player.entity.z, } } local file2 = getFile("w") if file2 then file2:write(json.encode(homes, true)) file2:close() end end, function(info) return {} end ) local target = nil local currentIndex = 1 local globalPath = nil local searchLock = false registerClientTick(function() if target then -- Если нет текущего пути или он закончился, и поиск не активен – запускаем новый поиск if (globalPath == nil or currentIndex > #globalPath) and not searchLock and not path_finder.isSearching() then searchLock = true threads.startThread(function() path_finder.findPath({ x = target.pos.x, y = target.pos.y + 1, z = target.pos.z }, function(path, err) searchLock = false if path then globalPath = path currentIndex = 1 print("Path found") elseif err == "no path found — partial path returned" then globalPath = path currentIndex = 1 print("Path found") elseif err == "no path found" then print("Path not found") globalPath = nil currentIndex = 1 target = nil end end) end) end -- Если есть путь и есть точки – телепортируемся if globalPath and currentIndex <= #globalPath then local point = globalPath[currentIndex] player.entity.teleport(point.x + 0.5, point.y, point.z + 0.5, true) currentIndex = currentIndex + 1 -- Проверяем, не закончился ли путь if currentIndex > #globalPath then local dist = math.sqrt( (player.entity.x - target.pos.x) ^ 2 + (player.entity.y - target.pos.y) ^ 2 + (player.entity.z - target.pos.z) ^ 2 ) if dist < 1.5 then -- Достигли цели player.addMessage("§9You have reached your home: §5" .. target.name) target = nil globalPath = nil else -- Не достигли – будем искать новый путь с текущей позиции globalPath = nil currentIndex = 1 -- На следующем тике автоматически запустится новый поиск end end end end end) registerCommand("home", function(commandName, args) local name = args[1] or "home" if not homes[name] then print("home not found") return end print("setting home: " .. name) player.addMessage("§9Teleporting to home: §5" .. name) target = homes[name] -- Сбрасываем старый путь при новом задании globalPath = nil currentIndex = 1 searchLock = false end, function(info) return {} end ) registerCommand("homes", function(commandName, args) player.addMessage("§9List homes:") for key, home in pairs(homes) do player.addMessage("§5" .. key) end end, function(info) return {} end )