Skip to content

Commit 608ccb8

Browse files
committed
fix panic on bot disconnect
1 parent a5cb21f commit 608ccb8

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

azalea-entity/src/plugin/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ mod relative_updates;
44
use std::collections::HashSet;
55

66
use azalea_block::{fluid_state::FluidKind, BlockState};
7-
use azalea_core::position::{BlockPos, ChunkPos, Vec3};
7+
use azalea_core::{
8+
position::{BlockPos, ChunkPos, Vec3},
9+
tick::GameTick,
10+
};
811
use azalea_world::{InstanceContainer, InstanceName, MinecraftEntityId};
912
use bevy_app::{App, Plugin, PreUpdate, Update};
1013
use bevy_ecs::prelude::*;
@@ -59,7 +62,7 @@ impl Plugin for EntityPlugin {
5962
),
6063
)
6164
.add_systems(Update, update_bounding_box)
62-
.add_systems(PreUpdate, update_in_loaded_chunk)
65+
.add_systems(GameTick, update_in_loaded_chunk)
6366
.init_resource::<EntityUuidIndex>();
6467
}
6568
}
@@ -215,6 +218,7 @@ pub fn update_in_loaded_chunk(
215218
for (entity, instance_name, position) in &query {
216219
let player_chunk_pos = ChunkPos::from(position);
217220
let Some(instance_lock) = instance_container.get(instance_name) else {
221+
commands.entity(entity).remove::<InLoadedChunk>();
218222
continue;
219223
};
220224

azalea-physics/src/fluids.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn update_in_water_state_and_do_fluid_pushing(
2323
for (mut physics, position, instance_name) in &mut query {
2424
let world_lock = instance_container
2525
.get(instance_name)
26-
.expect("All entities should be in a valid world");
26+
.expect("All entities with InLoadedChunk should be in a valid world");
2727
let world = world_lock.read();
2828

2929
physics.water_fluid_height = 0.;

azalea-physics/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ pub fn ai_step(
109109
if !physics.is_in_lava()
110110
|| physics.on_ground() && fluid_height <= fluid_jump_threshold
111111
{
112-
if physics.on_ground()
113-
|| in_water
114-
&& fluid_height <= fluid_jump_threshold
115-
&& physics.no_jump_delay == 0
112+
if (physics.on_ground() || in_water && fluid_height <= fluid_jump_threshold)
113+
&& physics.no_jump_delay == 0
116114
{
117115
jump_from_ground(
118116
&mut physics,

azalea-physics/tests/physics.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,49 @@ fn test_negative_coordinates_weird_wall_collision() {
363363
let entity_pos = app.world_mut().get::<Position>(entity).unwrap();
364364
assert_eq!(entity_pos.y, 70.5);
365365
}
366+
367+
#[test]
368+
fn spawn_and_unload_world() {
369+
let mut app = make_test_app();
370+
let world_lock = app.world_mut().resource_mut::<InstanceContainer>().insert(
371+
ResourceLocation::new("minecraft:overworld"),
372+
384,
373+
-64,
374+
);
375+
let mut partial_world = PartialInstance::default();
376+
377+
partial_world.chunks.set(
378+
&ChunkPos { x: -1, z: -1 },
379+
Some(Chunk::default()),
380+
&mut world_lock.write().chunks,
381+
);
382+
let _entity = app
383+
.world_mut()
384+
.spawn((
385+
EntityBundle::new(
386+
Uuid::nil(),
387+
Vec3 {
388+
x: -7.5,
389+
y: 73.,
390+
z: -7.5,
391+
},
392+
azalea_registry::EntityKind::Player,
393+
ResourceLocation::new("minecraft:overworld"),
394+
),
395+
MinecraftEntityId(0),
396+
LocalEntity,
397+
))
398+
.id();
399+
400+
// do a tick
401+
app.world_mut().run_schedule(GameTick);
402+
app.update();
403+
404+
// now unload the partial_world and world_lock
405+
drop(partial_world);
406+
drop(world_lock);
407+
408+
// do another tick
409+
app.world_mut().run_schedule(GameTick);
410+
app.update();
411+
}

0 commit comments

Comments
 (0)