Botcraft 1.21.4
Loading...
Searching...
No Matches
InventoryTasks.cpp
Go to the documentation of this file.
6
16
17using namespace ProtocolCraft;
18
19namespace Botcraft
20{
21 Status ClickSlotInContainerImpl(BehaviourClient& client, const short container_id, const short slot_id, const int click_type, const char button_num)
22 {
23 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
24
25 std::shared_ptr<ServerboundContainerClickPacket> click_window_msg = std::make_shared<ServerboundContainerClickPacket>();
26
27 click_window_msg->SetContainerId(static_cast<unsigned char>(container_id));
28 click_window_msg->SetSlotNum(slot_id);
29 click_window_msg->SetButtonNum(button_num);
30 click_window_msg->SetClickType(click_type);
31
32 // ItemStack/CarriedItem, StateId and ChangedSlots will be set in SendInventoryTransaction
33 int transaction_id = client.SendInventoryTransaction(click_window_msg);
34
35 // Wait for the click confirmation (versions < 1.17)
36#if PROTOCOL_VERSION < 755 /* < 1.17 */
37 auto start = std::chrono::steady_clock::now();
38 while (true)
39 {
40 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() >= 10000)
41 {
42 LOG_WARNING("Something went wrong trying to click slot (Timeout).");
43 return Status::Failure;
44 }
45 TransactionState transaction_state = inventory_manager->GetTransactionState(container_id, transaction_id);
46 if (transaction_state == TransactionState::Accepted)
47 {
48 break;
49 }
50 // The transaction has been refused by the server
51 else if (transaction_state == TransactionState::Refused)
52 {
53 return Status::Failure;
54 }
55
56 client.Yield();
57 }
58#endif
59 return Status::Success;
60 }
61
62 Status ClickSlotInContainer(BehaviourClient& client, const short container_id, const short slot_id, const int click_type, const char button_num)
63 {
64 constexpr std::array variable_names = {
65 "ClickSlotInContainer.container_id",
66 "ClickSlotInContainer.slot_id",
67 "ClickSlotInContainer.click_type",
68 "ClickSlotInContainer.button_num"
69 };
70
71 Blackboard& blackboard = client.GetBlackboard();
72
73 // Mandatory
74 blackboard.Set<short>(variable_names[0], container_id);
75 blackboard.Set<short>(variable_names[1], slot_id);
76 blackboard.Set<int>(variable_names[2], click_type);
77 blackboard.Set<char>(variable_names[3], button_num);
78
79 return ClickSlotInContainerImpl(client, container_id, slot_id, click_type, button_num);
80 }
81
83 {
84 constexpr std::array variable_names = {
85 "ClickSlotInContainer.container_id",
86 "ClickSlotInContainer.slot_id",
87 "ClickSlotInContainer.click_type",
88 "ClickSlotInContainer.button_num"
89 };
90
91 Blackboard& blackboard = client.GetBlackboard();
92
93 // Mandatory
94 const short container_id = blackboard.Get<short>(variable_names[0]);
95 const short slot_id = blackboard.Get<short>(variable_names[1]);
96 const int click_type = blackboard.Get<int>(variable_names[2]);
97 const char button_num = blackboard.Get<char>(variable_names[3]);
98
99 return ClickSlotInContainerImpl(client, container_id, slot_id, click_type, button_num);
100 }
101
102
103 Status SwapItemsInContainerImpl(BehaviourClient& client, const short container_id, const short first_slot, const short second_slot)
104 {
105 // If both slots are equal, clicking three times will transfer the content to the cursor instead of being a no-op
106 if (first_slot == second_slot)
107 {
108 return Status::Success;
109 }
110
111 // Left click on the first slot, transferring the slot to the cursor
112 if (ClickSlotInContainer(client, container_id, first_slot, 0, 0) == Status::Failure)
113 {
114 LOG_WARNING("Failed to swap items (first click)");
115 return Status::Failure;
116 }
117
118 // Left click on the second slot, transferring the cursor to the slot
119 if (ClickSlotInContainer(client, container_id, second_slot, 0, 0) == Status::Failure)
120 {
121 LOG_WARNING("Failed to swap items (second click)");
122 return Status::Failure;
123 }
124
125 // Left click on the first slot, transferring back the cursor to the slot
126 if (ClickSlotInContainer(client, container_id, first_slot, 0, 0) == Status::Failure)
127 {
128 LOG_WARNING("Failed to swap items (third click)");
129 return Status::Failure;
130 }
131
132 return Status::Success;
133 }
134
135 Status SwapItemsInContainer(BehaviourClient& client, const short container_id, const short first_slot, const short second_slot)
136 {
137 constexpr std::array variable_names = {
138 "SwapItemsInContainer.container_id",
139 "SwapItemsInContainer.first_slot",
140 "SwapItemsInContainer.second_slot"
141 };
142
143 Blackboard& blackboard = client.GetBlackboard();
144
145 blackboard.Set<short>(variable_names[0], container_id);
146 blackboard.Set<short>(variable_names[1], first_slot);
147 blackboard.Set<short>(variable_names[2], second_slot);
148
149 return SwapItemsInContainerImpl(client, container_id, first_slot, second_slot);
150 }
151
153 {
154 constexpr std::array variable_names = {
155 "SwapItemsInContainer.container_id",
156 "SwapItemsInContainer.first_slot",
157 "SwapItemsInContainer.second_slot"
158 };
159
160 Blackboard& blackboard = client.GetBlackboard();
161
162 // Mandatory
163 const short container_id = blackboard.Get<short>(variable_names[0]);
164 const short first_slot = blackboard.Get<short>(variable_names[1]);
165 const short second_slot = blackboard.Get<short>(variable_names[2]);
166
167 return SwapItemsInContainerImpl(client, container_id, first_slot, second_slot);
168 }
169
170
171 Status DropItemsFromContainerImpl(BehaviourClient& client, const short container_id, const short slot_id, const short num_to_keep)
172 {
173 if (ClickSlotInContainer(client, container_id, slot_id, 0, 0) == Status::Failure)
174 {
175 return Status::Failure;
176 }
177
178 // Drop all
179 if (num_to_keep == 0)
180 {
181 return ClickSlotInContainer(client, container_id, -999, 0, 0);
182 }
183
184 int item_count = client.GetInventoryManager()->GetCursor().GetItemCount();
185
186 // Drop the right amount of items
187 while (item_count > num_to_keep)
188 {
189 if (ClickSlotInContainer(client, container_id, -999, 0, 1) == Status::Failure)
190 {
191 return Status::Failure;
192 }
193 item_count -= 1;
194 }
195
196 // Put back remaining items in the initial slot
197 return ClickSlotInContainer(client, container_id, slot_id, 0, 0);
198 }
199
200 Status DropItemsFromContainer(BehaviourClient& client, const short container_id, const short slot_id, const short num_to_keep)
201 {
202 constexpr std::array variable_names = {
203 "DropItemsFromContainer.container_id",
204 "DropItemsFromContainer.slot_id",
205 "DropItemsFromContainer.num_to_keep"
206 };
207
208 Blackboard& blackboard = client.GetBlackboard();
209
210 blackboard.Set<short>(variable_names[0], container_id);
211 blackboard.Set<short>(variable_names[1], slot_id);
212 blackboard.Set<short>(variable_names[2], num_to_keep);
213
214 return DropItemsFromContainerImpl(client, container_id, slot_id, num_to_keep);
215 }
216
218 {
219 constexpr std::array variable_names = {
220 "DropItemsFromContainer.container_id",
221 "DropItemsFromContainer.slot_id",
222 "DropItemsFromContainer.num_to_keep"
223 };
224
225 Blackboard& blackboard = client.GetBlackboard();
226
227 // Mandatory
228 const short container_id = blackboard.Get<short>(variable_names[0]);
229 const short slot_id = blackboard.Get<short>(variable_names[1]);
230
231 // Optional
232 const short num_to_keep = blackboard.Get<short>(variable_names[2], 0);
233
234 return DropItemsFromContainerImpl(client, container_id, slot_id, num_to_keep);
235 }
236
237
238 Status PutOneItemInContainerSlotImpl(BehaviourClient& client, const short container_id, const short source_slot, const short destination_slot)
239 {
240 // Left click on the first slot, transferring the slot to the cursor
241 if (ClickSlotInContainer(client, container_id, source_slot, 0, 0) == Status::Failure)
242 {
243 LOG_WARNING("Failed to put one item in slot (first click)");
244 return Status::Failure;
245 }
246
247 // Right click on the second slot, transferring one item of the cursor to the slot
248 if (ClickSlotInContainer(client, container_id, destination_slot, 0, 1) == Status::Failure)
249 {
250 LOG_WARNING("Failed to put one item in slot (second click)");
251 return Status::Failure;
252 }
253
254 // Left click on the first slot, transferring back the cursor to the slot
255 if (ClickSlotInContainer(client, container_id, source_slot, 0, 0) == Status::Failure)
256 {
257 LOG_WARNING("Failed to put one item in slot (third click)");
258 return Status::Failure;
259 }
260
261 return Status::Success;
262 }
263
264 Status PutOneItemInContainerSlot(BehaviourClient& client, const short container_id, const short source_slot, const short destination_slot)
265 {
266 constexpr std::array variable_names = {
267 "PutOneItemInContainerSlot.container_id",
268 "PutOneItemInContainerSlot.source_slot",
269 "PutOneItemInContainerSlot.destination_slot"
270 };
271
272 Blackboard& blackboard = client.GetBlackboard();
273
274 blackboard.Set<short>(variable_names[0], container_id);
275 blackboard.Set<short>(variable_names[1], source_slot);
276 blackboard.Set<short>(variable_names[2], destination_slot);
277
278 return PutOneItemInContainerSlotImpl(client, container_id, source_slot, destination_slot);
279 }
280
282 {
283 constexpr std::array variable_names = {
284 "PutOneItemInContainerSlot.container_id",
285 "PutOneItemInContainerSlot.source_slot",
286 "PutOneItemInContainerSlot.destination_slot"
287 };
288
289 Blackboard& blackboard = client.GetBlackboard();
290
291 // Mandatory
292 const short container_id = blackboard.Get<short>(variable_names[0]);
293 const short source_slot = blackboard.Get<short>(variable_names[1]);
294 const short destination_slot = blackboard.Get<short>(variable_names[2]);
295
296 return PutOneItemInContainerSlotImpl(client, container_id, source_slot, destination_slot);
297 }
298
299
300 Status SetItemInHandImpl(BehaviourClient& client, const ItemId item_id, const Hand hand)
301 {
302 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
303
304 short inventory_correct_slot_index = -1;
305 short inventory_destination_slot_index = hand == Hand::Left ? Window::INVENTORY_OFFHAND_INDEX : (Window::INVENTORY_HOTBAR_START + inventory_manager->GetIndexHotbarSelected());
306
307 // We need to check the inventory
308 // If the currently selected item is the right one, just go for it
309 const Slot current_selected = hand == Hand::Left ? inventory_manager->GetOffHand() : inventory_manager->GetHotbarSelected();
310 if (!current_selected.IsEmptySlot()
311 && current_selected.GetItemId() == item_id)
312
313 {
314 return Status::Success;
315 }
316
317 // Otherwise we need to find a slot with the given item
318 { // slots scope
319 const auto slots = inventory_manager->GetPlayerInventory()->GetLockedSlots();
320 for (const auto& [id, slot] : *slots)
321 {
324 && !slot.IsEmptySlot()
325 && slot.GetItemId() == item_id)
326 {
327 inventory_correct_slot_index = id;
328 break;
329 }
330 }
331 }
332
333 // If there is no stack with the given item in the inventory
334 if (inventory_correct_slot_index == -1)
335 {
336 return Status::Failure;
337 }
338
339 return SwapItemsInContainer(client, Window::PLAYER_INVENTORY_INDEX, inventory_correct_slot_index, inventory_destination_slot_index);
340 }
341
342
343 Status SetItemIdInHand(BehaviourClient& client, const ItemId item_id, const Hand hand)
344 {
345 constexpr std::array variable_names = {
346 "SetItemIdInHand.item_name",
347 "SetItemIdInHand.hand"
348 };
349
350 Blackboard& blackboard = client.GetBlackboard();
351
352 blackboard.Set<ItemId>(variable_names[0], item_id);
353 blackboard.Set<Hand>(variable_names[1], hand);
354
355 return SetItemInHandImpl(client, item_id, hand);
356 }
357
359 {
360 constexpr std::array variable_names = {
361 "SetItemIdInHand.item_name",
362 "SetItemIdInHand.hand"
363 };
364
365 Blackboard& blackboard = client.GetBlackboard();
366
367 // Mandatory
368 const ItemId item_id = blackboard.Get<ItemId>(variable_names[0]);
369 const Hand hand = blackboard.Get<Hand>(variable_names[1], Hand::Right);
370
371 return SetItemInHandImpl(client, item_id, hand);
372 }
373
374 Status SetItemInHand(BehaviourClient& client, const std::string& item_name, const Hand hand)
375 {
376 constexpr std::array variable_names = {
377 "SetItemInHand.item_name",
378 "SetItemInHand.hand"
379 };
380
381 Blackboard& blackboard = client.GetBlackboard();
382
383 blackboard.Set<std::string>(variable_names[0], item_name);
384 blackboard.Set<Hand>(variable_names[1], hand);
385
386 const ItemId item_id = AssetsManager::getInstance().GetItemID(item_name);
387
388 return SetItemInHandImpl(client, item_id, hand);
389 }
390
392 {
393 constexpr std::array variable_names = {
394 "SetItemInHand.item_name",
395 "SetItemInHand.hand"
396 };
397
398 Blackboard& blackboard = client.GetBlackboard();
399
400 // Mandatory
401 const std::string& item_name = blackboard.Get<std::string>(variable_names[0]);
402 const Hand hand = blackboard.Get<Hand>(variable_names[1], Hand::Right);
403
404 const ItemId item_id = AssetsManager::getInstance().GetItemID(item_name);
405
406 return SetItemInHandImpl(client, item_id, hand);
407 }
408
409
410 Status PlaceBlockImpl(BehaviourClient& client, const std::string& item_name, const Position& pos, std::optional<PlayerDiggingFace> face, const bool wait_confirmation, const bool allow_midair_placing)
411 {
412 std::shared_ptr<World> world = client.GetWorld();
413 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
414 std::shared_ptr<EntityManager> entity_manager = client.GetEntityManager();
415 std::shared_ptr<NetworkManager> network_manager = client.GetNetworkManager();
416 std::shared_ptr<LocalPlayer> local_player = entity_manager->GetLocalPlayer();
417
418 // Compute the distance from the hand? Might be from somewhere else
419 const Vector3<double> hand_pos = local_player->GetPosition() + Vector3<double>(0.0, 1.0, 0.0);
420
421 if (hand_pos.SqrDist(Vector3<double>(0.5, 0.5, 0.5) + pos) > 16.0f)
422 {
423 if (GoTo(client, pos, 4, 0, 1) == Status::Failure)
424 {
425 return Status::Failure;
426 }
427 }
428
429 LookAt(client, Vector3<double>(0.5) + pos, true);
430
431 const std::vector<Position> neighbour_offsets({
432 Position(0, 1, 0), Position(0, -1, 0),
433 Position(0, 0, 1), Position(0, 0, -1),
434 Position(1, 0, 0), Position(-1, 0, 0)
435 });
436
437 bool midair_placing = true;
438 // If no face specified
439 if (!face.has_value())
440 {
441 if (allow_midair_placing) // Then we don't care and default to Up
442 {
444 }
445 else
446 {
447 std::vector<PlayerDiggingFace> premium_face_candidates; // Faces next to a solid block
448 premium_face_candidates.reserve(6);
449 std::vector<PlayerDiggingFace> second_choice_face_candidates; // Faces next to a non solid block (like ferns that would make the block replace the fern instead of going next to it)
450 second_choice_face_candidates.reserve(6);
451 for (int face_idx = 0; face_idx < 6; face_idx++)
452 {
453 const Blockstate* neighbour_block = world->GetBlock(pos + neighbour_offsets[face_idx]);
454 // Placing against fluids is not allowed
455 if (neighbour_block != nullptr && !neighbour_block->IsAir() && !neighbour_block->IsFluid())
456 {
457 (neighbour_block->IsSolid() ? premium_face_candidates : second_choice_face_candidates).push_back(static_cast<PlayerDiggingFace>(face_idx));
458 }
459 }
460 if (premium_face_candidates.size() + second_choice_face_candidates.size() == 0)
461 {
462 LOG_WARNING("Can't place a block in midair at " << pos);
463 return Status::Failure;
464 }
465 std::vector<PlayerDiggingFace>& face_candidates = (premium_face_candidates.size() > 0 ? premium_face_candidates : second_choice_face_candidates);
466 const Vector3<double> player_orientation = local_player->GetFrontVector();
467 std::sort( // Find the face face closest to player looking direction
468 face_candidates.begin(), face_candidates.end(), [&](const PlayerDiggingFace a, const PlayerDiggingFace b) -> bool
469 {
470 Vector3<double> a_offset = neighbour_offsets[static_cast<int>(a)];
471 Vector3<double> b_offset = neighbour_offsets[static_cast<int>(b)];
472 return player_orientation.dot(a_offset) > player_orientation.dot(b_offset);
473 // a > b because a negative dot product means the vectors are in opposite directions IE the player is looking at the face.
474 // But because we place the block in the inner faces we negate the result.
475 }
476 );
477 face = face_candidates.front(); // This does not guarantees that the choosed PlayerDiggingFace is facing the player IE player_orientation.dot(face) can be less or equal than 0.
478 }
479 }
480 else // Check if block is air
481 {
482 const Blockstate* block = world->GetBlock(pos);
483
484 if (block != nullptr && !block->IsAir() && !block->IsFluid())
485 {
486 return Status::Failure;
487 }
488
489 const Blockstate* neighbour_block = world->GetBlock(pos + neighbour_offsets[static_cast<int>(face.value())]);
490 midair_placing = neighbour_block == nullptr || neighbour_block->IsAir();
491
492 if (!allow_midair_placing && midair_placing)
493 {
494 LOG_WARNING("Can't place a block in midair at " << pos);
495 return Status::Failure;
496 }
497 }
498
499 // Check if item in inventory
500 if (SetItemInHand(client, item_name, Hand::Right) == Status::Failure)
501 {
502 return Status::Failure;
503 }
504
505 const int num_item_in_hand = inventory_manager->GetPlayerInventory()->GetSlot(Window::INVENTORY_HOTBAR_START + inventory_manager->GetIndexHotbarSelected()).GetItemCount();
506
507 // If cheating is not allowed, adjust the placing position to the block containing the face we're placing against
508 const Position placing_pos = (allow_midair_placing && midair_placing) ? pos : (pos + neighbour_offsets[static_cast<int>(face.value())]);
509
510 std::shared_ptr<ServerboundUseItemOnPacket> place_block_msg = std::make_shared<ServerboundUseItemOnPacket>();
511 place_block_msg->SetLocation(placing_pos.ToNetworkPosition());
512 place_block_msg->SetDirection(static_cast<int>(face.value()));
513 switch (face.value())
514 {
516 place_block_msg->SetCursorPositionX(0.5f);
517 place_block_msg->SetCursorPositionY(0.0f);
518 place_block_msg->SetCursorPositionZ(0.5f);
519 break;
521 place_block_msg->SetCursorPositionX(0.5f);
522 place_block_msg->SetCursorPositionY(1.0f);
523 place_block_msg->SetCursorPositionZ(0.5f);
524 break;
526 place_block_msg->SetCursorPositionX(0.5f);
527 place_block_msg->SetCursorPositionY(0.5f);
528 place_block_msg->SetCursorPositionZ(0.0f);
529 break;
531 place_block_msg->SetCursorPositionX(0.5f);
532 place_block_msg->SetCursorPositionY(0.5f);
533 place_block_msg->SetCursorPositionZ(1.0f);
534 break;
536 place_block_msg->SetCursorPositionX(1.0f);
537 place_block_msg->SetCursorPositionY(0.5f);
538 place_block_msg->SetCursorPositionZ(0.5f);
539 break;
541 place_block_msg->SetCursorPositionX(0.0f);
542 place_block_msg->SetCursorPositionY(0.5f);
543 place_block_msg->SetCursorPositionZ(0.5f);
544 break;
545 default:
546 break;
547 }
548#if PROTOCOL_VERSION > 452 /* > 1.13.2 */
549 place_block_msg->SetInside(false);
550#endif
551 place_block_msg->SetHand(static_cast<int>(Hand::Right));
552#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
553 place_block_msg->SetSequence(world->GetNextWorldInteractionSequenceId());
554#endif
555
556
557 // Place the block
558 network_manager->Send(place_block_msg);
559
560 std::shared_ptr<ServerboundSwingPacket> swing = std::make_shared<ServerboundSwingPacket>();
561 swing->SetHand(static_cast<int>(Hand::Right));
562 network_manager->Send(swing);
563
564 if (!wait_confirmation)
565 {
566 return Status::Success;
567 }
568
569 bool is_block_ok = false;
570 bool is_slot_ok = false;
571 auto start = std::chrono::steady_clock::now();
572 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
573 while (!is_block_ok || !is_slot_ok)
574 {
575 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() >= 60.0 * ms_per_tick)
576 {
577 LOG_WARNING('[' << network_manager->GetMyName() << "] "
578 << "Something went wrong waiting block placement confirmation at " << pos << " (Timeout). "
579 << "Block ok: " << is_block_ok << " Slot ok: " << is_slot_ok
580 );
581 return Status::Failure;
582 }
583 if (!is_block_ok)
584 {
585 const Blockstate* block = world->GetBlock(pos);
586
587 if (block != nullptr && block->GetName() == item_name)
588 {
589 is_block_ok = true;
590 }
591 }
592 if (!is_slot_ok)
593 {
594 const int new_num_item_in_hand = inventory_manager->GetPlayerInventory()->GetSlot(Window::INVENTORY_HOTBAR_START + inventory_manager->GetIndexHotbarSelected()).GetItemCount();
595 is_slot_ok = new_num_item_in_hand == num_item_in_hand - 1;
596 }
597
598 if (is_block_ok && is_slot_ok)
599 {
600 return Status::Success;
601 }
602
603 client.Yield();
604 }
605
606 return Status::Success;
607 }
608
609 Status PlaceBlock(BehaviourClient& client, const std::string& item_name, const Position& pos, std::optional<PlayerDiggingFace> face, const bool wait_confirmation, const bool allow_midair_placing)
610 {
611 constexpr std::array variable_names = {
612 "PlaceBlock.item_name",
613 "PlaceBlock.pos",
614 "PlaceBlock.face",
615 "PlaceBlock.wait_confirmation",
616 "PlaceBlock.allow_midair_placing"
617 };
618
619 Blackboard& blackboard = client.GetBlackboard();
620
621 blackboard.Set<std::string>(variable_names[0], item_name);
622 blackboard.Set<Position>(variable_names[1], pos);
623 blackboard.Set<std::optional<PlayerDiggingFace>>(variable_names[2], face);
624 blackboard.Set<bool>(variable_names[3], wait_confirmation);
625 blackboard.Set<bool>(variable_names[4], allow_midair_placing);
626
627 return PlaceBlockImpl(client, item_name, pos, face, wait_confirmation, allow_midair_placing);
628 }
629
631 {
632 constexpr std::array variable_names = {
633 "PlaceBlock.item_name",
634 "PlaceBlock.pos",
635 "PlaceBlock.face",
636 "PlaceBlock.wait_confirmation",
637 "PlaceBlock.allow_midair_placing"
638 };
639
640 Blackboard& blackboard = client.GetBlackboard();
641
642 // Mandatory
643 const std::string& item_name = blackboard.Get<std::string>(variable_names[0]);
644 const Position& pos = blackboard.Get<Position>(variable_names[1]);
645
646 // Optional
647 const std::optional<PlayerDiggingFace> face = blackboard.Get<std::optional<PlayerDiggingFace>>(variable_names[2], PlayerDiggingFace::Up);
648 const bool wait_confirmation = blackboard.Get<bool>(variable_names[3], false);
649 const bool allow_midair_placing = blackboard.Get<bool>(variable_names[4], false);
650
651
652 return PlaceBlockImpl(client, item_name, pos, face, wait_confirmation, allow_midair_placing);
653 }
654
655
656 Status EatImpl(BehaviourClient& client, const std::string& food_name, const bool wait_confirmation)
657 {
658 if (SetItemInHand(client, food_name, Hand::Left) == Status::Failure)
659 {
660 return Status::Failure;
661 }
662
663 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
664 std::shared_ptr<NetworkManager> network_manager = client.GetNetworkManager();
665
666 const char current_stack_size = inventory_manager->GetOffHand().GetItemCount();
667 std::shared_ptr<ServerboundUseItemPacket> use_item_msg = std::make_shared<ServerboundUseItemPacket>();
668 use_item_msg->SetHand(static_cast<int>(Hand::Left));
669#if PROTOCOL_VERSION > 758 /* > 1.18.2 */
670 use_item_msg->SetSequence(client.GetWorld()->GetNextWorldInteractionSequenceId());
671#endif
672 network_manager->Send(use_item_msg);
673
674 if (!wait_confirmation)
675 {
676 return Status::Success;
677 }
678
679 auto start = std::chrono::steady_clock::now();
680 const double ms_per_tick = client.GetPhysicsManager()->GetMsPerTick();
681 while (inventory_manager->GetOffHand().GetItemCount() == current_stack_size)
682 {
683 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() >= 60.0 * ms_per_tick)
684 {
685 LOG_WARNING("Something went wrong trying to eat (Timeout).");
686 return Status::Failure;
687 }
688 client.Yield();
689 }
690
691 return Status::Success;
692 }
693
694 Status Eat(BehaviourClient& client, const std::string& food_name, const bool wait_confirmation)
695 {
696 constexpr std::array variable_names = {
697 "Eat.food_name",
698 "Eat.wait_confirmation"
699 };
700
701 Blackboard& blackboard = client.GetBlackboard();
702
703 blackboard.Set<std::string>(variable_names[0], food_name);
704 blackboard.Set<bool>(variable_names[1], wait_confirmation);
705
706 return EatImpl(client, food_name, wait_confirmation);
707 }
708
710 {
711 constexpr std::array variable_names = {
712 "Eat.food_name",
713 "Eat.wait_confirmation"
714 };
715
716 Blackboard& blackboard = client.GetBlackboard();
717
718 // Mandatory
719 const std::string& food_name = blackboard.Get<std::string>(variable_names[0]);
720
721 // Optional
722 const bool wait_confirmation = blackboard.Get<bool>(variable_names[1], false);
723
724
725 return EatImpl(client, food_name, wait_confirmation);
726 }
727
728
730 {
731 // Open the container
733 {
734 return Status::Failure;
735 }
736
737 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
738
739 // Wait for a window to be opened
740 auto start = std::chrono::steady_clock::now();
741 while (inventory_manager->GetFirstOpenedWindowId() == -1)
742 {
743 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() >= 3000)
744 {
745 LOG_WARNING("Something went wrong trying to open container (Timeout).");
746 return Status::Failure;
747 }
748 client.Yield();
749 }
750
751 return Status::Success;
752 }
753
755 {
756 constexpr std::array variable_names = {
757 "OpenContainer.pos"
758 };
759
760 Blackboard& blackboard = client.GetBlackboard();
761
762 blackboard.Set<Position>(variable_names[0], pos);
763
764 return OpenContainerImpl(client, pos);
765 }
766
768 {
769 constexpr std::array variable_names = {
770 "OpenContainer.pos"
771 };
772
773 Blackboard& blackboard = client.GetBlackboard();
774
775 // Mandatory
776 const Position& pos = blackboard.Get<Position>(variable_names[0]);
777
778
779 return OpenContainerImpl(client, pos);
780 }
781
782
783 Status CloseContainerImpl(BehaviourClient& client, const short container_id)
784 {
785 std::shared_ptr<NetworkManager> network_manager = client.GetNetworkManager();
786 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
787
788 std::shared_ptr<ServerboundContainerClosePacket> close_container_msg = std::make_shared<ServerboundContainerClosePacket>();
789 short true_container_id = container_id;
790 if (true_container_id < 0)
791 {
792 true_container_id = inventory_manager->GetFirstOpenedWindowId();
793 }
794 close_container_msg->SetContainerId(static_cast<unsigned char>(true_container_id));
795 network_manager->Send(close_container_msg);
796
797 // There is no confirmation from the server, so we
798 // can simply close the window here
799 inventory_manager->EraseInventory(true_container_id);
800
801 return Status::Success;
802 }
803
804 Status CloseContainer(BehaviourClient& client, const short container_id)
805 {
806 constexpr std::array variable_names = {
807 "CloseContainer.container_id"
808 };
809
810 Blackboard& blackboard = client.GetBlackboard();
811
812 blackboard.Set<short>(variable_names[0], container_id);
813
814 return CloseContainerImpl(client, container_id);
815 }
816
818 {
819 constexpr std::array variable_names = {
820 "CloseContainer.container_id"
821 };
822
823 Blackboard& blackboard = client.GetBlackboard();
824
825 // Optional
826 const short container_id = blackboard.Get<short>(variable_names[0], -1);
827
828
829 return CloseContainerImpl(client, container_id);
830 }
831
832
834 {
835 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
836
837 std::stringstream output;
838 {
839 output << "Cursor --> " << inventory_manager->GetCursor().Serialize().Dump() << "\n";
840 auto slots = inventory_manager->GetPlayerInventory()->GetLockedSlots();
841 for (const auto& [id, slot] : *slots)
842 {
843 output << id << " --> " << slot.Serialize().Dump() << "\n";
844 }
845 }
846 LOG(output.str(), level);
847 return Status::Success;
848 }
849
851 {
852 constexpr std::array variable_names = {
853 "LogInventoryContent.level"
854 };
855
856 Blackboard& blackboard = client.GetBlackboard();
857
858 blackboard.Set<LogLevel>(variable_names[0], level);
859
860 return LogInventoryContentImpl(client, level);
861 }
862
864 {
865 constexpr std::array variable_names = {
866 "LogInventoryContent.level"
867 };
868
869 Blackboard& blackboard = client.GetBlackboard();
870
871 //Optional
872 const LogLevel level = blackboard.Get<LogLevel>(variable_names[0], LogLevel::Info);
873
874 return LogInventoryContentImpl(client, level);
875 }
876
877
878#if PROTOCOL_VERSION > 451 /* > 1.13.2 */
879 Status TradeImpl(BehaviourClient& client, const int item_id, const bool buy, const int trade_id)
880 {
881 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
882
883 // Make sure a trading window is opened and
884 // possible trades are available
885 auto start = std::chrono::steady_clock::now();
886 size_t num_trades = 0;
887 do
888 {
889 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() > 5000)
890 {
891 LOG_WARNING("Something went wrong waiting trade opening (Timeout).");
892 return Status::Failure;
893 }
894
895 num_trades = inventory_manager->GetAvailableMerchantOffers().size();
896 client.Yield();
897 } while (num_trades <= 0 || inventory_manager->GetFirstOpenedWindowId() == -1);
898
899 const short container_id = inventory_manager->GetFirstOpenedWindowId();
900 std::shared_ptr<Window> trading_container = inventory_manager->GetWindow(container_id);
901
902 if (trading_container == nullptr)
903 {
904 LOG_WARNING("Something went wrong during trade (window closed).");
905 return Status::Failure;
906 }
907
908 int trade_index = trade_id;
909 bool has_trade_second_item = false;
910 const std::vector<ProtocolCraft::MerchantOffer> trades = inventory_manager->GetAvailableMerchantOffers();
911
912 // Find which trade we want in the list
913 if (trade_id == -1)
914 {
915 for (int i = 0; i < trades.size(); ++i)
916 {
917 if ((buy && trades[i].GetOutputItem().GetItemId() == item_id)
918 || (!buy && trades[i].GetInputItem1().GetItemId() == item_id))
919 {
920 trade_index = i;
921 has_trade_second_item = trades[i].GetInputItem2().has_value();
922 break;
923 }
924 }
925 }
926
927 if (trade_index == -1)
928 {
929 LOG_WARNING("Failed trading (this villager does not sell/buy " << AssetsManager::getInstance().Items().at(item_id)->GetName() << ")");
930 return Status::Failure;
931 }
932
933 // Check that the trade is not locked
934 if (trades[trade_index].GetNumberOfTradesUses() >= trades[trade_index].GetMaximumNumberOfTradeUses())
935 {
936 LOG_WARNING("Failed trading (trade locked)");
937 return Status::Failure;
938 }
939
940 std::shared_ptr<NetworkManager> network_manager = client.GetNetworkManager();
941
942 // Select the trade in the list
943 std::shared_ptr<ServerboundSelectTradePacket> select_trade_msg = std::make_shared<ServerboundSelectTradePacket>();
944 select_trade_msg->SetItem(trade_index);
945
946 network_manager->Send(select_trade_msg);
947
948 start = std::chrono::steady_clock::now();
949 // Wait until the output/input is set with the correct item
950 bool correct_items = false;
951 do
952 {
953 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() > 5000)
954 {
955 LOG_WARNING("Something went wrong waiting trade selection (Timeout). Maybe an item was missing?");
956 return Status::Failure;
957 }
958
959 correct_items = (buy && trading_container->GetSlot(2).GetItemId() == item_id) ||
960 (!buy && !trading_container->GetSlot(2).IsEmptySlot()
961 && (trading_container->GetSlot(0).GetItemId() == item_id || trading_container->GetSlot(1).GetItemId() == item_id));
962 client.Yield();
963 } while (!correct_items);
964
965 // Check we have at least one empty slot to get back input remainings + outputs
966 std::vector<short> empty_slots(has_trade_second_item ? 3 : 2);
967 int empty_slots_index = 0;
968 {
969 auto slots = trading_container->GetLockedSlots();
970 for (const auto& [id, slot] : *slots)
971 {
972 if (id < trading_container->GetFirstPlayerInventorySlot())
973 {
974 continue;
975 }
976
977 if (slot.IsEmptySlot())
978 {
979 empty_slots[empty_slots_index] = id;
980 empty_slots_index++;
981 if (empty_slots_index == empty_slots.size())
982 {
983 break;
984 }
985 }
986 }
987 }
988 if (empty_slots_index == 0)
989 {
990 LOG_WARNING("No free space in inventory for trading to happen.");
991 return Status::Failure;
992 }
993 else if (empty_slots_index < empty_slots.size())
994 {
995 LOG_WARNING("Not enough free space in inventory for trading. Input items may be lost");
996 }
997
998 // Get a copy of the original input slots to see when they'll change
999 const Slot input_slot_1 = trading_container->GetSlot(0);
1000 const Slot input_slot_2 = trading_container->GetSlot(1);
1001
1002 // Get the output in the inventory
1003 if (SwapItemsInContainer(client, container_id, empty_slots[0], 2) == Status::Failure)
1004 {
1005 LOG_WARNING("Failed to swap output slot during trading attempt");
1006 return Status::Failure;
1007 }
1008
1009 // Wait for the server to update the input slots
1010 start = std::chrono::steady_clock::now();
1011 while (true)
1012 {
1013 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() > 5000)
1014 {
1015 LOG_WARNING("Something went wrong waiting trade input update (Timeout).");
1016 return Status::Failure;
1017 }
1018
1019 if ((input_slot_1.IsEmptySlot() || input_slot_1.GetItemCount() != trading_container->GetSlot(0).GetItemCount()) &&
1020 (input_slot_2.IsEmptySlot() || input_slot_2.GetItemCount() != trading_container->GetSlot(1).GetItemCount()))
1021 {
1022 break;
1023 }
1024 client.Yield();
1025 }
1026
1027 // Get back the input remainings in the inventory
1028 for (int i = 0; i < empty_slots_index - 1; ++i)
1029 {
1030 if (SwapItemsInContainer(client, container_id, empty_slots[i + 1], i) == Status::Failure)
1031 {
1032 LOG_WARNING("Failed to swap slots " << i << " after trading attempt");
1033 return Status::Failure;
1034 }
1035 }
1036
1037 // If we are here, everything is fine (or should be),
1038 // remove 1 to the possible trade counter on the villager
1039 inventory_manager->IncrementMerchantOfferUse(trade_index);
1040
1041 return Status::Success;
1042 }
1043
1044 Status Trade(BehaviourClient& client, const int item_id, const bool buy, const int trade_id)
1045 {
1046 constexpr std::array variable_names = {
1047 "Trade.item_id",
1048 "Trade.buy",
1049 "Trade.trade_id"
1050 };
1051
1052 Blackboard& blackboard = client.GetBlackboard();
1053
1054 blackboard.Set<int>(variable_names[0], item_id);
1055 blackboard.Set<bool>(variable_names[1], buy);
1056 blackboard.Set<int>(variable_names[2], trade_id);
1057
1058 return TradeImpl(client, item_id, buy, trade_id);
1059 }
1060
1062 {
1063 constexpr std::array variable_names = {
1064 "Trade.item_id",
1065 "Trade.buy",
1066 "Trade.trade_id"
1067 };
1068
1069 Blackboard& blackboard = client.GetBlackboard();
1070
1071 // Mandatory
1072 const int item_id = blackboard.Get<int>(variable_names[0]);
1073 const bool buy = blackboard.Get<bool>(variable_names[1]);
1074
1075 //Optional
1076 const int trade_id = blackboard.Get<int>(variable_names[2], -1);
1077
1078 return TradeImpl(client, item_id, buy, trade_id);
1079 }
1080
1081
1082 Status TradeNameImpl(BehaviourClient& client, const std::string& item_name, const bool buy, const int trade_id)
1083 {
1084 // Get item id corresponding to the name
1085 const int item_id = AssetsManager::getInstance().GetItemID(item_name);
1086 if (item_id < 0)
1087 {
1088 LOG_WARNING("Trying to trade an unknown item");
1089 return Status::Failure;
1090 }
1091
1092 return TradeImpl(client, item_id, buy, trade_id);
1093 }
1094
1095 Status TradeName(BehaviourClient& client, const std::string& item_name, const bool buy, const int trade_id)
1096 {
1097 constexpr std::array variable_names = {
1098 "TradeName.item_name",
1099 "TradeName.buy",
1100 "TradeName.trade_id"
1101 };
1102
1103 Blackboard& blackboard = client.GetBlackboard();
1104
1105 blackboard.Set<std::string>(variable_names[0], item_name);
1106 blackboard.Set<bool>(variable_names[1], buy);
1107 blackboard.Set<int>(variable_names[2], trade_id);
1108
1109 return TradeNameImpl(client, item_name, buy, trade_id);
1110 }
1111
1113 {
1114 constexpr std::array variable_names = {
1115 "TradeName.item_name",
1116 "TradeName.buy",
1117 "TradeName.trade_id"
1118 };
1119
1120 Blackboard& blackboard = client.GetBlackboard();
1121
1122 // Mandatory
1123 const std::string& item_name = blackboard.Get<std::string>(variable_names[0]);
1124 const bool buy = blackboard.Get<bool>(variable_names[1]);
1125
1126 // Optional
1127 const int trade_id = blackboard.Get<int>(variable_names[2], -1);
1128
1129 return TradeNameImpl(client, item_name, buy, trade_id);
1130 }
1131#endif
1132
1133 Status CraftImpl(BehaviourClient& client, const std::array<std::array<ItemId, 3>, 3>& inputs, const bool allow_inventory_craft)
1134 {
1135 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
1136
1137 int min_x = 3;
1138 int max_x = -1;
1139 int min_y = 3;
1140 int max_y = -1;
1141 bool use_inventory_craft = false;
1142 if (!allow_inventory_craft)
1143 {
1144 use_inventory_craft = false;
1145 min_x = 0;
1146 max_x = 2;
1147 min_y = 0;
1148 max_y = 2;
1149 }
1150 else
1151 {
1152 for (int y = 0; y < 3; ++y)
1153 {
1154 for (int x = 0; x < 3; ++x)
1155 {
1156#if PROTOCOL_VERSION < 350 /* < 1.13 */
1157 if (inputs[y][x].first != -1)
1158#else
1159 if (inputs[y][x] != -1)
1160#endif
1161 {
1162 min_x = std::min(x, min_x);
1163 max_x = std::max(x, max_x);
1164 min_y = std::min(y, min_y);
1165 max_y = std::max(y, max_y);
1166 }
1167 }
1168 }
1169
1170 use_inventory_craft = (max_x - min_x) < 2 && (max_y - min_y) < 2;
1171 }
1172
1173 int crafting_container_id = -1;
1174 // If we need a crafting table, make sure one is open
1175 if (!use_inventory_craft)
1176 {
1177 auto start = std::chrono::steady_clock::now();
1178 do
1179 {
1180 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() > 5000)
1181 {
1182 LOG_WARNING("Something went wrong waiting craft opening (Timeout).");
1183 return Status::Failure;
1184 }
1185 crafting_container_id = inventory_manager->GetFirstOpenedWindowId();
1186 client.Yield();
1187 } while (crafting_container_id == -1);
1188 }
1189 else
1190 {
1191 crafting_container_id = Window::PLAYER_INVENTORY_INDEX;
1192 }
1193
1194 std::shared_ptr<Window> crafting_container = inventory_manager->GetWindow(crafting_container_id);
1195
1196 if (crafting_container == nullptr)
1197 {
1198 LOG_WARNING("Something went wrong during craft (window closed).");
1199 return Status::Failure;
1200 }
1201
1202 Slot output_slot_before;
1203 // For each input slot
1204 for (int y = min_y; y < max_y + 1; ++y)
1205 {
1206 for (int x = min_x; x < max_x + 1; ++x)
1207 {
1208 // Save the output slot just before the last input is set
1209 if (y == min_y && x == min_x)
1210 {
1211 output_slot_before = crafting_container->GetSlot(0);
1212 }
1213
1214 const int destination_slot = use_inventory_craft ? (1 + x - min_x + (y - min_y) * 2) : (1 + x + 3 * y);
1215
1216 int source_slot = -1;
1217 int source_quantity = -1;
1218 // Search for the required item in inventory
1219 {
1220 auto slots = crafting_container->GetLockedSlots();
1221 for (const auto& [id, slot] : *slots)
1222 {
1223 if (id < crafting_container->GetFirstPlayerInventorySlot())
1224 {
1225 continue;
1226 }
1227#if PROTOCOL_VERSION < 350 /* < 1.13 */
1228 if (slot.GetBlockId() == inputs[y][x].first && slot.GetItemDamage() == inputs[y][x].second)
1229#else
1230 if (slot.GetItemId() == inputs[y][x])
1231#endif
1232 {
1233 source_slot = id;
1234 source_quantity = slot.GetItemCount();
1235 break;
1236 }
1237 }
1238 }
1239
1240 if (source_slot == -1)
1241 {
1242 LOG_WARNING("Not enough source item [" << AssetsManager::getInstance().Items().at(inputs[y][x])->GetName() << "] found in inventory for crafting.");
1243 return Status::Failure;
1244 }
1245
1246 if (ClickSlotInContainer(client, crafting_container_id, source_slot, 0, 0) == Status::Failure)
1247 {
1248 LOG_WARNING("Error trying to pick source item [" << AssetsManager::getInstance().Items().at(inputs[y][x])->GetName() << "] during crafting");
1249 return Status::Failure;
1250 }
1251
1252 // Right click in the destination slot
1253 if (ClickSlotInContainer(client, crafting_container_id, destination_slot, 0, 1) == Status::Failure)
1254 {
1255 LOG_WARNING("Error trying to place source item [" << AssetsManager::getInstance().Items().at(inputs[y][x])->GetName() << "] during crafting");
1256 return Status::Failure;
1257 }
1258
1259 // Put back the remaining items in the origin slot
1260 if (source_quantity > 1)
1261 {
1262 if (ClickSlotInContainer(client, crafting_container_id, source_slot, 0, 0) == Status::Failure)
1263 {
1264 LOG_WARNING("Error trying to place back source item [" << AssetsManager::getInstance().Items().at(inputs[y][x])->GetName() << "] during crafting");
1265 return Status::Failure;
1266 }
1267 }
1268 }
1269 }
1270
1271 // Wait for the server to send the output change
1272 // TODO: with the recipe book, we could know without waiting
1273 auto start = std::chrono::steady_clock::now();
1274 while (true)
1275 {
1276 if (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() > 5000)
1277 {
1278 LOG_WARNING("Something went wrong waiting craft output update (Timeout).");
1279 return Status::Failure;
1280 }
1281 if (!crafting_container->GetSlot(0).SameItem(output_slot_before))
1282 {
1283 break;
1284 }
1285 client.Yield();
1286 }
1287
1288 // All inputs are in place, output is ready, click on output
1289 if (ClickSlotInContainer(client, crafting_container_id, 0, 0, 0) == Status::Failure)
1290 {
1291 LOG_WARNING("Error trying to click on output during crafting");
1292 return Status::Failure;
1293 }
1294
1295 // Find an empty slot in inventory to place the cursor content
1296 int destination_slot = -999;
1297 {
1298 auto slots = crafting_container->GetLockedSlots();
1299 for (const auto& [id, slot] : *slots)
1300 {
1301 if (id < (use_inventory_craft ? Window::INVENTORY_STORAGE_START : crafting_container->GetFirstPlayerInventorySlot()))
1302 {
1303 continue;
1304 }
1305
1306 // If it fits in a slot (empty or with the same item)
1307 if (slot.IsEmptySlot() ||
1308 (inventory_manager->GetCursor().GetItemId() == slot.GetItemId() &&
1309 slot.GetItemCount() < AssetsManager::getInstance().Items().at(slot.GetItemId())->GetStackSize() - 1)
1310 )
1311 {
1312 destination_slot = id;
1313 break;
1314 }
1315 }
1316 }
1317
1318 if (destination_slot == -999)
1319 {
1320 LOG_INFO("No available space for crafted item, will be thrown out");
1321 }
1322
1323 if (ClickSlotInContainer(client, crafting_container_id, destination_slot, 0, 0) == Status::Failure)
1324 {
1325 LOG_WARNING("Error trying to put back output during crafting");
1326 return Status::Failure;
1327 }
1328
1329 return Status::Success;
1330 }
1331
1332#if PROTOCOL_VERSION < 350 /* < 1.13 */
1333 Status Craft(BehaviourClient& client, const std::array<std::array<std::pair<int, unsigned char>, 3>, 3>& inputs, const bool allow_inventory_craft)
1334#else
1335 Status Craft(BehaviourClient& client, const std::array<std::array<int, 3>, 3>& inputs, const bool allow_inventory_craft)
1336#endif
1337 {
1338 constexpr std::array variable_names = {
1339 "Craft.inputs",
1340 "Craft.allow_inventory_craft"
1341 };
1342
1343 Blackboard& blackboard = client.GetBlackboard();
1344
1345#if PROTOCOL_VERSION < 350 /* < 1.13 */
1346 blackboard.Set<std::array<std::array<std::pair<int, unsigned char>, 3>, 3>>(variable_names[0], inputs);
1347#else
1348 blackboard.Set<std::array<std::array<int, 3>, 3>>(variable_names[0], inputs);
1349#endif
1350 blackboard.Set<bool>(variable_names[1], allow_inventory_craft);
1351
1352 return CraftImpl(client, inputs, allow_inventory_craft);
1353 }
1354
1356 {
1357 constexpr std::array variable_names = {
1358 "Craft.inputs",
1359 "Craft.allow_inventory_craft"
1360 };
1361
1362 Blackboard& blackboard = client.GetBlackboard();
1363
1364 // Mandatory
1365#if PROTOCOL_VERSION < 350 /* < 1.13 */
1366 const std::array<std::array<std::pair<int, unsigned char>, 3>, 3>& inputs = blackboard.Get<std::array<std::array<std::pair<int, unsigned char>, 3>, 3>>(variable_names[0]);
1367#else
1368 const std::array<std::array<int, 3>, 3>& inputs = blackboard.Get<std::array<std::array<int, 3>, 3>>(variable_names[0]);
1369#endif
1370
1371 // Optional
1372 const bool allow_inventory_craft = blackboard.Get<bool>(variable_names[1], true);
1373
1374 return CraftImpl(client, inputs, allow_inventory_craft);
1375 }
1376
1377
1378 Status CraftNamedImpl(BehaviourClient& client, const std::array<std::array<std::string, 3>, 3>& inputs, const bool allow_inventory_craft)
1379 {
1380 const AssetsManager& assets_manager = AssetsManager::getInstance();
1381#if PROTOCOL_VERSION < 350 /* < 1.13 */
1382 std::array<std::array<std::pair<int, unsigned char>, 3>, 3> inputs_ids;
1383#else
1384 std::array<std::array<int, 3>, 3> inputs_ids;
1385#endif
1386 for (size_t i = 0; i < 3; ++i)
1387 {
1388 for (size_t j = 0; j < 3; ++j)
1389 {
1390#if PROTOCOL_VERSION < 350 /* < 1.13 */
1391 inputs_ids[i][j] = inputs[i][j] == "" ? std::pair<int, unsigned char>{ -1, 0 } : assets_manager.GetItemID(inputs[i][j]);
1392#else
1393 inputs_ids[i][j] = inputs[i][j] == "" ? -1 : assets_manager.GetItemID(inputs[i][j]);
1394#endif
1395 }
1396 }
1397 return Craft(client, inputs_ids, allow_inventory_craft);
1398 }
1399
1400 Status CraftNamed(BehaviourClient& client, const std::array<std::array<std::string, 3>, 3>& inputs, const bool allow_inventory_craft)
1401 {
1402 constexpr std::array variable_names = {
1403 "CraftNamed.inputs",
1404 "CraftNamed.allow_inventory_craft"
1405 };
1406
1407 Blackboard& blackboard = client.GetBlackboard();
1408
1409 blackboard.Set<std::array<std::array<std::string, 3>, 3>>(variable_names[0], inputs);
1410 blackboard.Set<bool>(variable_names[1], allow_inventory_craft);
1411
1412 return CraftNamedImpl(client, inputs, allow_inventory_craft);
1413 }
1414
1416 {
1417 constexpr std::array variable_names = {
1418 "CraftNamed.inputs",
1419 "CraftNamed.allow_inventory_craft"
1420 };
1421
1422 Blackboard& blackboard = client.GetBlackboard();
1423
1424 // Mandatory
1425 const std::array<std::array<std::string, 3>, 3>& inputs = blackboard.Get<std::array<std::array<std::string, 3>, 3>>(variable_names[0]);
1426
1427 // Optional
1428 const bool allow_inventory_craft = blackboard.Get<bool>(variable_names[1], true);
1429
1430 return CraftNamedImpl(client, inputs, allow_inventory_craft);
1431 }
1432
1433
1434 Status HasItemInInventoryImpl(BehaviourClient& client, const ItemId item_id, const int quantity)
1435 {
1436 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
1437
1438 int quantity_sum = 0;
1439 {
1440 auto slots = inventory_manager->GetPlayerInventory()->GetLockedSlots();
1441 for (const auto& [id, slot] : *slots)
1442 {
1444 {
1445 continue;
1446 }
1447
1448 if (!slot.IsEmptySlot() && item_id == slot.GetItemId())
1449 {
1450 quantity_sum += slot.GetItemCount();
1451 }
1452
1453 if (quantity_sum >= quantity)
1454 {
1455 return Status::Success;
1456 }
1457 }
1458 }
1459
1460 return Status::Failure;
1461 }
1462
1463 Status HasItemIdInInventory(BehaviourClient& client, const ItemId item_id, const int quantity)
1464 {
1465 constexpr std::array variable_names = {
1466 "HasItemIdInInventory.item_id",
1467 "HasItemIdInInventory.quantity"
1468 };
1469
1470 Blackboard& blackboard = client.GetBlackboard();
1471 blackboard.Set<ItemId>(variable_names[0], item_id);
1472 blackboard.Set<int>(variable_names[1], quantity);
1473
1474 return HasItemInInventoryImpl(client, item_id, quantity);
1475 }
1476
1478 {
1479 constexpr std::array variable_names = {
1480 "HasItemIdInInventory.item_id",
1481 "HasItemIdInInventory.quantity"
1482 };
1483
1484 Blackboard& blackboard = client.GetBlackboard();
1485
1486 // Mandatory
1487 const ItemId item_id = blackboard.Get<ItemId>(variable_names[0]);
1488
1489 // Optional
1490 const int quantity = blackboard.Get<int>(variable_names[1], 1);
1491
1492 return HasItemInInventoryImpl(client, item_id, quantity);
1493 }
1494
1495 Status HasItemInInventory(BehaviourClient& client, const std::string& item_name, const int quantity)
1496 {
1497 constexpr std::array variable_names = {
1498 "HasItemInInventory.item_name",
1499 "HasItemInInventory.quantity"
1500 };
1501
1502 Blackboard& blackboard = client.GetBlackboard();
1503 blackboard.Set<std::string>(variable_names[0], item_name);
1504 blackboard.Set<int>(variable_names[1], quantity);
1505
1506 const auto item_id = AssetsManager::getInstance().GetItemID(item_name);
1507
1508 return HasItemInInventoryImpl(client, item_id, quantity);
1509 }
1510
1512 {
1513 constexpr std::array variable_names = {
1514 "HasItemInInventory.item_name",
1515 "HasItemInInventory.quantity"
1516 };
1517
1518 Blackboard& blackboard = client.GetBlackboard();
1519
1520 // Mandatory
1521 const std::string& item_name = blackboard.Get<std::string>(variable_names[0]);
1522
1523 // Optional
1524 const int quantity = blackboard.Get<int>(variable_names[1], 1);
1525
1526 return HasItemInInventoryImpl(client, AssetsManager::getInstance().GetItemID(item_name), quantity);
1527 }
1528
1529
1531 {
1532 std::shared_ptr<InventoryManager> inventory_manager = client.GetInventoryManager();
1533 std::shared_ptr<Window> player_inventory = inventory_manager->GetPlayerInventory();
1534
1535 while (true)
1536 {
1537 short src_index = -1;
1538 short dst_index = -1;
1540 {
1541 const Slot dst_slot = player_inventory->GetSlot(i);
1542 if (dst_slot.IsEmptySlot())
1543 {
1544 continue;
1545 }
1546 // If this slot is not empty, and not full,
1547 // check if "upper" slot with same items that
1548 // could fit in it
1549 const int available_space = AssetsManager::getInstance().Items().at(dst_slot.GetItemId())->GetStackSize() - dst_slot.GetItemCount();
1550 if (available_space == 0)
1551 {
1552 continue;
1553 }
1554
1555 for (short j = i + 1; j < Window::INVENTORY_OFFHAND_INDEX + 1; ++j)
1556 {
1557 const Slot src_slot = player_inventory->GetSlot(j);
1558 if (!src_slot.IsEmptySlot()
1559 && dst_slot.SameItem(src_slot)
1560 && src_slot.GetItemCount() <= available_space)
1561 {
1562 src_index = j;
1563 break;
1564 }
1565 }
1566
1567 if (src_index != -1)
1568 {
1569 dst_index = i;
1570 break;
1571 }
1572 }
1573
1574 // Nothing to do
1575 if (src_index == -1 && dst_index == -1)
1576 {
1577 break;
1578 }
1579
1580 // Pick slot src, put it in dst
1582 {
1583 LOG_WARNING("Error trying to pick up slot during inventory sorting");
1584 return Status::Failure;
1585 }
1586
1588 {
1589 LOG_WARNING("Error trying to put down slot during inventory sorting");
1590 return Status::Failure;
1591 }
1592 }
1593
1594 return Status::Success;
1595 }
1596}
#define LOG_WARNING(osstream)
Definition Logger.hpp:44
#define LOG_INFO(osstream)
Definition Logger.hpp:43
#define LOG(osstream, level)
Definition Logger.hpp:28
const std::unordered_map< ItemId, std::unique_ptr< Item > > & Items() const
static AssetsManager & getInstance()
ItemId GetItemID(const std::string &item_name) const
A ManagersClient extended with a blackboard that can store any kind of data and a virtual Yield funct...
virtual void Yield()=0
A map wrapper to store arbitrary data.
void Set(const std::string &key, const T &value)
Set map entry at key to value.
const T & Get(const std::string &key)
Get the map value at key, casting it to T.
const std::string & GetName() const
std::shared_ptr< NetworkManager > GetNetworkManager() const
std::shared_ptr< EntityManager > GetEntityManager() const
std::shared_ptr< PhysicsManager > GetPhysicsManager() const
std::shared_ptr< InventoryManager > GetInventoryManager() const
int SendInventoryTransaction(const std::shared_ptr< ProtocolCraft::ServerboundContainerClickPacket > &transaction)
std::shared_ptr< World > GetWorld() const
static constexpr short INVENTORY_HOTBAR_START
Definition Window.hpp:26
static constexpr short INVENTORY_STORAGE_START
Definition Window.hpp:25
static constexpr short PLAYER_INVENTORY_INDEX
Definition Window.hpp:16
static constexpr short INVENTORY_OFFHAND_INDEX
Definition Window.hpp:27
bool IsEmptySlot() const
Definition Slot.hpp:92
bool SameItem(const Slot &s) const
Definition Slot.hpp:68
Status CraftNamedImpl(BehaviourClient &client, const std::array< std::array< std::string, 3 >, 3 > &inputs, const bool allow_inventory_craft)
Status OpenContainerImpl(BehaviourClient &client, const Position &pos)
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 ClickSlotInContainerImpl(BehaviourClient &client, const short container_id, const short slot_id, const int click_type, const char button_num)
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 GoTo(BehaviourClient &client, const Position &goal, const int dist_tolerance=0, const int min_end_dist=0, const int min_end_dist_xz=0, const bool allow_jump=true, const bool sprint=true, const float speed_factor=1.0f)
Find a path to a block position and navigate to it.
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 SetItemInHandImpl(BehaviourClient &client, const ItemId item_id, const Hand hand)
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 LookAt(BehaviourClient &client, const Vector3< double > &target, const bool set_pitch=true, const bool sync_to_server=true)
Turn the camera to look at a given target and send the new rotation to the server.
Status InteractWithBlock(BehaviourClient &client, const Position &pos, const PlayerDiggingFace face=PlayerDiggingFace::Up, const bool animation=true)
Interact (right click) with the block at the given location.
Status SortInventory(BehaviourClient &client)
Clean the inventory stacking same items together.
Status HasItemInInventoryImpl(BehaviourClient &client, const ItemId item_id, const int quantity)
int ItemId
Definition Item.hpp:15
Status DropItemsFromContainerImpl(BehaviourClient &client, const short container_id, const short slot_id, const short num_to_keep)
Status TradeImpl(BehaviourClient &client, const int item_id, const bool buy, const int trade_id)
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 CraftImpl(BehaviourClient &client, const std::array< std::array< ItemId, 3 >, 3 > &inputs, const bool allow_inventory_craft)
Status CloseContainerImpl(BehaviourClient &client, const short container_id)
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 LogInventoryContentImpl(BehaviourClient &client, const LogLevel level)
Status OpenContainerBlackboard(BehaviourClient &client)
Same thing as OpenContainer, but reads its parameters from the blackboard.
Status EatImpl(BehaviourClient &client, const std::string &food_name, const bool wait_confirmation)
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 TradeNameImpl(BehaviourClient &client, const std::string &item_name, const bool buy, const int trade_id)
Status SwapItemsInContainerImpl(BehaviourClient &client, const short container_id, const short first_slot, const short second_slot)
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.
Status PlaceBlockImpl(BehaviourClient &client, const std::string &item_name, const Position &pos, std::optional< PlayerDiggingFace > face, const bool wait_confirmation, const bool allow_midair_placing)
Status PutOneItemInContainerSlotImpl(BehaviourClient &client, const short container_id, const short source_slot, const short destination_slot)
double SqrDist(const Vector3 &v) const
Definition Vector3.hpp:192
ProtocolCraft::NetworkPosition ToNetworkPosition() const
Definition Vector3.hpp:272