删除和修改

删除新增的handleRequestTryAction事件和handleRequestAction事件
有关附魔接口均改采用命名空间
新增id转命名空间接口
修正获取最大玩家数代码
This commit is contained in:
子沐呀
2024-06-27 04:13:17 +08:00
Unverified
parent a7f5ad26ac
commit d78a63044b
8 changed files with 87 additions and 190 deletions
+35 -11
View File
@@ -1,4 +1,5 @@
#include "Global.h"
#include "mc/deps/core/string/HashedString.h"
#include <regex>
bool isInteger(const std::string& str) {
@@ -721,28 +722,51 @@ void Export_Compatibility_API() {
return result;
}
);
RemoteCall::exportAs("GMLIB_API", "getLegalEnchants", [](ItemStack const* item) -> std::vector<int> {
return EnchantUtils::getLegalEnchants(item->getItem());
RemoteCall::exportAs("GMLIB_API", "getLegalEnchants", [](ItemStack const* item) -> std::vector<std::string> {
std::vector<int> enchants = EnchantUtils::getLegalEnchants(item->getItem());
std::vector<std::string> result;
for (auto& enchant : enchants) {
result.push_back(Enchant::getEnchant((Enchant::Type)enchant)->getStringId());
}
return result;
});
RemoteCall::exportAs("GMLIB_API", "getEnchantTypeNameFromId", [](int id) -> std::string {
if (auto enchant = Enchant::getEnchant((Enchant::Type)id)) {
return enchant->getStringId();
}
return "";
});
RemoteCall::exportAs(
"GMLIB_API",
"applyEnchant",
[](ItemStack const* item, int id, int level, bool allowNonVanilla) -> bool {
return EnchantUtils::applyEnchant((ItemStackBase&)*item, (Enchant::Type)id, level, allowNonVanilla);
[](ItemStack const* item, std::string const& typeName, int level, bool allowNonVanilla) -> bool {
return EnchantUtils::applyEnchant(
(ItemStackBase&)*item,
Enchant::getEnchantTypeFromName(HashedString(typeName)),
level,
allowNonVanilla
);
}
);
RemoteCall::exportAs("GMLIB_API", "removeEnchants", [](ItemStack const* item) -> void {
EnchantUtils::removeEnchants((ItemStack&)*item);
});
RemoteCall::exportAs("GMLIB_API", "hasEnchant", [](ItemStack const* item, int id) -> bool {
return EnchantUtils::hasEnchant((Enchant::Type)id, (ItemStackBase&)*item);
RemoteCall::exportAs("GMLIB_API", "hasEnchant", [](ItemStack const* item, std::string const& typeName) -> bool {
return EnchantUtils::hasEnchant(Enchant::getEnchantTypeFromName(HashedString(typeName)), (ItemStackBase&)*item);
});
RemoteCall::exportAs("GMLIB_API", "getEnchantLevel", [](ItemStack const* item, int id) -> int {
return EnchantUtils::getEnchantLevel((Enchant::Type)id, (ItemStackBase&)*item);
});
RemoteCall::exportAs("GMLIB_API", "getEnchantNameAndLevel", [](int id, int level) -> std::string {
return EnchantUtils::getEnchantNameAndLevel((Enchant::Type)id, level);
RemoteCall::exportAs("GMLIB_API", "getEnchantLevel", [](ItemStack const* item, std::string const& typeName) -> int {
return EnchantUtils::getEnchantLevel(
Enchant::getEnchantTypeFromName(HashedString(typeName)),
(ItemStackBase&)*item
);
});
RemoteCall::exportAs(
"GMLIB_API",
"getEnchantNameAndLevel",
[](std::string const& typeName, int level) -> std::string {
return EnchantUtils::getEnchantNameAndLevel(Enchant::getEnchantTypeFromName(HashedString(typeName)), level);
}
);
RemoteCall::exportAs(
"GMLIB_API",
"dropPlayerItem",
@@ -1,41 +0,0 @@
#include "HandleRequestAction.h"
#include "Global.h"
HandleRequestActionBeforeEvent::HandleRequestActionBeforeEvent(
Player* player,
ItemStackRequestAction& requestAction
)
: mPlayer(player),
mRequestAction(requestAction) {}
Player* HandleRequestActionBeforeEvent::self() const { return mPlayer; }
ItemStackRequestAction& HandleRequestActionBeforeEvent::getRequestAction() const { return mRequestAction; }
HandleRequestActionAfterEvent::HandleRequestActionAfterEvent(
Player* player,
ItemStackRequestAction& requestAction
)
: mPlayer(player),
mRequestAction(requestAction) {}
Player* HandleRequestActionAfterEvent::self() const { return mPlayer; }
ItemStackRequestAction& HandleRequestActionAfterEvent::getRequestAction() const { return mRequestAction; }
LL_AUTO_TYPE_INSTANCE_HOOK(
HandleRequestActionHook,
HookPriority::Normal,
ItemStackRequestActionHandler,
"?handleRequestAction@ItemStackRequestActionHandler@@QEAA?AW4ItemStackNetResult@@AEBVItemStackRequestAction@@@Z",
void,
ItemStackRequestAction& requestAction
) {
auto beforeEvent = HandleRequestActionBeforeEvent(((Player*)(*(__int64*)this)), requestAction);
ll::event::EventBus::getInstance().publish(beforeEvent);
if (beforeEvent.isCancelled()) return;
ll::event::EventBus::getInstance().publish(HandleRequestActionAfterEvent(((Player*)(*(__int64*)this)), requestAction));
return origin(requestAction);
}
@@ -1,28 +0,0 @@
#include "Global.h"
#include "ll/api/event/Cancellable.h"
class HandleRequestActionBeforeEvent final : public ll::event::Cancellable<ll::event::Event> {
private:
Player* mPlayer{};
ItemStackRequestAction& mRequestAction;
public:
HandleRequestActionBeforeEvent(Player* player, ItemStackRequestAction& requestAction);
Player* self() const;
ItemStackRequestAction& getRequestAction() const;
};
class HandleRequestActionAfterEvent final : public ll::event::Event {
private:
Player* mPlayer;
ItemStackRequestAction& mRequestAction;
public:
HandleRequestActionAfterEvent(Player* player, ItemStackRequestAction& requestAction);
Player* self() const;
ItemStackRequestAction& getRequestAction() const;
};
-41
View File
@@ -1,5 +1,4 @@
#include "Global.h"
#include "Event/HandleRequestAction/HandleRequestAction.h"
using namespace ll::hash_utils;
void Export_Event_API() {
@@ -248,46 +247,6 @@ void Export_Event_API() {
);
return true;
}
case doHash("handleRequestTryAction"): {
auto Call =
RemoteCall::importAs<bool(Player * player, int requestAction, int slot, int containerNetId)>(
eventName,
eventId
);
eventBus->emplaceListener<HandleRequestActionBeforeEvent>([Call](HandleRequestActionBeforeEvent& ev) {
bool result = true;
try {
result = Call(
ev.self(),
(int)ev.getRequestAction().getActionType(),
(int)ev.getRequestAction().getSrc().mSlot,
(int)ev.getRequestAction().getSrc().mOpenContainerNetId
);
} catch (...) {}
if (!result) {
ev.cancel();
}
});
return true;
}
case doHash("handleRequestAction"): {
auto Call =
RemoteCall::importAs<bool(Player * player, int requestAction, int slot, int containerNetId)>(
eventName,
eventId
);
eventBus->emplaceListener<HandleRequestActionAfterEvent>([Call](HandleRequestActionAfterEvent& ev) {
try {
Call(
ev.self(),
(int)ev.getRequestAction().getActionType(),
(int)ev.getRequestAction().getSrc().mSlot,
(int)ev.getRequestAction().getSrc().mOpenContainerNetId
);
} catch (...) {}
});
return true;
}
default:
return false;
}
+4 -5
View File
@@ -1,8 +1,4 @@
#include "GMLIB/Server/FakeListAPI.h"
#include "GMLIB/Server/LevelAPI.h"
#include "Global.h"
#include "mc/world/ActorUniqueID.h"
#include <variant>
void Export_Legacy_GMLib_ServerAPI() {
RemoteCall::exportAs("GMLib_ServerAPI", "setEducationFeatureEnabled", []() -> void {
@@ -76,6 +72,9 @@ void Export_Legacy_GMLib_ServerAPI() {
return FakeList::removeAllFakeLists();
});
RemoteCall::exportAs("GMLib_ServerAPI", "getMaxPlayers", []() -> int {
return ll::memory::dAccess<int>(ll::service::getServerNetworkHandler().as_ptr(), 200);
if (auto level = GMLIB_Level::getInstance()) {
return level->getMaxPlayerCount();
}
return {};
});
}