feat(commands): view and set online player heights
This commit is contained in:
1 parent
e802319f45
commit
f011410b17
15 files changed
+380
-16
No files matched your search
@@ -1,6 +1,9 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -12,6 +15,18 @@ class HeightStoreTest {
|
||||
@TempDir
|
||||
Path temporaryDirectory;
|
||||
|
||||
@Test
|
||||
void failedSaveDoesNotChangeCachedOrSavedScale() throws Exception {
|
||||
UUID playerId = UUID.randomUUID();
|
||||
HeightStore store = new HeightStore(temporaryDirectory.toFile());
|
||||
store.save(playerId, 0.7);
|
||||
String original = Files.readString(temporaryDirectory.resolve("state.yml"));
|
||||
Files.createDirectory(temporaryDirectory.resolve("state.yml.tmp"));
|
||||
assertThrows(IOException.class, () -> store.save(playerId, 0.2));
|
||||
assertEquals(0.7, store.find(playerId));
|
||||
assertEquals(original, Files.readString(temporaryDirectory.resolve("state.yml")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorationReplacesPreviousSizeAndSurvivesReloadOutsideLimits() throws Exception {
|
||||
UUID playerId = UUID.randomUUID();
|
||||
|
||||
@@ -5,18 +5,77 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class HeightsCommandTest {
|
||||
private final List<HeightSettings> saved = new ArrayList<>();
|
||||
private final List<String> messages = new ArrayList<>();
|
||||
private final LiveHeightSettings live = new LiveHeightSettings(
|
||||
new HeightSettings(0.4, 2.0, 0.1, 0.5, 1.5, 20), saved::add);
|
||||
private final HeightsCommand command = new HeightsCommand(live, Logger.getAnonymousLogger());
|
||||
@TempDir
|
||||
Path directory;
|
||||
private final UUID playerId = UUID.randomUUID();
|
||||
private double playerScale = 0.7;
|
||||
private PlayerHeights players;
|
||||
private HeightStore playerStore;
|
||||
private HeightsCommand command;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
playerStore = new HeightStore(directory.toFile());
|
||||
players = new PlayerHeights(live, playerStore, name -> name.equalsIgnoreCase("Steve")
|
||||
? new PlayerHeights.Target(playerId, "Steve", playerScale, playerScale, value -> playerScale = value)
|
||||
: null, () -> List.of("Steve"));
|
||||
command = new HeightsCommand(live, players, Logger.getAnonymousLogger());
|
||||
}
|
||||
|
||||
@Test
|
||||
void viewsAndSetsOnlinePlayerWithNameAndSizeCompletion() {
|
||||
CommandSender sender = sender(true);
|
||||
command.onCommand(sender, null, "heights", new String[] {"player", "Steve"});
|
||||
assertTrue(messages.getLast().contains("0.7"));
|
||||
command.onCommand(sender, null, "heights", new String[] {"player", "steve", "0.5"});
|
||||
assertEquals(0.5, playerScale);
|
||||
assertEquals(0.5, playerStore.find(playerId));
|
||||
assertTrue(messages.getLast().contains("Steve"));
|
||||
assertEquals(List.of("player"), complete(sender, "pl"));
|
||||
assertEquals(List.of("Steve"), complete(sender, "player", "st"));
|
||||
assertTrue(complete(sender, "player", "Steve", "0.").contains("0.5"));
|
||||
assertTrue(complete(sender, "player", "Steve", "1").contains("1.0"));
|
||||
assertEquals(List.of(), complete(sender, "player", "Offline", ""));
|
||||
assertEquals(List.of(), complete(sender, "player", "Steve", "0.5", ""));
|
||||
for (String value : complete(sender, "player", "Steve", "")) {
|
||||
assertTrue(players.isValid(Double.parseDouble(value)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidPlayerCommandsAndFailedSavesAreReportedWithoutResizing() throws Exception {
|
||||
for (String[] args : new String[][] {
|
||||
{"player"}, {"player", "Steve", "0.5", "extra"},
|
||||
{"player", "Offline"}, {"player", "Ste", "0.5"},
|
||||
{"player", "Steve", "nope"}, {"player", "Steve", "NaN"},
|
||||
{"player", "Steve", "0.2"}, {"player", "Steve", "3.0"}}) {
|
||||
int before = messages.size();
|
||||
command.onCommand(sender(true), null, "heights", args);
|
||||
assertEquals(before + 1, messages.size());
|
||||
assertEquals(0.7, playerScale);
|
||||
assertEquals(null, playerStore.find(playerId));
|
||||
}
|
||||
java.nio.file.Files.createDirectory(directory.resolve("state.yml.tmp"));
|
||||
command.onCommand(sender(true), null, "heights", new String[] {"player", "Steve", "0.5"});
|
||||
assertTrue(messages.getLast().contains("Could not save"));
|
||||
assertEquals(0.7, playerScale);
|
||||
assertEquals(null, playerStore.find(playerId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizedSenderCanInspectAndChangeBounds() {
|
||||
@@ -37,6 +96,12 @@ class HeightsCommandTest {
|
||||
CommandSender sender = sender(false);
|
||||
command.onCommand(sender, null, "heights", new String[] {"set", "min", "0.2"});
|
||||
command.onCommand(sender, null, "heights", new String[] {"settings"});
|
||||
command.onCommand(sender, null, "heights", new String[] {"player", "Steve"});
|
||||
command.onCommand(sender, null, "heights", new String[] {"player", "Steve", "0.5"});
|
||||
assertEquals(0.7, playerScale);
|
||||
assertEquals(null, playerStore.find(playerId));
|
||||
assertEquals(List.of(), complete(sender, "player", ""));
|
||||
assertEquals(List.of(), complete(sender, "player", "Steve", ""));
|
||||
assertTrue(messages.stream().allMatch(message -> message.contains("permission")));
|
||||
assertEquals(List.of(), saved);
|
||||
assertEquals(List.of(), command.onTabComplete(sender, null, "heights", new String[] {""}));
|
||||
@@ -84,7 +149,7 @@ class HeightsCommandTest {
|
||||
LiveHeightSettings failing = new LiveHeightSettings(live.get(), settings -> {
|
||||
throw new IOException("test save failure");
|
||||
});
|
||||
new HeightsCommand(failing, Logger.getAnonymousLogger()).onCommand(
|
||||
new HeightsCommand(failing, players, Logger.getAnonymousLogger()).onCommand(
|
||||
sender(true), null, "heights", new String[] {"set", "min", "0.2"});
|
||||
assertTrue(messages.getLast().contains("Could not save"));
|
||||
assertEquals(0.4, failing.get().minimum());
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class PlayerHeightsTest {
|
||||
@TempDir
|
||||
Path directory;
|
||||
private final UUID id = UUID.randomUUID();
|
||||
private double scale = 0.7;
|
||||
private LiveHeightSettings settings;
|
||||
private HeightStore store;
|
||||
private PlayerHeights heights;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
settings = new LiveHeightSettings(new HeightSettings(0.4, 2.0, 0.1, 0.5, 1.5, 20), value -> {});
|
||||
store = new HeightStore(directory.toFile());
|
||||
heights = new PlayerHeights(settings, store, name -> name.equalsIgnoreCase("Steve")
|
||||
? new PlayerHeights.Target(id, "Steve", scale, scale * 2, value -> scale = value) : null,
|
||||
() -> List.of("Steve"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void viewsActualBaseAndEffectiveScaleAndPersistsChangesByUuid() throws Exception {
|
||||
assertTrue(heights.describe("steve").contains("0.7"));
|
||||
assertTrue(heights.describe("Steve").contains("1.4"));
|
||||
heights.set("STEVE", 0.5);
|
||||
assertEquals(0.5, scale);
|
||||
HeightStore reloaded = new HeightStore(directory.toFile());
|
||||
assertEquals(0.5, reloaded.find(id));
|
||||
assertEquals(0.5, HeightMath.safeStoredScale(reloaded.find(id), settings.get()));
|
||||
assertEquals(List.of("Steve"), heights.names());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownPlayersAndInvalidScalesWithoutChangingState() {
|
||||
assertThrows(IllegalArgumentException.class, () -> heights.describe("Ste"));
|
||||
assertThrows(IllegalArgumentException.class, () -> heights.set("Offline", 0.5));
|
||||
for (double value : new double[] {Double.NaN, Double.POSITIVE_INFINITY, -1, 0.2, 3.0}) {
|
||||
assertThrows(IllegalArgumentException.class, () -> heights.set("Steve", value));
|
||||
}
|
||||
assertEquals(0.7, scale);
|
||||
assertEquals(null, store.find(id));
|
||||
}
|
||||
|
||||
@Test
|
||||
void usesLiveLimitsAndAllowsRestorationOutsideThem() throws Exception {
|
||||
settings.setBound("max", 0.8);
|
||||
heights.set("Steve", 1.0);
|
||||
assertEquals(1.0, scale);
|
||||
assertEquals(1.0, HeightMath.safeStoredScale(new HeightStore(directory.toFile()).find(id), settings.get()));
|
||||
assertThrows(IllegalArgumentException.class, () -> heights.set("Steve", 0.9));
|
||||
settings.setBound("min", 0.2);
|
||||
heights.set("Steve", 0.2);
|
||||
assertEquals(0.2, scale);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedSaveLeavesLiveAndCachedHeightUnchanged() throws Exception {
|
||||
store.save(id, scale);
|
||||
Files.createDirectory(directory.resolve("state.yml.tmp"));
|
||||
assertThrows(IOException.class, () -> heights.set("Steve", 0.5));
|
||||
assertEquals(0.7, scale);
|
||||
assertEquals(0.7, store.find(id));
|
||||
assertEquals(0.7, new HeightStore(directory.toFile()).find(id));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user