Add four-ingredient cauldron soups with persistent brewing and stackable food

This commit is contained in:
2026-09-05 13:34:00 +03:00
commit 32471fb026
17 changed files with 1310 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
name: SoupSmokeTest
version: '1.0'
main: smoke.SoupSmokeTest
api-version: '26.2'
depend: [AstraSoups]
+212
View File
@@ -0,0 +1,212 @@
package smoke;
import online.astracraft.soups.*;
import io.papermc.paper.datacomponent.DataComponentTypes;
import io.papermc.paper.datacomponent.item.consumable.ConsumeEffect;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.block.data.Levelled;
import org.bukkit.entity.*;
import org.bukkit.event.Event;
import org.bukkit.event.block.*;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.*;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.*;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.server.level.ClientInformation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.CommonListenerCookie;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import java.nio.file.*;
import java.util.*;
/** ONLY for the disposable isolated server; never install this helper on the working server. */
public final class SoupSmokeTest extends JavaPlugin {
private final List<String> results = new ArrayList<>();
private AstraSoupsPlugin plugin;
private Player player;
private ServerPlayer nativePlayer;
private World world;
private Block pot;
@Override public void onEnable() {
Bukkit.getScheduler().runTaskLater(this, () -> {
String phase = Files.exists(getDataFolder().toPath().resolve("phase1.txt")) ? "phase2" : "phase1";
try {
setup();
if (phase.equals("phase1")) first(); else second();
results.add("PASS: ALL " + results.size() + " CHECKS (" + phase + ")");
} catch (Throwable ex) {
getLogger().log(java.util.logging.Level.SEVERE, "SMOKE FAILED", ex);
results.add("FAIL: " + ex);
}
try { Files.createDirectories(getDataFolder().toPath()); Files.write(getDataFolder().toPath().resolve(phase + ".txt"), results); }
catch (Exception e) { throw new RuntimeException(e); }
results.forEach(getLogger()::info);
Bukkit.shutdown();
}, 40);
}
private void setup() {
plugin = (AstraSoupsPlugin) Bukkit.getPluginManager().getPlugin("AstraSoups");
check(plugin != null && plugin.isEnabled(), "Plugin enables on Paper 26.2");
world = Bukkit.getWorlds().getFirst();
world.getChunkAt(0, 0).addPluginChunkTicket(this);
var server = ((CraftServer) Bukkit.getServer()).getServer();
var level = ((CraftWorld) world).getHandle();
var profile = new GameProfile(UUID.randomUUID(), "SoupTest");
nativePlayer = new ServerPlayer(server, level, profile, ClientInformation.createDefault());
nativePlayer.connection = new ServerGamePacketListenerImpl(server, new Connection(PacketFlow.SERVERBOUND), nativePlayer, CommonListenerCookie.createInitial(profile, false));
nativePlayer.setPos(2, 80, 2);
player = nativePlayer.getBukkitEntity();
player.setGameMode(GameMode.SURVIVAL);
pot = world.getBlockAt(2, 80, 2);
}
private void check(boolean value, String name) { if (!value) throw new AssertionError(name); results.add("PASS: " + name); }
private ItemStack item(Material material) { return new ItemStack(material); }
private ItemStack potion(Material material, PotionType type) {
ItemStack stack = item(material); PotionMeta meta = (PotionMeta) stack.getItemMeta();
meta.setBasePotionType(type); stack.setItemMeta(meta); return stack;
}
private ItemStack[] recipe() {
return new ItemStack[]{potion(Material.POTION, PotionType.LONG_NIGHT_VISION), item(Material.COOKED_BEEF), item(Material.GLOW_BERRIES), potion(Material.SPLASH_POTION, PotionType.STRONG_STRENGTH)};
}
private void fill(Block b) { b.setType(Material.WATER_CAULDRON); Levelled l = (Levelled) b.getBlockData(); l.setLevel(3); b.setBlockData(l); }
private PlayerInteractEvent click(Block block, ItemStack held, boolean sneak, boolean denied) {
player.getInventory().setItemInMainHand(held);
player.setSneaking(sneak);
PlayerInteractEvent event = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, held, block, BlockFace.UP, EquipmentSlot.HAND);
if (denied) event.setUseInteractedBlock(Event.Result.DENY);
Bukkit.getPluginManager().callEvent(event);
return event;
}
private void addFour(Block block) { for (ItemStack i : recipe()) click(block, i, false, false); }
private ItemStack soupInInventory() {
return Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull).filter(plugin.ingredients()::isCombined).findFirst().orElseThrow();
}
private List<PotionEffect> effects(ItemStack soup) {
return soup.getData(DataComponentTypes.CONSUMABLE).consumeEffects().stream().filter(e -> e instanceof ConsumeEffect.ApplyStatusEffects)
.flatMap(e -> ((ConsumeEffect.ApplyStatusEffects) e).effects().stream()).toList();
}
private void first() throws Exception {
Ingredients ingredients = plugin.ingredients();
check(Math.abs(ingredients.read(item(Material.COOKED_BEEF)).saturation() - 12.8f) < .001, "Steak saturation is 12.8 absolute points");
for (Material material : Material.values()) if (material.isItem() && material.isEdible()) {
check(ingredients.read(item(material)) != null, "Edible accepted: " + material);
}
check(ingredients.read(item(Material.CAKE)).nutrition() == 14, "Whole cake accepted as seven slices");
check(ingredients.read(item(Material.TIPPED_ARROW)) == null, "Tipped arrows are not potion ingredients");
check(ingredients.read(item(Material.STONE)) == null, "Nonfood rejected");
for (Material material : List.of(Material.MUSHROOM_STEW, Material.RABBIT_STEW, Material.BEETROOT_SOUP, Material.SUSPICIOUS_STEW)) {
ItemStack stack = item(material); Ingredients.normalize(stack);
check(stack.getMaxStackSize() == 16, "Vanilla soup stack size: " + material);
}
ItemStack soup = ingredients.brew(Arrays.asList(recipe()));
check(soup.getData(DataComponentTypes.FOOD).nutrition() == 10, "Example combines 10 hunger points");
check(Math.abs(soup.getData(DataComponentTypes.FOOD).saturation() - 13.2f) < .001, "Example combines 13.2 saturation points");
check(soup.getMaxStackSize() == 16, "Combined soup stacks to sixteen");
List<ItemStack> reversed = new ArrayList<>(Arrays.asList(recipe())); Collections.reverse(reversed);
check(ingredients.brew(reversed).isSimilar(soup), "Same recipe stacks regardless of ingredient insertion order");
check(effects(soup).stream().anyMatch(e -> e.getType().equals(PotionEffectType.NIGHT_VISION) && e.getDuration() == 9600 && e.getAmplifier() == 0), "Extended night vision preserved");
check(effects(soup).stream().anyMatch(e -> e.getType().equals(PotionEffectType.STRENGTH) && e.getDuration() == 1800 && e.getAmplifier() == 1), "Strength II from splash potion preserved without splash reduction");
ItemStack roundTrip = ItemStack.deserializeBytes(soup.serializeAsBytes());
check(roundTrip.isSimilar(soup), "All soup components survive binary serialization");
var yaml = new org.bukkit.configuration.file.YamlConfiguration(); yaml.set("soup", soup);
var loaded = new org.bukkit.configuration.file.YamlConfiguration(); loaded.loadFromString(yaml.saveToString());
check(soup.isSimilar(loaded.getItemStack("soup")), "Soup survives YAML serialization");
ItemStack appleSoup = ingredients.brew(Collections.nCopies(4, item(Material.ENCHANTED_GOLDEN_APPLE)));
check(effects(appleSoup).stream().anyMatch(e -> e.getType().equals(PotionEffectType.ABSORPTION) && e.getAmplifier() == 3), "Unique vanilla food effects preserved");
ItemStack stew = item(Material.SUSPICIOUS_STEW); SuspiciousStewMeta sm = (SuspiciousStewMeta) stew.getItemMeta();
sm.addCustomEffect(new PotionEffect(PotionEffectType.BLINDNESS, 240, 0), true); stew.setItemMeta(sm);
ItemStack stewSoup = ingredients.brew(List.of(stew, item(Material.APPLE), item(Material.APPLE), item(Material.APPLE)));
check(effects(stewSoup).stream().anyMatch(e -> e.getType().equals(PotionEffectType.BLINDNESS) && e.getDuration() == 240), "Suspicious stew effect retained");
ItemStack customPotion = potion(Material.LINGERING_POTION, PotionType.WATER);
PotionMeta pm = (PotionMeta) customPotion.getItemMeta(); pm.addCustomEffect(new PotionEffect(PotionEffectType.HASTE, 1234, 2, true, false, false), true); customPotion.setItemMeta(pm);
ItemStack customPotionSoup = ingredients.brew(List.of(customPotion, item(Material.APPLE), item(Material.APPLE), item(Material.APPLE)));
check(effects(customPotionSoup).stream().anyMatch(e -> e.getDuration() == 1234 && e.getAmplifier() == 2 && e.isAmbient() && !e.hasParticles() && !e.hasIcon()), "Custom lingering potion flags and exact ticks preserved");
// Real Minecraft consumption and vanilla remainder handling, using a disconnected native server player.
player.getInventory().clear(); player.setFoodLevel(5); player.setSaturation(0);
ItemStack stack = soup.clone(); stack.setAmount(2);
var nms = CraftItemStack.asNMSCopy(stack);
var remaining = nms.finishUsingItem(((CraftWorld) world).getHandle(), nativePlayer);
check(player.getFoodLevel() == 15 && Math.abs(player.getSaturation() - 13.2f) < .001, "Native eating applies summed hunger and saturation exactly once");
check(player.getPotionEffect(PotionEffectType.STRENGTH).getAmplifier() == 1
&& player.getPotionEffect(PotionEffectType.NIGHT_VISION).getDuration() == 9600, "Native eating applies strength and night vision");
check(remaining.getCount() == 1 && CraftItemStack.asBukkitCopy(remaining).isSimilar(soup), "Native eating keeps the remaining stacked soup");
check(player.getInventory().contains(Material.BOWL), "Native eating returns one bowl from a stack");
var last = remaining.finishUsingItem(((CraftWorld) world).getHandle(), nativePlayer);
check(CraftItemStack.asBukkitCopy(last).getType() == Material.BOWL, "Final soup becomes a bowl");
check(player.getFoodLevel() == 20 && player.getSaturation() == 20, "Native hunger and saturation remain capped at twenty");
player.addAttachment(this, "astrasoups.admin", true);
ItemStack unique = item(Material.PAPER); ItemMeta uniqueMeta = unique.getItemMeta();
uniqueMeta.displayName(net.kyori.adventure.text.Component.text("Custom test food")); unique.setItemMeta(uniqueMeta);
player.getInventory().setItemInMainHand(unique);
plugin.onCommand(player, plugin.getCommand("soups"), "soups", new String[]{"register", "test_food", "7", "4.5"});
check(plugin.ingredients().read(unique).nutrition() == 7 && plugin.ingredients().read(unique).tags().contains("custom:test_food"), "Registered nonvanilla food supplies custom nutrition and selector");
check(plugin.ingredients().read(item(Material.PAPER)) == null, "Registered custom food does not match plain base material");
var configPath = plugin.getDataFolder().toPath().resolve("config.yml");
String goodConfig = Files.readString(configPath);
Files.writeString(configPath, "incompatible: [broken");
plugin.onCommand(Bukkit.getConsoleSender(), plugin.getCommand("soups"), "soups", new String[]{"reload"});
check(plugin.ingredients().read(unique).nutrition() == 7, "Malformed reload retains previous working ingredient configuration");
Files.writeString(configPath, goodConfig);
plugin.onCommand(Bukkit.getConsoleSender(), plugin.getCommand("soups"), "soups", new String[]{"reload"});
player.getInventory().clear(); fill(pot);
ItemStack denied = new ItemStack(Material.APPLE, 2);
click(pot, denied, false, true);
check(player.getInventory().getItemInMainHand().getAmount() == 2, "Cancelled protected interaction does not take an ingredient");
addFour(pot);
check(player.getInventory().getItemInMainHand().getType().isAir(), "Adding a fourth ingredient consumes exactly one item");
long count = world.getEntities().stream().filter(e -> e instanceof Item && e.getPersistentDataContainer().has(new NamespacedKey(plugin, "cauldron_visual"))).count();
check(count == 4, "Four real item entities visualize the ingredients");
for (Entity e : world.getEntities()) if (e instanceof Item drop && e.getPersistentDataContainer().has(new NamespacedKey(plugin, "cauldron_visual"))) {
check(!drop.canPlayerPickup() && !drop.canMobPickup() && !drop.hasGravity() && drop.isUnlimitedLifetime(), "Display ingredient cannot be picked up or expire");
}
click(pot, item(Material.AIR), false, false);
check(player.getInventory().getItemInMainHand().getType() == Material.SPLASH_POTION, "Empty-hand withdrawal is LIFO");
click(pot, recipe()[3], false, false);
plugin.getConfig().set("incompatible", List.of(List.of("material:cooked_beef", "effect:minecraft:strength")));
plugin.saveConfig(); plugin.onCommand(Bukkit.getConsoleSender(), plugin.getCommand("soups"), "soups", new String[]{"reload"});
click(pot, item(Material.AIR), true, false);
check(world.getEntities().stream().noneMatch(e -> e instanceof BlockDisplay), "Incompatible recipe does not create ready soup");
click(pot, item(Material.AIR), false, false);
check(player.getInventory().getItemInMainHand().getType() == Material.SPLASH_POTION, "Rejected recipe retains its ingredients");
plugin.getConfig().set("incompatible", List.of()); plugin.saveConfig();
plugin.onCommand(Bukkit.getConsoleSender(), plugin.getCommand("soups"), "soups", new String[]{"reload"});
click(pot, recipe()[3], false, false);
click(pot, item(Material.AIR), true, false);
check(world.getEntities().stream().anyMatch(e -> e instanceof BlockDisplay), "Brew creates colored soup surface");
check(world.getEntities().stream().noneMatch(e -> e instanceof Item && e.getPersistentDataContainer().has(new NamespacedKey(plugin, "cauldron_visual"))), "Brew removes ingredient visuals");
click(pot, new ItemStack(Material.BOWL, 2), false, false);
check(player.getInventory().getItemInMainHand().getAmount() == 1, "Collecting soup consumes one bowl");
check(soupInInventory().isSimilar(soup), "Collecting yields exactly the expected recipe");
check(pot.getType() == Material.CAULDRON, "Collecting consumes cauldron water");
// Persist one raw and one cooked cauldron for a true process restart.
fill(pot); addFour(pot); click(pot, item(Material.STICK), false, false);
check(player.getInventory().getItemInMainHand().getType() == Material.STICK, "Ordinary right-click with an item starts cooking without consuming the tool");
Block raw = world.getBlockAt(5, 80, 2); fill(raw); click(raw, item(Material.GOLDEN_CARROT), false, false);
check(new java.io.File(plugin.getDataFolder(), "cauldrons.yml").length() > 0, "Cauldron states saved before restart");
}
private void second() {
check(pot.getType() == Material.WATER_CAULDRON, "Ready cauldron block survives restart");
click(pot, item(Material.BOWL), false, false);
ItemStack soup = soupInInventory();
check(soup.getMaxStackSize() == 16 && effects(soup).size() == 2, "Ready soup keeps stack size and effects across process restart");
player.getInventory().clear();
Block raw = world.getBlockAt(5, 80, 2);
click(raw, item(Material.AIR), false, false);
check(player.getInventory().getItemInMainHand().getType() == Material.GOLDEN_CARROT, "Raw ingredient can be withdrawn after process restart");
click(raw, item(Material.AIR), false, false);
check(player.getInventory().isEmpty(), "Withdrawn ingredient cannot be obtained twice");
}
}