Botcraft 1.21.4
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 /// @return Success if placement attempt was made (and confirmed by the server if wait_confirmation is true), Failure otherwise
104 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);
105
106 /// @brief Same thing as PlaceBlock, but reads its parameters from the blackboard
107 /// @param client The client performing the action
108 /// @return Success if placement attempt was made (and confirmed by the server if wait_confirmation is true), Failure otherwise
109 Status PlaceBlockBlackboard(BehaviourClient& client);
110
111
112 /// @brief Search for food item in the inventory and eat it
113 /// @param client The client performing the action
114 /// @param food_name The item to eat
115 /// @param wait_confirmation If true, waits for the eaten stack to be reduced by 1 before returning Success
116 /// @return Success if the item was eaten (and confirmed if wait_confirmation is true), Failure otherwise
117 Status Eat(BehaviourClient& client, const std::string& food_name, const bool wait_confirmation = true);
118
119 /// @brief Same thing as Eat, but reads its parameters from the blackboard
120 /// @param client The client performing the action
121 /// @return Success if the item was eaten (and confirmed if wait_confirmation is true), Failure otherwise
122 Status EatBlackboard(BehaviourClient& client);
123
124
125 /// @brief Open a container at a given position
126 /// @param client The client performing the action
127 /// @param pos The position of the container
128 /// @return Success if the container is opened, Failure otherwise
129 Status OpenContainer(BehaviourClient& client, const Position& pos);
130
131 /// @brief Same thing as OpenContainer, but reads its parameters from the blackboard
132 /// @param client The client performing the action
133 /// @return Success if the container is opened, Failure otherwise
134 Status OpenContainerBlackboard(BehaviourClient& client);
135
136
137 /// @brief Close an opened container
138 /// @param client The client performing the action
139 /// @param container_id The id of the container to close, if -1, will close the first one found
140 /// @return Always return Success
141 Status CloseContainer(BehaviourClient& client, const short container_id = -1);
142
143 /// @brief Same thing as CloseContainer, but reads its parameters from the blackboard
144 /// @param client The client performing the action
145 /// @return Always return Success
146 Status CloseContainerBlackboard(BehaviourClient& client);
147
148
149 /// @brief Log all the inventory content at given log level
150 /// @param client The client performing the action
151 /// @param level Desired log level
152 /// @return Always return Success
153 Status LogInventoryContent(BehaviourClient& client, const LogLevel level = LogLevel::Info);
154
155 /// @brief Same thing as LogInventoryContent, but reads its parameters from the blackboard
156 /// @param client The client performing the action
157 /// @return Always return Success
158 Status LogInventoryContentBlackboard(BehaviourClient& client);
159
160
161#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
162 /// @brief Buy or sell an item, assuming a trading window is currently opened.
163 /// @param client The client performing the action
164 /// @param item_id Id of the item to buy/sell
165 /// @param buy If true, the item is bought, otherwise is sold
166 /// @param trade_id If > -1, specify which trade we want to use in the list
167 /// (useful when the villager sells multiple variants of the same item like
168 /// enchanted books or bows)
169 /// @return Success if the exchange went sucessfully, Failure otherwise
170 Status Trade(BehaviourClient& client, const int item_id, const bool buy, const int trade_id = -1);
171
172 /// @brief Same thing as Trade, but reads its parameters from the blackboard
173 /// @param client The client performing the action
174 /// @return Success if the exchange went sucessfully, Failure otherwise
175 Status TradeBlackboard(BehaviourClient& client);
176
177
178 /// @brief Buy or sell an item, assuming a trading window is currently opened.
179 /// @param client The client performing the action
180 /// @param item_name Item to buy/sell
181 /// @param buy If true, the item is bought, otherwise is sold
182 /// @param trade_id If > -1, specify which trade we want to use in the list
183 /// (useful when the villager sells multiple variants of the same item like
184 /// enchanted books or bows)
185 /// @return Success if the exchange went sucessfully, Failure otherwise
186 Status TradeName(BehaviourClient& client, const std::string& item_name, const bool buy, const int trade_id = -1);
187
188 /// @brief Same thing as TradeName, but reads its parameters from the blackboard
189 /// @param client The client performing the action
190 /// @return Success if the exchange went sucessfully, Failure otherwise
191 Status TradeNameBlackboard(BehaviourClient& client);
192#endif
193
194
195 /// @brief Put item in a crafting container and click on the output, storing it in the inventory.
196 /// @param client The client performing the action
197 /// @param inputs Input items IDs in a 3x3 grid, inputs[0][1] refers to first line, second column
198 /// @param allow_inventory_craft If true, the client will use the inventory small 2x2 grid to craft if possible
199 /// @return Success if item is crafted, Failure otherwise
200 Status Craft(BehaviourClient& client, const std::array<std::array<ItemId, 3>, 3>& inputs, const bool allow_inventory_craft = true);
201
202
203 /// @brief Same thing as Craft, but reads its parameters from the blackboard
204 /// @param client The client performing the action
205 /// @return Success if item is crafted, Failure otherwise
206 Status CraftBlackboard(BehaviourClient& client);
207
208
209 /// @brief Put item in a crafting container and click on the output, storing it in the inventory.
210 /// @param client The client performing the action
211 /// @param inputs Input items names in a 3x3 grid, inputs[0][1] refers to first line, second column
212 /// @param allow_inventory_craft If true, the client will use the inventory small 2x2 grid to craft if possible
213 /// @return Success if item is crafted, Failure otherwise
214 Status CraftNamed(BehaviourClient& client, const std::array<std::array<std::string, 3>, 3>& inputs, const bool allow_inventory_craft = true);
215
216 /// @brief Same thing as CraftNamed, but reads its parameters from the blackboard
217 /// @param client The client performing the action
218 /// @return Success if item is crafted, Failure otherwise
219 Status CraftNamedBlackboard(BehaviourClient& client);
220
221
222 /// @brief Check if item_id is present in inventory
223 /// @param client The client performing the action
224 /// @param item_id Item id
225 /// @param quantity Min quantity to have
226 /// @return Success if inventory quantity is >= quantity else Failure
227 Status HasItemIdInInventory(BehaviourClient& client, const ItemId item_id, const int quantity = 1);
228
229 /// @brief Same thing as HasItemIdInInventory, but reads its parameters from the blackboard
230 /// @param client The client performing the action
231 /// @return Success if inventory quantity is >= quantity else Failure
232 Status HasItemIdInInventoryBlackboard(BehaviourClient& client);
233
234 /// @brief Check if item_name is present in inventory
235 /// @param client The client performing the action
236 /// @param item_name Item name
237 /// @param quantity Min quantity to have
238 /// @return Success if inventory quantity is >= quantity else Failure
239 Status HasItemInInventory(BehaviourClient& client, const std::string& item_name, const int quantity = 1);
240
241 /// @brief Same thing as HasItemInInventory, 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 HasItemInInventoryBlackboard(BehaviourClient& client);
245
246
247 /// @brief Clean the inventory stacking same items together
248 /// @param client The client performing the action
249 /// @return Success if no operation failed, Failure otherwise
250 Status SortInventory(BehaviourClient& client);
251}
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 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 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)
Try to place the item at given pos.
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.