diff --git a/.forgejo/workflows/dunechest.yml b/.forgejo/workflows/dunechest.yml index 11203c63..90f7e863 100644 --- a/.forgejo/workflows/dunechest.yml +++ b/.forgejo/workflows/dunechest.yml @@ -28,7 +28,7 @@ jobs: shell: bash run: | set -euo pipefail - gradle --no-daemon -p DuneChest test jar + gradle --no-daemon -p DuneChest test jar paperValidationJar python3 tools/test_plugin_fleet_install.py -v python3 tools/test_plugin_pack_properties.py -v mkdir chest-artifacts @@ -46,3 +46,8 @@ jobs: name: DuneRealms-Planet-DuneChest path: chest-artifacts/ if-no-files-found: error + - uses: actions/upload-artifact@v3 + with: + name: DuneChest-Paper-Validation + path: DuneChest/build/libs/DuneChestPaperValidation.jar + if-no-files-found: error diff --git a/DuneChest/build.gradle.kts b/DuneChest/build.gradle.kts index f695eab5..796861c0 100644 --- a/DuneChest/build.gradle.kts +++ b/DuneChest/build.gradle.kts @@ -18,3 +18,11 @@ tasks.jar { archiveFileName.set("DuneChest.jar") } java { toolchain.languageVersion.set(JavaLanguageVersion.of(21)) } tasks.withType { options.encoding = "UTF-8" } tasks.test { useJUnitPlatform() } + +val paperValidation by sourceSets.creating { + compileClasspath += sourceSets.main.get().output + configurations.compileClasspath.get() +} +tasks.register("paperValidationJar") { + archiveFileName.set("DuneChestPaperValidation.jar") + from(paperValidation.output) +} diff --git a/DuneChest/src/paperValidation/java/com/example/dunechest/validation/DuneChestPaperValidation.java b/DuneChest/src/paperValidation/java/com/example/dunechest/validation/DuneChestPaperValidation.java new file mode 100644 index 00000000..bc7de585 --- /dev/null +++ b/DuneChest/src/paperValidation/java/com/example/dunechest/validation/DuneChestPaperValidation.java @@ -0,0 +1,108 @@ +package com.example.dunechest.validation; + +import com.example.dunechest.DuneChest; +import com.example.dunechest.StorageModule; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Arrays; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.Chest; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.java.JavaPlugin; + +/** Test-only plugin for an isolated Paper server with a disposable world. */ +public final class DuneChestPaperValidation extends JavaPlugin implements Listener { + @Override + public void onEnable() { + if (!Boolean.getBoolean("dunechest.validation.disposable")) { + throw new IllegalStateException("This test requires an explicitly disposable server"); + } + Bukkit.getPluginManager().registerEvents(this, this); + } + + @EventHandler + public void onServerLoad(ServerLoadEvent event) { + if (event.getType() != ServerLoadEvent.LoadType.STARTUP) return; + Bukkit.getScheduler().runTask(this, () -> { + try { + validateCrafter(); + getLogger().info("DUNECHEST_VALIDATION {\"ok\":true,\"cases\":4}"); + } catch (Exception | AssertionError failure) { + getLogger().log(java.util.logging.Level.SEVERE, "DUNECHEST_VALIDATION {\"ok\":false}", failure); + } finally { + Bukkit.shutdown(); + } + }); + } + + private void validateCrafter() throws ReflectiveOperationException { + StorageModule module = JavaPlugin.getPlugin(DuneChest.class).storage(); + ItemStack mk1 = module.issue("speed_hopper", 1); + ItemStack mk2 = module.issue("speed_hopper", 2); + ItemStack mk3 = module.issue("speed_hopper", 3); + Block table = Bukkit.getWorlds().getFirst().getSpawnLocation().add(0, 12, 0).getBlock(); + table.setType(Material.CRAFTING_TABLE); + Inventory input = chest(table.getRelative(BlockFace.UP)); + Inventory output = chest(table.getRelative(BlockFace.DOWN)); + + Class placedType = Class.forName("com.example.dunechest.StorageData$Placed", + true, module.getClass().getClassLoader()); + var constructor = placedType.getDeclaredConstructor(); + constructor.setAccessible(true); + Object placed = constructor.newInstance(); + Field recipe = placedType.getDeclaredField("recipe"); + recipe.setAccessible(true); + ItemStack diamond = new ItemStack(Material.DIAMOND); + ItemStack redstone = new ItemStack(Material.REDSTONE_BLOCK); + recipe.set(placed, new ItemStack[]{ + diamond, redstone, diamond, redstone, mk1, redstone, diamond, redstone, diamond + }); + Method craft = module.getClass().getDeclaredMethod("crafter", Block.class, placedType); + craft.setAccessible(true); + + load(input, new ItemStack(Material.HOPPER), 4, redstone); + assertRejected(craft, module, table, placed, input, output); + input.setItem(0, mk3.clone()); + assertRejected(craft, module, table, placed, input, output); + load(input, mk1, 3, redstone); + assertRejected(craft, module, table, placed, input, output); + + load(input, mk3, 4, redstone); + input.setItem(3, mk1.asQuantity(2)); + craft.invoke(module, table, placed); + if (!output.containsAtLeast(mk2, 1) || output.containsAtLeast(mk2, 2) + || !mk3.equals(input.getItem(0)) || input.getItem(1) != null + || input.getItem(2) != null || !mk1.equals(input.getItem(3))) { + throw new AssertionError("Valid upgrade did not credit and debit the exact stacks"); + } + } + + private static Inventory chest(Block block) { + block.setType(Material.CHEST); + return ((Chest) block.getState()).getBlockInventory(); + } + + private static void load(Inventory inventory, ItemStack hopper, int diamonds, ItemStack redstone) { + inventory.clear(); + inventory.setItem(0, hopper.clone()); + inventory.setItem(1, new ItemStack(Material.DIAMOND, diamonds)); + inventory.setItem(2, redstone.asQuantity(4)); + } + + private static void assertRejected(Method craft, StorageModule module, Block table, Object placed, + Inventory input, Inventory output) throws ReflectiveOperationException { + ItemStack[] before = Arrays.stream(input.getStorageContents()) + .map(stack -> stack == null ? null : stack.clone()).toArray(ItemStack[]::new); + craft.invoke(module, table, placed); + if (!Arrays.equals(before, input.getStorageContents()) || !output.isEmpty()) { + throw new AssertionError("A rejected craft changed an inventory"); + } + } +} diff --git a/DuneChest/src/paperValidation/resources/plugin.yml b/DuneChest/src/paperValidation/resources/plugin.yml new file mode 100644 index 00000000..4f65c534 --- /dev/null +++ b/DuneChest/src/paperValidation/resources/plugin.yml @@ -0,0 +1,5 @@ +name: DuneChestPaperValidation +version: 1.0.0 +main: com.example.dunechest.validation.DuneChestPaperValidation +api-version: '1.21.8' +depend: [DuneChest]