From 1f5f098b99c6555ffcd808cf17fc03429b4f63e9 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 8 Sep 2021 11:48:41 -0400 Subject: [PATCH 1/3] Rewrite .give command to support multiple item stacks. --- Horion/Command/Commands/GiveCommand.cpp | 178 +++++++++++++++++------- Horion/Command/Commands/GiveCommand.h | 3 + 2 files changed, 130 insertions(+), 51 deletions(-) diff --git a/Horion/Command/Commands/GiveCommand.cpp b/Horion/Command/Commands/GiveCommand.cpp index 9612f8c94..c2e036974 100644 --- a/Horion/Command/Commands/GiveCommand.cpp +++ b/Horion/Command/Commands/GiveCommand.cpp @@ -12,73 +12,149 @@ bool GiveCommand::execute(std::vector* args) { assertTrue(args->size() > 2); int itemId = 0; - char count = static_cast(assertInt(args->at(2))); + uint32_t fullCount = static_cast(assertInt(args->at(2))); + unsigned int stackCount = fullCount / 64; // Get the amount of stacks we have. + char count = fullCount % 64; // Get the amount we have left. char itemData = 0; - if (args->size() > 3) + if (args->size() > 3) { itemData = static_cast(assertInt(args->at(3))); + } try { itemId = std::stoi(args->at(1)); } catch (const std::invalid_argument&) { } + + //clientMessageF("%sDEBUG:%s Will give %d stacks!", RED, GREEN, stackCount); + //clientMessageF("%sDEBUG:%s Will give %d as a remainder!", RED, GREEN, count); + + // Give us all the stacks of the items we want. + for (unsigned int i = 0; i < stackCount; i++) { + //clientMessageF("%sDEBUG:%s Giving stack %d of items!", RED, GREEN, i + 1); + std::string tag; + bool success = false; + + if (args->size() > 4) { + std::string tag = Utils::getClipboardText(); + } + if (itemId == 0) { + TextHolder tempText(args->at(1)); + success = giveItem(64, tempText, itemData, tag); + } else { + success = giveItem(64, itemId, itemData, tag); + } + // If one of these fail. Then something went wrong. + // Return to prevent a possible spam of error messages. + if (!success) return true; + } + + // Now give us our remainder. + if (count >= 1) { + //clientMessageF("%sDEBUG:%s Giving remaining %d items!", RED, GREEN, count); + std::string tag; + bool success = false; + + if (args->size() > 4) { + std::string tag = Utils::getClipboardText(); + } + if (itemId == 0) { + TextHolder tempText(args->at(1)); + success = giveItem(count, tempText, itemData, tag); + } else { + success = giveItem(count, itemId, itemData, tag); + } + if (!success) return true; + } + + clientMessageF("%sSuccessfully gave items!", GREEN); + return true; +} - C_Inventory* inv = g_Data.getLocalPlayer()->getSupplies()->inventory; - C_ItemStack* yot = nullptr; - auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); - - if (itemId == 0) { - TextHolder tempText(args->at(1)); - std::unique_ptr ItemPtr = std::make_unique(); - std::unique_ptr buffer = std::make_unique(); - C_Item*** cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), tempText); - if (*cStack == nullptr) { - clientMessageF("%sInvalid item name!", RED); - return true; - } - yot = new C_ItemStack(***cStack, count, itemData); - } else { - std::unique_ptr ItemPtr = std::make_unique(); - C_Item*** cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); - if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { - clientMessageF("%sInvalid item ID!", RED); - return true; - } - yot = new C_ItemStack(***cStack, count, itemData); - } +bool GiveCommand::giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag) { + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; + C_ItemStack *itemStack = nullptr; + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); - if (yot != nullptr) - yot->count = count; + std::unique_ptr ItemPtr = std::make_unique(); + C_Item ***cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); + if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { + clientMessageF("%sInvalid item ID!", RED); + return false; + } + itemStack = new C_ItemStack(***cStack, count, itemData); - int slot = inv->getFirstEmptySlot(); + if (itemStack != nullptr) { + itemStack->count = count; + } - if (args->size() > 4) { - std::string tag = Utils::getClipboardText(); - if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { - yot->setUserData(std::move(Mojangson::parseTag(tag))); - } - } + int slot = inv->getFirstEmptySlot(); - ItemDescriptor* desc = nullptr; - desc = new ItemDescriptor((*yot->item)->itemId, itemData); + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); + itemStack->fromTag(*Mojangson::parseTag(tag)); + } - C_InventoryAction* firstAction = nullptr; - C_InventoryAction* secondAction = nullptr; + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); + + // If we add the second action, Only one stack will come through for some reason. + // Otherwise all stacks will come through but will be buggy till dropped or + // till the world is saved then reloaded. - firstAction = new C_InventoryAction(0, desc,nullptr,yot, nullptr,count, 507, 99999); - secondAction = new C_InventoryAction(slot, nullptr, desc,nullptr, yot,count); + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); - //firstAction = new C_InventoryAction(0,yot, nullptr, 507, 99999); - //secondAction = new C_InventoryAction(slot, nullptr, yot); + transactionManager->addInventoryAction(*firstAction); + //transactionManager->addInventoryAction(*secondAction); - transactionManager->addInventoryAction(*firstAction); - transactionManager->addInventoryAction(*secondAction); + delete firstAction; + //delete secondAction; + delete desc; - delete firstAction; - delete secondAction; - delete desc; - - inv->addItemToFirstEmptySlot(yot); + inv->addItemToFirstEmptySlot(itemStack); + return true; +} - clientMessageF("%sSuccessfully given item!", GREEN); - return true; +bool GiveCommand::giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag) { + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; + C_ItemStack *itemStack = nullptr; + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); + + std::unique_ptr ItemPtr = std::make_unique(); + std::unique_ptr buffer = std::make_unique(); + C_Item ***cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), text); + if (*cStack == nullptr) { + clientMessageF("%sInvalid item name!", RED); + return false; + } + itemStack = new C_ItemStack(***cStack, count, itemData); + + if (itemStack != nullptr) { + itemStack->count = count; + } + + int slot = inv->getFirstEmptySlot(); + + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); + itemStack->fromTag(*Mojangson::parseTag(tag)); + } + + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); + + // If we add the second action, Only one stack will come through for some reason. + // Otherwise all stacks will come through but will be buggy till dropped or + // till the world is saved then reloaded. + + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); + + transactionManager->addInventoryAction(*firstAction); + //transactionManager->addInventoryAction(*secondAction); + + delete firstAction; + //delete secondAction; + delete desc; + + inv->addItemToFirstEmptySlot(itemStack); + return true; } diff --git a/Horion/Command/Commands/GiveCommand.h b/Horion/Command/Commands/GiveCommand.h index f9cf9fd82..f25170bb8 100644 --- a/Horion/Command/Commands/GiveCommand.h +++ b/Horion/Command/Commands/GiveCommand.h @@ -7,4 +7,7 @@ class GiveCommand : public IMCCommand { // Inherited via IMCCommand virtual bool execute(std::vector* args) override; +private: + bool giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag); + bool giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag); }; From 7e8f7be959038f850f1b79e7cf26cd4f5b92fef5 Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 11 Sep 2021 05:17:01 -0400 Subject: [PATCH 2/3] Redo indents for GiveCommand.cpp --- Horion/Command/Commands/GiveCommand.cpp | 224 ++++++++++++------------ 1 file changed, 112 insertions(+), 112 deletions(-) diff --git a/Horion/Command/Commands/GiveCommand.cpp b/Horion/Command/Commands/GiveCommand.cpp index c2e036974..915695bc1 100644 --- a/Horion/Command/Commands/GiveCommand.cpp +++ b/Horion/Command/Commands/GiveCommand.cpp @@ -13,148 +13,148 @@ bool GiveCommand::execute(std::vector* args) { int itemId = 0; uint32_t fullCount = static_cast(assertInt(args->at(2))); - unsigned int stackCount = fullCount / 64; // Get the amount of stacks we have. - char count = fullCount % 64; // Get the amount we have left. + unsigned int stackCount = fullCount / 64; // Get the amount of stacks we have. + char count = fullCount % 64; // Get the amount we have left. char itemData = 0; if (args->size() > 3) { itemData = static_cast(assertInt(args->at(3))); - } + } try { itemId = std::stoi(args->at(1)); } catch (const std::invalid_argument&) { } - //clientMessageF("%sDEBUG:%s Will give %d stacks!", RED, GREEN, stackCount); - //clientMessageF("%sDEBUG:%s Will give %d as a remainder!", RED, GREEN, count); + //clientMessageF("%sDEBUG:%s Will give %d stacks!", RED, GREEN, stackCount); + //clientMessageF("%sDEBUG:%s Will give %d as a remainder!", RED, GREEN, count); - // Give us all the stacks of the items we want. - for (unsigned int i = 0; i < stackCount; i++) { - //clientMessageF("%sDEBUG:%s Giving stack %d of items!", RED, GREEN, i + 1); - std::string tag; - bool success = false; - - if (args->size() > 4) { - std::string tag = Utils::getClipboardText(); - } - if (itemId == 0) { - TextHolder tempText(args->at(1)); - success = giveItem(64, tempText, itemData, tag); - } else { - success = giveItem(64, itemId, itemData, tag); - } - // If one of these fail. Then something went wrong. - // Return to prevent a possible spam of error messages. - if (!success) return true; - } + // Give us all the stacks of the items we want. + for (unsigned int i = 0; i < stackCount; i++) { + //clientMessageF("%sDEBUG:%s Giving stack %d of items!", RED, GREEN, i + 1); + std::string tag; + bool success = false; + + if (args->size() > 4) { + std::string tag = Utils::getClipboardText(); + } + if (itemId == 0) { + TextHolder tempText(args->at(1)); + success = giveItem(64, tempText, itemData, tag); + } else { + success = giveItem(64, itemId, itemData, tag); + } + // If one of these fail. Then something went wrong. + // Return to prevent a possible spam of error messages. + if (!success) return true; + } - // Now give us our remainder. - if (count >= 1) { - //clientMessageF("%sDEBUG:%s Giving remaining %d items!", RED, GREEN, count); - std::string tag; - bool success = false; - - if (args->size() > 4) { - std::string tag = Utils::getClipboardText(); - } - if (itemId == 0) { - TextHolder tempText(args->at(1)); - success = giveItem(count, tempText, itemData, tag); - } else { - success = giveItem(count, itemId, itemData, tag); - } - if (!success) return true; - } + // Now give us our remainder. + if (count >= 1) { + //clientMessageF("%sDEBUG:%s Giving remaining %d items!", RED, GREEN, count); + std::string tag; + bool success = false; + + if (args->size() > 4) { + std::string tag = Utils::getClipboardText(); + } + if (itemId == 0) { + TextHolder tempText(args->at(1)); + success = giveItem(count, tempText, itemData, tag); + } else { + success = giveItem(count, itemId, itemData, tag); + } + if (!success) return true; + } clientMessageF("%sSuccessfully gave items!", GREEN); return true; } bool GiveCommand::giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag) { - C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; - C_ItemStack *itemStack = nullptr; - auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); - - std::unique_ptr ItemPtr = std::make_unique(); - C_Item ***cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); - if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { - clientMessageF("%sInvalid item ID!", RED); - return false; - } - itemStack = new C_ItemStack(***cStack, count, itemData); - - if (itemStack != nullptr) { - itemStack->count = count; - } - - int slot = inv->getFirstEmptySlot(); - - if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { - //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); - itemStack->fromTag(*Mojangson::parseTag(tag)); - } - - ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; + C_ItemStack *itemStack = nullptr; + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); + + std::unique_ptr ItemPtr = std::make_unique(); + C_Item ***cStack = ItemRegistry::getItemFromId(ItemPtr.get(), itemId); + if (cStack == nullptr || *cStack == nullptr || **cStack == nullptr) { + clientMessageF("%sInvalid item ID!", RED); + return false; + } + itemStack = new C_ItemStack(***cStack, count, itemData); + + if (itemStack != nullptr) { + itemStack->count = count; + } + + int slot = inv->getFirstEmptySlot(); + + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); + itemStack->fromTag(*Mojangson::parseTag(tag)); + } + + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); - // If we add the second action, Only one stack will come through for some reason. - // Otherwise all stacks will come through but will be buggy till dropped or - // till the world is saved then reloaded. + // If we add the second action, Only one stack will come through for some reason. + // Otherwise all stacks will come through but will be buggy till dropped or + // till the world is saved then reloaded. - C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); - //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); - transactionManager->addInventoryAction(*firstAction); - //transactionManager->addInventoryAction(*secondAction); + transactionManager->addInventoryAction(*firstAction); + //transactionManager->addInventoryAction(*secondAction); - delete firstAction; - //delete secondAction; - delete desc; + delete firstAction; + //delete secondAction; + delete desc; - inv->addItemToFirstEmptySlot(itemStack); - return true; + inv->addItemToFirstEmptySlot(itemStack); + return true; } bool GiveCommand::giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag) { - C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; - C_ItemStack *itemStack = nullptr; - auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); - - std::unique_ptr ItemPtr = std::make_unique(); - std::unique_ptr buffer = std::make_unique(); - C_Item ***cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), text); - if (*cStack == nullptr) { - clientMessageF("%sInvalid item name!", RED); - return false; - } - itemStack = new C_ItemStack(***cStack, count, itemData); - - if (itemStack != nullptr) { - itemStack->count = count; - } - - int slot = inv->getFirstEmptySlot(); - - if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { - //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); - itemStack->fromTag(*Mojangson::parseTag(tag)); - } + C_Inventory *inv = g_Data.getLocalPlayer()->getSupplies()->inventory; + C_ItemStack *itemStack = nullptr; + auto transactionManager = g_Data.getLocalPlayer()->getTransactionManager(); + + std::unique_ptr ItemPtr = std::make_unique(); + std::unique_ptr buffer = std::make_unique(); + C_Item ***cStack = ItemRegistry::lookUpByName(ItemPtr.get(), buffer.get(), text); + if (*cStack == nullptr) { + clientMessageF("%sInvalid item name!", RED); + return false; + } + itemStack = new C_ItemStack(***cStack, count, itemData); + + if (itemStack != nullptr) { + itemStack->count = count; + } + + int slot = inv->getFirstEmptySlot(); + + if (tag.size() > 1 && tag.front() == MojangsonToken::COMPOUND_START.getSymbol() && tag.back() == MojangsonToken::COMPOUND_END.getSymbol()) { + //itemStack->setUserData(std::move(Mojangson::parseTag(tag))); + itemStack->fromTag(*Mojangson::parseTag(tag)); + } - ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); + ItemDescriptor *desc = new ItemDescriptor((*itemStack->item)->itemId, itemData); - // If we add the second action, Only one stack will come through for some reason. - // Otherwise all stacks will come through but will be buggy till dropped or + // If we add the second action, Only one stack will come through for some reason. + // Otherwise all stacks will come through but will be buggy till dropped or // till the world is saved then reloaded. - C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); - //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); + C_InventoryAction *firstAction = new C_InventoryAction(slot, desc, nullptr, itemStack, nullptr, count, 507, 99999); + //C_InventoryAction *secondAction = new C_InventoryAction(slot, nullptr, desc, nullptr, itemStack, count); - transactionManager->addInventoryAction(*firstAction); - //transactionManager->addInventoryAction(*secondAction); + transactionManager->addInventoryAction(*firstAction); + //transactionManager->addInventoryAction(*secondAction); - delete firstAction; - //delete secondAction; - delete desc; + delete firstAction; + //delete secondAction; + delete desc; - inv->addItemToFirstEmptySlot(itemStack); - return true; + inv->addItemToFirstEmptySlot(itemStack); + return true; } From e4dd1e9103073574267f37bc85d84a84637d1e79 Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 11 Sep 2021 05:18:02 -0400 Subject: [PATCH 3/3] Redo indents for GiveCommand,h --- Horion/Command/Commands/GiveCommand.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Horion/Command/Commands/GiveCommand.h b/Horion/Command/Commands/GiveCommand.h index f25170bb8..7f0714918 100644 --- a/Horion/Command/Commands/GiveCommand.h +++ b/Horion/Command/Commands/GiveCommand.h @@ -8,6 +8,6 @@ class GiveCommand : public IMCCommand { // Inherited via IMCCommand virtual bool execute(std::vector* args) override; private: - bool giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag); - bool giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag); + bool giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag); + bool giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag); };