Regione restore

EvgeniyND

Новичок
Пользователь
Баллы
11
Имя в Minecraft
EvgeniyND
Есть snapshot. Пытаюсь разобраться как восстановить регион из него самого.. Направьте на путь истинный))
 
Разбираю стандартную функцию из WG
Код:
private void restore() {

        BukkitConfiguration worldEditConfig = null;
        try {
            worldEditConfig = getWorldEdit().getLocalConfiguration();
        } catch (UnknownPluginException e) {
            e.printStackTrace();
        }
        if ((worldEditConfig != null ? worldEditConfig.snapshotRepo : null) == null) {
            log.warning("No snapshots configured, restoration cancelled.");
            return;
        }

        try {
            // Discover chunks
            Location battleLoc = new Location(world, config.x, config.y, config.z);

            for (Entity entity : world.getEntitiesByClasses(Item.class, TNTPrimed.class)) {
                if (region.contains(BukkitUtil.toVector(entity.getLocation()))) {
                    entity.remove();
                }
            }

            final Snapshot snap = worldEditConfig.snapshotRepo.getDefaultSnapshot(config.worldName);

            if (snap == null) {
                log.warning("No snapshot could be found, restoration cancelled.");
                return;
            }

            final List<Chunk> chunkList = new ArrayList<>();
            chunkList.add(battleLoc.getChunk());

            Vector min = region.getMinimumPoint();
            Vector max = region.getMaximumPoint();

            final int minX = min.getBlockX();
            final int minZ = min.getBlockZ();
            final int minY = min.getBlockY();
            final int maxX = max.getBlockX();
            final int maxZ = max.getBlockZ();
            final int maxY = max.getBlockY();

            Chunk c;
            for (int x = minX; x <= maxX; x += 16) {
                for (int z = minZ; z <= maxZ; z += 16) {
                    c = world.getBlockAt(x, minY, z).getChunk();
                    if (!chunkList.contains(c)) chunkList.add(c);
                }
            }

            log.info("Snapshot '" + snap.getName() + "' loaded; now restoring Area...");
            // Tell players restoration is beginning
            for (Player player : server.getOnlinePlayers()) {

                ChatUtil.sendWarning(player, "Restoring Area...");
            }

            // Setup task to progressively restore
            final EditSession fakeEditor = new EditSession(new BukkitWorld(world), -1);
            for (final Chunk chunk : chunkList) {
                BukkitTask aTask = server.getScheduler().runTaskLater(inst, new Runnable() {

                    @Override
                    public void run() {

                        ChunkStore chunkStore;

                        try {
                            chunkStore = snap._getChunkStore();
                        } catch (DataException | IOException e) {
                            log.warning("Failed to load snapshot: " + e.getMessage());
                            return;
                        }

                        try {
                            Block minBlock = chunk.getBlock(0, minY, 0);
                            Block maxBlock = chunk.getBlock(15, maxY, 15);
                            Vector minPt = new Vector(minBlock.getX(), minBlock.getY(), minBlock.getZ());
                            Vector maxPt = new Vector(maxBlock.getX(), maxBlock.getY(), maxBlock.getZ());

                            Region r = new CuboidRegion(minPt, maxPt);

                            // Restore snapshot
                            if (!chunk.isLoaded()) chunk.load();
                            SnapshotRestore restore = new SnapshotRestore(chunkStore, r);

                            try {
                                restore.restore(fakeEditor);
                            } catch (MaxChangedBlocksException e) {
                                log.warning("Congratulations! You got an error which makes no sense!");
                                e.printStackTrace();
                                return;

                            }

                            if (restore.hadTotalFailure()) {
                                String error = restore.getLastErrorMessage();
                                if (error != null) {
                                    log.warning("Errors prevented any blocks from being restored.");
                                    log.warning("Last error: " + error);
                                } else {
                                    log.warning("No chunks could be loaded. (Bad archive?)");
                                }
                            } else {
                                if (restore.getMissingChunks().size() > 0 || restore.getErrorChunks().size() > 0) {
                                    log.info(String.format("Restored, %d missing chunks and %d other errors.",
                                            restore.getMissingChunks().size(),
                                            restore.getErrorChunks().size()));
                                }
                                if (chunkList.indexOf(chunk) == chunkList.size() - 1) {
                                    Bukkit.broadcastMessage(ChatColor.YELLOW + "Restored successfully.");
                                }
                            }
                        } finally {
                            try {
                                chunkStore.close();
                            } catch (IOException ignored) {
                            }
                        }
                    }
                }, 5 * chunkList.indexOf(chunk));
                restorationTask.add(aTask);
            }

            // Setup a task to clear out any restoration task
            server.getScheduler().runTaskLater(inst, new Runnable() {
                @Override
                public void run() {

                    restorationTask.clear();
                }
            }, (5 * chunkList.size()) + 20);
        } catch (MissingWorldException e) {
            log.warning("The world: " + config.worldName + " could not be found, restoration cancelled.");
        }     
    }
 
Опешу подробнее: по команде /rstr мы должны восстановить регион "rts" в мире "park" из снапшота по умолчанию.
Если кто владеет подобной заготовкой в виде кода, поделитесь. Или подтолкните в верном направлении.
 
Назад
Верх