add enchantments (#7)

* adding enchantments

added enchantments, changed minor apis to ensure c# stays synced with the c++ server

* add missing enchant apis, oops

* add proper docs, and fix namings for stuff in itemmeta

---------

Co-authored-by: sylvessa <225480449+sylvessa@users.noreply.github.com>
This commit is contained in:
DrPerkyLegit 2026-03-28 14:33:54 -04:00 committed by GitHub
parent 921abd48a7
commit 017f42b7cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 1181 additions and 4 deletions

View file

@ -41,6 +41,7 @@
#include "Access\Access.h"
#include "Common\NetworkUtils.h"
#include "ServerLogManager.h"
#include "../Minecraft.World/ItemInstance.cpp"
namespace
{
@ -751,10 +752,12 @@ void __cdecl NativeOpenVirtualContainer(int entityId, int nativeType, const char
player->openContainer(container);
}
//didnt update this for enchants
// [nameLen:int32][nameUTF8:bytes][loreCount:int32][lore0Len:int32][lore0UTF8:bytes]
int __cdecl NativeGetItemMeta(int entityId, int slot, char *outBuf, int bufSize)
{
printf("NativeGetItemMeta::entry\n");
auto player = FindPlayer(entityId);
if (!player || !player->inventory)
return 0;
@ -775,6 +778,8 @@ int __cdecl NativeGetItemMeta(int entityId, int slot, char *outBuf, int bufSize)
bool hasName = display->contains(L"Name");
bool hasLore = display->contains(L"Lore");
bool hasEnchantments = item->isEnchanted();
if (!hasName && !hasLore)
return 0;
@ -827,6 +832,34 @@ int __cdecl NativeGetItemMeta(int entityId, int slot, char *outBuf, int bufSize)
offset += 4;
}
if (hasEnchantments) {
ListTag<CompoundTag>* list = item->getEnchantmentTags();
if (list != nullptr) {
int listSize = list->size();
if ((offset + 4 + (listSize * (4 + 4))) > bufSize) return 0;
memcpy(outBuf + offset, &listSize, 4);
offset += 4;
for (int i = 0; i < listSize; i++) {
int type = list->get(i)->getShort((wchar_t*)ItemInstance::TAG_ENCH_ID);
int level = list->get(i)->getShort((wchar_t*)ItemInstance::TAG_ENCH_LEVEL);
memcpy(outBuf + offset, &type, 4);
offset += 4;
memcpy(outBuf + offset, &level, 4);
offset += 4;
}
}
}
else {
int zero = 0;
if (offset + 4 > bufSize) return 0;
memcpy(outBuf + offset, &zero, 4);
offset += 4;
}
return offset;
}
@ -861,6 +894,11 @@ void __cdecl NativeSetItemMeta(int entityId, int slot, const char *inBuf, int bu
item->setTag(nullptr);
}
}
if (tag && tag->contains(L"ench"))
{
tag->remove(L"ench");
}
}
return;
}
@ -925,6 +963,47 @@ void __cdecl NativeSetItemMeta(int entityId, int slot, const char *inBuf, int bu
tag->getCompound(L"display")->remove(L"Lore");
}
}
if (offset + 4 > bufSize) return;
int enchantCount = 0;
memcpy(&enchantCount, inBuf + offset, 4);
offset += 4;
if (enchantCount > 0)
{
if (!item->hasTag()) item->setTag(new CompoundTag());
CompoundTag* tag = item->getTag();
if (!tag->contains(L"ench")) tag->put(L"ench", new ListTag<CompoundTag>(L"ench"));
ListTag<CompoundTag>* enchantments = static_cast<ListTag<CompoundTag> *>(tag->get(L"ench"));
for (int i = 0; i < enchantCount; i++) {
if (offset + (4 + 4) > bufSize) break;
int type = 0;
memcpy(&type, inBuf + offset, 4);
offset += 4;
int level = 0;
memcpy(&level, inBuf + offset, 4);
offset += 4;
CompoundTag* ench = new CompoundTag();
ench->putShort((wchar_t*)ItemInstance::TAG_ENCH_ID, static_cast<short>(type));
ench->putShort((wchar_t*)ItemInstance::TAG_ENCH_LEVEL, static_cast<byte>(level));
enchantments->add(ench);
}
}
else
{
if (item->hasTag())
{
CompoundTag* tag = item->getTag();
if (tag && tag->contains(L"ench"))
{
tag->remove(L"ench");
}
}
}
}
void __cdecl NativeSetHeldItemSlot(int entityId, int slot)