Botcraft 1.21.5
Loading...
Searching...
No Matches
InventoryTasks.hpp
Go to the documentation of this file.
1#pragma once
2
8
9namespace Botcraft
10{
11 class BehaviourClient;
12
13 /// @brief Perform a click action on a container
14 /// @param client The client performing the action
15 /// @param container_id Container id
16 /// @param slot_id Clicked slot id
17 /// @param click_type Type of click (same as in ServerboundContainerClickPacket)
18 /// @param button_num Button clicked (same as in ServerboundContainerClickPacket)
19 /// @return Success if the slots is clicked (for versions < 1.17 and the server confirms it), Failure otherwise
20 Status ClickSlotInContainer(BehaviourClient& client, const short container_id, const short slot_id, const int click_type, const char button_num);
21
22 /// @brief Same thing as ClickSlotInContainer, but reads its parameters from the blackboard
23 /// @param client The client performing the action
24 /// @return Success if the slots is clicked (for versions < 1.17 and the server confirms it), Failure otherwise
25 Status ClickSlotInContainerBlackboard(BehaviourClient& client);
26
27
28 /// @brief Swap two slots in a given container
29 /// @param client The client performing the action
30 /// @param container_id Container ID
31 /// @param first_slot First slot index
32 /// @param second_slot Second slot index
33 /// @return Success if the two slots have been correctly swapped, Failure otherwise
34 Status SwapItemsInContainer(BehaviourClient& client, const short container_id, const short first_slot, const short second_slot);
35
36 /// @brief Same thing as SwapItemsInContainer, but reads its parameters from the blackboard
37 /// @param client The client performing the action
38 /// @return Success if the two slots have been correctly swapped, Failure otherwise
39 Status SwapItemsInContainerBlackboard(BehaviourClient& client);
40
41
42 /// @brief Drop item out of inventory
43 /// @param client The client performing the action
44 /// @param container_id Container ID
45 /// @param slot_id Slot ID
46 /// @param num_to_keep Number of items to keep in the input slot
47 /// @return Success if items were correctly dropped, Failure otherwise
48 Status DropItemsFromContainer(BehaviourClient& client, const short container_id, const short slot_id, const short num_to_keep = 0);
49
50 /// @brief Same thing as DropItemsFromContainer, but reads its parameters from the blackboard
51 /// @param client The client performing the action
52 /// @return Success if items were correctly dropped, Failure otherwise
53 Status DropItemsFromContainerBlackboard(BehaviourClient& client);
54
55
56 /// @brief Take one item from source_slot, and put it on destination_slot
57 /// @param client The client performing the action
58 /// @param container_id Container ID
59 /// @param source_slot Slot from which the item is taken
60 /// @param destination_slot Slot receiving the item
61 /// @return Success if the item is correctly set, Failure otherwise
62 Status PutOneItemInContainerSlot(BehaviourClient& client, const short container_id, const short source_slot, const short destination_slot);
63
64 /// @brief Same thing as PutOneItemInContainerSlot, but reads its parameters from the blackboard
65 /// @param client The client performing the action
66 /// @return Success if the item is correctly set, Failure otherwise
67 Status PutOneItemInContainerSlotBlackboard(BehaviourClient& client);
68
69
70 /// @brief Try to set a given item in the given hand
71 /// @param client The client performing the action
72 /// @param item_id Item id to place in hand
73 /// @param hand Left or right hand
74 /// @return Success if the item is now in hand, Failure otherwise
75 Status SetItemIdInHand(BehaviourClient& client, const ItemId item_id, const Hand hand = Hand::Right);
76
77 /// @brief Same thing as SetItemIdInHand, but reads its parameters from the blackboard
78 /// @param client The client performing the action
79 /// @return Success if the item is now in hand, Failure otherwise
80 Status SetItemIdInHandBlackboard(BehaviourClient& client);
81
82 /// @brief Try to set a given item in the given hand
83 /// @param client The client performing the action
84 /// @param item_name Item name to place in hand
85 /// @param hand Left or right hand
86 /// @return Success if the item is now in hand, Failure otherwise
87 Status SetItemInHand(BehaviourClient& client, const std::string& item_name, const Hand hand = Hand::Right);
88
89 /// @brief Same thing as SetItemInHand, but reads its parameters from the blackboard
90 /// @param client The client performing the action
91 /// @return Success if the item is now in hand, Failure otherwise
92 Status SetItemInHandBlackboard(BehaviourClient& client);
93
94
95 /// @brief Try to place the item at given pos. If too far, will try
96 /// to pathfind toward the position first.
97 /// @param client The client performing the action
98 /// @param item_name The item to place
99 /// @param pos The location where we want to place the block
100 /// @param face The face on which the block is placed. If not specified the optimal placing face will be automatically detected using the position of the block relative to the player eyes
101 /// @param wait_confirmation If true, waits for the server to send the new block in pos
102 /// @param allow_midair_placing If false, task will fail if the neighbour block matching face is air
103 /// @param allow_pathfinding If true, the bot will try to pathfind toward the block if it's too far. If false the task will return Failure instead in this case.
104 /// @return Success if placement attempt was made (and confirmed by the server if wait_confirmation is true), Failure otherwise
105 Status PlaceBlock(BehaviourClient& client, const std::string& item_name, const Position& pos, std::optional<PlayerDiggingFace> face = std::nullopt, const bool wait_confirmation = false, const bool allow_midair_placing = false, const bool allow_pathfinding = true);
106
107 /// @brief Same thing as PlaceBlock, but reads its parameters from the blackboard
108 /// @param client The client performing the action
109 /// @return Success if placement attempt was made (and confirmed by the server if wait_confirmation is true), Failure otherwise
110 Status PlaceBlockBlackboard(BehaviourClient& client);
111
112
113 /// @brief Search for food item in the inventory and eat it
114 /// @param client The client performing the action
115 /// @param food_name The item to eat
116 /// @param wait_confirmation If true, waits for the eaten stack to be reduced by 1 before returning Success
117 /// @return Success if the item was eaten (and confirmed if wait_confirmation is true), Failure otherwise
118 Status Eat(BehaviourClient& client, const std::string& food_name, const bool wait_confirmation = true);
119
120 /// @brief Same thing as Eat, but reads its parameters from the blackboard
121 /// @param client The client performing the action
122 /// @return Success if the item was eaten (and confirmed if wait_confirmation is true), Failure otherwise
123 Status EatBlackboard(BehaviourClient& client);
124
125
126 /// @brief Open a container at a given position
127 /// @param client The client performing the action
128 /// @param pos The position of the container
129 /// @return Success if the container is opened, Failure otherwise
130 Status OpenContainer(BehaviourClient& client, const Position& pos);
131
132 /// @brief Same thing as OpenContainer, but reads its parameters from the blackboard
133 /// @param client The client performing the action
134 /// @return Success if the container is opened, Failure otherwise
135 Status OpenContainerBlackboard(BehaviourClient& client);
136
137
138 /// @brief Close an opened container
139 /// @param client The client performing the action
140 /// @param container_id The id of the container to close, if -1, will close the first one found
141 /// @return Always return Success
142 Status CloseContainer(BehaviourClient& client, const short container_id = -1);
143
144 /// @brief Same thing as CloseContainer, but reads its parameters from the blackboard
145 /// @param client The client performing the action
146 /// @return Always return Success
147 Status CloseContainerBlackboard(BehaviourClient& client);
148
149
150 /// @brief Log all the inventory content at given log level
151 /// @param client The client performing the action
152 /// @param level Desired log level
153 /// @return Always return Success
154 Status LogInventoryContent(BehaviourClient& client, const LogLevel level = LogLevel::Info);
155
156 /// @brief Same thing as LogInventoryContent, but reads its parameters from the blackboard
157 /// @param client The client performing the action
158 /// @return Always return Success
159 Status LogInventoryContentBlackboard(BehaviourClient& client);
160
161
162#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
163 /// @brief Buy or sell an item, assuming a trading window is currently opened.
164 /// @param client The client performing the action
165 /// @param item_id Id of the item to buy/sell
166 /// @param buy If true, the item is bought, otherwise is sold
167 /// @param trade_id If > -1, specify which trade we want to use in the list
168 /// (useful when the villager sells multiple variants of the same item like
169 /// enchanted books or bows)
170 /// @return Success if the exchange went sucessfully, Failure otherwise
171 Status Trade(BehaviourClient& client, const int item_id, const bool buy, const int trade_id = -1);
172
173 /// @brief Same thing as Trade, but reads its parameters from the blackboard
174 /// @param client The client performing the action
175 /// @return Success if the exchange went sucessfully, Failure otherwise
176 Status TradeBlackboard(BehaviourClient& client);
177
178
179 /// @brief Buy or sell an item, assuming a trading window is currently opened.
180 /// @param client The client performing the action
181 /// @param item_name Item to buy/sell
182 /// @param buy If true, the item is bought, otherwise is sold
183 /// @param trade_id If > -1, specify which trade we want to use in the list
184 /// (useful when the villager sells multiple variants of the same item like
185 /// enchanted books or bows)
186 /// @return Success if the exchange went sucessfully, Failure otherwise
187 Status TradeName(BehaviourClient& client, const std::string& item_name, const bool buy, const int trade_id = -1);
188
189 /// @brief Same thing as TradeName, but reads its parameters from the blackboard
190 /// @param client The client performing the action
191 /// @return Success if the exchange went sucessfully, Failure otherwise
192 Status TradeNameBlackboard(BehaviourClient& client);
193#endif
194
195
196 /// @brief Put item in a crafting container and click on the output, storing it in the inventory.
197 /// @param client The client performing the action
198 /// @param inputs Input items IDs in a 3x3 grid, inputs[0][1] refers to first line, second column
199 /// @param allow_inventory_craft If true, the client will use the inventory small 2x2 grid to craft if possible
200 /// @return Success if item is crafted, Failure otherwise
201 Status Craft(BehaviourClient& client, const std::array<std::array<ItemId, 3>, 3>& inputs, const bool allow_inventory_craft = true);
202
203
204 /// @brief Same thing as Craft, but reads its parameters from the blackboard
205 /// @param client The client performing the action
206 /// @return Success if item is crafted, Failure otherwise
207 Status CraftBlackboard(BehaviourClient& client);
208
209
210 /// @brief Put item in a crafting container and click on the output, storing it in the inventory.
211 /// @param client The client performing the action
212 /// @param inputs Input items names in a 3x3 grid, inputs[0][1] refers to first line, second column
213 /// @param allow_inventory_craft If true, the client will use the inventory small 2x2 grid to craft if possible
214 /// @return Success if item is crafted, Failure otherwise
215 Status CraftNamed(BehaviourClient& client, const std::array<std::array<std::string, 3>, 3>& inputs, const bool allow_inventory_craft = true);
216
217 /// @brief Same thing as CraftNamed, but reads its parameters from the blackboard
218 /// @param client The client performing the action
219 /// @return Success if item is crafted, Failure otherwise
220 Status CraftNamedBlackboard(BehaviourClient& client);
221
222
223 /// @brief Check if item_id is present in inventory
224 /// @param client The client performing the action
225 /// @param item_id Item id
226 /// @param quantity Min quantity to have
227 /// @return Success if inventory quantity is >= quantity else Failure
228 Status HasItemIdInInventory(BehaviourClient& client, const ItemId item_id, const int quantity = 1);
229
230 /// @brief Same thing as HasItemIdInInventory, but reads its parameters from the blackboard
231 /// @param client The client performing the action
232 /// @return Success if inventory quantity is >= quantity else Failure
233 Status HasItemIdInInventoryBlackboard(BehaviourClient& client);
234
235 /// @brief Check if item_name is present in inventory
236 /// @param client The client performing the action
237 /// @param item_name Item name
238 /// @param quantity Min quantity to have
239 /// @return Success if inventory quantity is >= quantity else Failure
240 Status HasItemInInventory(BehaviourClient& client, const std::string& item_name, const int quantity = 1);
241
242 /// @brief Same thing as HasItemInInventory, but reads its parameters from the blackboard
243 /// @param client The client performing the action
244 /// @return Success if inventory quantity is >= quantity else Failure
245 Status HasItemInInventoryBlackboard(BehaviourClient& client);
246
247
248 /// @brief Clean the inventory stacking same items together
249 /// @param client The client performing the action
250 /// @return Success if no operation failed, Failure otherwise
251 Status SortInventory(BehaviourClient& client);
252}
Status PutOneItemInContainerSlot(BehaviourClient &client, const short container_id, const short source_slot, const short destination_slot)
Take one item from source_slot, and put it on destination_slot.
Status HasItemInInventoryBlackboard(BehaviourClient &client)
Same thing as HasItemInInventory, but reads its parameters from the blackboard.
Status Craft(BehaviourClient &client, const std::array< std::array< ItemId, 3 >, 3 > &inputs, const bool allow_inventory_craft=true)
Put item in a crafting container and click on the output, storing it in the inventory.
Status TradeNameBlackboard(BehaviourClient &client)
Same thing as TradeName, but reads its parameters from the blackboard.
Status HasItemIdInInventory(BehaviourClient &client, const ItemId item_id, const int quantity=1)
Check if item_id is present in inventory.
Status TradeBlackboard(BehaviourClient &client)
Same thing as Trade, but reads its parameters from the blackboard.
Status SetItemIdInHandBlackboard(BehaviourClient &client)
Same thing as SetItemIdInHand, but reads its parameters from the blackboard.
Status DropItemsFromContainer(BehaviourClient &client, const short container_id, const short slot_id, const short num_to_keep=0)
Drop item out of inventory.
Status EatBlackboard(BehaviourClient &client)
Same thing as Eat, but reads its parameters from the blackboard.
Status CloseContainer(BehaviourClient &client, const short container_id=-1)
Close an opened container.
Status SetItemInHandBlackboard(BehaviourClient &client)
Same thing as SetItemInHand, but reads its parameters from the blackboard.
Status HasItemInInventory(BehaviourClient &client, const std::string &item_name, const int quantity=1)
Check if item_name is present in inventory.
Status LogInventoryContent(BehaviourClient &client, const LogLevel level=LogLevel::Info)
Log all the inventory content at given log level.
Status SwapItemsInContainer(BehaviourClient &client, const short container_id, const short first_slot, const short second_slot)
Swap two slots in a given container.
Status CraftNamed(BehaviourClient &client, const std::array< std::array< std::string, 3 >, 3 > &inputs, const bool allow_inventory_craft=true)
Put item in a crafting container and click on the output, storing it in the inventory.
Status CraftBlackboard(BehaviourClient &client)
Same thing as Craft, but reads its parameters from the blackboard.
Status ClickSlotInContainerBlackboard(BehaviourClient &client)
Same thing as ClickSlotInContainer, but reads its parameters from the blackboard.
Status HasItemIdInInventoryBlackboard(BehaviourClient &client)
Same thing as HasItemIdInInventory, but reads its parameters from the blackboard.
Status PutOneItemInContainerSlotBlackboard(BehaviourClient &client)
Same thing as PutOneItemInContainerSlot, but reads its parameters from the blackboard.
Vector3< int > Position
Definition Vector3.hpp:282
Status PlaceBlock(BehaviourClient &client, const std::string &item_name, const Position &pos, std::optional< PlayerDiggingFace > face=std::nullopt, const bool wait_confirmation=false, const bool allow_midair_placing=false, const bool allow_pathfinding=true)
Try to place the item at given pos.
Status SortInventory(BehaviourClient &client)
Clean the inventory stacking same items together.
int ItemId
Definition Item.hpp:15
Status SetItemInHand(BehaviourClient &client, const std::string &item_name, const Hand hand=Hand::Right)
Try to set a given item in the given hand.
Status OpenContainer(BehaviourClient &client, const Position &pos)
Open a container at a given position.
Status Eat(BehaviourClient &client, const std::string &food_name, const bool wait_confirmation=true)
Search for food item in the inventory and eat it.
Status ClickSlotInContainer(BehaviourClient &client, const short container_id, const short slot_id, const int click_type, const char button_num)
Perform a click action on a container.
Status DropItemsFromContainerBlackboard(BehaviourClient &client)
Same thing as DropItemsFromContainer, but reads its parameters from the blackboard.
Status LogInventoryContentBlackboard(BehaviourClient &client)
Same thing as LogInventoryContent, but reads its parameters from the blackboard.
Status PlaceBlockBlackboard(BehaviourClient &client)
Same thing as PlaceBlock, but reads its parameters from the blackboard.
Status CloseContainerBlackboard(BehaviourClient &client)
Same thing as CloseContainer, but reads its parameters from the blackboard.
Status OpenContainerBlackboard(BehaviourClient &client)
Same thing as OpenContainer, but reads its parameters from the blackboard.
Status SwapItemsInContainerBlackboard(BehaviourClient &client)
Same thing as SwapItemsInContainer, but reads its parameters from the blackboard.
Status Trade(BehaviourClient &client, const int item_id, const bool buy, const int trade_id=-1)
Buy or sell an item, assuming a trading window is currently opened.
Status TradeName(BehaviourClient &client, const std::string &item_name, const bool buy, const int trade_id=-1)
Buy or sell an item, assuming a trading window is currently opened.
Status SetItemIdInHand(BehaviourClient &client, const ItemId item_id, const Hand hand=Hand::Right)
Try to set a given item in the given hand.
Status CraftNamedBlackboard(BehaviourClient &client)
Same thing as CraftNamed, but reads its parameters from the blackboard.