Util [Save/Paste] Schematics via WorldEdit 6

Discussion in 'Resources' started by Eballer48, Jul 2, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Eballer48

    Currently in class so I do not have much time to document or explain the code, however the code is pretty easy to read and self-explanatory...

    The management of schematics seem to be a common issue, I had some issues upgrading to WorldEdit 6's API with the deprecation of ClipboardHolder, etc...

    So after successfully hooking into WE6's API I decided to release this to the community for anyone whom may be encountering the same issue.

    Using this API is as simple as:
    Code:
    // Saves player's clipboard as a schematic with declared name 'schematicName'
    Schematic.save(player, schematicName);
    
    and
    Code:
    // Loads a specified schematic by name (no need to include extension) and paste it at declared 'pasteLoc'
    Schematic.paste(schematicName, pasteLoc);
    
    Schematic.java
    Code:
    import java.io.File;
    import java.io.IOException;
    
    import me.blahberrys.meteorloot.MeteorLoot;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    import com.sk89q.worldedit.CuboidClipboard;
    import com.sk89q.worldedit.EditSession;
    import com.sk89q.worldedit.EmptyClipboardException;
    import com.sk89q.worldedit.LocalPlayer;
    import com.sk89q.worldedit.LocalSession;
    import com.sk89q.worldedit.MaxChangedBlocksException;
    import com.sk89q.worldedit.Vector;
    import com.sk89q.worldedit.WorldEdit;
    import com.sk89q.worldedit.bukkit.BukkitUtil;
    import com.sk89q.worldedit.bukkit.BukkitWorld;
    import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    import com.sk89q.worldedit.data.DataException;
    import com.sk89q.worldedit.schematic.SchematicFormat;
    import com.sk89q.worldedit.session.ClipboardHolder;
    
    /**
    *         @author BlahBerrys (eballer48) - [email protected]
    *
    *         An easy-to-use API for saving, loading, and pasting WorldEdit/MCEdit
    *         schematics. (Built against WorldEdit 6.1)
    *
    */
    
    @SuppressWarnings("deprecation")
    public class Schematic {
    
        public static void save(Player player, String schematicName) {
            try {
                File schematic = new File(PLUGIN.getDataFolder(), "/schematics/" + schematicName);
                File dir = new File(PLUGIN.getDataFolder(), "/schematics/");
                if (!dir.exists())
                    dir.mkdirs();
    
                WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
                WorldEdit we = wep.getWorldEdit();
    
                LocalPlayer localPlayer = wep.wrapPlayer(player);
                LocalSession localSession = we.getSession(localPlayer);
                ClipboardHolder selection = localSession.getClipboard();
                EditSession editSession = localSession.createEditSession(localPlayer);
    
                Vector min = selection.getClipboard().getMinimumPoint();
                Vector max = selection.getClipboard().getMaximumPoint();
    
                editSession.enableQueue();
                CuboidClipboard clipboard = new CuboidClipboard(max.subtract(min).add(new Vector(1, 1, 1)), min);
                clipboard.copy(editSession);
                SchematicFormat.MCEDIT.save(clipboard, schematic);
                editSession.flushQueue();
    
                player.sendMessage("Saved schematic!");
            } catch (IOException | DataException ex) {
                ex.printStackTrace();
            } catch (EmptyClipboardException ex) {
                ex.printStackTrace();
            }
        }
    
    
        public static void paste(String schematicName, Location pasteLoc) {
            try {
                File dir = new File(PLUGIN.getDataFolder(), "/schematics/" + schematicName);
    
                EditSession editSession = new EditSession(new BukkitWorld(pasteLoc.getWorld()), 999999999);
                editSession.enableQueue();
    
                SchematicFormat schematic = SchematicFormat.getFormat(dir);
                CuboidClipboard clipboard = schematic.load(dir);
    
                clipboard.paste(editSession, BukkitUtil.toVector(pasteLoc), true);
                editSession.flushQueue();
            } catch (DataException | IOException ex) {
                ex.printStackTrace();
            } catch (MaxChangedBlocksException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    
    
    You should check to make sure WorldEdit is on the server before calling to this API!
    Code:
    private void setupWorldEdit() {
            Plugin plugin = getServer().getPluginManager().getPlugin("WorldEdit");
            if (plugin == null || !(plugin instanceof WorldEditPlugin))
                return;
    
            worldEditSupport = true;
            worldEdit = (WorldEditPlugin) plugin;
            getServer().getConsoleSender().sendMessage(Settings.getInstance().title + "enabled WorldEdit support!");
        }
    
    Any suggestions to my code are welcome!

     
    Last edited: Jul 4, 2015
    Eggspurt and KingFaris11 like this.
  2. Offline

    Eballer48

    Reserve.
     
  3. Offline

    Eggspurt

    This is really awesome, I am going to play with it! Thanks for this
     
  4. Offline

    w4rmduscher

    Is there any way I can also restore AIR blocks? In my Arena the players Place Blocks, and when using Schematics.paste
    it only replaces Blocks that did exist before, not Blocks that didnt.
     
  5. Offline

    callum2904

    Could you add ability to rotate
     
Thread Status:
Not open for further replies.

Share This Page