Avoid saved WorldGuard regions during allocation
This commit is contained in:
@@ -90,6 +90,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
private HashMap<UUID, UserOverride> _userOverrides;
|
||||
private List<HomePermissionOverride> _permissionOverrides;
|
||||
private HashMap<UUID, Integer> _currentHomes;
|
||||
private List<Cuboid> _savedWorldGuardBounds = List.of();
|
||||
|
||||
private PlayerJoinListener _playerJoinListener = null;
|
||||
|
||||
@@ -891,7 +892,7 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return WorldGuardRegionOccupancy.overlapsAny(cuboid, _savedWorldGuardBounds);
|
||||
}
|
||||
|
||||
private boolean areBlocksOccupied(int row, int col, int i) {
|
||||
@@ -911,6 +912,8 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
private Cuboid findNextUnownedHomeRegion(int plotMultiplier) {
|
||||
refreshSavedWorldGuardBounds();
|
||||
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
int ring = 0;
|
||||
@@ -995,6 +998,11 @@ public class MetropolisPlugin extends JavaPlugin {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void refreshSavedWorldGuardBounds() {
|
||||
_savedWorldGuardBounds =
|
||||
WorldGuardRegionOccupancy.boundsToAvoid(regionManager.getRegions().values());
|
||||
}
|
||||
|
||||
private boolean isAvailableSupportedPlot(int row, int col, int plotMultiplier, int level) {
|
||||
return !areBlocksOccupied(row, col, plotMultiplier, level)
|
||||
&& PlotLevelSupport.hasCompleteSupport(
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
This file is part of Metropolis.
|
||||
|
||||
Metropolis is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Metropolis is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with Metropolis. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
|
||||
*/
|
||||
|
||||
package com.majinnaibu.bukkitplugins.metropolis;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
|
||||
public final class WorldGuardRegionOccupancy {
|
||||
private WorldGuardRegionOccupancy() {}
|
||||
|
||||
public static List<Cuboid> boundsToAvoid(Collection<ProtectedRegion> regions) {
|
||||
List<Cuboid> bounds = new ArrayList<>();
|
||||
for (ProtectedRegion region : regions) {
|
||||
String id = region.getId();
|
||||
if (id.equalsIgnoreCase("city") || id.equalsIgnoreCase("__global__")) {
|
||||
continue;
|
||||
}
|
||||
bounds.add(new Cuboid(region.getMinimumPoint(), region.getMaximumPoint()));
|
||||
}
|
||||
return List.copyOf(bounds);
|
||||
}
|
||||
|
||||
public static boolean overlapsAny(Cuboid candidate, Collection<Cuboid> occupiedBounds) {
|
||||
return occupiedBounds.stream().anyMatch(candidate::intersects);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
This file is part of Metropolis.
|
||||
|
||||
Metropolis is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Metropolis is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with Metropolis. If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
|
||||
*/
|
||||
|
||||
package com.majinnaibu.bukkitplugins.metropolis;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class WorldGuardRegionOccupancyTest {
|
||||
@Test
|
||||
void treatsArbitrarySavedRegionBoundsAsOccupiedButIgnoresCityAndGlobalRegions() {
|
||||
ProtectedRegion market =
|
||||
region("market", BlockVector3.at(40, 62, 40), BlockVector3.at(70, 90, 70));
|
||||
ProtectedRegion city =
|
||||
region("City", BlockVector3.at(-100, 0, -100), BlockVector3.at(100, 320, 100));
|
||||
ProtectedRegion global =
|
||||
region(
|
||||
"__global__",
|
||||
BlockVector3.at(-30_000_000, -64, -30_000_000),
|
||||
BlockVector3.at(30_000_000, 320, 30_000_000));
|
||||
|
||||
List<Cuboid> occupied = WorldGuardRegionOccupancy.boundsToAvoid(List.of(market, city, global));
|
||||
|
||||
assertEquals(1, occupied.size());
|
||||
assertTrue(WorldGuardRegionOccupancy.overlapsAny(new Cuboid(60, 70, 60, 80, 75, 80), occupied));
|
||||
assertFalse(WorldGuardRegionOccupancy.overlapsAny(new Cuboid(0, 62, 0, 31, 71, 31), occupied));
|
||||
}
|
||||
|
||||
private static ProtectedRegion region(String id, BlockVector3 minimum, BlockVector3 maximum) {
|
||||
ProtectedRegion region = mock(ProtectedRegion.class);
|
||||
when(region.getId()).thenReturn(id);
|
||||
when(region.getMinimumPoint()).thenReturn(minimum);
|
||||
when(region.getMaximumPoint()).thenReturn(maximum);
|
||||
return region;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user