Worldedit API

Discussion in 'Plugin Development' started by mastermustard, Mar 9, 2013.

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

    mastermustard

    So how could I use the worldedit api to place a schematic infront of a player when they type a command. After looking at the JavaDocs for Worldedit all I found was the load() method which copies a schematic to your clipboard in which I don't want to do.
     
  2. Offline

    xmarinusx

  3. Offline

    mastermustard

    Last edited by a moderator: May 31, 2016
  4. Offline

    Minecrell

    I'm sure this isn't the best way for this but it works for me.

    Code:java
    1. Player player = (Player) sender;
    2.  
    3. WorldEditPlugin we = (WorldEditPlugin) this.getServer().getPluginManager().getPlugin("WorldEdit");
    4.  
    5. LocalSession localSession = we.getSession(player);
    6.  
    7. LocalPlayer wePlayer = we.wrapPlayer(player);
    8.  
    9. SchematicFormat schematic = SchematicFormat.getFormat(new File(this.getDataFolder(), "test.schematic"));
    10.  
    11. try {
    12. CuboidClipboard clipboard = schematic.load(new File(this.getDataFolder(), "test.schematic"));
    13.  
    14. clipboard.paste(localSession.createEditSession(wePlayer), BukkitUtil.toVector(player.getLocation()), false);
    15. } catch (IOException | DataException | MaxChangedBlocksException e) {
    16. e.printStackTrace();
    17. }
     
  5. Offline

    AmShaegar

    I just did exactly what you want 2 hours ago. Took me some work in research but I think it was worth it because you do not need to use WorldEdit for my approach.

    This version is far from perfect and good style but you should be able to identify the important pieces of code. But you need some craftbukkit code so craftbukkit.jar has to be in your build path.

    Code:JAVA
    1. if(commandLabel.equalsIgnoreCase("load")) {
    2. if(!(sender instanceof Player))
    3. return false;
    4.  
    5. Player player = (Player) sender;
    6.  
    7. try {
    8. FileInputStream fis = new FileInputStream("load.schematic");
    9. NBTTagCompound nbtdata = NBTCompressedStreamTools.a(fis);
    10.  
    11.  
    12. short width = nbtdata.getShort("Width");
    13. short height = nbtdata.getShort("Height");
    14. short length = nbtdata.getShort("Length");
    15.  
    16. byte[] blocks = nbtdata.getByteArray("Blocks");
    17. byte[] data = nbtdata.getByteArray("Data");
    18.  
    19. World w = getServer().getWorlds().get(0);
    20. Location l, p = player.getLocation().clone();
    21.  
    22. for(int y = 0; y < height; y++) {
    23. for(int z = 0; z < length; z++) {
    24. for(int x = 0; x < width; x++) {
    25.  
    26. l = p.clone().add(x, y, z);
    27. int index = x + (y * length + z) * width;
    28. l.getBlock().setTypeIdAndData(blocks[index], data[index], false);
    29. }
    30. }
    31. }
    32. player.sendMessage("Success!");
    33. fis.close();
    34. } catch (Exception e) {
    35. e.printStackTrace();
    36. }
    37. }
    38.  
     
Thread Status:
Not open for further replies.

Share This Page