Skip to main content

Overview

The Server struct represents a Minecraft server instance and provides methods for managing worlds, players, commands, permissions, and server state.

Server Structure

pub struct Server {
    pub basic_config: BasicConfiguration,
    pub advanced_config: AdvancedConfiguration,
    pub plugin_manager: Arc<PluginManager>,
    pub permission_manager: Arc<RwLock<PermissionManager>>,
    pub command_dispatcher: RwLock<CommandDispatcher>,
    pub block_registry: Arc<BlockRegistry>,
    pub item_registry: Arc<ItemRegistry>,
    pub worlds: ArcSwap<Vec<Arc<World>>>,
    pub dimensions: Vec<Dimension>,
    // ... additional fields
}

Player Management

get_player_by_name

pub fn get_player_by_name(&self, name: &str) -> Option<Arc<Player>>
Searches for a player by username across all worlds.
name
&str
required
Username of the player to search for
returns
Option<Arc<Player>>
Player if found, or None

get_player_by_uuid

pub fn get_player_by_uuid(&self, id: uuid::Uuid) -> Option<Arc<Player>>
Searches for a player by UUID across all worlds.
id
uuid::Uuid
required
UUID of the player to search for
returns
Option<Arc<Player>>
Player if found, or None

get_all_players

pub fn get_all_players(&self) -> Vec<Arc<Player>>
Returns all players from all worlds.
returns
Vec<Arc<Player>>
Vector of all connected players

get_player_count

pub fn get_player_count(&self) -> usize
Counts the total number of players across all worlds.
returns
usize
Total number of connected players

get_random_player

pub fn get_random_player(&self) -> Option<Arc<Player>>
Returns a random player from any world, or None if all worlds are empty.
returns
Option<Arc<Player>>
Random player, or None if no players online

World Management

get_world_from_dimension

pub fn get_world_from_dimension(&self, dimension: &Dimension) -> Arc<World>
Gets the world instance for a specific dimension.
dimension
&Dimension
required
Dimension type (OVERWORLD, THE_NETHER, or THE_END)
returns
Arc<World>
World instance for the dimension
Example:
use pumpkin_data::dimension::Dimension;

let overworld = server.get_world_from_dimension(&Dimension::OVERWORLD);
let nether = server.get_world_from_dimension(&Dimension::THE_NETHER);

Broadcasting

broadcast_packet_all

pub async fn broadcast_packet_all<P: ClientPacket>(&self, packet: &P)
Broadcasts a packet to all players in all worlds.
packet
&P: ClientPacket
required
Packet to broadcast
Example:
use pumpkin_protocol::java::client::play::CDisconnect;

server.broadcast_packet_all(&CDisconnect::new(
    TextComponent::text("Server restarting")
)).await;

broadcast_message

pub async fn broadcast_message(
    &self,
    message: &TextComponent,
    sender_name: &TextComponent,
    chat_type: u8,
    target_name: Option<&TextComponent>,
)
Broadcasts a message to all players.
message
&TextComponent
required
Message to broadcast
sender_name
&TextComponent
required
Name of the sender
chat_type
u8
required
Chat type (0 = chat, 1 = system, etc.)
target_name
Option<&TextComponent>
Optional target name for directed messages

Difficulty Management

get_difficulty

pub fn get_difficulty(&self) -> Difficulty
Gets the current difficulty of the server.
returns
Difficulty
Current difficulty (Peaceful, Easy, Normal, Hard)

set_difficulty

pub async fn set_difficulty(&self, difficulty: Difficulty, force_update: bool)
Sets the difficulty of the server.
difficulty
Difficulty
required
New difficulty level
force_update
bool
required
If true, updates even if difficulty is locked
Example:
use pumpkin_util::Difficulty;

server.set_difficulty(Difficulty::Hard, false).await;

Entity Selection

select_entities

pub fn select_entities(
    &self,
    target_selector: &TargetSelector,
    source: Option<&CommandSender>,
) -> Vec<Arc<dyn EntityBase>>
Selects entities based on a target selector (e.g., @a, @e, @p).
target_selector
&TargetSelector
required
Target selector with filters
source
Option<&CommandSender>
Source of the selection (for relative positions)
returns
Vec<Arc<dyn EntityBase>>
Vector of selected entities
Example:
use pumpkin::command::args::entities::*;

let selector = TargetSelector {
    selector_type: EntitySelectorType::AllPlayers,
    conditions: vec![],
};
let players = server.select_entities(&selector, None);

Performance Metrics

get_tps

pub fn get_tps(&self) -> f64
Returns the current Ticks Per Second (TPS).
returns
f64
Current TPS value

get_mspt

pub fn get_mspt(&self) -> f64
Returns the average Milliseconds Per Tick (MSPT).
returns
f64
Average MSPT

get_average_tick_time_nanos

pub fn get_average_tick_time_nanos(&self) -> i64
Gets the rolling average tick time over the last 100 ticks, in nanoseconds.
returns
i64
Average tick time in nanoseconds

Task Management

spawn_task

pub fn spawn_task<F>(&self, task: F) -> JoinHandle<F::Output>
where
    F: Future + Send + 'static,
    F::Output: Send + 'static,
Spawns a task associated with the server. Tasks are awaited when the server stops.
task
F: Future + Send + 'static
required
Async task to spawn
returns
JoinHandle<F::Output>
Handle to the spawned task
Example:
server.spawn_task(async {
    tokio::time::sleep(Duration::from_secs(5)).await;
    println!("Task completed!");
});

Accessing Server from Context

// In event handlers or command executors
let server = &context.server;

// Get all players
let players = server.get_all_players();

// Broadcast a message
server.broadcast_message(
    &TextComponent::text("Server announcement"),
    &TextComponent::text("Server"),
    1,
    None
).await;

// Check TPS
let tps = server.get_tps();
if tps < 15.0 {
    println!("Warning: Low TPS detected: {}", tps);
}

Build docs developers (and LLMs) love