Solved Saving/pasting Schematics (WorldEdit 6.1)

Discussion in 'Plugin Development' started by Xp10d3, Mar 8, 2021.

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

    Xp10d3

    Hello. I am having quite a few issues with WorldEdit 6.1. I want to save and load schematics, however I am having trouble doing so as the documentation is mostly for the latest version of WorldEdit which doesn't work in 1.8. I got some help earlier and was directed to this lib:
    https://www.spigotmc.org/threads/lib-save-paste-schematics-via-worldedit-6.75421/
    However most of the classes they use are deprecated, and thus makes saving schematics difficult for me. Since SchematicFormat.MCEDIT.save(); requires CuboidClipboard which is deprecated, I can't copy or save using the methods given. I took a look at the documentation but still don't know how I'd copy/paste.

    Code:
    Code:java
    1.  
    2. package bridge.eltik.endran;
    3.  
    4. import java.io.File;
    5. import java.io.IOException;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.Location;
    9. import org.bukkit.entity.Player;
    10.  
    11. import com.sk89q.worldedit.CuboidClipboard;
    12. import com.sk89q.worldedit.EditSession;
    13. import com.sk89q.worldedit.EmptyClipboardException;
    14. import com.sk89q.worldedit.LocalPlayer;
    15. import com.sk89q.worldedit.LocalSession;
    16. import com.sk89q.worldedit.MaxChangedBlocksException;
    17. import com.sk89q.worldedit.Vector;
    18. import com.sk89q.worldedit.WorldEdit;
    19. import com.sk89q.worldedit.bukkit.BukkitUtil;
    20. import com.sk89q.worldedit.bukkit.BukkitWorld;
    21. import com.sk89q.worldedit.bukkit.WorldEditPlugin;
    22. import com.sk89q.worldedit.data.DataException;
    23. import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
    24. import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
    25. import com.sk89q.worldedit.regions.CuboidRegion;
    26. import com.sk89q.worldedit.schematic.SchematicFormat;
    27. import com.sk89q.worldedit.session.ClipboardHolder;
    28.  
    29. /**
    30. * @author BlahBerrys (eballer48) - [email][email protected][/email]
    31. *
    32. * An easy-to-use API for saving, loading, and pasting WorldEdit/MCEdit
    33. * schematics. (Built against WorldEdit 6.1)
    34. *
    35. */
    36.  
    37. @SuppressWarnings("deprecation")
    38. public class Schematic {
    39. static Core PLUGIN = Core.getPlugin(Core.class);
    40.  
    41. public static void save(Player player, String schematicName) {
    42. try {
    43. File schematic = new File(PLUGIN.getDataFolder(), "/schematics/" + schematicName);
    44. File dir = new File(PLUGIN.getDataFolder(), "/schematics/");
    45. if (!dir.exists())
    46. dir.mkdirs();
    47.  
    48. WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
    49. WorldEdit we = wep.getWorldEdit();
    50.  
    51. LocalPlayer localPlayer = wep.wrapPlayer(player);
    52. LocalSession localSession = we.getSession(localPlayer);
    53. ClipboardHolder selection = localSession.getClipboard();
    54. EditSession editSession = localSession.createEditSession(localPlayer);
    55.  
    56. Vector min = selection.getClipboard().getMinimumPoint();
    57. Vector max = selection.getClipboard().getMaximumPoint();
    58.  
    59. editSession.enableQueue();
    60.  
    61. CuboidRegion region = new CuboidRegion(max.subtract(min).add(new Vector(1, 1, 1)), min);
    62.  
    63. BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
    64. ForwardExtentCopy copyy = new ForwardExtentCopy(editSession, region, clipboard, region.getMinimumPoint());
    65. //clipboard.copy(editSession);
    66. SchematicFormat.MCEDIT.save(clipboard, schematic);
    67. editSession.flushQueue();
    68.  
    69. player.sendMessage("Saved schematic!");
    70. } catch (IOException | DataException ex) {
    71. ex.printStackTrace();
    72. } catch (EmptyClipboardException ex) {
    73. ex.printStackTrace();
    74. }
    75. }
    76.  
    77.  
    78. public static void paste(String schematicName, Location pasteLoc) {
    79. try {
    80. File dir = new File(PLUGIN.getDataFolder(), "/schematics/" + schematicName);
    81.  
    82. EditSession editSession = new EditSession(new BukkitWorld(pasteLoc.getWorld()), 999999999);
    83. editSession.enableQueue();
    84.  
    85. SchematicFormat schematic = SchematicFormat.getFormat(dir);
    86. CuboidClipboard clipboard = schematic.load(dir);
    87.  
    88. clipboard.paste(editSession, BukkitUtil.toVector(pasteLoc), true);
    89. editSession.flushQueue();
    90. } catch (DataException | IOException ex) {
    91. ex.printStackTrace();
    92. } catch (MaxChangedBlocksException ex) {
    93. ex.printStackTrace();
    94. }
    95. }
    96.  
    97. }
    98. [/code]
     
  2. Offline

    CraftCreeper6

  3. Offline

    Xp10d3

    Sorry for being AFK, but I've switched over to manually destroying + spawning blocks around the player instead of using WorldEdit. Marking as solved for now.
    EDIT: Documentation was definitely helpful, so thanks CraftCreeper :)
     
Thread Status:
Not open for further replies.

Share This Page