aboutsummaryrefslogtreecommitdiff
path: root/azalea/examples
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-11-18 21:52:09 -0600
committerGitHub <noreply@github.com>2022-11-18 21:52:09 -0600
commit7ad4b227267e3bb2da99ef5d76c30cd54d040157 (patch)
treea0a2fe96b10552b433dd82b632d85cca7b902d86 /azalea/examples
parentad6da947cdd0e3eace84135e054c0bfd0e1351fa (diff)
downloadazalea-drasl-7ad4b227267e3bb2da99ef5d76c30cd54d040157.tar.xz
Add functions to get ChatPacket author and content (#42)
* Add functions to get ChatPacket author and content * add ChatPacket::username and ChatPacket::content
Diffstat (limited to 'azalea/examples')
-rwxr-xr-xazalea/examples/craft_dig_straight_down.rs18
-rwxr-xr-xazalea/examples/echo.rs8
2 files changed, 14 insertions, 12 deletions
diff --git a/azalea/examples/craft_dig_straight_down.rs b/azalea/examples/craft_dig_straight_down.rs
index d6c0ed1c..3e6d9e31 100755
--- a/azalea/examples/craft_dig_straight_down.rs
+++ b/azalea/examples/craft_dig_straight_down.rs
@@ -27,17 +27,17 @@ async fn main() {
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
Event::Chat(m) => {
- if m.username == bot.player.username {
+ if m.username() == Some(bot.game_profile.name) {
return Ok(());
};
- if m.content == "go" {
- // make sure we only start once
- let ctx_lock = ctx.lock().unwrap();
- if ctx_lock.started {
- return;
- };
- ctx_lock.started = true;
- drop(ctx_lock);
+ if m.content() == "go" {
+ {
+ // make sure we only start once
+ if *state.started.lock() {
+ return Ok(());
+ };
+ *state.started.lock() = true;
+ }
bot.goto(pathfinder::Goals::NearXZ(5, azalea::BlockXZ(0, 0)))
.await;
diff --git a/azalea/examples/echo.rs b/azalea/examples/echo.rs
index 51896e53..5f6cf072 100755
--- a/azalea/examples/echo.rs
+++ b/azalea/examples/echo.rs
@@ -24,10 +24,12 @@ pub struct State {}
async fn handle(bot: Client, event: Event, state: State) -> anyhow::Result<()> {
match event {
Event::Chat(m) => {
- if m.username == bot.username {
- return Ok(()); // ignore our own messages
+ if let (Some(sender), content) = m.split_sender_and_content() {
+ if sender == bot.game_profile.name {
+ return Ok(()); // ignore our own messages
+ }
+ bot.chat(&content).await?;
};
- bot.chat(m.content).await;
}
_ => {}
}