feat(heights): add durable temporary throwable stature
This commit is contained in:
1 parent
82415ed501
commit
47d5060dac
22 files changed
+1434
-75
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.PotionMeta;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
/** Small boundary doubles: no server installation or mocked domain rules. */
|
||||
final class BukkitDoubles {
|
||||
private BukkitDoubles() {}
|
||||
|
||||
static <T> T proxy(Class<T> type, InvocationHandler handler) {
|
||||
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] {type}, handler));
|
||||
}
|
||||
|
||||
static PersistentDataContainer container() {
|
||||
Map<NamespacedKey, Object> values = new HashMap<>();
|
||||
Map<NamespacedKey, PersistentDataType<?, ?>> types = new HashMap<>();
|
||||
return proxy(PersistentDataContainer.class, (proxy, method, args) -> {
|
||||
return switch (method.getName()) {
|
||||
case "set" -> {
|
||||
values.put((NamespacedKey) args[0], args[2]);
|
||||
types.put((NamespacedKey) args[0], (PersistentDataType<?, ?>) args[1]);
|
||||
yield null;
|
||||
}
|
||||
case "get" -> types.get(args[0]) == args[1] ? values.get(args[0]) : null;
|
||||
case "has" -> values.containsKey(args[0]) && (args.length == 1 || types.get(args[0]) == args[1]);
|
||||
case "remove" -> { types.remove(args[0]); values.remove(args[0]); yield null; }
|
||||
case "getKeys" -> Set.copyOf(values.keySet());
|
||||
case "isEmpty" -> values.isEmpty();
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
static final class Item extends ItemStack {
|
||||
private final Material material;
|
||||
private final PersistentDataContainer data = container();
|
||||
private Object displayName;
|
||||
private final PotionMeta meta = proxy(PotionMeta.class, (proxy, method, args) -> {
|
||||
return switch (method.getName()) {
|
||||
case "getPersistentDataContainer" -> data;
|
||||
case "displayName" -> {
|
||||
if (args != null && args.length == 1) { displayName = args[0]; yield null; }
|
||||
yield displayName;
|
||||
}
|
||||
case "setColor" -> null;
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
};
|
||||
});
|
||||
|
||||
Item(Material material) { this.material = material; }
|
||||
@Override public Material getType() { return material; }
|
||||
@Override public PotionMeta getItemMeta() { return meta; }
|
||||
@Override public boolean setItemMeta(org.bukkit.inventory.meta.ItemMeta value) { return value == meta; }
|
||||
@Override public Item clone() { return this; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CompletionMailboxTest {
|
||||
@Test
|
||||
void shutdownCanDrainExactlyOnceEvenWhenScheduledWakeupWasDropped() {
|
||||
List<Runnable> scheduled = new ArrayList<>();
|
||||
List<String> results = new ArrayList<>();
|
||||
CompletionMailbox mailbox = new CompletionMailbox(scheduled::add);
|
||||
mailbox.execute(() -> results.add("failed save released reservation"));
|
||||
assertEquals(List.of(), results);
|
||||
mailbox.drain();
|
||||
assertEquals(List.of("failed save released reservation"), results);
|
||||
scheduled.forEach(Runnable::run);
|
||||
mailbox.drain();
|
||||
assertEquals(1, results.size());
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,23 @@ class HeightStoreTest {
|
||||
assertEquals(1.0, HeightMath.safeStoredScale(reloaded.find(playerId), settings));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sourceRemovalRetiresOnlyItsReceiptsWithoutChangingStature() throws Exception {
|
||||
UUID player = UUID.randomUUID();
|
||||
UUID source = UUID.randomUUID();
|
||||
UUID other = UUID.randomUUID();
|
||||
HeightStore store = new HeightStore(temporaryDirectory.toFile());
|
||||
HeightStore.Sequence sequence = new HeightStore.Sequence(1.0, java.time.Instant.EPOCH.plusSeconds(300));
|
||||
store.save(player, 0.8, sequence, source);
|
||||
store.save(player, 0.6, sequence, other);
|
||||
store.forgetSource(source);
|
||||
HeightStore reloaded = new HeightStore(temporaryDirectory.toFile());
|
||||
assertEquals(false, reloaded.hasReceipt(player, source));
|
||||
assertEquals(true, reloaded.hasReceipt(player, other));
|
||||
assertEquals(0.6, reloaded.find(player));
|
||||
assertEquals(sequence, reloaded.sequence(player));
|
||||
}
|
||||
|
||||
@Test
|
||||
void storesByUuidAndPreservesUnknownYamlFields() throws Exception {
|
||||
UUID playerId = UUID.randomUUID();
|
||||
|
||||
@@ -37,6 +37,25 @@ class HeightsCommandTest {
|
||||
command = new HeightsCommand(live, players, Logger.getAnonymousLogger());
|
||||
}
|
||||
|
||||
@Test
|
||||
void runtimeHeightSetterCanPersistAsynchronouslyWithoutBypassingPermissions() {
|
||||
List<java.util.function.Consumer<String>> replies = new ArrayList<>();
|
||||
HeightsCommand async = new HeightsCommand(live, players, Logger.getAnonymousLogger(), (name, value, reply) -> {
|
||||
assertEquals("Steve", name);
|
||||
assertEquals(0.5, value);
|
||||
replies.add(reply);
|
||||
});
|
||||
async.onCommand(sender(false), null, "heights", new String[] {"player", "Steve", "0.5"});
|
||||
assertEquals(0, replies.size());
|
||||
messages.clear();
|
||||
async.onCommand(sender(true), null, "heights", new String[] {"player", "Steve", "0.5"});
|
||||
assertEquals(1, replies.size());
|
||||
assertEquals(List.of(), messages);
|
||||
assertEquals(0.7, playerScale);
|
||||
replies.getFirst().accept("Saved and active.");
|
||||
assertEquals(List.of("Saved and active."), messages);
|
||||
}
|
||||
|
||||
@Test
|
||||
void viewsAndSetsOnlinePlayerWithNameAndSizeCompletion() {
|
||||
CommandSender sender = sender(true);
|
||||
|
||||
@@ -4,9 +4,72 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PotionRecipesTest {
|
||||
private final NamespacedKey key = new NamespacedKey("spigotheights", "stature_potion");
|
||||
private final PotionRecipes potions = new PotionRecipes(null, key, BukkitDoubles.Item::new);
|
||||
|
||||
@Test
|
||||
void authenticatesAllFormsByTypedMetadataNotDisplayName() {
|
||||
for (StaturePotion kind : StaturePotion.values()) {
|
||||
for (Material form : new Material[] {Material.POTION, Material.SPLASH_POTION, Material.LINGERING_POTION}) {
|
||||
BukkitDoubles.Item item = new BukkitDoubles.Item(form);
|
||||
item.getItemMeta().displayName(net.kyori.adventure.text.Component.text(kind.displayName()));
|
||||
assertNull(potions.identify(item));
|
||||
item.getItemMeta().getPersistentDataContainer().set(key, PersistentDataType.INTEGER, 1);
|
||||
assertNull(potions.identify(item));
|
||||
item.getItemMeta().getPersistentDataContainer().set(key, PersistentDataType.STRING, "UNKNOWN");
|
||||
assertNull(potions.identify(item));
|
||||
item.getItemMeta().getPersistentDataContainer().set(key, PersistentDataType.STRING, kind.name());
|
||||
assertEquals(kind, potions.identify(item));
|
||||
}
|
||||
BukkitDoubles.Item fake = new BukkitDoubles.Item(Material.STONE);
|
||||
fake.getItemMeta().getPersistentDataContainer().set(key, PersistentDataType.STRING, kind.name());
|
||||
assertNull(potions.identify(fake));
|
||||
}
|
||||
assertNull(potions.identify(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registersEightScopedBrewingConversionsWithAuthenticatedResults() {
|
||||
java.util.List<io.papermc.paper.potion.PotionMix> mixes = new java.util.ArrayList<>();
|
||||
java.util.List<NamespacedKey> removed = new java.util.ArrayList<>();
|
||||
org.bukkit.potion.PotionBrewer brewer = BukkitDoubles.proxy(org.bukkit.potion.PotionBrewer.class,
|
||||
(proxy, method, args) -> {
|
||||
switch (method.getName()) {
|
||||
case "addPotionMix" -> mixes.add((io.papermc.paper.potion.PotionMix) args[0]);
|
||||
case "removePotionMix" -> removed.add((NamespacedKey) args[0]);
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
}
|
||||
return null;
|
||||
});
|
||||
potions.registerBrewing(brewer);
|
||||
assertEquals(8, mixes.size());
|
||||
assertEquals(8, mixes.stream().map(io.papermc.paper.potion.PotionMix::getKey).distinct().count());
|
||||
assertEquals(mixes.stream().map(io.papermc.paper.potion.PotionMix::getKey).toList(), removed);
|
||||
for (var mix : mixes) {
|
||||
Material target = mix.getResult().getType();
|
||||
boolean splash = target == Material.SPLASH_POTION;
|
||||
assertEquals(splash ? Material.SPLASH_POTION : Material.LINGERING_POTION, target);
|
||||
StaturePotion kind = potions.identify(mix.getResult());
|
||||
org.junit.jupiter.api.Assertions.assertNotNull(kind);
|
||||
Material source = splash ? Material.POTION : Material.SPLASH_POTION;
|
||||
BukkitDoubles.Item input = new BukkitDoubles.Item(source);
|
||||
org.junit.jupiter.api.Assertions.assertFalse(mix.getInput().test(input));
|
||||
input.getItemMeta().getPersistentDataContainer().set(key, PersistentDataType.STRING, kind.name());
|
||||
org.junit.jupiter.api.Assertions.assertTrue(mix.getInput().test(input));
|
||||
org.junit.jupiter.api.Assertions.assertFalse(mix.getInput().test(mix.getResult()));
|
||||
org.junit.jupiter.api.Assertions.assertTrue(mix.getIngredient().test(new BukkitDoubles.Item(
|
||||
splash ? Material.GUNPOWDER : Material.DRAGON_BREATH)));
|
||||
org.junit.jupiter.api.Assertions.assertFalse(mix.getIngredient().test(new BukkitDoubles.Item(Material.SUGAR)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void recipesUseApprovedShapes() {
|
||||
assertArrayEquals(new String[] {"ACA", "AWA", "ACA"}, PotionRecipes.SHIFTING_SHAPE);
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.ThrownPotion;
|
||||
import org.bukkit.event.entity.EntityRemoveEvent;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StatureCloudsTest {
|
||||
@Test
|
||||
void unloadRetainsDurableReceiptsButPermanentSourceRemovalRetiresThem() {
|
||||
PotionRecipes potions = new PotionRecipes(null, new NamespacedKey("spigotheights", "stature_potion"),
|
||||
BukkitDoubles.Item::new);
|
||||
ThrowableStatureListener listener = new ThrowableStatureListener(potions,
|
||||
new NamespacedKey("spigotheights", "cloud_kind"), (source, player, kind, done) -> {});
|
||||
Server server = BukkitDoubles.proxy(Server.class, (proxy, method, args) -> {
|
||||
if (method.getName().equals("getWorlds")) { return List.of(); }
|
||||
throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
List<UUID> retired = new ArrayList<>();
|
||||
StatureClouds clouds = new StatureClouds(server, listener, retired::add);
|
||||
ThrownPotion source = ThrowableStatureListenerTest.entity(ThrownPotion.class,
|
||||
potions.create(StaturePotion.GROWTH, Material.SPLASH_POTION));
|
||||
clouds.onRemove(new EntityRemoveEvent(source, EntityRemoveEvent.Cause.UNLOAD));
|
||||
clouds.onRemove(new EntityRemoveEvent(ThrowableStatureListenerTest.entity(ThrownPotion.class,
|
||||
new BukkitDoubles.Item(Material.SPLASH_POTION)), EntityRemoveEvent.Cause.HIT));
|
||||
assertEquals(List.of(), retired);
|
||||
clouds.onRemove(new EntityRemoveEvent(source, EntityRemoveEvent.Cause.HIT));
|
||||
assertEquals(List.of(source.getUniqueId()), retired);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerItemConsumeEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StatureListenerTest {
|
||||
@Test
|
||||
void onlyUncancelledAuthenticatedDrinkableItemsReachPermanentService() {
|
||||
PotionRecipes potions = new PotionRecipes(null, new NamespacedKey("spigotheights", "stature_potion"),
|
||||
BukkitDoubles.Item::new);
|
||||
List<StaturePotion> drinks = new ArrayList<>();
|
||||
StatureListener listener = new StatureListener(potions, (player, kind) -> drinks.add(kind), player -> {}, Runnable::run);
|
||||
Player player = ThrowableStatureListenerTest.entity(Player.class, null);
|
||||
var cancelled = new PlayerItemConsumeEvent(player, potions.create(StaturePotion.GROWTH), EquipmentSlot.HAND);
|
||||
cancelled.setCancelled(true);
|
||||
listener.onConsume(cancelled);
|
||||
listener.onConsume(new PlayerItemConsumeEvent(player,
|
||||
potions.create(StaturePotion.GROWTH, Material.SPLASH_POTION), EquipmentSlot.HAND));
|
||||
listener.onConsume(new PlayerItemConsumeEvent(player, new BukkitDoubles.Item(Material.POTION), EquipmentSlot.HAND));
|
||||
assertEquals(List.of(), drinks);
|
||||
cancelled.setCancelled(false);
|
||||
listener.onConsume(cancelled);
|
||||
assertEquals(List.of(StaturePotion.GROWTH), drinks);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StatureQueueTest {
|
||||
@Test
|
||||
void capturesNextOperationOnlyAfterDurableCompletionAndNeverWritesOnMain() {
|
||||
ArrayDeque<Runnable> worker = new ArrayDeque<>();
|
||||
ArrayDeque<Runnable> main = new ArrayDeque<>();
|
||||
List<String> events = new ArrayList<>();
|
||||
double[] scale = {1.0};
|
||||
StatureQueue queue = new StatureQueue(worker::add, main::add);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
queue.submit(() -> {
|
||||
events.add("capture");
|
||||
double next = scale[0] - 0.2;
|
||||
return () -> { events.add("save"); return next; };
|
||||
}, (result, error) -> { assertNull(error); events.add("apply"); scale[0] = result; });
|
||||
}
|
||||
assertEquals(List.of("capture"), events);
|
||||
assertEquals(1.0, scale[0]);
|
||||
worker.removeFirst().run();
|
||||
assertEquals(List.of("capture", "save"), events);
|
||||
assertEquals(1.0, scale[0]);
|
||||
main.removeFirst().run();
|
||||
assertEquals(List.of("capture", "save", "apply", "capture"), events);
|
||||
worker.removeFirst().run(); main.removeFirst().run();
|
||||
assertEquals(0.6, scale[0], 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shutdownRejectsPendingAndNewOperationsButAllowsInflightSaveToFinish() {
|
||||
ArrayDeque<Runnable> worker = new ArrayDeque<>();
|
||||
ArrayDeque<Runnable> main = new ArrayDeque<>();
|
||||
List<Exception> failures = new ArrayList<>();
|
||||
StatureQueue queue = new StatureQueue(worker::add, main::add);
|
||||
queue.submit(() -> () -> 0.8, (result, error) -> assertNull(error));
|
||||
queue.submit(() -> () -> 0.6, (result, error) -> failures.add(error));
|
||||
queue.close();
|
||||
queue.submit(() -> () -> 0.4, (result, error) -> failures.add(error));
|
||||
assertEquals(2, failures.size());
|
||||
failures.forEach(org.junit.jupiter.api.Assertions::assertNotNull);
|
||||
worker.removeFirst().run(); main.removeFirst().run();
|
||||
assertEquals(0, worker.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failedPersistenceReportsFailureAndDoesNotStrandNextOperation() {
|
||||
ArrayDeque<Runnable> worker = new ArrayDeque<>();
|
||||
ArrayDeque<Runnable> main = new ArrayDeque<>();
|
||||
List<Double> applied = new ArrayList<>();
|
||||
StatureQueue queue = new StatureQueue(worker::add, main::add);
|
||||
queue.submit(() -> () -> { throw new java.io.IOException("disk failure"); },
|
||||
(result, error) -> { assertNull(result); assertNotNull(error); });
|
||||
queue.submit(() -> () -> 1.4, (result, error) -> { assertNull(error); applied.add(result); });
|
||||
assertEquals(1, worker.size());
|
||||
worker.removeFirst().run(); main.removeFirst().run();
|
||||
worker.removeFirst().run(); main.removeFirst().run();
|
||||
assertEquals(List.of(1.4), applied);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class TemporaryStatureTest {
|
||||
@TempDir Path directory;
|
||||
private final UUID player = UUID.randomUUID();
|
||||
private final HeightSettings settings = new HeightSettings(0.2, 3.0, 0.2, 0.5, 1.5, 20);
|
||||
private final Instant start = Instant.parse("2026-01-01T00:00:00Z");
|
||||
|
||||
@Test
|
||||
void applicationAfterExpiryStartsFromRestoredBaselineNotStaleTemporaryScale() throws Exception {
|
||||
TemporaryStature stature = new TemporaryStature(new HeightStore(directory.toFile()));
|
||||
stature.apply(player, 1.0, StaturePotion.DIMINUTION, settings, start, bound -> 0);
|
||||
assertEquals(1.2, stature.apply(player, 0.8, StaturePotion.GROWTH, settings,
|
||||
start.plusSeconds(300), bound -> 0));
|
||||
assertEquals(1.0, stature.resume(player, settings, start.plusSeconds(600)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void durableCloudReceiptPreventsDuplicateAfterUnloadReloadAndPermanentOverride() throws Exception {
|
||||
UUID source = UUID.randomUUID();
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
assertEquals(0.8, stature.applyOnce(player, source, 1.0, StaturePotion.DIMINUTION, settings, start, bound -> 0));
|
||||
store.save(player, 1.4);
|
||||
HeightStore reloaded = new HeightStore(directory.toFile());
|
||||
assertNull(new TemporaryStature(reloaded).applyOnce(player, source, 1.4, StaturePotion.DIMINUTION,
|
||||
settings, start.plusSeconds(10), bound -> 0));
|
||||
assertEquals(1.4, reloaded.find(player));
|
||||
assertNull(reloaded.sequence(player));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unobservedExpirySurvivesDisconnectBetweenSaveAndLiveApplication() throws Exception {
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
stature.apply(player, 2.0, StaturePotion.DIMINUTION, settings, start, bound -> 0);
|
||||
HeightSettings smaller = new HeightSettings(0.2, 0.8, 0.2, 0.5, 1.5, 20);
|
||||
assertEquals(2.0, stature.resume(player, smaller, start.plusSeconds(300)));
|
||||
// Player disconnected before observing the result: login must still restore exactly.
|
||||
assertEquals(2.0, new TemporaryStature(new HeightStore(directory.toFile()))
|
||||
.resume(player, smaller, start.plusSeconds(301)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void reloadCountsOfflineTimeAndRestoresExactBaselineOutsideChangedLimits() throws Exception {
|
||||
TemporaryStature stature = new TemporaryStature(new HeightStore(directory.toFile()));
|
||||
stature.apply(player, 2.0, StaturePotion.DIMINUTION, settings, start, bound -> 0);
|
||||
HeightStore reloaded = new HeightStore(directory.toFile());
|
||||
TemporaryStature restarted = new TemporaryStature(reloaded);
|
||||
HeightSettings smaller = new HeightSettings(0.2, 0.8, 0.2, 0.5, 1.5, 20);
|
||||
assertEquals(2.0, restarted.resume(player, smaller, start.plusSeconds(301)));
|
||||
restarted.acknowledgeRestoration(player, reloaded.sequence(player));
|
||||
assertNull(reloaded.sequence(player));
|
||||
// Only processing the expiry bypasses new limits; later ordinary joins use them.
|
||||
assertEquals(0.8, restarted.resume(player, smaller, start.plusSeconds(302)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void permanentSaveEndsSequenceAndFailedSavePreservesIt() throws Exception {
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
stature.apply(player, 1.0, StaturePotion.DIMINUTION, settings, start, bound -> 0);
|
||||
Files.createDirectory(directory.resolve("state.yml.tmp"));
|
||||
assertThrows(IOException.class, () -> store.save(player, 1.4));
|
||||
assertEquals(0.8, store.find(player));
|
||||
assertEquals(1.0, store.sequence(player).baseline());
|
||||
Files.delete(directory.resolve("state.yml.tmp"));
|
||||
store.save(player, 1.4);
|
||||
assertNull(new HeightStore(directory.toFile()).sequence(player));
|
||||
assertEquals(1.4, stature.resume(player, settings, start.plusSeconds(600)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void permanentDrinkUsesCurrentSizeAndEndsSequenceIncludingAtExpiryBoundary() throws Exception {
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
stature.apply(player, 1.0, StaturePotion.DIMINUTION, settings, start, bound -> 0);
|
||||
assertEquals(0.6, stature.drink(player, 0.8, StaturePotion.DIMINUTION, settings,
|
||||
start.plusSeconds(1), bound -> 0));
|
||||
assertNull(store.sequence(player));
|
||||
assertEquals(0.6, stature.resume(player, settings, start.plusSeconds(600)));
|
||||
stature.apply(player, 0.6, StaturePotion.GROWTH, settings, start, bound -> 0);
|
||||
assertEquals(0.4, stature.drink(player, 0.8, StaturePotion.DIMINUTION, settings,
|
||||
start.plusSeconds(300), bound -> 0));
|
||||
assertNull(store.sequence(player));
|
||||
}
|
||||
|
||||
@Test
|
||||
void allKindsRespectBoundsRestorationAndTimerRefreshEvenAtBounds() throws Exception {
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
assertEquals(0.2, stature.apply(player, 0.2, StaturePotion.DIMINUTION, settings, start, bound -> 0));
|
||||
assertEquals(3.0, stature.apply(player, 0.2, StaturePotion.SHIFTING, settings,
|
||||
start.plusSeconds(10), bound -> bound - 1));
|
||||
assertEquals(3.0, stature.apply(player, 3.0, StaturePotion.GROWTH, settings,
|
||||
start.plusSeconds(20), bound -> 0));
|
||||
assertEquals(start.plusSeconds(320), store.sequence(player).expiresAt());
|
||||
assertEquals(1.0, stature.apply(player, 3.0, StaturePotion.RESTORATION, settings,
|
||||
start.plusSeconds(30), bound -> 0));
|
||||
assertEquals(0.2, stature.resume(player, settings, start.plusSeconds(330)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chainedEffectsRestoreFirstBaselineFiveMinutesAfterLastApplication() throws Exception {
|
||||
HeightStore store = new HeightStore(directory.toFile());
|
||||
TemporaryStature stature = new TemporaryStature(store);
|
||||
assertEquals(0.8, stature.apply(player, 1.0, StaturePotion.DIMINUTION, settings, start, bound -> 0));
|
||||
assertEquals(0.6, stature.apply(player, 0.8, StaturePotion.DIMINUTION, settings,
|
||||
start.plusSeconds(240), bound -> 0));
|
||||
assertEquals(0.6, stature.resume(player, settings, start.plusSeconds(539)));
|
||||
assertEquals(1.0, stature.resume(player, settings, start.plusSeconds(540)));
|
||||
assertEquals(1.0, store.find(player));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package games.dmg.spigotheights;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Consumer;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.AreaEffectCloud;
|
||||
import org.bukkit.entity.LivingEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.entity.ThrownPotion;
|
||||
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
|
||||
import org.bukkit.event.entity.LingeringPotionSplashEvent;
|
||||
import org.bukkit.event.entity.PotionSplashEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ThrowableStatureListenerTest {
|
||||
private final PotionRecipes potions = new PotionRecipes(null,
|
||||
new NamespacedKey("spigotheights", "stature_potion"), BukkitDoubles.Item::new);
|
||||
private final List<StaturePotion> applied = new ArrayList<>();
|
||||
private final List<Consumer<Boolean>> pending = new ArrayList<>();
|
||||
private boolean asynchronous;
|
||||
private final ThrowableStatureListener listener = new ThrowableStatureListener(potions,
|
||||
new NamespacedKey("spigotheights", "cloud_kind"), (source, player, kind, done) -> {
|
||||
applied.add(kind);
|
||||
if (asynchronous) { pending.add(done); } else { done.accept(true); }
|
||||
});
|
||||
|
||||
@Test
|
||||
void splashUsesAuthenticatedItemPositiveIntensityAndOncePerPlayer() {
|
||||
Player player = entity(Player.class, null);
|
||||
Player missed = entity(Player.class, null);
|
||||
LivingEntity mob = entity(LivingEntity.class, null);
|
||||
ThrownPotion thrown = entity(ThrownPotion.class, potions.create(StaturePotion.DIMINUTION, Material.SPLASH_POTION));
|
||||
Map<LivingEntity, Double> recipients = new HashMap<>();
|
||||
recipients.put(player, 0.1); recipients.put(missed, 0.0); recipients.put(mob, 1.0);
|
||||
PotionSplashEvent event = new PotionSplashEvent(thrown, null, null, null, recipients);
|
||||
event.setCancelled(true);
|
||||
listener.onSplash(event);
|
||||
assertEquals(List.of(), applied);
|
||||
event.setCancelled(false);
|
||||
listener.onSplash(event);
|
||||
listener.onSplash(event);
|
||||
assertEquals(List.of(StaturePotion.DIMINUTION), applied);
|
||||
ThrownPotion ordinary = entity(ThrownPotion.class, new BukkitDoubles.Item(Material.SPLASH_POTION));
|
||||
listener.onSplash(new PotionSplashEvent(ordinary, null, null, null, recipients));
|
||||
assertEquals(1, applied.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cloudRequiresUncancelledAuthenticatedCreationAndAppliesOnlyOncePerPlayer() {
|
||||
Player player = entity(Player.class, null);
|
||||
AreaEffectCloud cloud = entity(AreaEffectCloud.class, null);
|
||||
var application = new AreaEffectCloudApplyEvent(cloud, new ArrayList<>(List.of(player)));
|
||||
listener.onCloud(application);
|
||||
assertEquals(0, applied.size());
|
||||
var creation = new LingeringPotionSplashEvent(entity(ThrownPotion.class,
|
||||
potions.create(StaturePotion.GROWTH, Material.LINGERING_POTION)), null, null, null, cloud);
|
||||
creation.setCancelled(true);
|
||||
listener.onLingering(creation);
|
||||
assertFalse(creation.allowsEmptyCreation());
|
||||
listener.onCloud(application);
|
||||
assertEquals(0, applied.size());
|
||||
creation.setCancelled(false);
|
||||
listener.onLingering(creation);
|
||||
assertTrue(creation.allowsEmptyCreation());
|
||||
application.setCancelled(true);
|
||||
listener.onCloud(application);
|
||||
assertEquals(0, applied.size());
|
||||
application.setCancelled(false);
|
||||
asynchronous = true;
|
||||
listener.onCloud(application);
|
||||
listener.onCloud(application);
|
||||
assertEquals(List.of(StaturePotion.GROWTH), applied);
|
||||
assertFalse(cloud.getPersistentDataContainer().has(new NamespacedKey("spigotheights",
|
||||
"hit_" + player.getUniqueId())), "Pending reservations must not be serialized on unload");
|
||||
pending.removeFirst().accept(false);
|
||||
listener.onCloud(application);
|
||||
assertEquals(2, applied.size());
|
||||
pending.removeFirst().accept(true);
|
||||
listener.onCloud(application);
|
||||
assertEquals(2, applied.size());
|
||||
// Receipts are cloud-owned metadata, not player-object or listener-instance identity.
|
||||
new ThrowableStatureListener(potions, new NamespacedKey("spigotheights", "cloud_kind"),
|
||||
(source, recipient, kind, done) -> { throw new AssertionError("duplicate after listener restart"); })
|
||||
.onCloud(application);
|
||||
}
|
||||
|
||||
@Test
|
||||
void effectlessCloudTickDispatchesCancellableEventAndHonorsWaitAndRadius() {
|
||||
Player inside = locatedPlayer(1.0, 0.0);
|
||||
Player outsideCircle = locatedPlayer(2.0, 2.0);
|
||||
PersistentDataContainer data = BukkitDoubles.container();
|
||||
int[] age = {9};
|
||||
UUID cloudId = UUID.randomUUID();
|
||||
AreaEffectCloud cloud = BukkitDoubles.proxy(AreaEffectCloud.class, (proxy, method, args) -> switch (method.getName()) {
|
||||
case "getPersistentDataContainer" -> data;
|
||||
case "isValid" -> true;
|
||||
case "getUniqueId" -> cloudId;
|
||||
case "getTicksLived" -> age[0];
|
||||
case "getWaitTime" -> 10;
|
||||
case "getRadius" -> 2.0f;
|
||||
case "getLocation" -> new org.bukkit.Location(null, 0, 0, 0);
|
||||
case "getBoundingBox" -> new org.bukkit.util.BoundingBox(-2, 0, -2, 2, 0.5, 2);
|
||||
case "getNearbyEntities" -> List.of(inside, outsideCircle);
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
listener.onLingering(new LingeringPotionSplashEvent(entity(ThrownPotion.class,
|
||||
potions.create(StaturePotion.RESTORATION, Material.LINGERING_POTION)), null, null, null, cloud));
|
||||
List<AreaEffectCloudApplyEvent> dispatched = new ArrayList<>();
|
||||
Consumer<AreaEffectCloudApplyEvent> cancel = event -> {
|
||||
dispatched.add(event); event.setCancelled(true); listener.onCloud(event);
|
||||
};
|
||||
listener.tickCloud(cloud, cancel);
|
||||
assertEquals(0, dispatched.size());
|
||||
age[0] = 10;
|
||||
listener.tickCloud(cloud, cancel);
|
||||
assertEquals(1, dispatched.size());
|
||||
assertEquals(List.of(inside), dispatched.getFirst().getAffectedEntities());
|
||||
assertEquals(0, applied.size());
|
||||
listener.tickCloud(cloud, listener::onCloud);
|
||||
listener.tickCloud(cloud, listener::onCloud);
|
||||
assertEquals(List.of(StaturePotion.RESTORATION), applied);
|
||||
}
|
||||
|
||||
private static Player locatedPlayer(double x, double z) {
|
||||
Player delegate = entity(Player.class, null);
|
||||
return BukkitDoubles.proxy(Player.class, (proxy, method, args) -> switch (method.getName()) {
|
||||
case "getLocation" -> new org.bukkit.Location(null, x, 0, z);
|
||||
case "getBoundingBox" -> new org.bukkit.util.BoundingBox(x - 0.2, 0, z - 0.2, x + 0.2, 1.8, z + 0.2);
|
||||
case "getGameMode" -> org.bukkit.GameMode.SURVIVAL;
|
||||
case "equals" -> proxy == args[0];
|
||||
default -> method.invoke(delegate, args);
|
||||
});
|
||||
}
|
||||
|
||||
static <T> T entity(Class<T> type, ItemStack item) {
|
||||
UUID id = UUID.randomUUID();
|
||||
PersistentDataContainer data = BukkitDoubles.container();
|
||||
return BukkitDoubles.proxy(type, (proxy, method, args) -> switch (method.getName()) {
|
||||
case "getUniqueId" -> id;
|
||||
case "getPersistentDataContainer" -> data;
|
||||
case "getItem" -> item;
|
||||
case "isOnline", "isValid" -> true;
|
||||
case "isDead" -> false;
|
||||
case "hashCode" -> id.hashCode();
|
||||
case "equals" -> proxy == args[0];
|
||||
case "toString" -> id.toString();
|
||||
default -> throw new UnsupportedOperationException(method.getName());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user