|
1 | 1 | #include "GiveCommand.h"
|
2 |
| -#include "../../../Utils/Utils.h" |
| 2 | + |
3 | 3 | #include "../../../SDK/Tag.h"
|
| 4 | +#include "../../../Utils/Utils.h" |
4 | 5 |
|
5 | 6 | GiveCommand::GiveCommand() : IMCCommand("give", "spawn items", "<itemName> <count> [itemData] [NBT]") {
|
6 | 7 | }
|
7 | 8 |
|
8 | 9 | GiveCommand::~GiveCommand() {
|
9 | 10 | }
|
10 | 11 |
|
11 |
| -bool GiveCommand::execute(std::vector<std::string>* args) { |
| 12 | +bool GiveCommand::execute(std::vector<std::string> *args) { |
12 | 13 | assertTrue(args->size() > 2);
|
13 | 14 |
|
14 | 15 | int itemId = 0;
|
15 |
| - char count = static_cast<char>(assertInt(args->at(2))); |
| 16 | + uint32_t fullCount = static_cast<uint32_t>(assertInt(args->at(2))); |
| 17 | + unsigned int stackCount = fullCount / 64; // Get the amount of stacks we have. |
| 18 | + char count = fullCount % 64; // Get the amount we have left. |
16 | 19 | char itemData = 0;
|
17 |
| - if (args->size() > 3) |
| 20 | + if (args->size() > 3) { |
18 | 21 | itemData = static_cast<char>(assertInt(args->at(3)));
|
| 22 | + } |
19 | 23 |
|
20 | 24 | try {
|
21 | 25 | itemId = std::stoi(args->at(1));
|
22 |
| - } catch (const std::invalid_argument&) { |
| 26 | + } catch (const std::invalid_argument &) { |
23 | 27 | }
|
24 | 28 |
|
25 |
| - C_Inventory* inv = g_Data.getLocalPlayer()->getSupplies()->inventory; |
26 |
| - C_ItemStack* yot = nullptr; |
27 |
| - auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); |
| 29 | + //clientMessageF("%sDEBUG:%s Will give %d stacks!", RED, GREEN, stackCount); |
| 30 | + //clientMessageF("%sDEBUG:%s Will give %d as a remainder!", RED, GREEN, count); |
28 | 31 |
|
29 |
| - if (itemId == 0) { |
30 |
| - TextHolder tempText(args->at(1)); |
31 |
| - std::unique_ptr<void*> ItemPtr = std::make_unique<void*>(); |
32 |
| - std::unique_ptr<void*> buffer = std::make_unique<void*>(); |
33 |
| - C_Item*** cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), tempText); |
34 |
| - if (*cStack == nullptr) { |
35 |
| - clientMessageF("%sInvalid item name!", RED); |
36 |
| - return true; |
| 32 | + // Give us all the stacks of the items we want. |
| 33 | + for (unsigned int i = 0; i < stackCount; i++) { |
| 34 | + //clientMessageF("%sDEBUG:%s Giving stack %d of items!", RED, GREEN, i + 1); |
| 35 | + std::string tag; |
| 36 | + bool success = false; |
| 37 | + |
| 38 | + if (args->size() > 4) { |
| 39 | + std::string tag = Utils::getClipboardText(); |
37 | 40 | }
|
38 |
| - yot = new C_ItemStack(***cStack, count, itemData); |
39 |
| - } else { |
40 |
| - std::unique_ptr<void*> ItemPtr = std::make_unique<void*>(); |
41 |
| - C_Item*** cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); |
42 |
| - if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { |
43 |
| - clientMessageF("%sInvalid item ID!", RED); |
44 |
| - return true; |
| 41 | + if (itemId == 0) { |
| 42 | + TextHolder tempText(args->at(1)); |
| 43 | + success = giveItem(64, tempText, itemData, tag); |
| 44 | + } else { |
| 45 | + success = giveItem(64, itemId, itemData, tag); |
45 | 46 | }
|
46 |
| - yot = new C_ItemStack(***cStack, count, itemData); |
| 47 | + // If one of these fail. Then something went wrong. |
| 48 | + // Return to prevent a possible spam of error messages. |
| 49 | + if (!success) return true; |
47 | 50 | }
|
48 | 51 |
|
49 |
| - if (yot != nullptr) |
50 |
| - yot->count = count; |
51 |
| - |
52 |
| - int slot = inv->getFirstEmptySlot(); |
| 52 | + // Now give us our remainder. |
| 53 | + if (count >= 1) { |
| 54 | + //clientMessageF("%sDEBUG:%s Giving remaining %d items!", RED, GREEN, count); |
| 55 | + std::string tag; |
| 56 | + bool success = false; |
53 | 57 |
|
| 58 | + if (args->size() > 4) { |
| 59 | + std::string tag = Utils::getClipboardText(); |
| 60 | + } |
| 61 | + if (itemId == 0) { |
| 62 | + TextHolder tempText(args->at(1)); |
| 63 | + success = giveItem(count, tempText, itemData, tag); |
| 64 | + } else { |
| 65 | + success = giveItem(count, itemId, itemData, tag); |
| 66 | + } |
| 67 | + if (!success) return true; |
| 68 | + } |
| 69 | + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; |
| 70 | + C_ItemStack *item = g_Data.getLocalPlayer()->getSelectedItem(); |
54 | 71 | if (args->size() > 4) {
|
55 |
| - std::string tag = Utils::getClipboardText(); |
| 72 | + std::string tag; |
| 73 | + tag = Utils::getClipboardText(); |
| 74 | + if (args->size() > 4) { |
| 75 | + g_Data.getLocalPlayer()->getTransactionManager()->addInventoryAction(C_InventoryAction(0, nullptr, nullptr, item, nullptr, 1, 507, 99999)); |
| 76 | + } |
| 77 | + |
56 | 78 | if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) {
|
57 |
| - yot->setUserData(std::move(Mojangson::parseTag(tag))); |
| 79 | + if (args->size() > 4) { |
| 80 | + item->setUserData(std::move(Mojangson::parseTag(tag))); |
| 81 | + } |
| 82 | + } else { |
| 83 | + clientMessageF("%sInvalid NBT tag!", RED); |
| 84 | + return true; |
58 | 85 | }
|
| 86 | + |
| 87 | + if (args->size() > 4) { |
| 88 | + g_Data.getLocalPlayer()->getTransactionManager()->addInventoryAction(C_InventoryAction(0, nullptr, nullptr, item, nullptr, 1, 507, 99999)); |
| 89 | + } |
| 90 | + clientMessageF("%s%s", GREEN, "Successfully loaded mojangson !"); |
| 91 | + } |
| 92 | + if (args->size() > 4) { |
| 93 | + C_InventoryAction *firstAction = nullptr; |
| 94 | + auto transactionMan = g_Data.getLocalPlayer()->getTransactionManager(); |
| 95 | + firstAction = new C_InventoryAction(0, item, nullptr, 507, 99999); |
| 96 | + transactionMan->addInventoryAction(*firstAction); |
| 97 | + inv->addItemToFirstEmptySlot(item); |
| 98 | + } |
| 99 | + |
| 100 | + clientMessageF("%sSuccessfully gave items!", GREEN); |
| 101 | + return true; |
| 102 | +} |
| 103 | + |
| 104 | +bool GiveCommand::giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag) { |
| 105 | + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; |
| 106 | + C_ItemStack *itemStack = nullptr; |
| 107 | + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); |
| 108 | + |
| 109 | + std::unique_ptr<void *> ItemPtr = std::make_unique<void *>(); |
| 110 | + C_Item ***cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); |
| 111 | + if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { |
| 112 | + clientMessageF("%sInvalid item ID!", RED); |
| 113 | + return false; |
59 | 114 | }
|
| 115 | + itemStack = new C_ItemStack(***cStack, count, itemData); |
60 | 116 |
|
61 |
| - ItemDescriptor* desc = nullptr; |
62 |
| - desc = new ItemDescriptor((*yot->item)->itemId, itemData); |
| 117 | + if (itemStack != nullptr) { |
| 118 | + itemStack->count = count; |
| 119 | + } |
63 | 120 |
|
64 |
| - C_InventoryAction* firstAction = nullptr; |
65 |
| - C_InventoryAction* secondAction = nullptr; |
| 121 | + int slot = inv->getFirstEmptySlot(); |
66 | 122 |
|
67 |
| - firstAction = new C_InventoryAction(0, desc,nullptr,yot, nullptr,count, 507, 99999); |
68 |
| - secondAction = new C_InventoryAction(slot, nullptr, desc,nullptr, yot,count); |
| 123 | + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { |
| 124 | + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); |
| 125 | + itemStack->fromTag(*Mojangson::parseTag(tag)); |
| 126 | + } |
69 | 127 |
|
70 |
| - //firstAction = new C_InventoryAction(0,yot, nullptr, 507, 99999); |
71 |
| - //secondAction = new C_InventoryAction(slot, nullptr, yot); |
| 128 | + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); |
| 129 | + |
| 130 | + // If we add the second action, Only one stack will come through for some reason. |
| 131 | + // Otherwise all stacks will come through but will be buggy till dropped or |
| 132 | + // till the world is saved then reloaded. |
| 133 | + |
| 134 | + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); |
| 135 | + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); |
72 | 136 |
|
73 | 137 | transactionManager->addInventoryAction(*firstAction);
|
74 |
| - transactionManager->addInventoryAction(*secondAction); |
| 138 | + //transactionManager->addInventoryAction(*secondAction); |
75 | 139 |
|
76 | 140 | delete firstAction;
|
77 |
| - delete secondAction; |
| 141 | + //delete secondAction; |
78 | 142 | delete desc;
|
79 | 143 |
|
80 |
| - inv->addItemToFirstEmptySlot(yot); |
| 144 | + inv->addItemToFirstEmptySlot(itemStack); |
| 145 | + return true; |
| 146 | +} |
| 147 | + |
| 148 | +bool GiveCommand::giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag) { |
| 149 | + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; |
| 150 | + C_ItemStack *itemStack = nullptr; |
| 151 | + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); |
| 152 | + |
| 153 | + std::unique_ptr<void *> ItemPtr = std::make_unique<void *>(); |
| 154 | + std::unique_ptr<void *> buffer = std::make_unique<void *>(); |
| 155 | + C_Item ***cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), text); |
| 156 | + if (*cStack == nullptr) { |
| 157 | + clientMessageF("%sInvalid item name!", RED); |
| 158 | + return false; |
| 159 | + } |
| 160 | + itemStack = new C_ItemStack(***cStack, count, itemData); |
| 161 | + |
| 162 | + if (itemStack != nullptr) { |
| 163 | + itemStack->count = count; |
| 164 | + } |
| 165 | + |
| 166 | + int slot = inv->getFirstEmptySlot(); |
| 167 | + |
| 168 | + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { |
| 169 | + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); |
| 170 | + itemStack->fromTag(*Mojangson::parseTag(tag)); |
| 171 | + } |
| 172 | + |
| 173 | + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); |
| 174 | + |
| 175 | + // If we add the second action, Only one stack will come through for some reason. |
| 176 | + // Otherwise all stacks will come through but will be buggy till dropped or |
| 177 | + // till the world is saved then reloaded. |
| 178 | + |
| 179 | + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); |
| 180 | + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); |
| 181 | + |
| 182 | + transactionManager->addInventoryAction(*firstAction); |
| 183 | + //transactionManager->addInventoryAction(*secondAction); |
| 184 | + |
| 185 | + delete firstAction; |
| 186 | + //delete secondAction; |
| 187 | + delete desc; |
81 | 188 |
|
82 |
| - clientMessageF("%sSuccessfully given item!", GREEN); |
| 189 | + inv->addItemToFirstEmptySlot(itemStack); |
83 | 190 | return true;
|
84 | 191 | }
|
0 commit comments