fix stop command

This commit is contained in:
Alexander Medvedev 2025-01-28 13:52:15 +01:00
parent 6c4eb88dd3
commit f63034e3a1
6 changed files with 65 additions and 82 deletions

View File

@ -1,9 +1 @@
pub mod entity_registry; pub mod entity_registry;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum FacingDirection {
North,
South,
East,
West,
}

View File

@ -88,11 +88,9 @@ impl Level {
pub async fn save(&self) { pub async fn save(&self) {
log::info!("Saving level..."); log::info!("Saving level...");
// lets first save all chunks
for chunk in self.loaded_chunks.iter() { // chunks are automatically saved when all players get removed
let chunk = chunk.read().await;
self.clean_chunk(&chunk.position).await;
}
// then lets save the world info // then lets save the world info
self.world_info_writer self.world_info_writer
.write_world_info(self.level_info.clone(), &self.level_folder) .write_world_info(self.level_info.clone(), &self.level_folder)

View File

@ -3,12 +3,9 @@ use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait; use async_trait::async_trait;
use pumpkin_protocol::server::play::SUseItemOn; use pumpkin_protocol::server::play::SUseItemOn;
use pumpkin_util::math::position::BlockPos; use pumpkin_util::math::position::BlockPos;
use pumpkin_world::{ use pumpkin_world::block::{
block::{ block_registry::{Block, BLOCKS},
block_registry::{Block, BLOCKS}, BlockFace,
BlockFace,
},
entity::FacingDirection,
}; };
use crate::world::World; use crate::world::World;
@ -24,7 +21,7 @@ pub trait BlockBehavior: Send + Sync {
face: &BlockFace, face: &BlockFace,
block_pos: &BlockPos, block_pos: &BlockPos,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
player_direction: &FacingDirection, player_direction: &Direction,
) -> u16; ) -> u16;
async fn is_updateable( async fn is_updateable(
&self, &self,
@ -39,6 +36,7 @@ pub trait BlockBehavior: Send + Sync {
pub enum BlockProperty { pub enum BlockProperty {
Waterlogged(bool), Waterlogged(bool),
Facing(Direction), Facing(Direction),
Powered(bool),
SlabType(SlabPosition), SlabType(SlabPosition),
StairShape(StairShape), StairShape(StairShape),
Half(BlockHalf), // Add other properties as needed Half(BlockHalf), // Add other properties as needed
@ -66,7 +64,7 @@ pub enum StairShape {
OuterRight, OuterRight,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub enum Direction { pub enum Direction {
North, North,
South, South,
@ -110,7 +108,7 @@ impl BlockPropertiesManager {
face: &BlockFace, face: &BlockFace,
block_pos: &BlockPos, block_pos: &BlockPos,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
player_direction: &FacingDirection, player_direction: &Direction,
) -> u16 { ) -> u16 {
if let Some(behaviour) = self.properties_registry.get(&block.id) { if let Some(behaviour) = self.properties_registry.get(&block.id) {
return behaviour return behaviour

View File

@ -5,11 +5,11 @@ use std::{
use pumpkin_protocol::server::play::SUseItemOn; use pumpkin_protocol::server::play::SUseItemOn;
use pumpkin_util::math::position::BlockPos; use pumpkin_util::math::position::BlockPos;
use pumpkin_world::block::block_registry::Block;
use pumpkin_world::block::{block_registry::Property, BlockFace}; use pumpkin_world::block::{block_registry::Property, BlockFace};
use pumpkin_world::{block::block_registry::Block, entity::FacingDirection};
use crate::{ use crate::{
block::block_properties_manager::{get_property_key, BlockBehavior, BlockProperty}, block::block_properties_manager::{get_property_key, BlockBehavior, BlockProperty, Direction},
world::World, world::World,
}; };
@ -75,21 +75,21 @@ impl SlabBehavior {
pub fn evalute_property_type( pub fn evalute_property_type(
block: &Block, block: &Block,
clicked_block: &Block, clicked_block: &Block,
face: BlockFace, face: &BlockFace,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
) -> String { ) -> String {
if block.id == clicked_block.id && face == BlockFace::Top { if block.id == clicked_block.id && face == &BlockFace::Top {
return format!("{}{}", "type", "double"); return format!("{}{}", "type", "double");
} }
if face == BlockFace::Top { if face == &BlockFace::Top {
return format!("{}{}", "type", "bottom"); return format!("{}{}", "type", "bottom");
} }
if face == BlockFace::North if face == &BlockFace::North
|| face == BlockFace::South || face == &BlockFace::South
|| face == BlockFace::West || face == &BlockFace::West
|| face == BlockFace::East || face == &BlockFace::East
{ {
let y_pos = use_item_on.cursor_pos.y; let y_pos = use_item_on.cursor_pos.y;
if y_pos > 0.5 { if y_pos > 0.5 {
@ -119,7 +119,7 @@ impl BlockBehavior for SlabBehavior {
face: &BlockFace, face: &BlockFace,
block_pos: &BlockPos, block_pos: &BlockPos,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
_player_direction: &FacingDirection, _player_direction: &Direction,
) -> u16 { ) -> u16 {
let clicked_block = world.get_block(block_pos).await.unwrap(); let clicked_block = world.get_block(block_pos).await.unwrap();
let mut hmap_key: Vec<String> = Vec::with_capacity(block.properties.len()); let mut hmap_key: Vec<String> = Vec::with_capacity(block.properties.len());
@ -129,7 +129,7 @@ impl BlockBehavior for SlabBehavior {
let state = match get_property_key(property.name.as_str()).expect("Property not found") let state = match get_property_key(property.name.as_str()).expect("Property not found")
{ {
BlockProperty::SlabType(_) => { BlockProperty::SlabType(_) => {
Self::evalute_property_type(block, clicked_block, *face, use_item_on) Self::evalute_property_type(block, clicked_block, face, use_item_on)
} }
BlockProperty::Waterlogged(false) => Self::evalute_property_waterlogged(block), BlockProperty::Waterlogged(false) => Self::evalute_property_waterlogged(block),
_ => panic!("Property not found"), _ => panic!("Property not found"),

View File

@ -6,13 +6,10 @@ use std::{
use pumpkin_protocol::server::play::SUseItemOn; use pumpkin_protocol::server::play::SUseItemOn;
use pumpkin_util::math::{position::BlockPos, vector3::Vector3}; use pumpkin_util::math::{position::BlockPos, vector3::Vector3};
use pumpkin_world::block::block_registry::Block; use pumpkin_world::block::block_registry::Block;
use pumpkin_world::{ use pumpkin_world::block::{block_registry::Property, BlockFace};
block::{block_registry::Property, BlockFace},
entity::FacingDirection,
};
use crate::{ use crate::{
block::block_properties_manager::{get_property_key, BlockBehavior, BlockProperty}, block::block_properties_manager::{get_property_key, BlockBehavior, BlockProperty, Direction},
world::World, world::World,
}; };
@ -85,11 +82,11 @@ impl StairBehavior {
} }
fn calculate_positions( fn calculate_positions(
player_direction: FacingDirection, player_direction: &Direction,
block_pos: BlockPos, block_pos: &BlockPos,
) -> (BlockPos, BlockPos) { ) -> (BlockPos, BlockPos) {
match player_direction { match player_direction {
FacingDirection::North => ( Direction::North => (
BlockPos(Vector3::new( BlockPos(Vector3::new(
block_pos.0.x, block_pos.0.x,
block_pos.0.y, block_pos.0.y,
@ -101,7 +98,7 @@ impl StairBehavior {
block_pos.0.z + 1, block_pos.0.z + 1,
)), )),
), ),
FacingDirection::South => ( Direction::South => (
BlockPos(Vector3::new( BlockPos(Vector3::new(
block_pos.0.x, block_pos.0.x,
block_pos.0.y, block_pos.0.y,
@ -113,7 +110,7 @@ impl StairBehavior {
block_pos.0.z - 1, block_pos.0.z - 1,
)), )),
), ),
FacingDirection::East => ( Direction::East => (
BlockPos(Vector3::new( BlockPos(Vector3::new(
block_pos.0.x + 1, block_pos.0.x + 1,
block_pos.0.y, block_pos.0.y,
@ -125,7 +122,7 @@ impl StairBehavior {
block_pos.0.z, block_pos.0.z,
)), )),
), ),
FacingDirection::West => ( Direction::West => (
BlockPos(Vector3::new( BlockPos(Vector3::new(
block_pos.0.x - 1, block_pos.0.x - 1,
block_pos.0.y, block_pos.0.y,
@ -145,11 +142,11 @@ impl StairBehavior {
block_pos: &BlockPos, block_pos: &BlockPos,
face: &BlockFace, face: &BlockFace,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
player_direction: &FacingDirection, player_direction: &Direction,
) -> String { ) -> String {
let block_half = Self::evaluate_property_half(*face, use_item_on); let block_half = Self::evaluate_property_half(face, use_item_on);
let (front_block_pos, back_block_pos) = let (front_block_pos, back_block_pos) =
Self::calculate_positions(*player_direction, *block_pos); Self::calculate_positions(player_direction, block_pos);
let front_block_and_state = world.get_block_and_block_state(&front_block_pos).await; let front_block_and_state = world.get_block_and_block_state(&front_block_pos).await;
let back_block_and_state = world.get_block_and_block_state(&back_block_pos).await; let back_block_and_state = world.get_block_and_block_state(&back_block_pos).await;
@ -169,18 +166,18 @@ impl StairBehavior {
let is_facing_south = properties.contains(&"facingsouth".to_owned()); let is_facing_south = properties.contains(&"facingsouth".to_owned());
let is_facing_east = properties.contains(&"facingeast".to_owned()); let is_facing_east = properties.contains(&"facingeast".to_owned());
if (is_facing_north && *player_direction == FacingDirection::West) if (is_facing_north && *player_direction == Direction::West)
|| (is_facing_west && *player_direction == FacingDirection::South) || (is_facing_west && *player_direction == Direction::South)
|| (is_facing_south && *player_direction == FacingDirection::East) || (is_facing_south && *player_direction == Direction::East)
|| (is_facing_east && *player_direction == FacingDirection::North) || (is_facing_east && *player_direction == Direction::North)
{ {
return "shapeouter_right".to_owned(); return "shapeouter_right".to_owned();
} }
if (is_facing_north && *player_direction == FacingDirection::East) if (is_facing_north && *player_direction == Direction::East)
|| (is_facing_west && *player_direction == FacingDirection::North) || (is_facing_west && *player_direction == Direction::North)
|| (is_facing_south && *player_direction == FacingDirection::West) || (is_facing_south && *player_direction == Direction::West)
|| (is_facing_east && *player_direction == FacingDirection::South) || (is_facing_east && *player_direction == Direction::South)
{ {
return "shapeouter_left".to_owned(); return "shapeouter_left".to_owned();
} }
@ -210,18 +207,18 @@ impl StairBehavior {
let is_facing_south = properties.contains(&"facingsouth".to_owned()); let is_facing_south = properties.contains(&"facingsouth".to_owned());
let is_facing_east = properties.contains(&"facingeast".to_owned()); let is_facing_east = properties.contains(&"facingeast".to_owned());
if (is_facing_north && *player_direction == FacingDirection::West) if (is_facing_north && *player_direction == Direction::West)
|| (is_facing_west && *player_direction == FacingDirection::South) || (is_facing_west && *player_direction == Direction::South)
|| (is_facing_south && *player_direction == FacingDirection::East) || (is_facing_south && *player_direction == Direction::East)
|| (is_facing_east && *player_direction == FacingDirection::North) || (is_facing_east && *player_direction == Direction::North)
{ {
return "shapeinner_right".to_owned(); return "shapeinner_right".to_owned();
} }
if (is_facing_north && *player_direction == FacingDirection::East) if (is_facing_north && *player_direction == Direction::East)
|| (is_facing_west && *player_direction == FacingDirection::North) || (is_facing_west && *player_direction == Direction::North)
|| (is_facing_south && *player_direction == FacingDirection::West) || (is_facing_south && *player_direction == Direction::West)
|| (is_facing_east && *player_direction == FacingDirection::South) || (is_facing_east && *player_direction == Direction::South)
{ {
return "shapeinner_left".to_owned(); return "shapeinner_left".to_owned();
} }
@ -250,24 +247,24 @@ impl StairBehavior {
format!("{}{}", "waterlogged", "false") format!("{}{}", "waterlogged", "false")
} }
pub fn evaluate_property_facing(face: BlockFace, player_direction: FacingDirection) -> String { pub fn evaluate_property_facing(face: &BlockFace, player_direction: &Direction) -> String {
let facing = match face { let facing = match face {
BlockFace::North => "south", BlockFace::North => "south",
BlockFace::South => "north", BlockFace::South => "north",
BlockFace::East => "west", BlockFace::East => "west",
BlockFace::West => "east", BlockFace::West => "east",
BlockFace::Top | BlockFace::Bottom => match player_direction { BlockFace::Top | BlockFace::Bottom => match player_direction {
FacingDirection::North => "north", Direction::North => "north",
FacingDirection::South => "south", Direction::South => "south",
FacingDirection::East => "east", Direction::East => "east",
FacingDirection::West => "west", Direction::West => "west",
}, },
}; };
format!("facing{facing}") format!("facing{facing}")
} }
pub fn evaluate_property_half(face: BlockFace, use_item_on: &SUseItemOn) -> String { pub fn evaluate_property_half(face: &BlockFace, use_item_on: &SUseItemOn) -> String {
match face { match face {
BlockFace::Top => format!("{}{}", "half", "bottom"), BlockFace::Top => format!("{}{}", "half", "bottom"),
BlockFace::Bottom => format!("{}{}", "half", "top"), BlockFace::Bottom => format!("{}{}", "half", "top"),
@ -292,7 +289,7 @@ impl BlockBehavior for StairBehavior {
face: &BlockFace, face: &BlockFace,
block_pos: &BlockPos, block_pos: &BlockPos,
use_item_on: &SUseItemOn, use_item_on: &SUseItemOn,
player_direction: &FacingDirection, player_direction: &Direction,
) -> u16 { ) -> u16 {
let mut hmap_key: Vec<String> = Vec::with_capacity(block.properties.len()); let mut hmap_key: Vec<String> = Vec::with_capacity(block.properties.len());
let stair_behaviour = Self::get(); let stair_behaviour = Self::get();
@ -300,10 +297,8 @@ impl BlockBehavior for StairBehavior {
for property in &block.properties { for property in &block.properties {
let state = match get_property_key(property.name.as_str()).expect("Property not found") let state = match get_property_key(property.name.as_str()).expect("Property not found")
{ {
BlockProperty::Facing(_) => { BlockProperty::Facing(_) => Self::evaluate_property_facing(face, player_direction),
Self::evaluate_property_facing(*face, *player_direction) BlockProperty::Half(_) => Self::evaluate_property_half(face, use_item_on),
}
BlockProperty::Half(_) => Self::evaluate_property_half(*face, use_item_on),
BlockProperty::StairShape(_) => { BlockProperty::StairShape(_) => {
Self::evaluate_property_shape( Self::evaluate_property_shape(
world, world,
@ -315,7 +310,7 @@ impl BlockBehavior for StairBehavior {
.await .await
} }
BlockProperty::Waterlogged(_) => Self::evaluate_property_waterlogged(block), BlockProperty::Waterlogged(_) => Self::evaluate_property_waterlogged(block),
BlockProperty::SlabType(_) => panic!("SlabType BlockProperty invalid for Stairs"), _ => panic!("BlockProperty invalid for Stairs"),
}; };
hmap_key.push(state); hmap_key.push(state);
} }

View File

@ -2,6 +2,7 @@ use std::num::NonZeroU8;
use std::sync::Arc; use std::sync::Arc;
use crate::block::block_manager::BlockActionResult; use crate::block::block_manager::BlockActionResult;
use crate::block::block_properties_manager::Direction;
use crate::entity::mob; use crate::entity::mob;
use crate::net::PlayerConfig; use crate::net::PlayerConfig;
use crate::{ use crate::{
@ -43,7 +44,6 @@ use pumpkin_util::{
}; };
use pumpkin_world::block::block_registry::get_block_collision_shapes; use pumpkin_world::block::block_registry::get_block_collision_shapes;
use pumpkin_world::block::block_registry::Block; use pumpkin_world::block::block_registry::Block;
use pumpkin_world::entity::FacingDirection;
use pumpkin_world::item::item_registry::get_item_by_id; use pumpkin_world::item::item_registry::get_item_by_id;
use pumpkin_world::item::ItemStack; use pumpkin_world::item::ItemStack;
use pumpkin_world::{ use pumpkin_world::{
@ -1145,15 +1145,15 @@ impl Player {
Ok(true) Ok(true)
} }
fn get_player_direction(&self) -> FacingDirection { fn get_player_direction(&self) -> Direction {
let adjusted_yaw = (self.living_entity.entity.yaw.load() % 360.0 + 360.0) % 360.0; // Normalize yaw to [0, 360) let adjusted_yaw = (self.living_entity.entity.yaw.load() % 360.0 + 360.0) % 360.0; // Normalize yaw to [0, 360)
match adjusted_yaw { match adjusted_yaw {
0.0..=45.0 | 315.0..=360.0 => FacingDirection::South, 0.0..=45.0 | 315.0..=360.0 => Direction::South,
45.0..=135.0 => FacingDirection::West, 45.0..=135.0 => Direction::West,
135.0..=225.0 => FacingDirection::North, 135.0..=225.0 => Direction::North,
225.0..=315.0 => FacingDirection::East, 225.0..=315.0 => Direction::East,
_ => FacingDirection::South, // Default case, should not occur _ => Direction::South, // Default case, should not occur
} }
} }