Smooth cauldron item orbits and lower soup level after each serving

This commit is contained in:
2026-09-05 15:24:44 +03:00
parent 6c03e62761
commit 945188193a
9 changed files with 177 additions and 106 deletions
@@ -18,7 +18,6 @@ import org.bukkit.inventory.*;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Transformation;
import org.bukkit.util.Vector;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import net.kyori.adventure.text.Component;
@@ -51,9 +50,9 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
return new Position(UUID.fromString(p[0]), Integer.parseInt(p[1]), Integer.parseInt(p[2]), Integer.parseInt(p[3]));
}
}
record Pot(List<ItemStack> items, ItemStack soup, int brewTicks, int servings) {
record Pot(List<ItemStack> items, ItemStack soup, int brewTicks, int servings, int capacity) {
Pot { items = List.copyOf(items); }
Pot(List<ItemStack> items, ItemStack soup) { this(items, soup, 0, soup == null ? 0 : 1); }
Pot(List<ItemStack> items, ItemStack soup) { this(items, soup, 0, soup == null ? 0 : 1, soup == null ? 0 : 1); }
boolean brewing() { return brewTicks > 0; }
}
@@ -109,14 +108,16 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
ItemStack soup = c.getItemStack("soup");
int brewTicks = c.getInt("brew-ticks", 0);
int servings = c.getInt("servings", soup == null ? 0 : 1);
int capacity = c.getInt("capacity", soup == null ? 0 : Math.max(servings, getConfig().getInt("servings-per-brew", 4)));
if (items.size() > 4 || brewTicks < 0 || brewTicks > 1200
|| (soup == null ? capacity != 0 : capacity < servings || capacity > 16)
|| (soup == null ? servings != 0 : servings < 1 || servings > 16)
|| (soup != null && (soup.getAmount() != 1 || !ingredients.isCombined(soup)))
|| (brewTicks > 0 ? items.size() != 4 || soup == null
: (soup != null ? !items.isEmpty() : items.isEmpty()))) {
throw new IllegalArgumentException("Invalid cauldron at " + id);
}
pots.put(Position.parse(id), new Pot(items, soup, brewTicks, servings));
pots.put(Position.parse(id), new Pot(items, soup, brewTicks, servings, capacity));
}
}
@@ -133,12 +134,14 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
data.set(prefix + ".soup", entry.getValue().soup);
if (entry.getValue().brewing()) data.set(prefix + ".brew-ticks", entry.getValue().brewTicks);
if (entry.getValue().soup != null) data.set(prefix + ".servings", entry.getValue().servings);
if (entry.getValue().soup != null) data.set(prefix + ".capacity", entry.getValue().capacity);
}
Path temp = dataFile.toPath().resolveSibling("cauldrons.yml.tmp");
Files.writeString(temp, data.saveToString());
try { Files.move(temp, dataFile.toPath(), StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING); }
catch (AtomicMoveNotSupportedException e) { Files.move(temp, dataFile.toPath(), StandardCopyOption.REPLACE_EXISTING); }
removeVisuals(pos);
// Keep the same visuals when starting a brew or taking a portion: no respawn flash.
if (old == null || next == null || !old.items.equals(next.items)) removeVisuals(pos);
brewElapsed.remove(pos);
return true;
} catch (Exception e) {
@@ -159,7 +162,7 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
return;
}
if (event.useInteractedBlock() == Event.Result.DENY || event.useItemInHand() == Event.Result.DENY) return;
if (block.getType() != Material.WATER_CAULDRON) return;
if (!validBlock(block, pot)) return;
Player player = event.getPlayer();
if (!player.hasPermission("astrasoups.use") || player.getGameMode() == GameMode.SPECTATOR) return;
ItemStack held = player.getInventory().getItemInMainHand();
@@ -183,11 +186,11 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
if (!bowl) { say(player, "Суп готов. Порций: " + pot.servings + ". Возьмите его миской — ПКМ."); return; }
ItemStack result = pot.soup.clone();
int remaining = pot.servings - 1;
if (!change(pos, remaining == 0 ? null : new Pot(List.of(), pot.soup, 0, remaining))) { saveError(player); return; }
if (!change(pos, remaining == 0 ? null : new Pot(List.of(), pot.soup, 0, remaining, pot.capacity))) { saveError(player); return; }
takeOne(player);
give(player, result);
if (remaining == 0) block.setType(Material.CAULDRON);
else show(pos, pots.get(pos), block);
else { syncWater(block, pots.get(pos)); show(pos, pots.get(pos), block); }
block.getWorld().playSound(block.getLocation(), Sound.ITEM_BOTTLE_FILL, 0.8f, 0.7f);
say(player, remaining == 0 ? "Последняя порция собрана. Котёл пуст." : "Осталось порций: " + remaining + ".");
return;
@@ -198,7 +201,8 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
if (error != null) { say(player, error + " ПКМ пустой рукой возвращает последний ингредиент."); return; }
ItemStack soup = ingredients.brew(pot.items);
int duration = (int) Math.round(getConfig().getDouble("brew-seconds", 4.0) * 20);
if (!change(pos, new Pot(pot.items, soup, duration, getConfig().getInt("servings-per-brew", 4)))) { saveError(player); return; }
int capacity = getConfig().getInt("servings-per-brew", 4);
if (!change(pos, new Pot(pot.items, soup, duration, capacity, capacity))) { saveError(player); return; }
show(pos, pots.get(pos), block);
block.getWorld().playSound(block.getLocation(), Sound.BLOCK_BREWING_STAND_BREW, 1, 1);
say(player, "Варка началась! Дождитесь окончания водоворота.");
@@ -242,7 +246,7 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
Position pos = entry.getKey();
Block block = pos.loadedBlock();
if (block == null) { removeVisuals(pos); continue; }
if (block.getType() != Material.WATER_CAULDRON) {
if (!validBlock(block, entry.getValue())) {
// External block editing: return raw ingredients only once, never resurrect a missing block.
if (change(pos, null)) for (ItemStack item : entry.getValue().items) block.getWorld().dropItemNaturally(block.getLocation().add(.5, .5, .5), item.clone());
continue;
@@ -252,25 +256,28 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
int elapsed = brewElapsed.merge(pos, 1, Integer::sum);
// On a save failure keep ingredients locked and retry once per second.
if (elapsed >= pot.brewTicks && (elapsed - pot.brewTicks) % 20 == 0
&& change(pos, new Pot(List.of(), pot.soup, 0, pot.servings))) {
&& change(pos, new Pot(List.of(), pot.soup, 0, pot.servings, pot.capacity))) {
syncWater(block, pots.get(pos));
show(pos, pots.get(pos), block);
block.getWorld().playSound(block.getLocation(), Sound.BLOCK_BUBBLE_COLUMN_BUBBLE_POP, .8f, 1.2f);
block.getWorld().spawnParticle(Particle.CLOUD, block.getLocation().add(.5, 1.05, .5), 12, .2, .12, .2, .01);
continue;
}
}
if (pot.soup != null && !pot.brewing()) syncWater(block, pot);
List<Entity> entities = visuals.get(pos);
if (entities == null || entities.stream().anyMatch(e -> !e.isValid())) {
removeVisuals(pos); show(pos, entry.getValue(), block);
} else {
for (int i = 0; i < entities.size(); i++) if (entities.get(i) instanceof Item item) {
Location location = itemLocation(block, i, pot);
item.teleport(location);
item.setVelocity(new Vector());
for (int i = 0; i < entities.size(); i++) if (entities.get(i) instanceof ItemDisplay item && ticks % 2 == 0) {
// Client interpolates the local transform; the entity itself never teleports.
item.setInterpolationDelay(0);
item.setTransformation(itemTransform(block, i, pot, 2));
if (pot.brewing() && ticks % 5 == 0) {
Vector3f offset = item.getTransformation().getTranslation();
double height = .375 + .1875 * ((Levelled) block.getBlockData()).getLevel();
block.getWorld().spawnParticle(Particle.SPLASH,
new Location(block.getWorld(), location.getX(), block.getY() + height + .01, location.getZ()),
block.getLocation().add(.5 + offset.x, height + .01, .5 + offset.z),
1, .015, 0, .015, 0);
}
}
@@ -284,21 +291,47 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
}
}
private Location itemLocation(Block block, int i, Pot pot) {
private boolean validBlock(Block block, Pot pot) {
return block.getType() == Material.WATER_CAULDRON
|| (block.getType() == Material.CAULDRON && pot != null && pot.soup != null && !pot.brewing());
}
private void syncWater(Block block, Pot pot) {
int level = CauldronAnimation.waterLevel(pot.servings, pot.capacity);
if (level == 0) { if (block.getType() != Material.CAULDRON) block.setType(Material.CAULDRON); }
else {
if (block.getType() != Material.WATER_CAULDRON) block.setType(Material.WATER_CAULDRON);
Levelled water = (Levelled) block.getBlockData();
if (water.getLevel() != level) { water.setLevel(level); block.setBlockData(water); }
}
}
private Transformation itemTransform(Block block, int i, Pot pot, int ahead) {
int waterLevel = block.getBlockData() instanceof Levelled l ? l.getLevel() : 3;
CauldronAnimation.Offset frame = CauldronAnimation.frame(i, waterLevel, ticks,
brewElapsed.getOrDefault(Position.at(block), 0), pot.brewTicks);
return block.getLocation().add(frame.x(), frame.y(), frame.z());
CauldronAnimation.Offset frame = CauldronAnimation.frame(i, waterLevel, ticks + ahead,
brewElapsed.getOrDefault(Position.at(block), 0) + ahead, pot.brewTicks);
return new Transformation(new Vector3f((float) frame.x() - .5f, (float) frame.y() - .5f, (float) frame.z() - .5f),
new Quaternionf(), new Vector3f(.3f), new Quaternionf().rotationY((ticks + ahead) * .035f + i));
}
private Transformation liquidTransform(Pot pot) {
return new Transformation(new Vector3f(-.075f, (float) CauldronAnimation.liquidHeight(pot.servings, pot.capacity) + .005f - .5f, .375f),
new Quaternionf().rotationX((float) (-Math.PI / 2)), new Vector3f(6, 3, 1), new Quaternionf());
}
private void show(Position pos, Pot pot, Block block) {
if (visuals.containsKey(pos)) return;
if (visuals.containsKey(pos)) {
if (pot.soup != null && !pot.brewing()) for (Entity entity : visuals.get(pos)) if (entity instanceof TextDisplay display) {
display.setInterpolationDelay(0);
display.setTransformation(liquidTransform(pot));
}
return;
}
List<Entity> entities = new ArrayList<>();
visuals.put(pos, entities);
if (pot.soup != null && !pot.brewing()) {
int rgb = pot.soup.getItemMeta().getPersistentDataContainer().getOrDefault(colorKey, PersistentDataType.INTEGER, 0xB77A36);
int waterLevel = block.getBlockData() instanceof Levelled l ? l.getLevel() : 3;
TextDisplay display = block.getWorld().spawn(block.getLocation().add(.5, .375 + .1875 * waterLevel + .005, .5), TextDisplay.class, d -> {
TextDisplay display = block.getWorld().spawn(block.getLocation().add(.5, .5, .5), TextDisplay.class, d -> {
markVisual(d);
// One default-font space: native background spans 5x10 pixels at 0.025 blocks/pixel.
// Center the rectangle, scale to 0.75x0.75 and face upwards inside the cauldron.
@@ -307,22 +340,22 @@ public final class AstraSoupsPlugin extends JavaPlugin implements Listener {
d.setBackgroundColor(Color.fromARGB(0xFF000000 | rgb));
d.setShadowed(false);
d.setSeeThrough(false);
d.setTransformation(new Transformation(new Vector3f(-.075f, 0, .375f),
new Quaternionf().rotationX((float) (-Math.PI / 2)), new Vector3f(6, 3, 1), new Quaternionf()));
d.setTransformation(liquidTransform(pot));
d.setInterpolationDuration(5);
d.setBrightness(new Display.Brightness(12, 12));
d.setViewRange(.5f);
});
entities.add(display);
} else for (int i = 0; i < pot.items.size(); i++) {
ItemStack stack = pot.items.get(i).clone();
Item item = block.getWorld().spawn(itemLocation(block, i, pot), Item.class, e -> {
int slot = i;
ItemDisplay item = block.getWorld().spawn(block.getLocation().add(.5, .5, .5), ItemDisplay.class, e -> {
markVisual(e);
e.setItemStack(stack);
e.setCanPlayerPickup(false);
e.setCanMobPickup(false);
e.setPickupDelay(Integer.MAX_VALUE);
e.setUnlimitedLifetime(true);
e.setVelocity(new Vector());
e.setItemDisplayTransform(ItemDisplay.ItemDisplayTransform.FIXED);
e.setTransformation(itemTransform(block, slot, pot, 0));
e.setInterpolationDuration(2);
e.setViewRange(.5f);
});
entities.add(item);
}
@@ -1,19 +1,28 @@
package online.astracraft.soups;
/** Coordinates relative to a cauldron; item renderers add their own bob above the entity origin. */
/** ItemDisplay centers relative to the cauldron, without native dropped-item physics or bobbing. */
public final class CauldronAnimation {
private CauldronAnimation() {}
public record Offset(double x, double y, double z) {}
public static Offset frame(int slot, int waterLevel, int tick, int elapsed, int duration) {
double progress = duration > 0 ? Math.clamp((double) elapsed / duration, 0, 1) : 0;
double eased = progress * progress * (3 - 2 * progress);
double[] phases = {-3 * Math.PI / 4, -Math.PI / 4, 3 * Math.PI / 4, Math.PI / 4};
double angle = phases[slot] + 2 * Math.PI * (2 * progress + 2 * progress * progress);
double radius = .255 * (1 - .85 * eased);
double angle = phases[slot] + (duration > 0 ? elapsed * .1 : 0);
double radius = .23;
double surface = .375 + .1875 * waterLevel;
double bob = Math.sin(tick * .06 + slot) * .01 * (1 - progress);
double bob = Math.sin(tick * .04 + slot) * .004;
return new Offset(.5 + Math.cos(angle) * radius,
Math.max(.26, surface - .28 - .14 * eased + bob), .5 + Math.sin(angle) * radius);
surface - .055 + bob, .5 + Math.sin(angle) * radius);
}
public static double liquidHeight(int servings, int capacity) {
return .25 + .6875 * Math.clamp((double) servings / capacity, 0, 1);
}
public static int waterLevel(int servings, int capacity) {
double height = liquidHeight(servings, capacity);
int level = 0;
for (int i = 1; i <= 3; i++) if (.375 + .1875 * i <= height + .00001) level = i;
return level;
}
}