World
World view
A WorldView is one dimension. Use the view that a callback (event, action, tick) receives only during that call.
| Area | Methods |
|---|---|
| Basics | dimension(), isClientSide(), gameTime(), players() |
| Blocks | blockAt(pos), setBlock(pos, block), stateAt(pos), setProperty(pos, property, value), blockEntityAt(pos), emittedLight(pos) |
| Entities | spawnEntity(type, x, y, z), entitiesNear(x, y, z, radius) |
| Effects | playSound(pos, sound, volume, pitch), spawnParticles(particle, x, y, z, count, spread) |
| Game rules | gameRule(key), setGameRule(key, value) |
| Storage | storage(id), storage(id, options) |
| Scheduling | schedule(ticks, task) |
| Chunks and biomes | setChunkForced(x, z, forced), isChunkLoaded(x, z), biomeAt(pos), naturalSpawns(pos, category) |
| Dimensions | otherDimension(id) |
| Tags | tags(), isBlockIn(pos, tag) (Tags) |
world.setBlock(pos, ResourceId.minecraft("stone"));
world.spawnEntity(ResourceId.minecraft("pig"), x, y, z);
Optional<WorldView> pocket = world.otherDimension(ResourceId.of("my_mod", "pocket"));
Create a BlockPos with BlockPos.of(x, y, z) and move it with offset, above, and below.
World storage
mod.events().register(WorldEvents.READY, world -> {
StoredData stats = world.storage(ResourceId.of("my_mod", "stats")); // per dimension, saved with the world
stats.data().putInt("loads", stats.data().getInt("loads", 0) + 1);
stats.markChanged();
});
- Each dimension has its own storage, and it is saved with the world.
- Call
markChanged()after you change it. - Versions, migrations, and client sync options are in Stored data and versions and Networking and sync.
DataTree
StoredData.data(), block entity data, and entity data are all a DataTree.
| Method | Meaning |
|---|---|
getInt/putInt, getLong/putLong, getDouble/putDouble, getBoolean/putBoolean, getString/putString | Read a value (you give a default) and write a value. put… can be chained |
getBytes/putBytes | Byte array |
child(key), findChild(key) | Subtree (creates it if missing / returns an Optional) |
has, remove, keys, clear, copy | Key management |
Scheduling tasks
world.schedule(20, () -> world.setBlock(pos, ResourceId.minecraft("stone"))); // after 20 ticks, on the server thread
world.schedule(20, () -> world.playSound(pos, ResourceId.minecraft("block.note_block.bell"), 1, 1));
- Scheduled tasks disappear when the server stops. They are not saved.
- If a scheduled task throws an exception, Poly Mod Loader logs
POLY-SCHEDULE-001and keeps running the next tasks.
Force-loading chunks
world.setChunkForced(chunkX, chunkZ, true); // same as /forceload, saved with the world
world.isChunkLoaded(chunkX, chunkZ);
world.biomeAt(pos);
mod.events().register(WorldEvents.CHUNK_LOADED, (w, chunkX, chunkZ) -> { /* … */ }); // CHUNK_UNLOADED exists too
World events
On the server, each dimension goes through LOADED → READY → SAVING (on every save) → UNLOADED per server start. LOADED, SAVING, and UNLOADED share one listener type, WorldListener (world -> …).
mod.events().register(WorldEvents.LOADED, world -> { /* every dimension exists, before the first tick */ });
mod.events().register(WorldEvents.SAVING, world -> { /* changes made now are saved with the world */ });
mod.events().register(WorldEvents.UNLOADED, world -> { /* the last moment to read it while stopping */ });
SAVINGfires before autosaves,/save-all, and the save while stopping, before chunks and level data are written.- Changes made in
UNLOADEDare not saved any more.
Timings, CHUNK_LOADED, CHUNK_UNLOADED, and the other events are listed in Events.