enderal-se-easy-crafting/src/src/EventListener.cpp

103 lines
3.1 KiB
C++
Raw Normal View History

2022-08-17 21:43:01 +00:00
#include "EventListener.h"
#include "Util.h"
auto EventListener::GetSingleton() -> EventListener*
{
static EventListener singleton{};
return std::addressof(singleton);
}
void EventListener::Install()
{
SKSE::GetModCallbackEventSource()->AddEventSink(EventListener::GetSingleton());
RE::ScriptEventSourceHolder::GetSingleton()->AddEventSink<RE::TESActivateEvent>(EventListener::GetSingleton());
RE::UI::GetSingleton()->AddEventSink<RE::MenuOpenCloseEvent>(EventListener::GetSingleton());
}
auto EventListener::ProcessEvent(
const SKSE::ModCallbackEvent* a_event,
RE::BSTEventSource<SKSE::ModCallbackEvent>* a_eventSource)
-> RE::BSEventNotifyControl
{
if (a_event->eventName == "Enderal_StartCrafting") {
const auto refr = a_event->sender->As<RE::TESObjectREFR>();
if (refr) {
const auto furn = refr->GetBaseObject()->As<RE::TESFurniture>();
if (furn) {
SKSE::GetTaskInterface()->AddTask([furn]() {
FetchSupplies(furn);
});
}
}
}
return RE::BSEventNotifyControl::kContinue;
}
auto EventListener::ProcessEvent(
const RE::MenuOpenCloseEvent* a_event,
RE::BSTEventSource<RE::MenuOpenCloseEvent>* a_eventSource)
-> RE::BSEventNotifyControl
{
if (!a_event->opening && a_event->menuName == RE::CraftingMenu::MENU_NAME) {
SKSE::GetTaskInterface()->AddTask([]() {
ReturnSupplies();
});
}
return RE::BSEventNotifyControl::kContinue;
}
auto EventListener::ProcessEvent(
const RE::TESActivateEvent* a_event,
RE::BSTEventSource<RE::TESActivateEvent>* a_eventSource)
-> RE::BSEventNotifyControl
{
if (!a_event->actionRef->IsPlayerRef()) {
return RE::BSEventNotifyControl::kContinue;
}
const auto refr = a_event->objectActivated.get();
if (!refr || !refr->GetBaseObject()->Is(RE::FormType::Furniture)) {
return RE::BSEventNotifyControl::kContinue;
}
auto* furn = refr->GetBaseObject()->As<RE::TESFurniture>();
if (furn->workBenchData.benchType == RE::TESFurniture::WorkBenchData::BenchType::kNone) {
return RE::BSEventNotifyControl::kContinue;
}
if (RE::PlayerCharacter::GetSingleton()->GetRace()->As<RE::BGSKeywordForm>()->HasKeywordString("ActorTypeCreature")) {
// No crafting in the werewolf form
return RE::BSEventNotifyControl::kContinue;
}
if (furn->As<RE::BGSKeywordForm>()->HasKeywordString("WICraftingSmithing")) {
// Opens crafting menu without delay
SKSE::GetTaskInterface()->AddTask([furn]() {
FetchSupplies(furn);
});
2022-08-18 12:35:02 +00:00
std::thread([]() {
2022-08-17 21:43:01 +00:00
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!RE::UI::GetSingleton()->IsMenuOpen(RE::CraftingMenu::MENU_NAME)) {
2022-08-18 12:35:02 +00:00
SKSE::GetTaskInterface()->AddTask([]() {
ReturnSupplies();
});
2022-08-17 21:43:01 +00:00
}
}).detach();
} else {
std::thread([furn]() {
std::this_thread::sleep_for(std::chrono::milliseconds(300));
if (RE::PlayerCharacter::GetSingleton()->GetSitSleepState() == RE::SIT_SLEEP_STATE::kWaitingForSitAnim) {
SKSE::GetTaskInterface()->AddTask([furn]() {
FetchSupplies(furn);
});
}
}).detach();
}
return RE::BSEventNotifyControl::kContinue;
}