Class Universe

All Implemented Interfaces:
MetricProvider, CommandOwner, IMessageReceiver

public class Universe extends JavaPlugin implements IMessageReceiver, MetricProvider
A plugin that manages the universe of worlds and players on the server.
  • Field Details

  • Constructor Details

    • Universe

      public Universe(@Nonnull JavaPluginInit init)
      Default Constructor for a Universe instance.
      Parameters:
      init - The plugin initialization step for this plugin.
  • Method Details

    • get

      public static Universe get()
      Returns:
      the global Universe instance.
    • registerResource

      @Nonnull public static <T> UniverseResourceType<T> registerResource(@Nonnull Class<T> typeClass, @Nonnull String id, @Nonnull BuilderCodec<T> codec)
      Registers a universe-scoped persistent resource. Call during plugin setup, before the universe starts.
      Type Parameters:
      T - The resource type.
      Parameters:
      typeClass - The class of the resource.
      id - The unique id of the resource, also its file name on disk.
      codec - The codec that persists the resource. Its default value seeds a new universe.
      Returns:
      the token to read and flush the resource with.
    • runBackup

      @Nonnull public CompletableFuture<Void> runBackup()
      Called to run a backup of the universe.
      Returns:
      a future that will complete when the backup is done.
    • isSavingLocked

      public boolean isSavingLocked()
      Whether saving of universe-level data is prevented due to another process wanting to access the files manually. e.g. backups
    • lockSaving

      public void lockSaving()
      Increments the saving lock counter, preventing universe-level saving systems from writing to disk.
    • unlockSaving

      public void unlockSaving()
      Decrements the saving lock counter, allowing saving to resume once all locks are released.
    • getResource

      @Nonnull public <T> T getResource(@Nonnull UniverseResourceType<T> type)
      Returns the shared instance of a universe resource. Any world thread may call this; the resource owns its own thread-safety.
      Type Parameters:
      T - The resource type.
      Parameters:
      type - The resource type token.
      Returns:
      the loaded resource, never null.
    • flushResource

      @Nonnull public CompletableFuture<Void> flushResource(@Nonnull UniverseResourceType<?> type)
      Writes one universe resource to disk. A write requested while saving is locked (for example during a backup) is queued and runs when the lock releases.
      Parameters:
      type - The resource type to write.
      Returns:
      a future that completes when the bytes are on disk.
    • disconnectAllPLayers

      public void disconnectAllPLayers()
      Called when the HytaleServer or Universe is shutdown. Disconnects all connected players.
    • shutdownAllWorlds

      public void shutdownAllWorlds()
      Called when the HytaleServer or Universe is shutdown. Stops and removes all worlds.
    • toMetricResults

      @Nonnull public MetricResults toMetricResults()
      Specified by:
      toMetricResults in interface MetricProvider
    • getUniverseReady

      public CompletableFuture<Void> getUniverseReady()
      Returns:
      a future that will complete when the universe is ready.
    • getWorldMarkersResourceType

      public ResourceType<ChunkStore, WorldMarkersResource> getWorldMarkersResourceType()
      Returns:
      the resource type shared user world markers
    • isWorldLoadable

      public boolean isWorldLoadable(@Nonnull String name)
      Returns whether the world should be loadable.

      This does not mean the world will successfully load as things like missing plugins could prevent it.

      Parameters:
      name - The name of the world
      Returns:
      whether the world should be loadable
    • addWorld

      @Nonnull @CheckReturnValue public CompletableFuture<World> addWorld(@Nonnull String name)
      Creates a new world with the given name using the default world generation and chunk storage.
      Parameters:
      name - The name of the new world
      Returns:
      A future that will resolve to the created world
    • addWorld

      @Nonnull @Deprecated @CheckReturnValue public CompletableFuture<World> addWorld(@Nonnull String name, @Nullable String generatorType, @Nullable String chunkStorageType)
      Deprecated.
      Does not allow any control over the generator settings
      Creates a new world with the given name using the provided world generator and chunk storage type.
      Parameters:
      name - The name of the new world
      generatorType - The name of the generator to use or null.
      chunkStorageType - The name of the chunk storage to use or null.
      Returns:
      A future that will resolve to the created world
    • validateWorldPath

      public Path validateWorldPath(@Nonnull String name)
    • makeWorld

      @Nonnull @CheckReturnValue public CompletableFuture<World> makeWorld(@Nonnull String name, @Nonnull Path savePath, @Nonnull WorldConfig worldConfig)
      Creates or loads the world with the given name using the provided world configuration.
      Parameters:
      name - The name of the world
      savePath - The location to store the world folder
      worldConfig - The configuration to use for the world
      Returns:
      A future that will resolve to the loaded/created world
    • makeWorld

      @Nonnull @CheckReturnValue public CompletableFuture<World> makeWorld(@Nonnull String name, @Nonnull Path savePath, @Nonnull WorldConfig worldConfig, boolean start)
    • loadWorld

      @Nonnull @CheckReturnValue public CompletableFuture<World> loadWorld(@Nonnull String name)
      Loads a world that was previously created.
      Parameters:
      name - The name of the world to load
      Returns:
      A future that will resolve to the loaded world
    • getWorld

      @Nullable public World getWorld(@Nullable String worldName)
      Called to fetch a specific world instance from the given name.
      Parameters:
      worldName - The name of the world to fetch.
      Returns:
      the World instance, or null if no world with the given name exists.
    • getWorld

      @Nullable public World getWorld(@Nonnull UUID uuid)
      Called to fetch a specific world instance from the given UUID.
      Parameters:
      uuid - The UUID of the world to fetch.
      Returns:
      the World instance, or null if no world with the given UUID exists.
    • getDefaultWorld

      @Nullable public World getDefaultWorld()
      Returns:
      the default World instance of this universe, or null if no default world is set.
    • removeWorld

      public boolean removeWorld(@Nonnull String name)
      Fires a RemoveWorldEvent, which, if not cancelled, allows the world to then be removed from this Universe.
      Parameters:
      name - The name of the world.
      Returns:
      true if the world was removed, false if the RemoveWorldEvent was cancelled.
      See Also:
    • removeWorldExceptionally

      public void removeWorldExceptionally(@Nonnull String name, Map<UUID, PlayerRef> players)
      Remove a world with the knowledge that this removal was caused by an exception causing the thread to crash. This allows us to make sure we aren't attempting to call anything extra on the world, and that it might not be in a normal state.
      Parameters:
      name - The name of the world to remove.
      See Also:
    • getPath

      @Nonnull public Path getPath()
      Returns:
      the path to the universe directory.
    • getWorldsPath

      public Path getWorldsPath()
      Returns:
      the path to the worlds folder within the universe directory.
    • getWorldsDeletedPath

      public Path getWorldsDeletedPath()
      Returns:
      the path to (temporarily) deleted worlds, moved before they are completely erased from disk.
    • getWorlds

      @Nonnull public Map<String,World> getWorlds()
      Returns:
      the map of worlds in this universe.
    • getPlayers

      @Nonnull public Collection<PlayerRef> getPlayers()
      Returns:
      a list of PlayerRef components for all connected players.
    • getPlayer

      @Nullable public PlayerRef getPlayer(@Nonnull UUID uuid)
      Called to fetch the PlayerRef component of a specific player based on their UUID.
      Parameters:
      uuid - The UUID of the player to fetch.
      Returns:
      the PlayerRef instance, or null if no player with the given UUID is connected.
    • getPlayer

      @Nullable public PlayerRef getPlayer(@Nonnull String value, @Nonnull NameMatching matching)
      Called to fetch the PlayerRef component of a specific player based on their display name.
      Parameters:
      value - The display name of the player to fetch.
      matching - The name matching strategy to use.
      Returns:
      the PlayerRef instance, or null if no player with the given name is connected.
    • getPlayer

      @Nullable public PlayerRef getPlayer(@Nonnull String value, @Nonnull Comparator<String> comparator, @Nonnull BiPredicate<String,String> equality)
      Called to fetch the PlayerRef component of a specific player based on their display name.
      Parameters:
      value - The display name of the player to fetch.
      comparator - The comparator to use.
      equality - The equality predicate to use.
      Returns:
      the PlayerRef instance, or null if no player with the given name is connected.
    • getPlayerByUsername

      @Nullable public PlayerRef getPlayerByUsername(@Nonnull String value, @Nonnull NameMatching matching)
      Called to fetch the PlayerRef component of a specific player based on their username.
      Parameters:
      value - The username of the player to fetch.
      matching - The name matching strategy to use.
      Returns:
      the PlayerRef instance, or null if no player with the given username is connected.
    • getPlayerByUsername

      @Nullable public PlayerRef getPlayerByUsername(@Nonnull String value, @Nonnull Comparator<String> comparator, @Nonnull BiPredicate<String,String> equality)
      Called to fetch the PlayerRef component of a specific player based on their username.
      Parameters:
      value - The username of the player to fetch.
      comparator - The comparator to use.
      equality - The equality predicate to use.
      Returns:
      the PlayerRef instance, or null if no player with the given username is connected.
    • getPlayerCount

      public int getPlayerCount()
      Returns:
      the count of connected players.
    • addPlayer

      @Nonnull public CompletableFuture<PlayerRef> addPlayer(@Nonnull ChannelConnection channel, @Nonnull String language, @Nonnull ProtocolVersion protocolVersion, @Nonnull PlayerAuthentication auth, int clientViewRadiusChunks)
      Called to add a new player to the universe.
      Parameters:
      channel - The channel of the client connecting.
      language - The language of the client.
      protocolVersion - The protocol version of the client.
      auth - The PlayerAuthentication data for the player.
      clientViewRadiusChunks - The view radius of the client in chunks.
      Returns:
      a future that will complete with the PlayerRef of the added player.
    • removePlayer

      public void removePlayer(@Nonnull PlayerRef playerRef)
      Called to remove a player from the universe, via their player ref
      Parameters:
      playerRef - The PlayerRef instance of the player to remove
    • resetPlayer

      @Nonnull public CompletableFuture<PlayerRef> resetPlayer(@Nonnull PlayerRef oldPlayer)
      Called to fetch a completed future that will reset the given player.
      Parameters:
      oldPlayer - The old PlayerRef component of the player to reset.
      Returns:
      a future that will complete with the new PlayerRef of the reset player.
    • resetPlayer

      @Nonnull public CompletableFuture<PlayerRef> resetPlayer(@Nonnull PlayerRef oldPlayer, @Nonnull Holder<EntityStore> holder)
      Called to fetch a completed future that will reset the given player.
      Parameters:
      oldPlayer - The old PlayerRef component of the player to reset.
      holder - The EntityStore holder to use for the new player.
      Returns:
      a future that will complete with the new PlayerRef of the reset player.
    • transferPlayerAsync

      @Nonnull public static CompletableFuture<PlayerRef> transferPlayerAsync(@Nonnull PlayerRef playerRef, @Nonnull World originalWorld, @Nonnull CompletableFuture<World> targetWorld, @Nonnull Function<World, CompletableFuture<Transform>> spawnResolver)
      Transfers a player to a world that may still be loading.

      Removes the player from originalWorld asynchronously, waits up to 30 seconds for targetWorld to complete, then places the player at the transform returned by spawnResolver. A spawnResolver that completes with null uses the world's default spawn point.

      The returned future completes with the player's new PlayerRef on success, or exceptionally if the target world times out or the player cannot be added. Callers must attach a CompletableFuture.whenComplete(BiConsumer) handler to implement fallback behaviour (e.g. returning the player to originalWorld, sending them to the default world, or disconnecting them).

      Parameters:
      playerRef - The player to transfer.
      originalWorld - The world the player is currently in; used as the async executor for removal.
      targetWorld - A future resolving to the destination world.
      spawnResolver - Resolves the spawn transform given the destination world; may complete with null.
      Returns:
      a future completing with the PlayerRef on success, or exceptionally on failure.
    • resetPlayer

      @Nonnull public CompletableFuture<PlayerRef> resetPlayer(@Nonnull PlayerRef playerRef, @Nonnull Holder<EntityStore> holder, @Nullable World world, @Nullable Transform transform)
      Called to fetch a completed future that will reset the given player.
      Parameters:
      playerRef - The old PlayerRef component of the player to reset.
      holder - The EntityStore holder to use for the new player.
      world - The world to move the player to, or null to use their current world.
      transform - The transform to move the player to, or null to use their current position.
      Returns:
      a future that will complete with the new PlayerRef of the reset player.
    • sendMessage

      public void sendMessage(@Nonnull Message message)
      Description copied from interface: IMessageReceiver
      Called to send a message to this receiver.
      Specified by:
      sendMessage in interface IMessageReceiver
      Parameters:
      message - The message to be sent.
    • broadcastPacket

      public void broadcastPacket(@Nonnull ToClientPacket packet)
      Called to broadcast the given packet to all players in the universe.
      Parameters:
      packet - The packet to broadcast.
    • broadcastPacketNoCache

      public void broadcastPacketNoCache(@Nonnull ToClientPacket packet)
      Called to broadcast the given packet to all players in the universe without caching it.
      Parameters:
      packet - The packet to broadcast.
    • broadcastPacket

      public void broadcastPacket(@Nonnull ToClientPacket... packets)
      Called to broadcast the given packets to all players in the universe.
      Parameters:
      packets - The array of packets to broadcast.
    • getStorageManager

      public StorageManager getStorageManager()
    • getPlayerStorage

      public PlayerStorage getPlayerStorage()
      Returns:
      the PlayerStorage of this universe.
    • setPlayerStorage

      public void setPlayerStorage(@Nonnull PlayerStorage playerStorage)
      Called to set the PlayerStorage of this universe.
      Parameters:
      playerStorage - The player storage to set.
    • getWorldConfigProvider

      public WorldConfigProvider getWorldConfigProvider()
      Returns:
      the WorldConfigProvider of this universe.
    • getPlayerRefComponentType

      @Nonnull public ComponentType<EntityStore, PlayerRef> getPlayerRefComponentType()
      Returns:
      the component type for the PlayerRef component.
    • getLegacyBlockIdMap

      @Nonnull @Deprecated public static Map<Integer,String> getLegacyBlockIdMap()
      Deprecated.
    • getWorldGenPath

      public static Path getWorldGenPath()