2D Regions

Since 1.0.0

2D regions can be created with the Region2D class, as expected, this regions are created on a two-dimensional space, where only the x and z axis are taken into account, meaning that height is ignored.

Creating 2D regions

Let's see how to create a 2D region taking the 0/0 coordinate as the center of it.

World world = Bukkit.getWorld("world");

// By coordinates: x1, z1, x2, z2
Region2D region = new Region2D(world, 5, 5, -5, -5);

// By locations: loc1, loc2
Location loc1 = new Location(world, 5, 0, 5); // Y coordinate doesn't matter.
Location loc2 = new Location(world, -5, 0, -5); // Y coordinate doesn't matter.
Region2D region = new Region2D(loc1, loc2);

Here is a representation of this region in-game

Checking if anything is inside the region

You can check whether a Location is inside of a Region2D or not, for this example we will use a player, as it's the most common use case for regions.

Player player = Bukkit.getPlayer("xDec0de_");
Region2D region = new Region2D(world, 5, 5, -5, -5);

if (region.contains(player.getLocation()))
    player.sendMessage("You are inside the region");
else
    player.sendMessage("You are outside the region");

Contains vs overlaps

You can also check if a Region2D contains or overlaps another Region2D, but first, let's make sure we understand the difference between these two terms.

  • contains: The region is totally inside of another region

  • overlaps: The region collides with another region, even if it's only by one block.

Alright, let's see some examples then

Region2D region1 = new Region2D(world, 5, 5, -5, -5);
Region2D region2 = new Region2D(world, 1, 1, -1, -1);
Region2D region3 = new Region2D(world, 5, 5, 0, 0);

region1.contains(region2); // true as region2 is totally inside of region1
region2.contains(region1); // false as region1 is bigger than region2
region3.contains(region1); // false as region3 is not totally inside of region1

region1.overlaps(region2); // true as region1 collides with region2
region2.overlaps(region1); // true as region2 also collides with region1 (Obviously)
region3.overlaps(region1); // true as region3 collides with region13

Last updated