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.
Username of the player to search for
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.
UUID of the player to search for
get_all_players
pub fn get_all_players(&self) -> Vec<Arc<Player>>
Returns all players from all worlds.
Vector of all connected players
get_player_count
pub fn get_player_count(&self) -> usize
Counts the total number of players across all worlds.
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.
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 type (OVERWORLD, THE_NETHER, or THE_END)
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.
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.
Chat type (0 = chat, 1 = system, etc.)
Optional target name for directed messages
Difficulty Management
get_difficulty
pub fn get_difficulty(&self) -> Difficulty
Gets the current difficulty of the server.
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.
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 with filters
Source of the selection (for relative positions)
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);
get_tps
pub fn get_tps(&self) -> f64
Returns the current Ticks Per Second (TPS).
get_mspt
pub fn get_mspt(&self) -> f64
Returns the average Milliseconds Per Tick (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.
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
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);
}