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