Add the EntitySpawns service and EntitySpec

This commit is contained in:
piexel committed 2026-10-09 22:16:35 +02:00
1 parent 27ffdb08bd
commit 93f1768174
2 files changed
+212

No files matched your search

@@ -0,0 +1,86 @@
package com.volmit.entity;
import com.volmit.item.ItemAttribute;
import com.volmit.item.ItemDrop;
import java.util.List;
import java.util.Objects;
/**
* An entity a module decided to spawn, with every random choice made: the host creates it on the platform and applies
* what is set. Names are as authored in the pack, with {@code &} colour codes.
*
* @param type the entity type's key, such as {@code minecraft:zombie}; ignored when {@code specialType} is set
* @param specialType a mob of another plugin, written {@code PluginName:MobName}, or empty
* @param reason the platform's spawn reason by name, or null for a natural spawn
* @param customName the name shown above the entity, empty for none
* @param flags the settings that every entity kind understands
* @param equipment what the entity wears and holds
* @param passengers entities that ride this one
* @param attributes attribute modifiers to apply
* @param lootTables keys of loot tables the entity drops, instead of the platform's drops, when it dies
* @param lootShuffleSeed seeds how the drops of {@code lootTables} are shuffled
* @param leashHolder the entity that holds this one on a leash, or null
* @param spawnEffect an effect to show when the entity appears, or null
* @param riseOutOfGround whether the entity slowly climbs out of the ground when players are near
* @param pandaMainGene for pandas the main gene by name, or null
* @param pandaHiddenGene for pandas the hidden gene by name, or null
* @param applySettingsToSpecial whether the settings also apply to a mob of another plugin
* @param commands console commands to run where the entity appears
*/
public record EntitySpec(String type, String specialType, String reason, String customName, Flags flags, Equipment equipment,
List<EntitySpec> passengers, List<ItemAttribute> attributes, List<String> lootTables,
long lootShuffleSeed, EntitySpec leashHolder, SpawnEffect spawnEffect, boolean riseOutOfGround,
String pandaMainGene, String pandaHiddenGene, boolean applySettingsToSpecial,
List<Command> commands) {
public EntitySpec {
Objects.requireNonNull(type, "type");
Objects.requireNonNull(specialType, "specialType");
Objects.requireNonNull(customName, "customName");
Objects.requireNonNull(flags, "flags");
Objects.requireNonNull(equipment, "equipment");
passengers = List.copyOf(passengers);
attributes = List.copyOf(attributes);
lootTables = List.copyOf(lootTables);
commands = List.copyOf(commands);
}
/** Whether the entity is a mob of another plugin. */
public boolean isSpecial() {
return !specialType.isEmpty();
}
/** What an entity can be set to be, whatever its kind; the host applies the ones its kind has. */
public record Flags(boolean customNameVisible, boolean aware, boolean ai, boolean glowing, boolean gravity,
boolean invulnerable, boolean silent, boolean pickupItems, boolean removable,
boolean keepEntity, boolean baby) {
}
/** The items an entity wears and holds; null for an empty slot. */
public record Equipment(ItemDrop helmet, ItemDrop chestplate, ItemDrop leggings, ItemDrop boots, ItemDrop mainHand,
ItemDrop offHand) {
public static final Equipment NONE = new Equipment(null, null, null, null, null, null);
}
/**
* Sound and particles shown around an entity that appears. Offsets and the randomness of the particle motion are
* as in an effect of the pack; the host makes the random choices.
*/
public record SpawnEffect(String sound, int soundDistance, double minPitch, double maxPitch, double volume,
String particle, int particleOffset, int particleCount, double altX, double altY,
double altZ, boolean randomAltX, boolean randomAltY, boolean randomAltZ, double extra) {
}
/**
* Console commands run when an entity appears. {@code {x}}, {@code {y}} and {@code {z}} stand for its block position.
*
* @param commands the commands
* @param delay server ticks to wait before running them
* @param repeat whether to run them again and again until the server stops
* @param repeatDelay server ticks between repeats
*/
public record Command(List<String> commands, long delay, boolean repeat, long repeatDelay) {
public Command {
commands = List.copyOf(commands);
}
}
}
@@ -0,0 +1,126 @@
package com.volmit.service;
import com.volmit.entity.EntitySpec;
import com.volmit.world.BlockPosition;
import com.volmit.world.PackWorld;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* The entities a module spawns in the world over time: spawners the dimension, region and biomes list, and the spawners
* markers in the world name. The host decides when and where to look, asks the providers, and spawns what the first
* answer plans, after checking the conditions of the platform that come with it (light, the surface below, free space).
* It tells the module how many entities appeared so that the module can keep its limits on how often a spawner fires.
*/
@Service(Providers.ALL)
public interface EntitySpawns {
/** What the module plans to spawn in the chunk or at the marker, or empty to spawn nothing. */
Optional<Plan> plan(PackWorld world, Site site);
/** Called with the number of entities of {@code plan} that appeared, so the spawner's rate limits count them. */
void spawned(PackWorld world, Plan plan, int count);
/** The entity of the pack named {@code key} as this module would spawn it, for tools that spawn one on request; empty for an unknown key. */
Optional<EntitySpec> entity(PackWorld world, String key);
/** What a marker in the world means for spawning; empty for a marker this module does not know. */
Optional<MarkerRule> marker(PackWorld world, String tag);
/** Where entities may spawn. */
sealed interface Site permits Chunk, Marker {
}
/**
* An ambient spawn in a chunk.
*
* @param chunkX chunk coordinates
* @param livingEntities how many living entities the chunk already holds
* @param initial whether this is the spawn of a chunk that was just generated
*/
record Chunk(int chunkX, int chunkZ, int livingEntities, boolean initial) implements Site {
}
/**
* A spawn at a marker.
*
* @param position the marker's block: world X and Z, Y the height above the dimension's bottom
* @param marker the marker's tag
* @param spawner the key of the spawner chosen among the marker's
* @param initial whether the spawner's initial spawns are used rather than its ordinary ones
*/
record Marker(BlockPosition position, String marker, String spawner, boolean initial) implements Site {
public Marker {
Objects.requireNonNull(position, "position");
Objects.requireNonNull(marker, "marker");
Objects.requireNonNull(spawner, "spawner");
}
}
/**
* What a marker does.
*
* @param spawners keys of the spawners at the marker
* @param emptyAbove whether it only counts with free space above it
* @param removeOnChange whether it goes away when its block changes
*/
record MarkerRule(List<String> spawners, boolean emptyAbove, boolean removeOnChange) {
public MarkerRule {
spawners = List.copyOf(spawners);
}
}
/**
* Entities to spawn, tried one after the other.
*
* @param spawner the key of the spawner that planned them
* @param chunkX the chunk they spawn in, where the spawner's per-chunk limit counts them
* @param exhaustsMarker whether the marker that led here is used up and should be removed
* @param attempts the spawns to try
*/
record Plan(String spawner, int chunkX, int chunkZ, boolean exhaustsMarker, List<Attempt> attempts) {
public Plan {
Objects.requireNonNull(spawner, "spawner");
attempts = List.copyOf(attempts);
}
}
/**
* One entity to spawn at a block. The host spawns it only if the block's light level lies in the range (when the
* range is restricted) and, when {@code checkSurface}, the block below suits the entity's {@code surface} and the
* space the entity needs is free.
*
* @param at world X and Z, Y the height above the dimension's bottom
* @param entity what to spawn
* @param surface the kind of ground or fluid the entity needs below it
* @param minLight lowest light level allowed, 0 for any
* @param maxLight highest light level allowed, 15 for any
* @param checkSurface whether to check the surface and the free space
*/
record Attempt(BlockPosition at, EntitySpec entity, Surface surface, int minLight, int maxLight, boolean checkSurface) {
public Attempt {
Objects.requireNonNull(at, "at");
Objects.requireNonNull(entity, "entity");
Objects.requireNonNull(surface, "surface");
}
/** Whether the light level matters. */
public boolean restrictsLight() {
return minLight > 0 || maxLight < 15;
}
}
/** The ground or fluid an entity spawns on. */
enum Surface {
/** Solid ground. */
LAND,
/** Ground animals spawn on, such as grass, dirt and podzol. */
ANIMAL,
/** Within water. */
WATER,
/** Solid ground or water. */
OVERWORLD,
/** Within lava. */
LAVA
}
}