fix(navigation): hide guidance near base
This commit is contained in:
@@ -10,6 +10,7 @@ final class BaseNavigationController implements Runnable {
|
||||
private final Server server;
|
||||
private final BaseStateManager stateManager;
|
||||
private final PluginSettingsProvider settings;
|
||||
private final BaseBoundsService boundsService;
|
||||
|
||||
BaseNavigationController(
|
||||
Server server,
|
||||
@@ -19,6 +20,7 @@ final class BaseNavigationController implements Runnable {
|
||||
this.server = server;
|
||||
this.stateManager = stateManager;
|
||||
this.settings = settings;
|
||||
this.boundsService = new BaseBoundsService(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -32,7 +34,16 @@ final class BaseNavigationController implements Runnable {
|
||||
if (!player.getWorld().getUID().equals(base.worldId())) {
|
||||
continue;
|
||||
}
|
||||
Location origin = player.getLocation().clone().add(0.0, 0.15, 0.0);
|
||||
Location playerLocation = player.getLocation();
|
||||
if (!BaseNavigationVisibility.isBeyondBorderBuffer(
|
||||
base,
|
||||
boundsService.radius(state),
|
||||
playerLocation.getX(),
|
||||
playerLocation.getZ()
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
Location origin = playerLocation.clone().add(0.0, 0.15, 0.0);
|
||||
Vector direction = new Vector(
|
||||
base.x() + 0.5 - origin.getX(),
|
||||
0.0,
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
final class BaseNavigationVisibility {
|
||||
static final int BORDER_BUFFER = 25;
|
||||
|
||||
private BaseNavigationVisibility() {
|
||||
}
|
||||
|
||||
static boolean isBeyondBorderBuffer(
|
||||
BaseLocation base,
|
||||
int radius,
|
||||
double playerX,
|
||||
double playerZ
|
||||
) {
|
||||
if (base == null) {
|
||||
throw new IllegalArgumentException("base is required");
|
||||
}
|
||||
if (radius <= 0) {
|
||||
throw new IllegalArgumentException("base radius must be positive");
|
||||
}
|
||||
double deltaX = playerX - (base.x() + 0.5);
|
||||
double deltaZ = playerZ - (base.z() + 0.5);
|
||||
double minimumDistance = radius + BORDER_BUFFER;
|
||||
return deltaX * deltaX + deltaZ * deltaZ > minimumDistance * minimumDistance;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Particle;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class BaseNavigationControllerTest {
|
||||
@Test
|
||||
void showsParticlesOnlyMoreThanTwentyFiveBlocksBeyondBorder() {
|
||||
UUID playerId = UUID.randomUUID();
|
||||
UUID worldId = UUID.randomUUID();
|
||||
PlayerState state = PlayerState.newPlayer(playerId, "Builder")
|
||||
.withAdministrativeLevels(2, 0, 0, 0, 0, true, false, false)
|
||||
.withBase(new BaseLocation(worldId, "world", 0, 64, 0, 0, 0), Instant.EPOCH);
|
||||
World world = mock(World.class);
|
||||
when(world.getUID()).thenReturn(worldId);
|
||||
Player player = mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(playerId);
|
||||
when(player.getName()).thenReturn("Builder");
|
||||
when(player.getWorld()).thenReturn(world);
|
||||
when(player.getLocation()).thenReturn(new Location(world, 35.5, 64.0, 0.5));
|
||||
Server server = mock(Server.class);
|
||||
doReturn(java.util.List.of(player)).when(server).getOnlinePlayers();
|
||||
BaseStateManager stateManager = mock(BaseStateManager.class);
|
||||
when(stateManager.player(playerId, "Builder")).thenReturn(state);
|
||||
PluginSettingsProvider settings = new PluginSettingsProvider(
|
||||
PluginSettings.from(Map.of())
|
||||
);
|
||||
|
||||
new BaseNavigationController(server, stateManager, settings).run();
|
||||
|
||||
verify(player, never()).spawnParticle(
|
||||
eq(Particle.END_ROD), any(Location.class), eq(1),
|
||||
eq(0.0), eq(0.0), eq(0.0), eq(0.0)
|
||||
);
|
||||
|
||||
when(player.getLocation()).thenReturn(new Location(world, 35.51, 64.0, 0.5));
|
||||
new BaseNavigationController(server, stateManager, settings).run();
|
||||
|
||||
verify(player, times(settings.current().navigationParticleCount())).spawnParticle(
|
||||
eq(Particle.END_ROD), any(Location.class), eq(1),
|
||||
eq(0.0), eq(0.0), eq(0.0), eq(0.0)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package games.dmg.spigotbase;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
final class BaseNavigationVisibilityTest {
|
||||
private static final BaseLocation BASE = new BaseLocation(
|
||||
UUID.randomUUID(), "world", 0, 64, 0, 0, 0
|
||||
);
|
||||
|
||||
@Test
|
||||
void hidesNavigationAtAndWithinTwentyFiveBlocksBeyondBorder() {
|
||||
assertFalse(BaseNavigationVisibility.isBeyondBorderBuffer(BASE, 10, 35.5, 0.5));
|
||||
assertFalse(BaseNavigationVisibility.isBeyondBorderBuffer(BASE, 10, 20.5, 0.5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void showsNavigationMoreThanTwentyFiveBlocksBeyondCurrentBorder() {
|
||||
assertTrue(BaseNavigationVisibility.isBeyondBorderBuffer(BASE, 10, 35.51, 0.5));
|
||||
assertFalse(BaseNavigationVisibility.isBeyondBorderBuffer(BASE, 75, 100.5, 0.5));
|
||||
assertTrue(BaseNavigationVisibility.isBeyondBorderBuffer(BASE, 75, 100.51, 0.5));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user