Solved World Edit API not pasting

Gnirekcip

New member
Jan 27, 2025
2
0
1
I am using WE API to paste a .schem file in a world, and when the code is run, nothing happens. No errors are thrown, the blocks just don't get pasted.

The schematic file exists and the location exists.
Version 1.21.4, worldedit api 7.3.9


Java:
public static void paste(Location location, File schematicFile) {
    if (!schematicFile.exists()) {
        DebugUtil.severe("Schematic File " + schematicFile.getName() + " doesn't exist.");
        return;
    }
    try {
        EditSession session = WorldEdit.getInstance().newEditSession(new BukkitWorld(location.getWorld()));
        ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
        if (format == null) {
            DebugUtil.severe("Clipboard format for schematic File " + schematicFile.getName() + " doesn't exist.");
            return;
        }
    ClipboardReader reader = format.getReader(new FileInputStream(schematicFile));
    Clipboard schematic = reader.read();

    Operation operation = new ClipboardHolder(schematic)
                .createPaste(session)
                .to(BukkitAdapter.asBlockVector(location))
                .build();

    Operations.complete(operation);


    } catch (IOException | WorldEditException e) {
        e.printStackTrace();
    }
}
 

Gnirekcip

New member
Jan 27, 2025
2
0
1
Solved it!


Not sure why, but this code worked:


Java:
public static void paste(Location location, File schematicFile) {
        Clipboard clipboard = null;
        ClipboardFormat format = ClipboardFormats.findByFile(schematicFile);
        if (format == null) {
            DebugUtil.severe("World Edit format = null");
            return;
        }
        try (ClipboardReader reader = format.getReader(new FileInputStream(schematicFile))) {
            clipboard = reader.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (clipboard == null) {
            DebugUtil.severe("World Edit Clipboard = null");
            return;
        }

        try (EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(location.getWorld()))) {
            Operation operation = new ClipboardHolder(clipboard)
                    .createPaste(editSession)
                    .to(BlockVector3.at(location.getX(), location.getY(), location.getZ()))
                    .build();
            Operations.complete(operation);
        } catch (WorldEditException e) {
            e.printStackTrace();
        }


    }



Possibly something about having separate edit sessions, but I'm not sure.