refactor: from async to sync Part 1
This commit is contained in:
1 parent
26f91f0b0f
commit
8ee11b06ce
485 files changed
+24441
-28048
No files matched your search
@@ -6,9 +6,8 @@ use crate::command::CommandSender;
|
||||
use crate::entity::EntityBase;
|
||||
use crate::{
|
||||
block::{
|
||||
BlockBehaviour, BlockFuture, BlockMetadata, CanPlaceAtArgs, NormalUseArgs,
|
||||
OnNeighborUpdateArgs, OnPlaceArgs, OnScheduledTickArgs, PlacedArgs,
|
||||
registry::BlockActionResult,
|
||||
BlockBehaviour, BlockMetadata, CanPlaceAtArgs, NormalUseArgs, OnNeighborUpdateArgs,
|
||||
OnPlaceArgs, OnScheduledTickArgs, PlacedArgs, registry::BlockActionResult,
|
||||
},
|
||||
server::Server,
|
||||
world::World,
|
||||
@@ -30,7 +29,7 @@ impl CommandBlock {
|
||||
dir: Facing,
|
||||
) -> Option<(BlockPos, CommandBlockLikeProperties)> {
|
||||
let target_pos = pos.offset(dir.to_block_direction().to_offset());
|
||||
let block = world.get_block(&target_pos);
|
||||
let (block, state_id) = world.get_block_and_state_id(&target_pos);
|
||||
|
||||
let allowed_blocks = [
|
||||
Block::COMMAND_BLOCK.name,
|
||||
@@ -41,7 +40,6 @@ impl CommandBlock {
|
||||
return None;
|
||||
}
|
||||
|
||||
let state_id = world.get_block_state_id(&target_pos);
|
||||
let props = CommandBlockLikeProperties::from_state_id(state_id, block);
|
||||
|
||||
Some((target_pos, props))
|
||||
@@ -161,7 +159,7 @@ impl CommandBlock {
|
||||
if !command_blocks_work {
|
||||
return;
|
||||
}
|
||||
let block = world.get_block(&pos);
|
||||
let (block, state_id) = world.get_block_and_state_id(&pos);
|
||||
|
||||
if block.id != Block::CHAIN_COMMAND_BLOCK.id {
|
||||
break;
|
||||
@@ -178,13 +176,16 @@ impl CommandBlock {
|
||||
};
|
||||
let powered = command_entity.powered.load(Ordering::Relaxed);
|
||||
let auto = command_entity.auto.load(Ordering::Relaxed);
|
||||
let state_id = world.get_block_state_id(&pos);
|
||||
let props = CommandBlockLikeProperties::from_state_id(state_id, block);
|
||||
|
||||
if powered || auto {
|
||||
let conditions_met = Self::conditions_met(&world, &pos, direction);
|
||||
if conditions_met {
|
||||
let command = command_entity.command.lock().await;
|
||||
let command = command_entity
|
||||
.command
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone();
|
||||
let Some(entity) = world.get_block_entity(&pos) else {
|
||||
warn!("Command block entity disappeared during execution");
|
||||
break;
|
||||
@@ -220,16 +221,14 @@ impl BlockMetadata for CommandBlock {
|
||||
}
|
||||
|
||||
impl BlockBehaviour for CommandBlock {
|
||||
fn on_place<'a>(&'a self, args: OnPlaceArgs<'a>) -> BlockFuture<'a, BlockStateId> {
|
||||
Box::pin(async move {
|
||||
let mut props = CommandBlockLikeProperties::default(args.block);
|
||||
props.facing = args.player.get_entity().get_facing().opposite();
|
||||
props.to_state_id(args.block)
|
||||
})
|
||||
fn on_place(&self, args: OnPlaceArgs<'_>) -> BlockStateId {
|
||||
let mut props = CommandBlockLikeProperties::default(args.block);
|
||||
props.facing = args.player.get_entity().get_facing().opposite();
|
||||
props.to_state_id(args.block)
|
||||
}
|
||||
|
||||
fn normal_use<'a>(&'a self, args: NormalUseArgs<'a>) -> BlockFuture<'a, BlockActionResult> {
|
||||
Box::pin(async move {
|
||||
fn normal_use(&self, args: NormalUseArgs<'_>) -> BlockActionResult {
|
||||
{
|
||||
if args.player.permission_lvl.load() < PermissionLvl::Two {
|
||||
return BlockActionResult::Pass;
|
||||
}
|
||||
@@ -238,11 +237,11 @@ impl BlockBehaviour for CommandBlock {
|
||||
};
|
||||
args.world.update_block_entity(&block_entity);
|
||||
BlockActionResult::SuccessServer
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn on_neighbor_update<'a>(&'a self, args: OnNeighborUpdateArgs<'a>) -> BlockFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
fn on_neighbor_update(&self, args: OnNeighborUpdateArgs<'_>) {
|
||||
{
|
||||
let command_blocks_work =
|
||||
{ args.world.level_info.load().game_rules.command_blocks_work };
|
||||
if !command_blocks_work {
|
||||
@@ -264,64 +263,68 @@ impl BlockBehaviour for CommandBlock {
|
||||
args.block,
|
||||
command_entity,
|
||||
args.position,
|
||||
block_receives_redstone_power(args.world, args.position).await,
|
||||
block_receives_redstone_power(args.world, args.position),
|
||||
);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn on_scheduled_tick<'a>(&'a self, args: OnScheduledTickArgs<'a>) -> BlockFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
let command_blocks_work =
|
||||
{ args.world.level_info.load().game_rules.command_blocks_work };
|
||||
if !command_blocks_work {
|
||||
return;
|
||||
}
|
||||
let Some(block_entity) = args.world.get_block_entity(args.position) else {
|
||||
return;
|
||||
};
|
||||
if block_entity.resource_location() != CommandBlockEntity::ID {
|
||||
return;
|
||||
}
|
||||
fn on_scheduled_tick(&self, args: OnScheduledTickArgs<'_>) {
|
||||
let command_blocks_work = { args.world.level_info.load().game_rules.command_blocks_work };
|
||||
if !command_blocks_work {
|
||||
return;
|
||||
}
|
||||
let Some(block_entity) = args.world.get_block_entity(args.position) else {
|
||||
return;
|
||||
};
|
||||
if block_entity.resource_location() != CommandBlockEntity::ID {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(command_entity) = block_entity.as_any().downcast_ref::<CommandBlockEntity>()
|
||||
let Some(command_entity) = block_entity.as_any().downcast_ref::<CommandBlockEntity>()
|
||||
else {
|
||||
warn!("Block entity at {} is not a command block", args.position);
|
||||
return;
|
||||
};
|
||||
let Some(server) = args.world.server.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let props = CommandBlockLikeProperties::from_state_id(
|
||||
args.world.get_block_state_id(args.position),
|
||||
args.block,
|
||||
);
|
||||
|
||||
let world = args.world.clone();
|
||||
let entity_clone = block_entity.clone();
|
||||
let position = *args.position;
|
||||
let facing = props.facing;
|
||||
tokio::spawn(async move {
|
||||
let Some(command_entity) = entity_clone.as_any().downcast_ref::<CommandBlockEntity>()
|
||||
else {
|
||||
warn!("Block entity at {} is not a command block", args.position);
|
||||
return;
|
||||
};
|
||||
let Some(server) = args.world.server.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let props = CommandBlockLikeProperties::from_state_id(
|
||||
args.world.get_block_state_id(args.position),
|
||||
args.block,
|
||||
);
|
||||
|
||||
Self::execute(
|
||||
&server,
|
||||
args.world.clone(),
|
||||
block_entity.clone(),
|
||||
&command_entity.command.lock().await,
|
||||
)
|
||||
.await;
|
||||
|
||||
let command = command_entity
|
||||
.command
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
.clone();
|
||||
Self::execute(&server, world.clone(), entity_clone, &command).await;
|
||||
Self::chain_execute(
|
||||
&server,
|
||||
args.world.clone(),
|
||||
args.position
|
||||
.offset(props.facing.to_block_direction().to_offset()),
|
||||
props.facing,
|
||||
world,
|
||||
position.offset(facing.to_block_direction().to_offset()),
|
||||
facing,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
|
||||
let block = args.world.get_block(args.position);
|
||||
let is_auto = command_entity.auto.load(Ordering::Relaxed);
|
||||
let can_run = command_entity.powered.load(Ordering::Relaxed) || is_auto;
|
||||
if block == &Block::REPEATING_COMMAND_BLOCK && can_run {
|
||||
args.world
|
||||
.schedule_block_tick(block, *args.position, 1, TickPriority::Normal);
|
||||
}
|
||||
})
|
||||
let block = args.world.get_block(args.position);
|
||||
let is_auto = command_entity.auto.load(Ordering::Relaxed);
|
||||
let can_run = command_entity.powered.load(Ordering::Relaxed) || is_auto;
|
||||
if block == &Block::REPEATING_COMMAND_BLOCK && can_run {
|
||||
args.world
|
||||
.schedule_block_tick(block, *args.position, 1, TickPriority::Normal);
|
||||
}
|
||||
}
|
||||
|
||||
fn can_place_at(&self, args: CanPlaceAtArgs<'_>) -> bool {
|
||||
@@ -334,8 +337,8 @@ impl BlockBehaviour for CommandBlock {
|
||||
false
|
||||
}
|
||||
|
||||
fn placed<'a>(&'a self, args: PlacedArgs<'a>) -> BlockFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
fn placed(&self, args: PlacedArgs<'_>) {
|
||||
{
|
||||
let send_command_feedback = {
|
||||
let game_rules = &args.world.level_info.load().game_rules;
|
||||
game_rules.send_command_feedback
|
||||
@@ -347,14 +350,11 @@ impl BlockBehaviour for CommandBlock {
|
||||
args.block.id == Block::CHAIN_COMMAND_BLOCK.id,
|
||||
);
|
||||
args.world.add_block_entity(Arc::new(entity));
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn get_comparator_output<'a>(
|
||||
&'a self,
|
||||
args: crate::block::GetComparatorOutputArgs<'a>,
|
||||
) -> BlockFuture<'a, Option<u8>> {
|
||||
Box::pin(async {
|
||||
fn get_comparator_output(&self, args: crate::block::GetComparatorOutputArgs<'_>) -> Option<u8> {
|
||||
{
|
||||
let entity = args.world.get_block_entity(args.position);
|
||||
|
||||
entity.map_or_else(
|
||||
@@ -368,6 +368,6 @@ impl BlockBehaviour for CommandBlock {
|
||||
command_block_entity.map(|e| e.success_count.load(Ordering::Acquire) as u8)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user