feat(crafting): guard against automated character crafting
Release / release (push) Successful in 4m37s
CI / build (push) Successful in 2m29s

This commit is contained in:
dmg committed 2026-09-17 21:14:52 -04:00
1 parent 272acfde11
commit 6aa7ea469b
4 files changed
+85 -2

No files matched your search

@@ -0,0 +1,67 @@
package games.dmg.fantasystick;
import static org.junit.jupiter.api.Assertions.*;
import games.dmg.fantasystick.crafting.ManualCraftingGuard;
import games.dmg.fantasystick.items.CharacterSheets;
import java.lang.reflect.Proxy;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.event.block.CrafterCraftEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class ManualCraftingGuardNativeTest {
@BeforeAll static void bootstrap() throws Exception { NativeRuntime.bootstrap(); }
private static Block untouchedBlock() {
// The event boundary supplies a block, but cancellation must not access/mutate world or inventory.
return (Block) Proxy.newProxyInstance(Block.class.getClassLoader(), new Class<?>[]{Block.class},
(proxy, method, arguments) -> { throw new AssertionError("Unexpected block access: " + method.getName()); });
}
@Test
void leavesOtherRecipesAndExistingCancellationsAlone() {
var guard = new ManualCraftingGuard();
for (var key : new NamespacedKey[]{new NamespacedKey("minecraft", "paper"), new NamespacedKey("other", "character_sheet")}) {
var recipe = new ShapelessRecipe(key, new ItemStack(Material.PAPER)).addIngredient(Material.SUGAR_CANE);
for (boolean cancelled : new boolean[]{false, true}) {
var event = new CrafterCraftEvent(untouchedBlock(), recipe, recipe.getResult());
event.setCancelled(cancelled);
guard.onAutomatedCraft(event);
assertEquals(cancelled, event.isCancelled());
assertEquals(recipe.getResult(), event.getResult());
}
}
var ours = new CharacterSheets().recipe();
var alreadyCancelled = new CrafterCraftEvent(untouchedBlock(), ours, ours.getResult());
alreadyCancelled.setCancelled(true);
guard.onAutomatedCraft(alreadyCancelled);
assertTrue(alreadyCancelled.isCancelled());
}
@Test
void declaresTheNativeEventHookWithoutOverridingPriorCancellations() throws Exception {
var annotation = ManualCraftingGuard.class.getMethod("onAutomatedCraft", CrafterCraftEvent.class).getAnnotation(EventHandler.class);
assertNotNull(annotation);
assertEquals(EventPriority.HIGHEST, annotation.priority());
assertTrue(annotation.ignoreCancelled());
}
@Test
void cancelsAutomatedSheetCraftWithoutTouchingTheBlockOrResult() {
var recipe = new CharacterSheets().recipe();
var result = recipe.getResult();
var event = new CrafterCraftEvent(untouchedBlock(), recipe, result);
new ManualCraftingGuard().onAutomatedCraft(event);
assertTrue(event.isCancelled(), "The native Crafter must stop before consumption/ejection");
assertEquals(result, event.getResult());
assertEquals(Material.PAPER, event.getResult().getType());
assertTrue(new CharacterSheets().identify(event.getResult()).isEmpty());
}
}