|
13 | 13 | import org.bukkit.Bukkit; |
14 | 14 | import org.bukkit.Location; |
15 | 15 | import org.bukkit.World; |
| 16 | +import org.bukkit.block.Block; |
16 | 17 | import org.bukkit.entity.Player; |
| 18 | +import org.mockito.ArgumentMatchers; |
17 | 19 | import org.bukkit.configuration.file.YamlConfiguration; |
18 | 20 | import org.junit.jupiter.api.Test; |
19 | 21 | import fr.xephi.authme.TempFolder; |
|
27 | 29 | import org.bukkit.GameRule; |
28 | 30 |
|
29 | 31 | import static org.hamcrest.Matchers.both; |
| 32 | +import static org.hamcrest.Matchers.closeTo; |
30 | 33 | import static org.hamcrest.Matchers.equalTo; |
31 | 34 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
32 | 35 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
@@ -147,24 +150,127 @@ public void shouldReturnExactWorldSpawnForServerPriorityWithZeroRadius() { |
147 | 150 | } |
148 | 151 |
|
149 | 152 | @Test |
150 | | - public void shouldReturnLocationWithinRadiusForServerPriority() { |
| 153 | + public void shouldReturnExactYAndFaceTowardCenterWhenBaseYIsAlreadySafe() { |
151 | 154 | // given |
152 | 155 | given(settings.getProperty(RestrictionSettings.SPAWN_PRIORITY)).willReturn("server"); |
153 | 156 | spawnLoader.reload(); |
154 | 157 |
|
155 | 158 | World world = mock(World.class); |
| 159 | + // worldSpawn at (100, 64, 200); place the result due east (+X) so the expected yaw is -90° |
156 | 160 | Location worldSpawn = new Location(world, 100.0, 64.0, 200.0); |
157 | 161 | given(world.getSpawnLocation()).willReturn(worldSpawn); |
| 162 | + // radius=0 forces dx=dz=0 → exact worldSpawn → returned as-is (no yaw recalculation) |
158 | 163 | given(world.getGameRuleValue(GameRule.SPAWN_RADIUS)).willReturn(10); |
159 | | - given(world.getHighestBlockYAt(org.mockito.ArgumentMatchers.anyInt(), org.mockito.ArgumentMatchers.anyInt())).willReturn(63); |
| 164 | + |
| 165 | + Block passable = mock(Block.class); |
| 166 | + given(passable.isPassable()).willReturn(true); |
| 167 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(64), ArgumentMatchers.anyInt())) |
| 168 | + .willReturn(passable); |
| 169 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(65), ArgumentMatchers.anyInt())) |
| 170 | + .willReturn(passable); |
160 | 171 |
|
161 | 172 | // when |
162 | 173 | Location result = spawnLoader.getSpawnLocation(world); |
163 | 174 |
|
164 | | - // then |
| 175 | + // then – position within radius, Y unchanged, pitch = 0 |
165 | 176 | assertThat(result.getX(), both(greaterThanOrEqualTo(90.5)).and(lessThanOrEqualTo(110.5))); |
166 | 177 | assertThat(result.getZ(), both(greaterThanOrEqualTo(190.5)).and(lessThanOrEqualTo(210.5))); |
167 | | - assertThat(result.getY(), equalTo(64.0)); // highestBlockYAt(63) + 1 = 64 |
| 178 | + assertThat(result.getY(), equalTo(64.0)); |
| 179 | + assertThat((double) result.getPitch(), closeTo(0.0, 0.001)); |
| 180 | + // yaw must point from the result position toward worldSpawn (100, 200) |
| 181 | + double expectedYaw = Math.toDegrees(Math.atan2(-(100.0 - result.getX()), 200.0 - result.getZ())); |
| 182 | + assertThat((double) result.getYaw(), closeTo(expectedYaw, 0.001)); |
| 183 | + } |
| 184 | + |
| 185 | + @Test |
| 186 | + public void shouldSearchDownwardWhenBaseYIsInAVoid() { |
| 187 | + // given – foot at baseY is passable but head (baseY+1) is solid (1-block-high gap, e.g. near Nether ceiling) |
| 188 | + given(settings.getProperty(RestrictionSettings.SPAWN_PRIORITY)).willReturn("server"); |
| 189 | + spawnLoader.reload(); |
| 190 | + |
| 191 | + World world = mock(World.class); |
| 192 | + Location worldSpawn = new Location(world, 0.0, 70.0, 0.0); |
| 193 | + given(world.getSpawnLocation()).willReturn(worldSpawn); |
| 194 | + given(world.getGameRuleValue(GameRule.SPAWN_RADIUS)).willReturn(5); |
| 195 | + |
| 196 | + Block passable = mock(Block.class); |
| 197 | + given(passable.isPassable()).willReturn(true); |
| 198 | + Block solid = mock(Block.class); |
| 199 | + given(solid.isPassable()).willReturn(false); |
| 200 | + |
| 201 | + // y=70 passable, y=71 solid → initial check fails; foot is passable → search downward |
| 202 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(70), ArgumentMatchers.anyInt())) |
| 203 | + .willReturn(passable); |
| 204 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(71), ArgumentMatchers.anyInt())) |
| 205 | + .willReturn(solid); |
| 206 | + // y=69 passable → isPassable(69) && isPassable(70) = true → safe at y=69 |
| 207 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(69), ArgumentMatchers.anyInt())) |
| 208 | + .willReturn(passable); |
| 209 | + |
| 210 | + // when |
| 211 | + Location result = spawnLoader.getSpawnLocation(world); |
| 212 | + |
| 213 | + // then – first clear 2-block gap found going downward is at y=69 |
| 214 | + assertThat(result.getY(), equalTo(69.0)); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void shouldSearchUpwardWhenBaseYIsInsideASolidBlock() { |
| 219 | + // given – baseY is inside a solid block (e.g. underground) |
| 220 | + given(settings.getProperty(RestrictionSettings.SPAWN_PRIORITY)).willReturn("server"); |
| 221 | + spawnLoader.reload(); |
| 222 | + |
| 223 | + World world = mock(World.class); |
| 224 | + Location worldSpawn = new Location(world, 0.0, 60.0, 0.0); |
| 225 | + given(world.getSpawnLocation()).willReturn(worldSpawn); |
| 226 | + given(world.getGameRuleValue(GameRule.SPAWN_RADIUS)).willReturn(5); |
| 227 | + |
| 228 | + Block passable = mock(Block.class); |
| 229 | + given(passable.isPassable()).willReturn(true); |
| 230 | + Block solid = mock(Block.class); |
| 231 | + given(solid.isPassable()).willReturn(false); |
| 232 | + |
| 233 | + // y=60 is solid → search upward |
| 234 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(60), ArgumentMatchers.anyInt())) |
| 235 | + .willReturn(solid); |
| 236 | + // y=61 solid, y=62 solid, y=63 passable, y=64 passable → safe at y=63 |
| 237 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(61), ArgumentMatchers.anyInt())) |
| 238 | + .willReturn(solid); |
| 239 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(62), ArgumentMatchers.anyInt())) |
| 240 | + .willReturn(solid); |
| 241 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(63), ArgumentMatchers.anyInt())) |
| 242 | + .willReturn(passable); |
| 243 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.eq(64), ArgumentMatchers.anyInt())) |
| 244 | + .willReturn(passable); |
| 245 | + |
| 246 | + // when |
| 247 | + Location result = spawnLoader.getSpawnLocation(world); |
| 248 | + |
| 249 | + // then |
| 250 | + assertThat(result.getY(), equalTo(63.0)); |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + public void shouldFallbackToExactWorldSpawnWhenNoSafeSpotFoundWithinMargin() { |
| 255 | + // given |
| 256 | + given(settings.getProperty(RestrictionSettings.SPAWN_PRIORITY)).willReturn("server"); |
| 257 | + spawnLoader.reload(); |
| 258 | + |
| 259 | + World world = mock(World.class); |
| 260 | + Location worldSpawn = new Location(world, 12.0, 64.0, -34.0); |
| 261 | + given(world.getSpawnLocation()).willReturn(worldSpawn); |
| 262 | + given(world.getGameRuleValue(GameRule.SPAWN_RADIUS)).willReturn(5); |
| 263 | + |
| 264 | + Block solid = mock(Block.class); |
| 265 | + given(solid.isPassable()).willReturn(false); |
| 266 | + given(world.getBlockAt(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) |
| 267 | + .willReturn(solid); |
| 268 | + |
| 269 | + // when |
| 270 | + Location result = spawnLoader.getSpawnLocation(world); |
| 271 | + |
| 272 | + // then – falls back to exact worldSpawn (X/Y/Z unchanged) |
| 273 | + assertThat(result, equalTo(worldSpawn)); |
168 | 274 | } |
169 | 275 |
|
170 | 276 | @Test |
|
0 commit comments