Code Patterns

Copy-paste examples for common plugin tasks. Commands, events, ECS, GUI, and more.

← Back to Patterns
ecs

universe-world-hierarchy

Hytale world structure: Universe contains multiple Worlds, each World has an EntityStore. The Universe represents the entire game session, Worlds are individual dimensions/areas.

Example Code

java
// HYTALE WORLD HIERARCHY:
//
// Universe (top level - entire game session)
//   |-- World (individual dimension/area)
//         |-- EntityStore (ECS database for this world)
//               |-- Entities (via Ref<EntityStore>)
//                     |-- Components (Position, Health, etc.)

// Getting the Universe:
Universe universe = getUniverse();

// Getting a specific World:
World mainWorld = universe.getWorld("main");
World dungeonWorld = universe.getWorld("dungeon_1");

// Each World has its own EntityStore
// Entities in one world cannot directly reference entities in another

// When a player moves between worlds:
// 1. Entity is removed from old world EntityStore
// 2. Entity is created in new world EntityStore
// 3. Components are transferred

// World.execute() for thread-safe access from outside systems:
world.execute((store, commandBuffer) -> {
    // Safe to access components here
    Ref<EntityStore> playerRef = findPlayerRef(playerId);
    Player player = store.getComponent(playerRef, Player.getComponentType());
});

// Common World operations:
world.getPlayers(); // Get all players in this world
world.spawnEntity(entityType, position); // Spawn new entity
world.getBlockAt(position); // Get block at position

Thread Safety

Cross-world operations require careful synchronization. Use world.execute() when accessing another world entities.

Common Mistakes

Trying to access entities across worlds without proper synchronization. Storing World references long-term (worlds can be unloaded).