[Tutorial] [WorldEditAPI: setting blocks with %'s and ratios]

Discussion in 'Resources' started by WD_MUFFINFLUFFER, Apr 22, 2014.

Thread Status:
Not open for further replies.
  1. Hey guys so I recently had this problem when using the WorldEdit API
    How do I do something like
    //set1%diamond,2%gold with WorldEdit API?
    Eventually here is how I did it:​

    -----------------------------------------------------------------------------------------------
    First, if you do not want to use a players world edit selection, you can create a Cuboid selection with the following code. It takes a World, Location, Location.
    Code:java
    1. CuboidSelection selection = new CuboidSelection(world, minimumLocation, maximumLoc);

    Next we want to turn that selection into a region:
    Code:java
    1. Region region = null;

    Then use a try catch to set the region
    Code:java
    1. try {
    2. region = selection.getRegionSelector().getRegion();
    3. } catch (IncompleteRegionException e) {
    4. e.printStackTrace();
    5. }

    Next we need to create an editSession to be able to set blocks in the region
    Before we create an editSession you need to convert your World to a LocalWorld:
    Code:java
    1. LocalWorld lw = BukkitUtil.getLocalWorld(world); //The world that the region is in

    Next lets create the editSession. It takes a LocalWorld and a -1 this way we do not get a MaxChangedBlocksException error
    Code:java
    1. EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(lw, -1);

    Next, we must put all the BlockChances into a list.
    Create the list with:
    Code:java
    1. List<BlockChance> blocks = new ArrayList<BlockChance>();

    Create a block that we will be setting with:
    Code:java
    1. BaseBlock bb = new BaseBlock(BLOCK ID HERE);

    Then put that base block into the BlockChance list, along with the chance (%) of its spawn.
    Code:java
    1. blocks.add(new BlockChance(bb, CHANCE)); //CHANCE is an Integer or double, I do not remember

    Finally we create a RandomFillPattern which creates a random set of blocks to fill the region with. However, since we included a chance, it will take note of the chance of that block spawning:
    Code:java
    1. RandomFillPattern pattern = new RandomFillPattern(blocks);

    Finally we set the pattern to the blocks
    Code:java
    1. try {
    2. editSession.setBlocks(region, pattern);
    3. } catch (MaxChangedBlocksException e) {
    4. e.printStackTrace();
    5. }

    -----------------------------------------------------------------------------------------------
    I hope you understood the tutorial! Any questions post below :D
     
  2. Offline

    bigteddy98

    Nice tut, only one little question, will calling these methods async cause problems?
     
  3. bigteddy98
    No, but you can use ASyncWorldEdit for that. It requires Spout [I think]
    <Edit by Moderator: Redacted not allowed paid resource url>
     
    Last edited by a moderator: Feb 7, 2021
Thread Status:
Not open for further replies.

Share This Page