Overview
The World struct represents a Minecraft dimension (Overworld, Nether, End) and provides methods for managing chunks, blocks, entities, and world state.
World Structure
pub struct World {
pub uuid: Uuid,
pub level: Arc<Level>,
pub players: ArcSwap<Vec<Arc<Player>>>,
pub entities: ArcSwap<Vec<Arc<dyn EntityBase>>>,
pub scoreboard: Mutex<Scoreboard>,
pub worldborder: Mutex<Worldborder>,
pub level_time: Mutex<LevelTime>,
pub dimension: Dimension,
pub sea_level: i32,
pub min_y: i32,
pub weather: Mutex<Weather>,
pub block_registry: Arc<BlockRegistry>,
// ... additional fields
}
Block Operations
get_block
pub async fn get_block(&self, pos: &BlockPos) -> &'static Block
Gets the block at a position.
Example:
use pumpkin_util::math::position::BlockPos;
use pumpkin_util::math::vector3::Vector3;
let pos = BlockPos::new(0, 64, 0);
let block = world.get_block(&pos).await;
println!("Block: {:?}", block);
get_block_state
pub async fn get_block_state(&self, pos: &BlockPos) -> &'static BlockState
Gets the block state at a position (includes block properties).
Block state at the position
set_block
pub async fn set_block(
&self,
pos: BlockPos,
block_state_id: BlockStateId,
flags: BlockFlags,
) -> Result<(), ()>
Sets a block at a position.
Flags controlling update behavior (NOTIFY_NEIGHBORS, SEND_TO_CLIENT, etc.)
Ok if successful, Err if failed
Example:
use pumpkin_data::Block;
use pumpkin_world::world::BlockFlags;
let stone_state_id = Block::STONE.default_state_id;
world.set_block(
BlockPos::new(0, 64, 0),
stone_state_id,
BlockFlags::NOTIFY_NEIGHBORS | BlockFlags::SEND_TO_CLIENT
).await?;
get_top_block
pub async fn get_top_block(&self, position: Vector2<i32>) -> i32
Gets the Y position of the first non-air block from the top down.
Y position of the top block
get_motion_blocking_height
pub async fn get_motion_blocking_height(&self, x: i32, z: i32) -> i32
Gets the MOTION_BLOCKING heightmap value for a given XZ position.
Height value from motion blocking heightmap
Player Operations
get_player_by_name
pub fn get_player_by_name(&self, name: &str) -> Option<Arc<Player>>
Finds a player by name in this world.
Player if found in this world
get_player_by_uuid
pub fn get_player_by_uuid(&self, id: uuid::Uuid) -> Option<Arc<Player>>
Finds a player by UUID in this world.
Player if found in this world
Broadcasting
broadcast_packet_all
pub async fn broadcast_packet_all<P: ClientPacket>(&self, packet: &P)
Broadcasts a packet to all players in this world.
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 in this world.
Sound and Particles
play_sound
pub async fn play_sound(
&self,
sound: Sound,
category: SoundCategory,
position: &Vector3<f64>,
)
Plays a sound at a position.
Sound category (MASTER, BLOCKS, etc.)
Position to play sound at
spawn_particle
pub async fn spawn_particle(
&self,
position: Vector3<f64>,
offset: Vector3<f32>,
max_speed: f32,
particle_count: i32,
particle: Particle,
)
Spawns particles at a position.
Offset for particle spread
Number of particles to spawn
Example:
use pumpkin_data::particle::Particle;
use pumpkin_util::math::vector3::Vector3;
world.spawn_particle(
Vector3::new(0.0, 64.0, 0.0),
Vector3::new(0.5, 0.5, 0.5),
0.1,
50,
Particle::Heart
).await;
Entity Management
spawn_entity
pub async fn spawn_entity(&self, entity: Arc<dyn EntityBase>)
Spawns an entity in the world.
entity
Arc<dyn EntityBase>
required
Entity to spawn
Example:
use pumpkin::entity::Entity;
use pumpkin_data::entity::EntityType;
let entity = Entity::new(
world.clone(),
Vector3::new(0.0, 64.0, 0.0),
&EntityType::ZOMBIE
);
world.spawn_entity(Arc::new(entity)).await;
Collision Detection
get_block_collisions
pub async fn get_block_collisions(
&self,
bounding_box: BoundingBox,
entity: &dyn EntityBase,
) -> (Vec<BoundingBox>, Vec<(usize, BlockPos)>)
Gets all block collision boxes intersecting with a bounding box.
Entity performing the check
returns
(Vec<BoundingBox>, Vec<(usize, BlockPos)>)
Tuple of collision boxes and their positions
is_space_empty
pub async fn is_space_empty(&self, bounding_box: BoundingBox) -> bool
Checks if a space is empty (no collisions).
true if space is empty, false if collisions exist
Difficulty
set_difficulty
pub fn set_difficulty(&self, difficulty: Difficulty)
Sets the difficulty for this world.
Difficulty level (Peaceful, Easy, Normal, Hard)
Example: Creating Explosion
use pumpkin::world::explosion::Explosion;
use pumpkin_util::math::vector3::Vector3;
let explosion = Explosion::new(
world.clone(),
None, // No entity source
Vector3::new(0.0, 64.0, 0.0),
4.0, // Power
true, // Causes fire
Explosion::DEFAULT_EXPLOSION
);
explosion.explode().await;
Example: Custom Block Placement
use pumpkin_data::Block;
use pumpkin_world::world::BlockFlags;
// Place a diamond block
let pos = BlockPos::new(100, 64, 100);
world.set_block(
pos,
Block::DIAMOND_BLOCK.default_state_id,
BlockFlags::NOTIFY_NEIGHBORS | BlockFlags::SEND_TO_CLIENT
).await?;
// Play placement sound
world.play_block_sound(
Sound::BlockStonePlace,
SoundCategory::Blocks,
pos
).await;