Skip to content
This repository was archived by the owner on Dec 26, 2021. It is now read-only.

Commit b36345a

Browse files
authored
Fixed .give nbt support. ex: .give dirt 1 0 1 (#4522)
* Fixed .give nbt support. ex: .give dirt 1 0 1 * Fixed .give nbt support. ex: .give dirt 1 0 1 Fixed .give nbt support. ex: .give dirt 1 0 1. Credits to theclashingfritz for the multiple stack support pull. * Fixed .give nbt support. ex: .give dirt 1 0 1 Fixed .give nbt support. ex: .give dirt 1 0 1. Credits to theclashingfritz for the multiple stack support pull.
1 parent c3f258b commit b36345a

File tree

2 files changed

+154
-43
lines changed

2 files changed

+154
-43
lines changed
+149-42
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,191 @@
11
#include "GiveCommand.h"
2-
#include "../../../Utils/Utils.h"
2+
33
#include "../../../SDK/Tag.h"
4+
#include "../../../Utils/Utils.h"
45

56
GiveCommand::GiveCommand() : IMCCommand("give", "spawn items", "<itemName> <count> [itemData] [NBT]") {
67
}
78

89
GiveCommand::~GiveCommand() {
910
}
1011

11-
bool GiveCommand::execute(std::vector<std::string>* args) {
12+
bool GiveCommand::execute(std::vector<std::string> *args) {
1213
assertTrue(args->size() > 2);
1314

1415
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.
1619
char itemData = 0;
17-
if (args->size() > 3)
20+
if (args->size() > 3) {
1821
itemData = static_cast<char>(assertInt(args->at(3)));
22+
}
1923

2024
try {
2125
itemId = std::stoi(args->at(1));
22-
} catch (const std::invalid_argument&) {
26+
} catch (const std::invalid_argument &) {
2327
}
2428

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);
2831

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();
3740
}
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);
4546
}
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;
4750
}
4851

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;
5357

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();
5471
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+
5678
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;
5885
}
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;
59114
}
115+
itemStack = new C_ItemStack(***cStack, count, itemData);
60116

61-
ItemDescriptor* desc = nullptr;
62-
desc = new ItemDescriptor((*yot->item)->itemId, itemData);
117+
if (itemStack != nullptr) {
118+
itemStack->count = count;
119+
}
63120

64-
C_InventoryAction* firstAction = nullptr;
65-
C_InventoryAction* secondAction = nullptr;
121+
int slot = inv->getFirstEmptySlot();
66122

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+
}
69127

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);
72136

73137
transactionManager->addInventoryAction(*firstAction);
74-
transactionManager->addInventoryAction(*secondAction);
138+
//transactionManager->addInventoryAction(*secondAction);
75139

76140
delete firstAction;
77-
delete secondAction;
141+
//delete secondAction;
78142
delete desc;
79143

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;
81188

82-
clientMessageF("%sSuccessfully given item!", GREEN);
189+
inv->addItemToFirstEmptySlot(itemStack);
83190
return true;
84191
}

Horion/Command/Commands/GiveCommand.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ class GiveCommand : public IMCCommand {
66
~GiveCommand();
77

88
// Inherited via IMCCommand
9-
virtual bool execute(std::vector<std::string>* args) override;
9+
virtual bool execute(std::vector<std::string> *args) override;
10+
11+
private:
12+
bool giveItem(uint8_t count, int itemId, uint8_t itemData, std::string &tag);
13+
bool giveItem(uint8_t count, TextHolder &text, uint8_t itemData, std::string &tag);
1014
};

0 commit comments

Comments
 (0)