[WE] Pasting and undo-ing schematics

Discussion in 'Plugin Development' started by khave, Apr 17, 2014.

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

    khave

    The title pretty much explains it.
    How do I paste a schematic with the World Edit plugin and how do I undo it afterwards?

    Thank you very much in advanced.
     
  2. Offline

    rfsantos1996

    //undo (?)
     
  3. Offline

    khave

    I mean with code ^^
    I don't want it spamming //Paste, //undo
     
  4. Offline

    rfsantos1996

    Link
     
  5. Offline

    khave

    I've read what it says on there, but it also says: "whether anything was undone" Meaning it's not the action of doing //undo and //redo or am I totally in the wrong here?
     
  6. Offline

    Garris0n

    It would return false if there was nothing left to undo.
     
  7. Offline

    khave

    Garris0n
    I see, but the edit session needs to return something. What should it return? newBlockBag?
    And how would I paste a schematic?
    I've found some old threads about it but I get this certain error that tells me that the DataException class doesn't exist. Is there something wrong with my WorldEdit.jar or is it the code?
    Code:
    Code:
                WorldEditPlugin we = (WorldEditPlugin) this.getServer().getPluginManager().getPlugin("WorldEdit");
                LocalSession localSession = we.getSession(p);//Get player session
                com.sk89q.worldedit.LocalPlayer wePlayer = we.wrapCommandSender(sender);//if sender is player //convert to localplayer
     
                SchematicFormat schematic = SchematicFormat.getFormat(new File(this.getDataFolder(), "supervip.schematic"));
                CuboidClipboard clipboard = schematic.load(new File(this.getDataFolder(), "supervip.schematic"));
                clipboard.paste(localSession.createEditSession(wePlayer), BukkitUtil.toVector(b.getLocation()), true);
            } catch (NullPointerException ex) {
                System.out.println("Schematic need to be have .schematic");
            } catch (IOException | DataException | MaxChangedBlocksException e) {
                e.printStackTrace();
            }
    Before that it also gives me the error: "Multi-catches are not supported in this language level" at the catches (I tried putting them away from each other and that's what gave the no class exists error).
     
  8. Offline

    Garris0n

    Use Java 7 if you want those.

    And perhaps the DataException was done away with.
     
  9. Offline

    khave


    Garris0n okay, but if I remove the DataException then schematic.load gets an error. It needs a DataException.
     
  10. Offline

    Garris0n

  11. Offline

    khave

    Garris0n Hmmmm, that's weird.
    Is the error not because of missing the class DataException?

    Error:
    Code:
    [15:30:32 ERROR]: Could not load 'plugins/Susanoo.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: com/sk89q/worldedit/data/DataException
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:308) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:231) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.loadPlugins(CraftServer.java:255) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.reload(CraftServer.java:628) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.Bukkit.reload(Bukkit.java:279) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:23) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:196) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServer.java:542) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(CraftServer.java:529) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:251) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NoClassDefFoundError: com/sk89q/worldedit/data/DataException
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_51]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 14 more
    Caused by: java.lang.ClassNotFoundException: com.sk89q.worldedit.data.DataException
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[?:1.7.0_51]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[?:1.7.0_51]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0_51]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_51]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_51]
        at java.lang.Class.forName0(Native Method) ~[?:1.7.0_51]
        at java.lang.Class.forName(Class.java:270) ~[?:1.7.0_51]
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:173) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
        ... 14 more
    Code:
    Code:java
    1. try {
    2. WorldEditPlugin we = (WorldEditPlugin) this.getServer().getPluginManager().getPlugin("WorldEdit");
    3. LocalSession localSession = we.getSession(p);//Get player session
    4. com.sk89q.worldedit.LocalPlayer wePlayer = we.wrapCommandSender(sender);//if sender is player //convert to localplayer
    5.  
    6. SchematicFormat schematic = SchematicFormat.getFormat(new File(this.getDataFolder(), "supervip.schematic"));
    7. CuboidClipboard clipboard = schematic.load(new File(this.getDataFolder(), "supervip.schematic"));
    8. clipboard.paste(localSession.createEditSession(wePlayer), BukkitUtil.toVector(p.getLocation()), true);
    9. } catch (NullPointerException ex) {
    10. System.out.println("Schematic need to be have .schematic");
    11. } catch (IOException e){
    12. e.printStackTrace();
    13. }catch (DataException e){
    14. e.printStackTrace();
    15. } catch (MaxChangedBlocksException e) {
    16. e.printStackTrace();
    17. }
     
  12. Offline

    Garris0n

    It's in the worldedit jar, are you sure you're using the latest version of worldedit?
     
  13. Offline

    khave

    Yea. I just downloaded it a day ago.. I'll try again.
     
  14. Offline

    Garris0n

    And you're using the same version in your build path as on the server?
     
  15. Offline

    khave

    Garris0n still gives me a no class error D:
     
  16. Offline

    Garris0n

    What is the import you're using?
     
  17. Offline

    khave

    Garris0n
    Code:
    import com.sk89q.worldedit.*;
    import com.sk89q.worldedit.bags.BlockBag;
    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;
     
  18. Offline

    Garris0n

    Do you have a dependency on worldedit in your plugin?
     
  19. Offline

    khave

    Garris0n You my good sir, is the besets, most sweetest person I know. Thank you for sticking through with me on this! If you ever need anything I will gladly help you!

    Though now I have the need for how to do the //undo and //redo
    The EditSession statements needs a return value. Should it just return the newBlockBag?
     
  20. Offline

    Garris0n

    What do you mean? I haven't looked at the methods and I'm not sure I understand your question.
     
  21. Offline

    khave

    Garris0n The code:
    Code:java
    1. public EditSession undo(BlockBag newBlockBag, LocalPlayer player){
    2.  
    3. }

    Needs to return an EditSession, but how can I return an EditSession?
     
  22. Offline

    Garris0n

    Where is that code coming from?
     
  23. Offline

    khave

    Garris0n
    Link

    But I found out that you can do worldEditPlugin.getSession(p).undo(BlockBag, LocalPlayer)
    Do you know what a BlockBag and a LocaPlayer is?
     
  24. Offline

    Garris0n

    I'm guessing LocalPlayer is a WE representation of a player and the BlockBag docs say "Represents a source to get blocks from and store removed ones.".

    My recommendation is to go find the /undo command in the WE source and see how it works.
     
  25. Offline

    khave

    Garris0n I've managed to sum it down to this code by looking at the WE way of doing it.
    Code:java
    1. public void undo(LocalSession session, LocalPlayer player) throws WorldEditException {
    2. EditSession undone;
    3. undone = session.undo(session.getBlockBag(player), player);
    4. }

    But it seems to not be doing anything at all.
     
  26. Offline

    Garris0n

    How are you calling it?
     
  27. Offline

    khave

    Garris0n

    Code:java
    1. try {
    2. BukkitPlayer localPlayer = worldEditPlugin.wrapPlayer(p);
    3. this.undo(worldEditPlugin.getSession(p), localPlayer);
    4. return true;
    5. } catch(WorldEditException e){
    6. System.out.println("WorldEditException: " + e);
    7. }

    Like that.
     
  28. Offline

    Garris0n

    And is it returning null? Are you undoing actions on the server or that schematic pasting thing, as if that's the case perhaps the scematic pasting isn't saving history properly.
     
  29. Offline

    khave

    Garris0n
    Oh that might be it. I'll try doing a //paste beforehand.

    You were completely right. It's undoing correctly. It's not saving the //paste though.

    EDIT:
    I've tried looking through world edits code, but I cannot find anything that works for me.
    I've found out that they set the clipboard so that they can later undo it, but this code seems to not work:
    Code:java
    1. SchematicFormat format = schematic == null ? null : SchematicFormat.getFormat(schematic);
    2.  
    3. undo.setClipboard(format.load(schematic)); // Undo is a LocalSession
     
  30. Offline

    Garris0n

    I'm not really sure where WE is actually doing it, but you could try using LocalSession#remember(EditSession).
     
Thread Status:
Not open for further replies.

Share This Page