Give modules per-world state, lifecycle hooks and services: PackWorld with its state store, InvocationContext.world(), ModuleLifecycle, @Service with Providers ALL, FIRST and ONE, ObjectsAt and StructureLocator, and the host's WorldStates, LifecycleRunner and catalog lookups

This commit is contained in:
piexel committed 2026-10-09 14:32:54 +02:00
1 parent ef4b640867
commit b20a3cf719
29 files changed
+1286 -17

No files matched your search

@@ -0,0 +1,29 @@
package com.volmit.service;
import com.volmit.world.BlockPosition;
import java.util.Objects;
/** How a search for something in a world ended: found at a position, not there, or given up before covering the whole
* area asked for.
*/
public sealed interface LocateResult {
LocateResult NOT_FOUND = new NotFound();
LocateResult SEARCH_LIMIT_REACHED = new SearchLimitReached();
static LocateResult found(BlockPosition position) {
return new Found(position);
}
/** Found at {@code position}: world X and Z, and Y the height above the dimension's bottom. */
record Found(BlockPosition position) implements LocateResult {
public Found {
Objects.requireNonNull(position, "position");
}
}
/** The whole area was searched and holds none. */
record NotFound() implements LocateResult {}
/** The search stopped before covering the area, such as after checking as many candidates as it allows itself. */
record SearchLimitReached() implements LocateResult {}
}
@@ -0,0 +1,18 @@
package com.volmit.service;
import com.volmit.world.PackWorld;
import java.util.Set;
/** Which pack objects a module places in a chunk, predicted without generating it. The host's object search and its
* studio tools ask every provider and use all the keys together.
*/
@Service(Providers.ALL)
public interface ObjectsAt {
/**
* The keys of the pack objects this module would place in the chunk at {@code chunkX}, {@code chunkZ} (chunk
* coordinates, world X and Z divided by 16), in the form packs write them ({@code trees/oak}). It is a prediction:
* it may name an object whose placement the chunk's generation later rejects. Empty when the module places nothing
* there.
*/
Set<String> objectsAt(PackWorld world, int chunkX, int chunkZ);
}
@@ -0,0 +1,16 @@
package com.volmit.service;
/** How the host combines the providers of one {@link Service}. A service chooses one; its javadoc says how the host
* uses the answers. Providers are always in module id order, each module's in the order it registered them, so the
* outcome never depends on how modules were discovered.
*/
public enum Providers {
/** The host asks every provider and combines their answers, such as the objects each module places in a chunk. */
ALL,
/** The host asks the providers in order until one answers, such as the module that knows a structure key. A provider
* that has no answer says so in the service's own terms (an empty result) and the next one is asked.
*/
FIRST,
/** A pack has at most one provider. A second module registering one fails the pack load, naming both modules. */
ONE
}
@@ -0,0 +1,25 @@
package com.volmit.service;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** Marks an interface as a service: a capability modules provide and the host looks up, such as finding a module's
* structures, instead of knowing the feature behind it. Modules register providers with
* {@link com.volmit.FeatureRegistry#registerService}; the host gets them from the pack's catalog.
*
* <p>Providers hold no state of their own: a provider is shared by every world, and the host may call it from any
* thread, also from several at once. A provider keeps what it needs in the world's state store
* ({@link com.volmit.world.PackWorld#state}), which is why service methods take the world. Services compute answers;
* they never act on the platform themselves: what should happen in the world comes back as a value the host carries out
* on the right thread.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Service {
/** How the host treats several modules providing this service. */
Providers value();
}
@@ -0,0 +1,30 @@
package com.volmit.service;
import com.volmit.resource.PackResources;
import com.volmit.world.PackWorld;
import java.util.Set;
/** Finds the structures a module places, for the host's structure commands and index. Structures are named by their
* keys, in the form packs write them ({@code castle}, {@code villages/plains}). The host asks the providers in order and
* sends a key to the first that can locate it in the world.
*/
@Service(Providers.FIRST)
public interface StructureLocator {
/** Every structure key of the pack this module knows, placed in a world or not, such as one per structure file. The
* host lists them in its index of the structures a pack has.
*/
Set<String> structures(PackResources pack);
/** The keys of the structures this module places in {@code world} and can find with {@link #locate}: commands offer
* them and send a search for one of them here. A structure no placement of the world uses is not among them.
*/
Set<String> locatable(PackWorld world);
/**
* The start of structure {@code key} nearest to the column {@code fromX}, {@code fromZ} (world coordinates), searched
* up to {@code maxRadiusChunks} chunks away from it. The position is the structure's origin: world X and Z, and Y the
* height above the dimension's bottom of its base. Called with a key from {@link #locatable}. May take long; the
* host calls it off the server's main thread.
*/
LocateResult locate(PackWorld world, String key, int fromX, int fromZ, int maxRadiusChunks);
}