diff --git a/src/command/CommandTpa.cc b/src/command/CommandTpa.cc index 0983e9c..196c20d 100644 --- a/src/command/CommandTpa.cc +++ b/src/command/CommandTpa.cc @@ -58,7 +58,7 @@ void registerCommandWithTpa(const string& _commandName) { } else if (senders.size() == 1) { // 只有一个请求 auto req = pool->getRequest(receiver.getRealName(), senders[0]); - param.option == TpOption::accept ? req->accept() : req->deny(); + param.option == TpOption::accept ? req->_accept() : req->_deny(); return; } else { @@ -68,7 +68,7 @@ void registerCommandWithTpa(const string& _commandName) { fm.appendButton(sender, [sender, pool, param](Player& receiver2) { auto req = pool->getRequest(receiver2.getRealName(), sender); if (req->getAvailable() == tpa::Available::Available) { - param.option == TpOption::accept ? req->accept() : req->deny(); + param.option == TpOption::accept ? req->_accept() : req->_deny(); } }); } @@ -78,44 +78,37 @@ void registerCommandWithTpa(const string& _commandName) { ); // tps tpa - cmd.overload().text("tpa").required("type").required("target").execute([](CommandOrigin const& origin, - CommandOutput& output, - const ParamTp& param) { - CHECK_COMMAND_TYPE(output, origin, CommandOriginType::Player); - auto& sender = *static_cast(origin.getEntity()); - if (!Config::checkOpeningDimensions(Config::cfg.Tpa.OpenDimensions, sender.getDimensionId())) { - sendText(output, "当前维度不允许使用此功能!"_tr()); - return; - } + cmd.overload().text("tpa").required("type").required("target").execute( + [](CommandOrigin const& origin, CommandOutput& output, const ParamTp& param) { + CHECK_COMMAND_TYPE(output, origin, CommandOriginType::Player); + auto& sender = *static_cast(origin.getEntity()); + if (!Config::checkOpeningDimensions(Config::cfg.Tpa.OpenDimensions, sender.getDimensionId())) { + sendText(output, "当前维度不允许使用此功能!"_tr()); + return; + } - auto li = param.target.results(origin); - if (li.empty()) { - sendText(output, "请至少选择一位玩家!"_tr()); - return; - } else if (li.size() > 1) { - sendText(output, "仅支持对一位玩家发起TPA!"_tr()); - return; - } + auto li = param.target.results(origin); + if (li.empty()) { + sendText(output, "请至少选择一位玩家!"_tr()); + return; + } else if (li.size() > 1) { + sendText(output, "仅支持对一位玩家发起TPA!"_tr()); + return; + } - auto request = - std::make_shared(sender, *(*li.data)[0], param.type, Config::cfg.Tpa.CacheExpirationTime); + auto request = tpa::TpaRequestPool::getInstance().makeRequest(sender, *(*li.data)[0], param.type); - tpa::Available avail = request->ask(); // 发送请求 + tpa::Available avail = request->sendAskForm(); // 发送请求 - if (avail != tpa::Available::Available) { - sendText(sender, "{}"_tr(tpa::TpaRequest::getAvailableDescription(avail))); - } + if (avail != tpa::Available::Available) { + sendText(sender, "{}"_tr(tpa::TpaRequest::getAvailableDescription(avail))); + } - // Tpa 请求发送事件 - ll::event::EventBus::getInstance().publish(event::TpaRequestSendEvent( - request->sender, - request->receiver, - *request->time, - request->type, - request->lifespan - )); - }); + // Tpa 请求发送事件 + ll::event::EventBus::getInstance().publish(event::TpaRequestSendEvent(request)); + } + ); } } // namespace tps::command \ No newline at end of file diff --git a/src/event/EventManager.cc b/src/event/EventManager.cc index 7854ff4..2d70425 100644 --- a/src/event/EventManager.cc +++ b/src/event/EventManager.cc @@ -49,20 +49,28 @@ void registerEvent() { // 发送tpa请求事件 if (Config::cfg.Tpa.Enable) { mTpaRequestSendListener = eventBus.emplaceListener([](TpaRequestSendEvent& ev) { - auto sender = ll::service::getLevel()->getPlayer(ev.getSender()); // 获取发送者 - auto receiver = ll::service::getLevel()->getPlayer(ev.getReciever()); // 获取接收者 + auto req = ev.getRequest(); + + auto sender = ll::service::getLevel()->getPlayer(req->getSender()); // 获取发送者 + auto receiver = ll::service::getLevel()->getPlayer(req->getReceiver()); // 获取接收者 if (sender) { utils::mc::sendText( sender, - "已向 {} 发起 {} 请求."_tr(receiver->getRealName(), tpa::TpaRequest::tpaTypeToString(ev.getType())) + "已向 {} 发起 {} 请求."_tr( + receiver->getRealName(), + tpa::TpaRequest::tpaTypeToString(req->getType()) + ) ); } if (receiver) { utils::mc::sendText( receiver, - "收到来自 {} 的 {} 请求."_tr(sender->getRealName(), tpa::TpaRequest::tpaTypeToString(ev.getType())) + "收到来自 {} 的 {} 请求."_tr( + sender->getRealName(), + tpa::TpaRequest::tpaTypeToString(req->getType()) + ) ); } }); diff --git a/src/event/TpaRequestSendEvent.cc b/src/event/TpaRequestSendEvent.cc index 5e52285..df95fec 100644 --- a/src/event/TpaRequestSendEvent.cc +++ b/src/event/TpaRequestSendEvent.cc @@ -1,14 +1,8 @@ #include "TpaRequestSendEvent.h" -#include "utils/Date.h" namespace tps::event { -string const& TpaRequestSendEvent::getSender() { return mSender; } -string const& TpaRequestSendEvent::getReciever() { return mReciever; } -tpa::TpaType TpaRequestSendEvent::getType() { return mType; } -int TpaRequestSendEvent::getLifeSpan() { return mLifeSpan; } -utils::Date const& TpaRequestSendEvent::getTime() { return mTime; } - +tpa::TpaRequest* TpaRequestSendEvent::getRequest() const { return mRequest; } } // namespace tps::event \ No newline at end of file diff --git a/src/event/TpaRequestSendEvent.h b/src/event/TpaRequestSendEvent.h index e082d7d..e78c227 100644 --- a/src/event/TpaRequestSendEvent.h +++ b/src/event/TpaRequestSendEvent.h @@ -1,41 +1,16 @@ #include "ll/api/event/Event.h" #include "tpa/core/TpaRequest.h" -#include "utils/Date.h" -#include - - -using string = std::string; namespace tps::event { - class TpaRequestSendEvent final : public ll::event::Event { private: - string const& mSender; - string const& mReciever; - utils::Date const& mTime; - tpa::TpaType mType; - int mLifeSpan; + tpa::TpaRequest* mRequest; public: - constexpr explicit TpaRequestSendEvent( - string const& sender, - string const& reciever, - utils::Date const& time, - tpa::TpaType type, - int lifespan - ) - : mSender(sender), - mReciever(reciever), - mTime(time), - mType(type), - mLifeSpan(lifespan) {} + constexpr explicit TpaRequestSendEvent(tpa::TpaRequest* req) : mRequest(req) {} - string const& getSender(); - string const& getReciever(); - utils::Date const& getTime(); - tpa::TpaType getType(); - int getLifeSpan(); + tpa::TpaRequest* getRequest() const; }; diff --git a/src/tpa/core/TpaRequest.cc b/src/tpa/core/TpaRequest.cc index 9dd813a..616b89c 100644 --- a/src/tpa/core/TpaRequest.cc +++ b/src/tpa/core/TpaRequest.cc @@ -8,46 +8,57 @@ #include "mc/world/level/Level.h" #include "modules/Moneys.h" #include "rule/RuleManager.h" -#include "tpa/gui/TpaAskForm.h" -#include "utils/Date.h" #include "utils/Mc.h" -#include -#include -#include -#include namespace tps::tpa { + using ll::i18n_literals::operator""_tr; +TpaRequest::TpaRequest(Player& sender, Player& receiver, TpaType type) { + this->mSender = sender.getRealName(); + this->mReceiver = receiver.getRealName(); + this->mType = type; + this->mTime = Date(); + this->mLifespan = Config::cfg.Tpa.CacheExpirationTime; + + + // 构造表单 + string tpaDescription; + if (mType == TpaType::Tpa) { + tpaDescription = "{0} 希望传送到您这里"_tr(mSender); + } else if (mType == TpaType::TpaHere) { + tpaDescription = "{0} 希望将您传送至他那里"_tr(mSender); + } else { + tpaDescription = "未知请求类型"_tr(); + } -std::unordered_map> mAskList; -static uint64_t NextRequestId = 0; + mAskForm.setTitle("TPA Request"_tr()); + mAskForm.setContent(tpaDescription); -TpaRequest::TpaRequest(Player& sender, Player& receiver, TpaType type, int lifespan) : mRequestId(NextRequestId++) { - this->sender = sender.getRealName(); - this->receiver = receiver.getRealName(); - this->type = type; - this->time = std::make_unique(); - this->lifespan = lifespan; -} -TpaRequest::~TpaRequest() { - auto iter = mAskList.find(this->mRequestId); - if (iter != mAskList.end()) { - mAskList.erase(iter); - } + mAskForm.appendButton("接受"_tr(), "textures/ui/realms_green_check", "path", [this](Player&) { this->_accept(); }); + + mAskForm.appendButton("拒绝"_tr(), "textures/ui/realms_red_x", "path", [this](Player&) { this->_deny(); }); + + mAskForm.appendButton("暂时忽略\n(有效期至: {0})"_tr(mTime.toString()), "textures/ui/backup_replace", "path"); } +string const& TpaRequest::getSender() const { return mSender; } +string const& TpaRequest::getReceiver() const { return mReceiver; } +TpaType TpaRequest::getType() const { return mType; } +Date const& TpaRequest::getTime() const { return mTime; } +int TpaRequest::getLifespan() const { return mLifespan; } -void TpaRequest::destoryThisRequestFromPool() { +void TpaRequest::destroy() const { auto& pool = TpaRequestPool::getInstance(); - if (pool.hasRequest(receiver, sender)) { - pool.deleteRequest(receiver, sender); + if (pool.hasRequest(mReceiver, mSender)) { + pool.deleteRequest(mReceiver, mSender); } } -bool TpaRequest::isOutdated() { - if (Date{}.getTime() - this->time->getTime() >= this->lifespan) { +bool TpaRequest::isAvailable() const { return getAvailable() == Available::Available; } +bool TpaRequest::isOutdated() const { + if (Date{}.getTime() - this->mTime.getTime() >= this->mLifespan) { return true; } return false; @@ -56,27 +67,27 @@ bool TpaRequest::isOutdated() { using namespace ll::service; using namespace tps::utils::mc; -void TpaRequest::accept() { +void TpaRequest::_accept() const { Available avail = getAvailable(); if (avail != Available::Available) { if (avail != Available::SenderOffline) { - sendText(sender, "{}", getAvailableDescription(avail)); + sendText(mSender, "{}", getAvailableDescription(avail)); } return; } auto level = ll::service::getLevel(); - auto receiverPlayer = level->getPlayer(this->receiver); - auto senderPlayer = level->getPlayer(this->sender); + auto receiverPlayer = level->getPlayer(this->mReceiver); + auto senderPlayer = level->getPlayer(this->mSender); if (!receiverPlayer || !senderPlayer) { - destoryThisRequestFromPool(); // 销毁请求 + destroy(); // 销毁请求 return; } - if (type == TpaType::Tpa) { + if (mType == TpaType::Tpa) { senderPlayer ->teleport(receiverPlayer->getPosition(), receiverPlayer->getDimensionId(), senderPlayer->getRotation()); - } else if (type == TpaType::TpaHere) { + } else if (mType == TpaType::TpaHere) { receiverPlayer ->teleport(senderPlayer->getPosition(), senderPlayer->getDimensionId(), receiverPlayer->getRotation()); } @@ -84,64 +95,67 @@ void TpaRequest::accept() { // 扣除经济 tps::modules::Moneys::getInstance().reduceMoney(senderPlayer, Config::cfg.Tpa.Money); - sendText(senderPlayer, "'{0}' 接受了您的 '{1}' 请求。"_tr(receiver, tpaTypeToString(type))); - sendText(receiverPlayer, "您接受了来自 '{0}' 的 '{1}' 请求。"_tr(sender, tpaTypeToString(type))); + sendText(senderPlayer, "'{0}' 接受了您的 '{1}' 请求。"_tr(mReceiver, tpaTypeToString(mType))); + sendText( + receiverPlayer, + "您接受了来自 '{0}' 的 '{1}' 请求。"_tr(mSender, tpaTypeToString(mType)) + ); - destoryThisRequestFromPool(); // 销毁请求 + destroy(); // 销毁请求 } -void TpaRequest::deny() { - sendText(sender, "'{0}' 拒绝了您的 '{1}' 请求。"_tr(receiver, tpaTypeToString(type))); - sendText(receiver, "您拒绝了来自 '{0}' 的 '{1}' 请求。"_tr(sender, tpaTypeToString(type))); +void TpaRequest::_deny() const { + sendText(mSender, "'{0}' 拒绝了您的 '{1}' 请求。"_tr(mReceiver, tpaTypeToString(mType))); + sendText(mReceiver, "您拒绝了来自 '{0}' 的 '{1}' 请求。"_tr(mSender, tpaTypeToString(mType))); - destoryThisRequestFromPool(); // 销毁请求 + destroy(); // 销毁请求 } -Available TpaRequest::ask() { +Available TpaRequest::sendAskForm() { Available avail = getAvailable(); if (avail != Available::Available) { if (avail != Available::SenderOffline) { - sendText(sender, "{}", getAvailableDescription(avail)); + sendText(mSender, "{}", getAvailableDescription(avail)); } return avail; } // 创建询问表单 - auto ptr = std::make_unique(TpaRequestPtr(shared_from_this())); - // 移交表单所有权 - mAskList[this->mRequestId] = std::move(ptr); - - // 检查玩家是否接受弹窗, 接受则发送弹窗,否则缓存到请求池 - if (rule::RuleManager::getInstance().getPlayerRule(receiver).tpaPopup) { - ptr->sendTo(*ll::service::getLevel()->getPlayer(receiver)); // 发送弹窗给接收者 - } else { - ptr->cacheRequest(); // 缓存到请求池 + // 检查玩家是否接受弹窗, 接受则发送弹窗,否则 + if (rule::RuleManager::getInstance().getPlayerRule(mReceiver).tpaPopup) { + mAskForm.sendTo(*ll::service::getLevel()->getPlayer(mReceiver), [](Player&, int, ll::form::FormCancelReason) { + }); } + return avail; } - -Available TpaRequest::getAvailable() { +Available TpaRequest::getAvailable() const { if (isOutdated()) { return Available::Expired; } - if (getLevel()->getPlayer(sender) == nullptr) { + if (getLevel()->getPlayer(mSender) == nullptr) { return Available::SenderOffline; } - if (getLevel()->getPlayer(receiver) == nullptr) { + if (getLevel()->getPlayer(mReceiver) == nullptr) { return Available::RecieverOffline; } - if (modules::Moneys::getInstance().getMoney(sender) < Config::cfg.Tpa.Money && Config::cfg.Tpa.Money != 0) { + if (modules::Moneys::getInstance().getMoney(mSender) < Config::cfg.Tpa.Money && Config::cfg.Tpa.Money != 0) { return Available::Unaffordable; } // 检查对方是否禁止发送tpa请求 - if (!rule::RuleManager::getInstance().getPlayerRule(receiver).allowTpa) { + if (!rule::RuleManager::getInstance().getPlayerRule(mReceiver).allowTpa) { return Available::ProhibitTpaRequest; } return Available::Available; } +// TpaRequest::operator bool() const { return isAvailable(); } +bool TpaRequest::operator!=(const TpaRequest& other) const { return !(*this == other); } +bool TpaRequest::operator==(const TpaRequest& other) const { + return mSender == other.mSender && mReceiver == other.mReceiver && mType == other.mType; +} // static string TpaRequest::getAvailableDescription(Available avail) { diff --git a/src/tpa/core/TpaRequest.h b/src/tpa/core/TpaRequest.h index f7eaa5f..3f6da94 100644 --- a/src/tpa/core/TpaRequest.h +++ b/src/tpa/core/TpaRequest.h @@ -1,7 +1,7 @@ #pragma once +#include "ll/api/form/SimpleForm.h" #include "mc/world/actor/player/Player.h" #include "utils/Date.h" -#include #include #include @@ -26,43 +26,52 @@ enum class TpaType : int { }; -class TpaRequest : public std::enable_shared_from_this { +class TpaRequest { public: - string sender; // 请求者 - string receiver; // 接收者 - TpaType type; // 请求类型 - std::unique_ptr time; // 请求创建时间 - int lifespan; // 请求有效期 + TpaRequest(Player& sender, Player& receiver, TpaType type); - uint64_t const mRequestId; // 请求id + // getters + string const& getSender() const; + string const& getReceiver() const; + TpaType getType() const; + Date const& getTime() const; + int getLifespan() const; - TpaRequest(Player& sender, Player& receiver, TpaType type, int lifespan); - ~TpaRequest(); // 析构函数 + // others + bool isAvailable() const; // 判断请求是否可用 + bool isOutdated() const; // 判断请求是否过期 - bool operator==(const TpaRequest& other) const; - bool operator!=(const TpaRequest& other) const; + void _accept() const; // 接受请求 + void _deny() const; // 拒绝请求 - bool isOutdated(); // 判断请求是否过期 + Available sendAskForm(); // 发送请求表单 + Available getAvailable() const; // 获取可用状态 - void accept(); // 接受请求 - void deny(); // 拒绝请求 + void destroy() const; // 销毁请求 - Available ask(); // 向接受者发送请求 + // operators + bool operator==(const TpaRequest& other) const; + bool operator!=(const TpaRequest& other) const; + // explicit operator bool() const; // isAvailable - Available getAvailable(); // 获取可用状态 // static static string getAvailableDescription(Available avail); // 获取可用状态描述 static string tpaTypeToString(TpaType type); // 获取请求类型描述 private: - void destoryThisRequestFromPool(); // 销毁请求 - - TpaRequest(const TpaRequest&) = delete; // 禁止拷贝构造 - TpaRequest& operator=(const TpaRequest&) = delete; // 禁止拷贝赋值 + ll::form::SimpleForm mAskForm; // 请求表单 + string mSender; // 请求者 + string mReceiver; // 接收者 + TpaType mType; // 请求类型 + Date mTime; // 请求创建时间 + int mLifespan; // 请求有效期 + + // TpaRequest(const TpaRequest&) = delete; // 禁止拷贝构造 + // TpaRequest& operator=(const TpaRequest&) = delete; // 禁止拷贝赋值 + // TpaRequest(TpaRequest&&) = delete; // 禁止移动构造 + // TpaRequest& operator=(TpaRequest&&) = delete; // 禁止移动赋值 }; -using TpaRequestPtr = std::shared_ptr; - } // namespace tps::tpa diff --git a/src/tpa/core/TpaRequestPool.cc b/src/tpa/core/TpaRequestPool.cc index 6510f76..cde191b 100644 --- a/src/tpa/core/TpaRequestPool.cc +++ b/src/tpa/core/TpaRequestPool.cc @@ -6,6 +6,7 @@ #include "ll/api/schedule/Task.h" #include "ll/api/service/Bedrock.h" #include "tpa/core/TpaRequest.h" +#include #include #include #include @@ -53,7 +54,7 @@ void TpaRequestPool::_initTask() { } void TpaRequestPool::_initReceiver(const string& receiver) { if (mPool.find(receiver) == mPool.end()) { - mPool.emplace(string(receiver), std::unordered_map()); + mPool.emplace(string(receiver), std::unordered_map>()); } } @@ -79,11 +80,11 @@ bool TpaRequestPool::hasRequest(const string& receiver, const string& sender) co return true; } -bool TpaRequestPool::addRequest(TpaRequestPtr request) { +bool TpaRequestPool::addRequest(std::unique_ptr request) { auto& logger = entry::getInstance().getSelf().getLogger(); - string const& receiver = request->receiver; - string const& sender = request->sender; + string const& receiver = request->getReceiver(); + string const& sender = request->getSender(); _initReceiver(receiver); // 初始化接收者 @@ -139,7 +140,7 @@ std::vector TpaRequestPool::getSenderList(const string& receiver) const return senders; } -TpaRequestPtr TpaRequestPool::getRequest(const string& receiver, const string& sender) const { +TpaRequest* TpaRequestPool::getRequest(const string& receiver, const string& sender) const { auto senderPool = mPool.find(receiver); // receiver => sender => request if (senderPool == mPool.end()) { return nullptr; @@ -149,10 +150,10 @@ TpaRequestPtr TpaRequestPool::getRequest(const string& receiver, const string& s if (request == senderPool->second.end()) { return nullptr; } - return request->second; + return request->second.get(); } -std::unordered_map* TpaRequestPool::getSenderPool(const string& receiver) { +std::unordered_map>* TpaRequestPool::getSenderPool(const string& receiver) { auto senderPool = mPool.find(receiver); // receiver => sender => request if (senderPool == mPool.end()) { return nullptr; @@ -160,5 +161,10 @@ std::unordered_map* TpaRequestPool::getSenderPool(const s return &senderPool->second; } +TpaRequest* TpaRequestPool::makeRequest(Player& sender, Player& receiver, TpaType type) { + addRequest(std::make_unique(sender, receiver, type)); + return getRequest(receiver.getRealName(), sender.getRealName()); +} + } // namespace tps::tpa \ No newline at end of file diff --git a/src/tpa/core/TpaRequestPool.h b/src/tpa/core/TpaRequestPool.h index 3b0ae78..da49109 100644 --- a/src/tpa/core/TpaRequestPool.h +++ b/src/tpa/core/TpaRequestPool.h @@ -13,7 +13,7 @@ namespace tps::tpa { class TpaRequestPool { public: // 接收者 => 发起者池 => 发起者 => 请求实例 - std::unordered_map> mPool; + std::unordered_map>> mPool; TpaRequestPool() = default; TpaRequestPool(const TpaRequestPool&) = delete; @@ -23,7 +23,7 @@ class TpaRequestPool { bool hasRequest(const string& receiver, const string& sender) const; - bool addRequest(TpaRequestPtr request); + bool addRequest(std::unique_ptr request); // 添加请求实例 (需移交所有权) bool deleteRequest(const string& receiver, const string& sender); @@ -31,10 +31,11 @@ class TpaRequestPool { std::vector getSenderList(const string& receiver) const; // 获取所有发起者 - TpaRequestPtr getRequest(const string& receiver, const string& sender) const; // 获取请求实例 + TpaRequest* getRequest(const string& receiver, const string& sender) const; // 获取请求实例 - std::unordered_map* getSenderPool(const string& receiver); // 获取发起者池 + std::unordered_map>* getSenderPool(const string& receiver); // 获取发起者池 + TpaRequest* makeRequest(Player& sender, Player& receiver, TpaType type); // 创建请求实例 // private: void _initReceiver(const string& receiver); diff --git a/src/tpa/gui/TpaAskForm.cc b/src/tpa/gui/TpaAskForm.cc deleted file mode 100644 index 1694717..0000000 --- a/src/tpa/gui/TpaAskForm.cc +++ /dev/null @@ -1,65 +0,0 @@ -#include "TpaAskForm.h" -#include "ll/api/form/SimpleForm.h" -#include "ll/api/i18n/I18n.h" -#include "mc/world/actor/player/Player.h" -#include "tpa/core/TpaRequestPool.h" -#include "utils/Mc.h" -#include - - -namespace tps::tpa { - -using string = std::string; -using ll::i18n_literals::operator""_tr; -using namespace tps::utils::mc; -using namespace tps::utils; - -TpaAskForm::TpaAskForm(TpaRequestPtr request) { - this->request = request; - - string tpaDescription; - if (request->type == TpaType::Tpa) { - tpaDescription = format("{0} 希望传送到您这里"_tr(request->sender)); - } else if (request->type == TpaType::TpaHere) { - tpaDescription = format("{0} 希望将您传送至他那里"_tr(request->sender)); - } else { - tpaDescription = "未知请求类型"_tr(); - } - - form.setTitle("TPA Request"_tr()); - form.setContent(tpaDescription); - - form.appendButton("接受"_tr(), "textures/ui/realms_green_check", "path", [request](Player&) { request->accept(); }); - - form.appendButton("拒绝"_tr(), "textures/ui/realms_red_x", "path", [request](Player&) { request->deny(); }); - - form.appendButton("缓存本次请求"_tr(), "textures/ui/backup_replace", "path", [this, request](Player& p) { - cacheRequest(p); - }); -} -void TpaAskForm::sendTo(Player& player) { - using namespace ll::form; - form.sendTo(player, [this](Player& self, int idk, FormCancelReason) { - if (idk == -1) { - this->cacheRequest(self); // 表单被关闭 - } - }); -} - - -bool TpaAskForm::cacheRequest() { return TpaRequestPool::getInstance().addRequest(request); } -bool TpaAskForm::cacheRequest(Player& player) { - bool success = cacheRequest(); - if (success) { - sendText(player, "已缓存来自 {0} 的 {1} 请求"_tr(request->sender, TpaRequest::tpaTypeToString(request->type))); - return true; - } - sendText(player, "无法缓存来自 {0} 的 {1} 请求"_tr(request->sender, TpaRequest::tpaTypeToString(request->type))); - return false; -} - - -std::unique_ptr TpaAskForm::create(TpaRequestPtr request) { return std::make_unique(request); } - - -} // namespace tps::tpa \ No newline at end of file diff --git a/src/tpa/gui/TpaAskForm.h b/src/tpa/gui/TpaAskForm.h deleted file mode 100644 index 12523ce..0000000 --- a/src/tpa/gui/TpaAskForm.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "ll/api/form/SimpleForm.h" -#include "mc/world/actor/player/Player.h" -#include "mc/world/level/Level.h" -#include "tpa/core/TpaRequest.h" -#include - - -namespace tps::tpa { - -class TpaAskForm { -public: - TpaAskForm(TpaRequestPtr request); - - bool cacheRequest(Player& player); - bool cacheRequest(); - - void sendTo(Player& player); - - static std::unique_ptr create(TpaRequestPtr request); - -private: - ll::form::SimpleForm form; - TpaRequestPtr request; -}; - -} // namespace tps::tpa \ No newline at end of file diff --git a/src/tpa/gui/TpaEntry.h b/src/tpa/gui/TpaEntry.h index 2260085..f69627a 100644 --- a/src/tpa/gui/TpaEntry.h +++ b/src/tpa/gui/TpaEntry.h @@ -1,16 +1,5 @@ -#include "config/Config.h" -#include "ll/api/form/SimpleForm.h" -#include "ll/api/i18n/I18n.h" -#include "ll/api/service/Bedrock.h" #include "mc/world/actor/player/Player.h" #include "mc/world/level/Level.h" -#include "tpa/core/TpaRequest.h" -#include "tpa/core/TpaRequestPool.h" -#include "utils/Date.h" -#include "utils/Mc.h" -#include -#include - namespace tps::tpa { diff --git a/src/tpa/gui/TpaForm.cc b/src/tpa/gui/TpaForm.cc index a63acd5..bdb9404 100644 --- a/src/tpa/gui/TpaForm.cc +++ b/src/tpa/gui/TpaForm.cc @@ -6,6 +6,7 @@ #include "mc/world/actor/player/Player.h" #include "mc/world/level/Level.h" #include "tpa/core/TpaRequest.h" +#include "tpa/core/TpaRequestPool.h" #include "utils/Date.h" #include "utils/Mc.h" #include @@ -39,18 +40,16 @@ TpaForm::TpaForm(Player& player, TpaType type) { level->forEachPlayer([type, this](Player& target) { appendButton(target.getRealName(), [&target, type](Player& sender) { try { - auto req = std::make_shared(sender, target, type, Config::cfg.Tpa.CacheExpirationTime); + auto req = TpaRequestPool::getInstance().makeRequest(sender, target, type); // 发送请求 - tpa::Available avail = req->ask(); + tpa::Available avail = req->sendAskForm(); if (avail != tpa::Available::Available) { tps::utils::mc::sendText(sender, "{}", TpaRequest::getAvailableDescription(avail)); } // Tpa 请求发送事件 - ll::event::EventBus::getInstance().publish( - event::TpaRequestSendEvent(req->sender, req->receiver, *req->time, req->type, req->lifespan) - ); + ll::event::EventBus::getInstance().publish(event::TpaRequestSendEvent(req)); } catch (...) {} }); return true; diff --git a/src/tpa/gui/TpaForm.h b/src/tpa/gui/TpaForm.h index 93e2705..cccece8 100644 --- a/src/tpa/gui/TpaForm.h +++ b/src/tpa/gui/TpaForm.h @@ -1,14 +1,7 @@ #include "ll/api/form/SimpleForm.h" -#include "ll/api/i18n/I18n.h" -#include "ll/api/service/Bedrock.h" #include "mc/world/actor/player/Player.h" #include "mc/world/level/Level.h" #include "tpa/core/TpaRequest.h" -#include "tpa/core/TpaRequestPool.h" -#include "utils/Date.h" -#include "utils/Mc.h" -#include -#include namespace tps::tpa {