aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2025-02-21 22:50:19 +0000
committermat <git@matdoes.dev>2025-02-21 22:50:39 +0000
commit27945c8870c832970dfe15b9bf9f567c0ad96ea4 (patch)
tree08c6598d52ac379505ec556ffb7d40aec5dcb3d8 /azalea/examples
parentf5f15362f2cb48088eb8ccf80988f994fecd81c9 (diff)
downloadazalea-drasl-27945c8870c832970dfe15b9bf9f567c0ad96ea4.tar.xz
despawn entities when switching worlds and some testbot fixes
Diffstat (limited to 'azalea/examples')
-rw-r--r--azalea/examples/testbot/commands/debug.rs2
-rw-r--r--azalea/examples/testbot/main.rs31
2 files changed, 20 insertions, 13 deletions
diff --git a/azalea/examples/testbot/commands/debug.rs b/azalea/examples/testbot/commands/debug.rs
index a7f15d2b..10b9711d 100644
--- a/azalea/examples/testbot/commands/debug.rs
+++ b/azalea/examples/testbot/commands/debug.rs
@@ -197,6 +197,8 @@ pub fn register(commands: &mut CommandDispatcher<Mutex<CommandSource>>) {
.unwrap();
}
+ writeln!(report).unwrap();
+
for (info, _) in ecs.iter_resources() {
writeln!(report, "Resource: {}", info.name()).unwrap();
writeln!(report, "- Size: {} bytes", info.layout().size()).unwrap();
diff --git a/azalea/examples/testbot/main.rs b/azalea/examples/testbot/main.rs
index 97340c55..958c17d0 100644
--- a/azalea/examples/testbot/main.rs
+++ b/azalea/examples/testbot/main.rs
@@ -57,14 +57,18 @@ async fn main() {
Account::offline(username_or_email)
};
- let mut commands = CommandDispatcher::new();
- register_commands(&mut commands);
-
- builder = builder.add_account_with_state(account, State::new(args.clone(), commands));
+ builder = builder.add_account_with_state(account, State::new());
}
+ let mut commands = CommandDispatcher::new();
+ register_commands(&mut commands);
+
builder
.join_delay(Duration::from_millis(100))
+ .set_swarm_state(SwarmState {
+ args,
+ commands: Arc::new(commands),
+ })
.start(join_address)
.await
.unwrap();
@@ -102,17 +106,13 @@ pub enum BotTask {
#[derive(Component, Clone, Default)]
pub struct State {
- pub args: Args,
- pub commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
pub killaura: bool,
pub task: Arc<Mutex<BotTask>>,
}
impl State {
- fn new(args: Args, commands: CommandDispatcher<Mutex<CommandSource>>) -> Self {
+ fn new() -> Self {
Self {
- args,
- commands: Arc::new(commands),
killaura: true,
task: Arc::new(Mutex::new(BotTask::None)),
}
@@ -120,9 +120,14 @@ impl State {
}
#[derive(Resource, Default, Clone)]
-struct SwarmState;
+struct SwarmState {
+ pub args: Args,
+ pub commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
+}
async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Result<()> {
+ let swarm = bot.resource::<SwarmState>();
+
match event {
azalea::Event::Init => {
bot.set_client_information(ClientInformation {
@@ -130,7 +135,7 @@ async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Resu
..Default::default()
})
.await?;
- if state.args.pathfinder_debug_particles {
+ if swarm.args.pathfinder_debug_particles {
bot.ecs
.lock()
.entity_mut(bot.entity)
@@ -141,7 +146,7 @@ async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Resu
let (Some(username), content) = chat.split_sender_and_content() else {
return Ok(());
};
- if username != state.args.owner_username {
+ if username != swarm.args.owner_username {
return Ok(());
}
@@ -153,7 +158,7 @@ async fn handle(bot: Client, event: azalea::Event, state: State) -> anyhow::Resu
content.strip_prefix('!').map(|s| s.to_owned())
};
if let Some(command) = command {
- match state.commands.execute(
+ match swarm.commands.execute(
command,
Mutex::new(CommandSource {
bot: bot.clone(),