WorldGuard/Edit Region API (Get Blocks from Poly)

Discussion in 'Plugin Development' started by Stoux, Oct 16, 2013.

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


    Hello all,

    I've worked with the WorldGuard API before, but mainly just to get regions etc. Now I came up with some idea's of changing some Regions to a particular block (Aka WorldEdit command: //Set [Block]), where the user could enter which regions should be changed in the plugin.yml.
    Getting the WorldGuard is, as I said, no problem. If it's a Cuboid WorldGuard it's no problem either to get all the blocks (You get the Top point, Bottom point -> Loop row for row , level for level). The problem starts when it's a Polygon region (which will be very likely). Here it is impossible to loop thru the row by row (level by level still works), because it's (most likely) not a square any more. So:

    Too long; Didn't read: How to get all the org.bukkit.block.Block's in a specific WorldGuard polygon region.

    Thanks in advance,
  2. Offline


    use google
    WorldGuard get blocks from poly bukkit

    lol that text went big cus i copy it lol
  3. Offline


    TheUpdater I've done that (This thread is the first entry..). Also looked @ the source code to figure it out, but I find it quite hard to read thru.
  4. Offline


    If you have a WorldEdit region you can just iterate through the blocks with it. But as of you only have the WorldGuard region (?) and not the WorldEdit region you first need to get it from the WorldGuard region. I don't know a better way of getting the WorldEdit region from a WorldGuard region than using the points of the region to create a new WorldEdit region. If you find a better way you can use it of course.
    So if you want to get all blocks in a WorldGuard polygon region you need the region and the world where the region is.
    1. ProtectedPolygonalRegion wgRegion; // WorldGuard region
    2. LocalWorld world; // World of the region
    Now you can create the WorldEdit region:
    1. Polygonal2DRegion weRegion = new Polygonal2DRegion(world, wgRegion.getPoints(), wgRegion.getMinimumPoint().getBlockY(), wgRegion.getMaximumPoint().getBlockY());
    And iterate through the blocks of it:
    1. for (BlockVector block : weRegion) {
    2. Block bukkitBlock = BukkitUtil.toBlock(new BlockWorldVector(world, block));
    3. // Do something with the block
    4. }
    The BlockVector contains only the position of the block, not the world. To get the Bukkit block from it you first need to create a new BlockWorldVector, or you use the Bukkit world and get the block through World.getBlockAt(x, y, z). ;)

    If you only want to change the blocks of a WorldGuard region, you can also use WorldEdit to set the blocks. This is a little bit simpler (You still need the WorldEdit region though):
    1. // Create a edit session with unlimited blocks
    2. EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1);
    3. try {
    4. editSession.setBlocks(weRegion, new BaseBlock(BlockID.STONE));
    5. } catch (MaxChangedBlocksException e) {
    6. // As of the blocks are unlimited this should not be called
    7. }

    You can use the same way for the cuboid region by the way. :)
    Stoux likes this.
  5. Offline


    Minecrell Awesome, thanks for the detailed explanation!
Thread Status:
Not open for further replies.

Share This Page