Skip to content

Commit

Permalink
feat(skymp5-server): add onUpdateAppearanceAttempt gamemode event (#2261
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Pospelove authored Dec 11, 2024
1 parent 064d62a commit 889833e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
18 changes: 14 additions & 4 deletions skymp5-server/cpp/server_guest_lib/ActionListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "gamemode_events/CraftEvent.h"
#include "gamemode_events/CustomEvent.h"
#include "gamemode_events/EatItemEvent.h"
#include "gamemode_events/UpdateAppearanceAttemptEvent.h"
#include "script_objects/EspmGameObject.h"
#include <fmt/format.h>
#include <spdlog/spdlog.h>
Expand Down Expand Up @@ -201,12 +202,21 @@ void ActionListener::OnUpdateAppearance(const RawMessageData& rawMsgData,
{ // TODO: validate

MpActor* actor = partOne.serverState.ActorByUser(rawMsgData.userId);
if (!actor || !actor->IsRaceMenuOpen())
if (!actor) {
return;
}

actor->SetRaceMenuOpen(false);
actor->SetAppearance(&appearance);
SendToNeighbours(idx, rawMsgData, true);
const bool isAllowed = actor->IsRaceMenuOpen();

if (isAllowed) {
actor->SetRaceMenuOpen(false);
actor->SetAppearance(&appearance);
SendToNeighbours(idx, rawMsgData, true);
}

UpdateAppearanceAttemptEvent updateAppearanceAttemptEvent(actor, appearance,
isAllowed);
updateAppearanceAttemptEvent.Fire(actor->GetParent());
}

void ActionListener::OnUpdateEquipment(
Expand Down
1 change: 1 addition & 0 deletions skymp5-server/cpp/server_guest_lib/Appearance.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Tint

struct Appearance
{
// TODO: port to archives
static Appearance FromJson(const nlohmann::json& j);
static Appearance FromJson(simdjson::dom::element& j);
std::string ToJson() const;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "UpdateAppearanceAttemptEvent.h"

#include "MpActor.h"
#include <spdlog/spdlog.h>

UpdateAppearanceAttemptEvent::UpdateAppearanceAttemptEvent(
MpActor* actor_, const Appearance& appearance_, bool isAllowed_)
: actor(actor_)
, appearance(appearance_)
, isAllowed(isAllowed_)
{
}

const char* UpdateAppearanceAttemptEvent::GetName() const
{
return "onUpdateAppearanceAttempt";
}

std::string UpdateAppearanceAttemptEvent::GetArgumentsJsonArray() const
{
std::string result;
result += "[";
result += std::to_string(actor->GetFormId());
result += ",";
result += appearance.ToJson();
result += ",";
result += isAllowed ? "true" : "false";
result += "]";
return result;
}

void UpdateAppearanceAttemptEvent::OnFireSuccess(WorldState* worldState)
{
}

void UpdateAppearanceAttemptEvent::OnFireBlocked(WorldState* worldState)
{
spdlog::warn(
"UpdateAppearanceAttemptEvent::OnFireBlocked - Not implemented. Please "
"consider never blocking {} event in gamemode",
GetName());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "GameModeEvent.h"

class MpActor;

#include "Appearance.h"

class UpdateAppearanceAttemptEvent : public GameModeEvent
{
public:
UpdateAppearanceAttemptEvent(MpActor* actor_, const Appearance& appearance_,
bool isAllowed_);

const char* GetName() const override;

std::string GetArgumentsJsonArray() const override;

private:
void OnFireSuccess(WorldState* worldState) override;
void OnFireBlocked(WorldState* worldState) override;

MpActor* actor = nullptr;
Appearance appearance;
bool isAllowed = false;
};

0 comments on commit 889833e

Please sign in to comment.