feat(heights): discover stature potion recipes
Release / release (push) Successful in 5m12s
CI / build (push) Successful in 1m7s

This commit is contained in:
dmg committed 2026-09-20 14:30:46 -04:00
1 parent 960e8891a8
commit a6c80e7ac9
8 files changed
+1233 -21

No files matched your search

@@ -3,6 +3,7 @@ package games.dmg.spigotheights;
import static org.junit.jupiter.api.Assertions.*;
import java.io.DataInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
@@ -27,6 +28,30 @@ class PurpurDistributionTest {
}
}
@Test
void packagesTheIntegratedRecipeDiscoveryLifecycle() throws Exception {
try (var jar = new JarFile(System.getProperty("distribution.jar"))) {
String plugin = classConstants(jar, "games/dmg/spigotheights/SpigotHeightsPlugin.class");
String lifecycle = classConstants(jar, "games/dmg/spigotheights/RecipeLifecycle.class");
String discovery = classConstants(jar, "games/dmg/spigotheights/RecipeDiscovery.class");
String recipes = classConstants(jar, "games/dmg/spigotheights/PotionRecipes.class");
assertTrue(plugin.contains("RecipeLifecycle"));
assertTrue(lifecycle.contains("getOnlinePlayers"));
assertTrue(lifecycle.contains("registerEvents"));
assertTrue(discovery.contains("discoverRecipes"));
assertTrue(discovery.contains("hasDiscoveredRecipe"));
assertTrue(recipes.contains("CraftingBookCategory"));
assertTrue(recipes.contains("AWKWARD"));
assertTrue(recipes.contains("RecipeChoice$ExactChoice"));
assertTrue(recipes.contains("removeRecipe"));
}
}
private static String classConstants(JarFile jar, String entry) throws Exception {
assertNotNull(jar.getJarEntry(entry), entry);
return new String(jar.getInputStream(jar.getJarEntry(entry)).readAllBytes(), StandardCharsets.ISO_8859_1);
}
@Test
void pluginBytecodeTargetsJava25() throws Exception {
try (var jar = new JarFile(System.getProperty("distribution.jar"));
@@ -0,0 +1,143 @@
package games.dmg.spigotheights;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerJoinEvent;
import org.junit.jupiter.api.Test;
class RecipeDiscoveryTest {
private final List<NamespacedKey> keys = List.of(
new NamespacedKey("spigotheights", "shifting_stature"),
new NamespacedKey("spigotheights", "growth"),
new NamespacedKey("spigotheights", "diminution"),
new NamespacedKey("spigotheights", "restoration"));
@Test
void allAlreadyOnlinePlayersReceiveEveryUnknownRecipe() {
Set<NamespacedKey> firstKnown = new LinkedHashSet<>();
Set<NamespacedKey> secondKnown = new LinkedHashSet<>();
RecipeDiscovery discovery = new RecipeDiscovery(keys, Logger.getAnonymousLogger());
discovery.discoverAll(List.of(
player("First", firstKnown, new ArrayList<>(), false),
player("Second", secondKnown, new ArrayList<>(), false)));
assertEquals(new LinkedHashSet<>(keys), firstKnown);
assertEquals(new LinkedHashSet<>(keys), secondKnown);
}
@Test
void joiningPlayerReceivesEveryUnknownRecipe() {
Set<NamespacedKey> known = new LinkedHashSet<>();
List<List<NamespacedKey>> awards = new ArrayList<>();
Player player = player("Joiner", known, awards, false);
RecipeDiscovery discovery = new RecipeDiscovery(keys, Logger.getAnonymousLogger());
discovery.onJoin(new PlayerJoinEvent(player, "joined"));
assertEquals(List.of(keys), awards);
assertEquals(new LinkedHashSet<>(keys), known);
}
@Test
void onePlayerExceptionIsLoggedWithoutBlockingOthersAndRetriesLater() {
Set<NamespacedKey> failedKnown = new LinkedHashSet<>();
Set<NamespacedKey> healthyKnown = new LinkedHashSet<>();
AtomicBoolean firstAttempt = new AtomicBoolean(true);
List<String> warnings = new ArrayList<>();
Logger logger = logger(warnings);
Player failing = BukkitDoubles.proxy(Player.class, (proxy, method, args) -> switch (method.getName()) {
case "getName" -> "Failing";
case "hasDiscoveredRecipe" -> failedKnown.contains(args[0]);
case "discoverRecipes" -> {
if (firstAttempt.getAndSet(false)) {
throw new IllegalStateException("simulated discovery failure");
}
List<NamespacedKey> requested = ((Collection<?>) args[0]).stream()
.map(NamespacedKey.class::cast).toList();
failedKnown.addAll(requested);
yield requested.size();
}
default -> throw new UnsupportedOperationException(method.getName());
});
RecipeDiscovery discovery = new RecipeDiscovery(keys, logger);
assertDoesNotThrow(() -> discovery.discover(failing));
discovery.discover(player("Healthy", healthyKnown, new ArrayList<>(), false));
discovery.discover(failing);
assertEquals(new LinkedHashSet<>(keys), healthyKnown);
assertEquals(new LinkedHashSet<>(keys), failedKnown);
assertEquals(1, warnings.size());
}
@Test
void missingPostconditionIsLoggedAndOnlyMissingRecipesRetryLater() {
Set<NamespacedKey> known = new LinkedHashSet<>();
List<List<NamespacedKey>> awards = new ArrayList<>();
List<String> warnings = new ArrayList<>();
Logger logger = logger(warnings);
RecipeDiscovery discovery = new RecipeDiscovery(keys, logger);
discovery.discover(player("Ada", known, awards, true));
discovery.discover(player("Ada", known, awards, false));
assertEquals(List.of(keys, List.of(keys.getLast())), awards);
assertEquals(1, warnings.size());
assertEquals(new LinkedHashSet<>(keys), known);
}
@Test
void discoversOnlyUnknownRecipesAndRepeatedDiscoveryIsSilent() {
Set<NamespacedKey> known = new LinkedHashSet<>();
List<List<NamespacedKey>> awards = new ArrayList<>();
Player player = player("Ada", known, awards, false);
RecipeDiscovery discovery = new RecipeDiscovery(keys, Logger.getAnonymousLogger());
discovery.discover(player);
discovery.discover(player);
assertEquals(List.of(keys), awards);
assertEquals(new LinkedHashSet<>(keys), known);
}
private static Logger logger(List<String> warnings) {
Logger logger = Logger.getAnonymousLogger();
logger.setUseParentHandlers(false);
logger.addHandler(new Handler() {
@Override public void publish(LogRecord record) { warnings.add(record.getMessage()); }
@Override public void flush() {}
@Override public void close() {}
});
return logger;
}
static Player player(String name, Set<NamespacedKey> known, List<List<NamespacedKey>> awards,
boolean omitLastAward) {
return BukkitDoubles.proxy(Player.class, (proxy, method, args) -> switch (method.getName()) {
case "getName" -> name;
case "hasDiscoveredRecipe" -> known.contains(args[0]);
case "discoverRecipes" -> {
List<NamespacedKey> requested = ((Collection<?>) args[0]).stream()
.map(NamespacedKey.class::cast).toList();
awards.add(requested);
int limit = omitLastAward ? Math.max(0, requested.size() - 1) : requested.size();
known.addAll(requested.subList(0, limit));
yield limit;
}
default -> throw new UnsupportedOperationException(method.getName());
});
}
}