Generate complete roads without rewrites

This commit is contained in:
2026-09-27 07:30:31 -07:00
parent fd5bf785d3
commit 30c31c185b
7 changed files with 264 additions and 103 deletions

View File

@@ -0,0 +1,63 @@
/*
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.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.type.Stairs;
import org.junit.jupiter.api.Test;
class RoadBlockWriterTest {
@Test
void doesNotRewriteAnExistingRoadBlockOrItsClearance() {
World world = mock(World.class);
Block roadBlock = mock(Block.class);
when(world.getBlockAt(4, 62, 8)).thenReturn(roadBlock);
when(roadBlock.getType()).thenReturn(Material.COBBLESTONE);
RoadBlockWriter.build(world, 4, 62, 8, Material.COBBLESTONE, Material.STONE, true, 4);
verify(roadBlock, never()).setType(any(Material.class));
verify(world, never()).getBlockAt(4, 63, 8);
}
@Test
void fillsMissingRoadBlocksAndPreservesExistingStairTreads() {
World world = mock(World.class);
Block missingRoadBlock = mock(Block.class);
Block stairBlock = mock(Block.class);
when(world.getBlockAt(4, 62, 8)).thenReturn(missingRoadBlock);
when(world.getBlockAt(5, 76, 12)).thenReturn(stairBlock);
when(missingRoadBlock.getType()).thenReturn(Material.AIR);
when(stairBlock.getType()).thenReturn(Material.COBBLESTONE_STAIRS);
when(stairBlock.getBlockData()).thenReturn(mock(Stairs.class));
RoadBlockWriter.build(world, 4, 62, 8, Material.COBBLESTONE, Material.STONE, false, 0);
RoadBlockWriter.build(world, 5, 76, 12, Material.COBBLESTONE, Material.STONE, false, 4);
verify(missingRoadBlock).setType(Material.COBBLESTONE);
verify(stairBlock, never()).setType(any(Material.class));
}
}

View File

@@ -0,0 +1,48 @@
/*
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.assertTrue;
import java.util.HashSet;
import java.util.List;
import org.junit.jupiter.api.Test;
class RoadLayoutTest {
private static final Cuboid PLOT = new Cuboid(2, 62, 2, 33, 71, 33);
@Test
void buildsFullRoadWidthAndNonOverlappingCornersAtOnce() {
List<RoadLayout.BlockPosition> road = RoadLayout.surroundingRoad(PLOT, 4);
HashSet<RoadLayout.BlockPosition> uniqueBlocks = new HashSet<>(road);
assertEquals(576, road.size());
assertEquals(road.size(), uniqueBlocks.size());
assertTrue(road.contains(new RoadLayout.BlockPosition(34, 2)));
assertTrue(road.contains(new RoadLayout.BlockPosition(37, 33)));
assertTrue(road.contains(new RoadLayout.BlockPosition(2, 34)));
assertTrue(road.contains(new RoadLayout.BlockPosition(33, 37)));
}
@Test
void returnsNoRoadForZeroWidth() {
assertTrue(RoadLayout.surroundingRoad(PLOT, 0).isEmpty());
}
}