|
| 1 | +# Filling a region using a pattern |
| 2 | + |
| 3 | +## Foreword |
| 4 | +First of all, patterns usually come from the core part of WorldEdit or FAWE. You can find all existing patterns [here](https://intellectualsites.github.io/fastasyncworldedit-javadocs/worldedit-core/com/sk89q/worldedit/function/pattern/Pattern.html) |
| 5 | + |
| 6 | +## Biome Cuboid Example |
| 7 | +Given are 3 things: |
| 8 | +- Position1: X=300, y=-64, z=300 |
| 9 | +- Position2: X=600, y=128, z=600 |
| 10 | +- BiomeType: Badlands |
| 11 | + |
| 12 | +```java |
| 13 | +public void setBiomeArea(World world, BlockVector3 position1, BlockVector3 position 2) { |
| 14 | + // Here we must first adapt the world as in the WorldEdit example or translate it into the WorldEdit specialized object. |
| 15 | + World faweWorld = BukkitAdapter.adapt(world); |
| 16 | + |
| 17 | + //Now we define a square region using our 2 positions |
| 18 | + CuboidRegion cuboidRegion = new CuboidRegion(position1, position2); |
| 19 | + |
| 20 | + // Now we open an EditSession, which contains the complete process or information during an edit process in the world. |
| 21 | + // Through the try-and-catch-with-resources we let Java automatically close the resource EditSession after use |
| 22 | + try (EditSession editSession = WorldEdit.getInstance().newEditSession(faweWorld)) { |
| 23 | + // Here we create our biome pattern |
| 24 | + var pattern = new BiomeApplyingPattern(editSession, BiomeTypes.BADLANDS); |
| 25 | + |
| 26 | + //This sets the pattern to the region |
| 27 | + editSession.setBlocks((Region) cuboidRegion, pattern); |
| 28 | + } |
| 29 | +} |
| 30 | +// This code would be called by the plugin in the main thread |
| 31 | +public void call() { |
| 32 | + // Here we set our position 1 and 2 |
| 33 | + BlockVector3 position1 = BlockVector3.at(300, -64, 300); |
| 34 | + BlockVector3 position2 = BlockVector3.at(600, 128, 600); |
| 35 | + World bukkitWorld = Bukkit.getWorld("world"); |
| 36 | + setBiomeArea(bukkitWorld, position1, position2); |
| 37 | +} |
| 38 | +``` |
| 39 | +_This code is still Fawe unspecific and can also be used in WorldEdit in this way_ |
| 40 | + |
| 41 | +### Let's set blocks async with FAWE |
| 42 | + |
| 43 | +Now we run the code Async to let Fawe manage server resources: |
| 44 | +```java |
| 45 | +// Now we wrap our code in a runnable with a functional interface and have it executed by Bukkit's scheduler system |
| 46 | +Runnable executeCode = () -> { |
| 47 | + // Here we set our position 1 and 2 |
| 48 | + BlockVector3 position1 = BlockVector3.at(300, -64, 300); |
| 49 | + BlockVector3 position2 = BlockVector3.at(600, 128, 600); |
| 50 | + World bukkitWorld = Bukkit.getWorld("world"); |
| 51 | + setBiomeArea(bukkitWorld, position1, position2); |
| 52 | +}; |
| 53 | +Bukkit.getScheduler().runTaskAsynchronously(Bukkit.getPluginManager().getPlugin("YourPluginNameHere"), executeCode); |
| 54 | +``` |
0 commit comments