Implement ExactDestination#fromLocation

This commit is contained in:
Ben Woo 2024-11-21 09:57:07 +08:00
parent d3f6b11fe5
commit cee6a4879a
3 changed files with 22 additions and 0 deletions

View File

@ -36,6 +36,16 @@ public class ExactDestination implements Destination<ExactDestination, ExactDest
return "e";
}
/**
* Make a new {@link ExactDestinationInstance} from a {@link Location}.
*
* @param location The target location
* @return A new {@link ExactDestinationInstance}
*/
public @NotNull ExactDestinationInstance fromLocation(@NotNull Location location) {
return new ExactDestinationInstance(this, location);
}
/**
* {@inheritDoc}
*/

View File

@ -31,6 +31,7 @@ public class ExactDestinationInstance extends DestinationInstance<ExactDestinati
*/
@Override
public @NotNull Option<Location> getLocation(@NotNull Entity teleportee) {
// todo: maybe check if the world is null?
return Option.of(location);
}

View File

@ -69,6 +69,17 @@ class DestinationTest : TestWithMockBukkit() {
assertEquals("e:world:1.2,2.0,3.0:10.5:9.5", destination.toString())
}
@Test
fun `Exact destination instance from location`() {
val exactDestination = serviceLocator.getActiveService(ExactDestination::class.java).takeIf { it != null } ?: run {
throw IllegalStateException("ExactDestination is not available as a service") }
val location = Location(world.bukkitWorld.orNull, 1.2, 2.0, 3.0, 9.5F, 10.5F)
val destination = exactDestination.fromLocation(location)
assertEquals(location, destination.getLocation(player).orNull)
assertEquals("e:world:1.2,2.0,3.0:10.5:9.5", destination.toString())
}
@Test
fun `Player destination instance`() {
assertTrue(destinationsProvider.getDestinationById("pl") is PlayerDestination)