Skip to content

Commit

Permalink
Minor optimization to MultiRegion#recalculate, and you can no longer …
Browse files Browse the repository at this point in the history
…add Regions from different worlds to the same MultiRegion
  • Loading branch information
boxbeam committed May 26, 2020
1 parent b4cb233 commit f2c62b2
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/redempt/redlib/region/MultiRegion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -71,6 +68,9 @@ public MultiRegion(Region... regions) {
* @param region The Region to add
*/
public void add(Region region) {
if (!region.getWorld().equals(getWorld())) {
throw new IllegalArgumentException("Region is not in the same world as this MultiRegion");
}
if (region.isMulti()) {
MultiRegion multi = (MultiRegion) region;
for (Region r : multi.getRegions()) {
Expand All @@ -86,10 +86,13 @@ public void add(Region region) {
/**
* Subtracts a Region from this MultiRegion. A subtracted Region overrides all positive Regions,
* meaning adding a Region that overlaps a previously subtracted Region will not add the overlapping blocks.
* Calling {@link MultiRegion#recalculate()} will coalesce into only added Regions,
* Calling {@link MultiRegion#recalculate()} will coalesce into only added Regions.
* @param region The Region to subtract
*/
public void subtract(Region region) {
if (!region.getWorld().equals(getWorld())) {
throw new IllegalArgumentException("Region is not in the same world as this MultiRegion");
}
if (region.isMulti()) {
MultiRegion multi = (MultiRegion) region;
for (Region r : multi.getRegions()) {
Expand All @@ -116,13 +119,6 @@ private static boolean contains(List<Region> regions, Location loc) {
return regions.stream().anyMatch(r -> r.contains(loc));
}

/**
* @return The World this MultiRegion is in
*/
public World getWorld() {
return start.getWorld();
}

/**
* Gets all the cuboid Regions that form this MultiRegion
* @return The list of Regions that form this MultiRegion
Expand Down Expand Up @@ -317,8 +313,8 @@ public void recalculate() {
for (Region region : regions) {
region.stream().map(Block::getLocation)
.filter(l -> this.contains(l) && !contains(newRegions, l))
.sorted((a, b) -> (int) Math.signum(a.distanceSquared(center) - b.distanceSquared(center)))
.findFirst().ifPresent(l -> {
.min((a, b) -> (int) Math.signum(a.distanceSquared(center) - b.distanceSquared(center)))
.ifPresent(l -> {
added[0] = true;
Region reg = new Region(l, l.clone().add(1, 1, 1));
expandToMax(reg, newRegions);
Expand Down

0 comments on commit f2c62b2

Please sign in to comment.