pub struct ContainerHandleRef { /* private fields */ }Expand description
A handle to a container that may be open.
This does not close the container when it’s dropped. See ContainerHandle
if that behavior is desired.
Implementations§
Source§impl ContainerHandleRef
impl ContainerHandleRef
pub fn new(id: i32, client: Client) -> Self
pub fn close(&self)
Sourcepub fn id(&self) -> i32
pub fn id(&self) -> i32
Get the ID of the container.
If this is 0, that means it’s the player’s inventory. Otherwise, the number isn’t really meaningful since only one container can be open at a time.
Returns the menu of the container.
If the container is closed, this will return None.
Note that any modifications you make to the Menu you’re given will not
actually cause any packets to be sent. If you’re trying to modify your
inventory, use Self::click instead
Sourcepub fn contents(&self) -> Option<Vec<ItemStack>>
pub fn contents(&self) -> Option<Vec<ItemStack>>
Returns the item slots in the container, not including the player’s inventory.
If the container is closed, this will return None.
Examples found in repository?
42async fn steal(bot: Client, state: State) -> anyhow::Result<()> {
43 {
44 let mut is_stealing = state.is_stealing.lock();
45 if *is_stealing {
46 bot.chat("Already stealing");
47 return Ok(());
48 }
49 *is_stealing = true;
50 }
51
52 state.checked_chests.lock().clear();
53
54 loop {
55 let chest_block = bot
56 .world()
57 .read()
58 .find_blocks(bot.position(), &BlockKind::Chest.into())
59 .find(
60 // find the closest chest that hasn't been checked
61 |block_pos| !state.checked_chests.lock().contains(block_pos),
62 );
63 let Some(chest_block) = chest_block else {
64 break;
65 };
66
67 state.checked_chests.lock().push(chest_block);
68
69 bot.goto(RadiusGoal::new(chest_block.center(), 3.)).await;
70
71 let Some(chest) = bot.open_container_at(chest_block).await else {
72 println!("Couldn't open chest at {chest_block:?}");
73 continue;
74 };
75
76 println!("Getting contents of chest at {chest_block:?}");
77 for (index, slot) in chest.contents().unwrap_or_default().iter().enumerate() {
78 println!("Checking slot {index}: {slot:?}");
79 let ItemStack::Present(item) = slot else {
80 continue;
81 };
82 if item.kind == ItemKind::Diamond {
83 println!("clicking slot ^");
84 chest.click(QuickMoveClick::Left { slot: index as u16 });
85 }
86 }
87 }
88
89 bot.chat("Done");
90
91 *state.is_stealing.lock() = false;
92
93 Ok(())
94}Sourcepub fn slots(&self) -> Option<Vec<ItemStack>>
pub fn slots(&self) -> Option<Vec<ItemStack>>
Return the contents of the menu, including the player’s inventory.
If the container is closed, this will return None.
Sourcepub fn title(&self) -> Option<FormattedText>
pub fn title(&self) -> Option<FormattedText>
Returns the title of the container, or None if no container is open.
let inventory = bot.get_inventory();
let inventory_title = inventory.title().unwrap_or_default().to_string();
// would be true if an unnamed chest is open
assert_eq!(inventory_title, "Chest");Sourcepub fn left_click(&self, slot: impl Into<usize>)
pub fn left_click(&self, slot: impl Into<usize>)
A shortcut for Self::click with PickupClick::Left.
Sourcepub fn shift_click(&self, slot: impl Into<usize>)
pub fn shift_click(&self, slot: impl Into<usize>)
A shortcut for Self::click with QuickMoveClick::Left.
Sourcepub fn right_click(&self, slot: impl Into<usize>)
pub fn right_click(&self, slot: impl Into<usize>)
A shortcut for Self::click with PickupClick::Right.
Sourcepub fn click(&self, operation: impl Into<ClickOperation>)
pub fn click(&self, operation: impl Into<ClickOperation>)
Simulate a click in the container and send the packet to perform the action.
Examples found in repository?
42async fn steal(bot: Client, state: State) -> anyhow::Result<()> {
43 {
44 let mut is_stealing = state.is_stealing.lock();
45 if *is_stealing {
46 bot.chat("Already stealing");
47 return Ok(());
48 }
49 *is_stealing = true;
50 }
51
52 state.checked_chests.lock().clear();
53
54 loop {
55 let chest_block = bot
56 .world()
57 .read()
58 .find_blocks(bot.position(), &BlockKind::Chest.into())
59 .find(
60 // find the closest chest that hasn't been checked
61 |block_pos| !state.checked_chests.lock().contains(block_pos),
62 );
63 let Some(chest_block) = chest_block else {
64 break;
65 };
66
67 state.checked_chests.lock().push(chest_block);
68
69 bot.goto(RadiusGoal::new(chest_block.center(), 3.)).await;
70
71 let Some(chest) = bot.open_container_at(chest_block).await else {
72 println!("Couldn't open chest at {chest_block:?}");
73 continue;
74 };
75
76 println!("Getting contents of chest at {chest_block:?}");
77 for (index, slot) in chest.contents().unwrap_or_default().iter().enumerate() {
78 println!("Checking slot {index}: {slot:?}");
79 let ItemStack::Present(item) = slot else {
80 continue;
81 };
82 if item.kind == ItemKind::Diamond {
83 println!("clicking slot ^");
84 chest.click(QuickMoveClick::Left { slot: index as u16 });
85 }
86 }
87 }
88
89 bot.chat("Done");
90
91 *state.is_stealing.lock() = false;
92
93 Ok(())
94}Trait Implementations§
Auto Trait Implementations§
impl Freeze for ContainerHandleRef
impl !RefUnwindSafe for ContainerHandleRef
impl Send for ContainerHandleRef
impl Sync for ContainerHandleRef
impl Unpin for ContainerHandleRef
impl !UnwindSafe for ContainerHandleRef
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.