Skip to content

Commit

Permalink
feat(skymp5-server): add onCraft gamemode event (skyrim-multiplayer#1733
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Pospelove authored Nov 10, 2023
1 parent ea3186c commit c4cea74
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
22 changes: 17 additions & 5 deletions skymp5-server/cpp/server_guest_lib/ActionListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,22 @@ void ActionListener::OnConsoleCommand(
ConsoleCommands::Execute(*me, consoleCommandName, args);
}

void UseCraftRecipe(MpActor* me, espm::COBJ::Data recipeData,
void UseCraftRecipe(MpActor* me, const espm::COBJ* recipeUsed,
espm::CompressedFieldsCache& cache,
const espm::CombineBrowser& br, int espmIdx)
{
auto recipeData = recipeUsed->GetData(cache);
auto mapping = br.GetCombMapping(espmIdx);

std::vector<Inventory::Entry> entries;
for (auto& entry : recipeData.inputObjects) {
auto formId = espm::utils::GetMappedId(entry.formId, *mapping);
entries.push_back({ formId, entry.count });
}

auto outputFormId =
espm::utils::GetMappedId(recipeData.outputObjectFormId, *mapping);

if (spdlog::should_log(spdlog::level::debug)) {
std::string s = fmt::format("User formId={:#x} crafted", me->GetFormId());
for (const auto& entry : entries) {
Expand All @@ -382,8 +387,16 @@ void UseCraftRecipe(MpActor* me, espm::COBJ::Data recipeData,
s += fmt::format(" +{:#x} x{}", outputFormId, recipeData.outputCount);
spdlog::debug("{}", s);
}
me->RemoveItems(entries);
me->AddItem(outputFormId, recipeData.outputCount);

auto recipeId = espm::utils::GetMappedId(recipeUsed->GetId(), *mapping);

if (me->MpApiCraft(outputFormId, recipeData.outputCount, recipeId)) {
spdlog::trace("onCraft - not blocked by gamemode");
me->RemoveItems(entries);
me->AddItem(outputFormId, recipeData.outputCount);
} else {
spdlog::trace("onCraft - blocked by gamemode");
}
}

void ActionListener::OnCraftItem(const RawMessageData& rawMsgData,
Expand Down Expand Up @@ -422,8 +435,7 @@ void ActionListener::OnCraftItem(const RawMessageData& rawMsgData,
throw std::runtime_error("Unable to craft without Actor attached");
}

auto recipeData = recipeUsed->GetData(cache);
UseCraftRecipe(me, recipeData, br, espmIdx);
UseCraftRecipe(me, recipeUsed, cache, br, espmIdx);
}

void ActionListener::OnHostAttempt(const RawMessageData& rawMsgData,
Expand Down
26 changes: 24 additions & 2 deletions skymp5-server/cpp/server_guest_lib/MpActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,7 @@ void MpActor::MpApiDeath(MpActor* killer)
simdjson::dom::parser parser;
bool isRespawnBlocked = false;

std::string s =
"[" + std::to_string(killer ? killer->GetFormId() : 0) + " ]";
std::string s = "[" + std::to_string(killer ? killer->GetFormId() : 0) + "]";
auto args = parser.parse(s).value();

if (auto wst = GetParent()) {
Expand All @@ -614,11 +613,34 @@ void MpActor::MpApiDeath(MpActor* killer)
};
}
}

if (!isRespawnBlocked) {
RespawnWithDelay();
}
}

bool MpActor::MpApiCraft(uint32_t craftedItemBaseId, uint32_t count,
uint32_t recipeId)
{
simdjson::dom::parser parser;
bool isCraftBlocked = false;

std::string s = "[" + std::to_string(craftedItemBaseId) + "," +
std::to_string(count) + "," + std::to_string(recipeId) + "]";
auto args = parser.parse(s).value();

if (auto wst = GetParent()) {
const auto id = GetFormId();
for (auto& listener : wst->listeners) {
if (listener->OnMpApiEvent("onCraft", args, id) == false) {
isCraftBlocked = true;
};
}
}

return !isCraftBlocked;
}

void MpActor::EatItem(uint32_t baseId, espm::Type t)
{
auto espmProvider = GetParent();
Expand Down
3 changes: 3 additions & 0 deletions skymp5-server/cpp/server_guest_lib/MpActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class MpActor : public MpObjectReference

void EquipBestWeapon();

bool MpApiCraft(uint32_t craftedItemBaseId, uint32_t count,
uint32_t recipeId);

private:
struct Impl;
std::shared_ptr<Impl> pImpl;
Expand Down

0 comments on commit c4cea74

Please sign in to comment.