Solved Creating/Loading schematic is corrupted!

Discussion in 'Plugin Development' started by Tim_M, Oct 26, 2021.

  1. Offline

    Tim_M

    I'm trying to save a chunk into a schematic and then paste it. However it always results in making the whole chunk nothing but AIR, I believe the saving is broken, but idk.

    Code:
    public void saveChunkTemporary(Chunk chunk, String path)
        {
            try {
                File saveFile = new File(path);
                Block min = chunk.getBlock(0, 0, 0);
                Block max = chunk.getBlock(15, 255, 15);
    
                Region region = new CuboidRegion(BlockVector3.at(min.getLocation().getBlockX(), 0, min.getLocation().getBlockZ()),
                        BlockVector3.at(max.getLocation().getBlockX(), 255, max.getLocation().getBlockZ()));
                region.setWorld(BukkitAdapter.adapt(Main.instance.gameWorld));
                Clipboard clipboard = new BlockArrayClipboard(region);
                clipboard.setOrigin(region.getMinimumPoint());
                try (ClipboardWriter writer = BuiltInClipboardFormat.FAST.getWriter(new FileOutputStream(saveFile))) {
                    writer.write(clipboard.lazyCopy(region));
                }
                clipboard.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    
    Code:
    public void loadMapTemporary()
        {
            File saveFile = new File(Main.instance.getDataFolder() + File.separator + "backupdata");
            if (saveFile.listFiles() != null) {
                for (File f : saveFile.listFiles()) {
                    if (!f.isDirectory() && !f.getName().equalsIgnoreCase(".ds_store")) {
                        try {
                            String[] name = f.getName().split("\\.")[0].split("_");
                            int[] coords = new int[]{Integer.parseInt(name[0]), Integer.parseInt(name[1])};
                            Chunk chunk = Main.instance.gameWorld.getChunkAt(coords[0], coords[1]);
    
                            Block min = chunk.getBlock(0, 0, 0);
                            Block max = chunk.getBlock(15, 255, 15);
    
                            Region region = new CuboidRegion(BlockVector3.at(min.getLocation().getBlockX(), 0, min.getLocation().getBlockZ()),
                                    BlockVector3.at(max.getLocation().getBlockX(), 255, max.getLocation().getBlockZ()));
                            region.setWorld(BukkitAdapter.adapt(Main.instance.gameWorld));
                            try (ClipboardReader reader = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getReader(new FileInputStream(f)))
                            {
                                Clipboard clipboard = reader.read();
                                clipboard.paste(BukkitAdapter.adapt(Main.instance.gameWorld), region.getMinimumPoint());
                                clipboard.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    
    Turns out this code is COMPLETELY wrong. I have now fixed it.
     
    Last edited: Oct 27, 2021

Share This Page