1.16.5 WorldEdit API from selection save a schematic

Discussion in 'Spigot Plugin Development' started by TurtlesAreHot, Feb 1, 2021.

  1. The title really says it all here. I want to get a selection from a player and save that selection as a schematic. How could I accomplish this?

    My current code:

    Code (Java):
        public static boolean saveSchem(Clipboard cb, File schemFile) {

            Region sel = WorldEditAPI.getSelection(p);
            BlockArrayClipboard cp = new BlockArrayClipboard(sel.getBoundingBox());
            File schemFile = new File(WorldEditAPI.getWorldEdit().getDataFolder().getAbsolutePath() + "/schematics/" + this.getName() + ".schem");
            Clipboard cb = cp;
            try(ClipboardWriter writer = BuiltInClipboardFormat.SPONGE_SCHEMATIC.getWriter(new FileOutputStream(schemFile))) {
                writer.write(cb);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    public class WorldEditAPI {
     public static WorldEditPlugin getWorldEdit() {
         return (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit");
     }
     public static Region getSelection(Player p) {
         LocalSession ses = getWorldEdit().getSession(p);
         Region sel = null;
         try {
             sel = ses.getSelection(ses.getSelectionWorld());
             return sel;
         }  catch (IncompleteRegionException e) {
             return null;
         }
     }
    }

     
    Thank you.
     
    #1 TurtlesAreHot, Feb 1, 2021
    Last edited: Feb 1, 2021