diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a97c49..d1a11a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.0.0] - 2024-10-13 + +### Added + ++ Added some new native api + +### Changed + ++ `MANAGER_VERSION` changed to `2` (warning: you cannot use old simplayer data now) ++ Changed `build` subcommand and api + ## [1.0.0] - 2024-10-11 ### Added @@ -12,4 +23,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 + Move CoralFans SimulatedPlayer System from [CoralFans](https://github.com/CoralFans-Dev/CoralFans) to here + Added Script Arg +[2.0.0]: https://github.com/CoralFans-Dev/CFSP/compare/v1.0.0...v2.0.0 [1.0.0]: https://github.com/CoralFans-Dev/CFSP/releases/tag/v1.0.0 diff --git a/src/cfsp/CFSP.cpp b/src/cfsp/CFSP.cpp index 3a8e831..ffe29a9 100644 --- a/src/cfsp/CFSP.cpp +++ b/src/cfsp/CFSP.cpp @@ -46,14 +46,6 @@ bool CFSP::enable() { hookSimPlayer(true); - // get DefaultDataLoadHelper - mod.getDefaultDataLoadHelper() = - static_cast(ll::memory::resolveSymbol("??_7DefaultDataLoadHelper@@6B@")); - if (!mod.getDefaultDataLoadHelper()) { - getSelf().getLogger().error("Cannot get DefaultDataLoadHelper from symbol."); - return false; - } - if (mod.getConfig().command.sp.enabled) commands::registerSpCommand(mod.getConfig().command.sp.permission); // load simplayer data diff --git a/src/cfsp/base/Macros.h b/src/cfsp/base/Macros.h index 2dae17c..58c3375 100644 --- a/src/cfsp/base/Macros.h +++ b/src/cfsp/base/Macros.h @@ -1,3 +1,5 @@ +#pragma once + #define COMMAND_CHECK_PLAYER \ auto* entity = origin.getEntity(); \ if (entity == nullptr || !entity->isType(ActorType::Player)) { \ @@ -42,10 +44,10 @@ auto it = this -> mNameSimPlayerMap.find(spname); \ if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \ if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \ - || noCheck || uuid == it->second.ownerUuid) { \ - if (it->second.status != SimPlayerStatus::Alive) \ + || noCheck || uuid == it->second->ownerUuid) { \ + if (it->second->status != SimPlayerStatus::Alive) \ return {"translate.simplayer.error.statuserror"_tr(), false}; \ - if (it->second.simPlayer) it->second.ACTION(arg); \ + if (it->second->simPlayer) it->second->ACTION(arg); \ return {"translate.simplayer.success"_tr(), true}; \ } \ return {"translate.simplayer.error.permissiondenied"_tr(), false}; \ @@ -78,10 +80,10 @@ auto it = this -> mNameSimPlayerMap.find(spname); \ if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \ if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \ - || noCheck || uuid == it->second.ownerUuid) { \ - if (it->second.status != SimPlayerStatus::Alive) \ + || noCheck || uuid == it->second->ownerUuid) { \ + if (it->second->status != SimPlayerStatus::Alive) \ return {"translate.simplayer.error.statuserror"_tr(), false}; \ - if (it->second.simPlayer) it->second.ACTION(); \ + if (it->second->simPlayer) it->second->ACTION(); \ return {"translate.simplayer.success"_tr(), true}; \ } \ return {"translate.simplayer.error.permissiondenied"_tr(), false}; \ @@ -113,17 +115,17 @@ auto it = this -> mNameSimPlayerMap.find(spname); \ if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \ if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \ - || noCheck || uuid == it->second.ownerUuid) { \ - if (it->second.status != SimPlayerStatus::Alive) \ + || noCheck || uuid == it->second->ownerUuid) { \ + if (it->second->status != SimPlayerStatus::Alive) \ return {"translate.simplayer.error.statuserror"_tr(), false}; \ - if (!it->second.isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \ - if (it->second.simPlayer) it->second.ACTION(arg); \ + if (!it->second->isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \ + if (it->second->simPlayer) it->second->ACTION(arg); \ if (interval >= 1) { \ - it->second.taskid = \ + it->second->taskid = \ this->mScheduler->add(interval, [times, sp = it->second, arg](unsigned long long t) mutable { \ if (times > 0 && t > (unsigned long long)times - 1) return false; \ - if (sp.simPlayer) [[likely]] { \ - sp.ACTION(arg); \ + if (sp->simPlayer) [[likely]] { \ + sp->ACTION(arg); \ return true; \ } \ return false; \ @@ -165,17 +167,17 @@ auto it = this -> mNameSimPlayerMap.find(spname); \ if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; \ if (player->getCommandPermissionLevel() >= coral_fans::cfsp::mod().getConfig().simPlayer.adminPermission \ - || noCheck || uuid == it->second.ownerUuid) { \ - if (it->second.status != SimPlayerStatus::Alive) \ + || noCheck || uuid == it->second->ownerUuid) { \ + if (it->second->status != SimPlayerStatus::Alive) \ return {"translate.simplayer.error.statuserror"_tr(), false}; \ - if (!it->second.isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \ - if (it->second.simPlayer) it->second.ACTION(); \ + if (!it->second->isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; \ + if (it->second->simPlayer) it->second->ACTION(); \ if (interval >= 1) { \ - it->second.taskid = \ + it->second->taskid = \ this->mScheduler->add(interval, [times, sp = it->second](unsigned long long t) mutable { \ if (times > 0 && t > (unsigned long long)times - 1) return false; \ - if (sp.simPlayer) [[likely]] { \ - sp.ACTION(); \ + if (sp->simPlayer) [[likely]] { \ + sp->ACTION(); \ return true; \ } \ return false; \ diff --git a/src/cfsp/base/Mod.h b/src/cfsp/base/Mod.h index 656df08..6b4596b 100644 --- a/src/cfsp/base/Mod.h +++ b/src/cfsp/base/Mod.h @@ -1,19 +1,25 @@ #pragma once #include "cfsp/Config.h" -#include "mc/dataloadhelper/DefaultDataLoadHelper.h" - namespace coral_fans::cfsp { class CFSPMod { private: - config::Config mConfig; - DefaultDataLoadHelper* mDefaultDataLoadHelper; + config::Config mConfig; + +public: + inline config::Config& getConfig() { return this->mConfig; } + +private: + CFSPMod() {} + ~CFSPMod() {} + CFSPMod(const CFSPMod&); + CFSPMod& operator=(const CFSPMod&); public: - inline config::Config& getConfig() { return this->mConfig; } - inline DefaultDataLoadHelper*& getDefaultDataLoadHelper() { return this->mDefaultDataLoadHelper; } + friend CFSPMod& mod(); + friend class CFSP; }; CFSPMod& mod(); diff --git a/src/cfsp/commands/Sp.cpp b/src/cfsp/commands/Sp.cpp index 2d8a5a9..aa4f84e 100644 --- a/src/cfsp/commands/Sp.cpp +++ b/src/cfsp/commands/Sp.cpp @@ -694,22 +694,15 @@ void registerSpCommand(CommandPermissionLevel permission) { spCommand, "build", {}, - taskArg, + {}, [](Player* player, ll::command::RuntimeCommand const& self) { - return coral_fans::cfsp::SimPlayerManager::getInstance().simPlayerBuild( - player, - self["name"].get(), - false, - self["interval"].has_value() ? self["interval"].get() : 20, - self["times"].has_value() ? self["times"].get() : 1 - ); + return coral_fans::cfsp::SimPlayerManager::getInstance() + .simPlayerBuild(player, self["name"].get(), false); }, [](Player* player, ll::command::RuntimeCommand const& self) { return coral_fans::cfsp::SimPlayerManager::getInstance().groupBuild( player, - self["name"].get(), - self["interval"].has_value() ? self["interval"].get() : 20, - self["times"].has_value() ? self["times"].get() : 1 + self["name"].get() ); } ); diff --git a/src/cfsp/simplayer/CFSP.h b/src/cfsp/simplayer/CFSP.h index 1e66d16..dc84ba3 100644 --- a/src/cfsp/simplayer/CFSP.h +++ b/src/cfsp/simplayer/CFSP.h @@ -21,17 +21,20 @@ #include "mc/world/level/BlockPos.h" #include "mc/world/phys/HitResultType.h" #include +#include #include #include #include #include +#include #include +#include #include #include #include #include -#define MANAGER_VERSION 1 +#define MANAGER_VERSION 2 #define INFO_VERSION 1 namespace coral_fans::cfsp { @@ -57,14 +60,14 @@ class SimPlayerManager { float offlineRotY; std::string offlineGameType; bool offlineEmptyInv; - SimulatedPlayer* simPlayer; - std::shared_ptr scheduler; - unsigned long long taskid; - unsigned long long scriptid; + SimulatedPlayer* simPlayer; // no-save + std::shared_ptr scheduler; // no-save + unsigned long long taskid; // no-save + unsigned long long scriptid; // no-save // serialization template void serialize(Archive& ar, const unsigned int version) { - if (version == 1) { + if (version == INFO_VERSION) { ar & name; ar & xuid; ar & ownerUuid; @@ -278,15 +281,15 @@ class SimPlayerManager { }; private: - std::unordered_map mNameSimPlayerMap; - std::unordered_map> mOwnerNameMap; - std::unordered_map> mGroupNameMap; - std::unordered_map> mGroupAdminMap; - unsigned long long mOnlineCount; - unsigned long long mSpawnCount; - std::shared_ptr mScheduler; - bool autorespawn; - bool autojoin; + std::unordered_map> mNameSimPlayerMap; + std::unordered_map> mOwnerNameMap; + std::unordered_map> mGroupNameMap; + std::unordered_map> mGroupAdminMap; + unsigned long long mOnlineCount; // no-save + unsigned long long mSpawnCount; // no-save + std::shared_ptr mScheduler; // no-save + bool autorespawn; + bool autojoin; private: SimPlayerManager() @@ -309,7 +312,7 @@ class SimPlayerManager { friend class boost::serialization::access; template void serialize(Archive& ar, const unsigned int version) { - if (version == 1) { + if (version == MANAGER_VERSION) { ar & mNameSimPlayerMap; ar & mOwnerNameMap; ar & mGroupNameMap; @@ -358,6 +361,22 @@ class SimPlayerManager { public: CFSP_API void setDead(std::string const&); +public: + CFSP_API bool hasGroup(std::string const& gname) { + return mGroupAdminMap.contains(gname) && mGroupNameMap.contains(gname); + } + CFSP_API bool isGroupAdmin(Player* player, std::string const& gname) { + return hasGroup(gname) && mGroupAdminMap[gname].contains(player->getUuid().asString()); + } + CFSP_API bool inGroup(std::string const& spname, std::string const& gname) { + return hasGroup(gname) && mGroupNameMap[gname].contains(spname); + } + CFSP_API bool hasSimPlayer(std::string const& spname) { return mNameSimPlayerMap.contains(spname); } + CFSP_API bool isSimPlayerOwner(Player* player, std::string const& spname) { + return hasSimPlayer(spname) && mNameSimPlayerMap[spname]->ownerUuid == player->getUuid().asString(); + } + CFSP_API std::optional> fetchSimPlayer(std::string const&); + public: SP_REG_DEF(Stop) SP_REG_DEF(Sneaking, bool) @@ -373,7 +392,7 @@ class SimPlayerManager { SP_REG_DEF(Interact, int, int) SP_REG_DEF(Jump, int, int) SP_REG_DEF(Use, int, int, int) - SP_REG_DEF(Build, int, int) + SP_REG_DEF(Build) SP_REG_DEF(LookAt, Vec3 const&) SP_REG_DEF(MoveTo, Vec3 const&) SP_REG_DEF(NavTo, Vec3 const&) diff --git a/src/cfsp/simplayer/SimPlayer.cpp b/src/cfsp/simplayer/SimPlayer.cpp index 67e842c..200dbb6 100644 --- a/src/cfsp/simplayer/SimPlayer.cpp +++ b/src/cfsp/simplayer/SimPlayer.cpp @@ -27,11 +27,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -40,10 +42,10 @@ namespace coral_fans::cfsp { namespace sputils { -bool saveSpNbt(SimulatedPlayer* sp, std::filesystem::path basePath) { - if (!sp) return false; +bool saveSpNbt(boost::shared_ptr sp, std::filesystem::path basePath) { + if (!sp->simPlayer) return false; auto tag = std::make_unique(); - if (!sp->save(*tag)) return false; + if (!sp->simPlayer->save(*tag)) return false; if (!tag) return false; std::ofstream f(basePath / "nbt", std::ios_base::out | std::ios_base::trunc); if (!f.is_open()) return false; @@ -52,8 +54,8 @@ bool saveSpNbt(SimulatedPlayer* sp, std::filesystem::path basePath) { return true; } -bool loadSpNbt(SimulatedPlayer* sp, std::filesystem::path basePath) { - if (!sp) return false; +bool loadSpNbt(boost::shared_ptr sp, std::filesystem::path basePath) { + if (!sp->simPlayer) return false; if (!std::filesystem::exists(basePath / "nbt")) return false; std::ifstream f(basePath / "nbt"); if (!f.is_open()) return false; @@ -61,19 +63,21 @@ bool loadSpNbt(SimulatedPlayer* sp, std::filesystem::path basePath) { f >> snbt; f.close(); try { - sp->load(CompoundTag::fromSnbt(snbt).value(), *mod().getDefaultDataLoadHelper()); + DefaultDataLoadHelper helper; + sp->simPlayer->load(CompoundTag::fromSnbt(snbt).value(), helper); } catch (...) { return false; } return true; } -bool emptyInv(SimulatedPlayer* sp) { +bool emptyInv(boost::shared_ptr sp) { + if (!sp->simPlayer) return false; bool ender = true; - auto ec = sp->getEnderChestContainer(); + auto ec = sp->simPlayer->getEnderChestContainer(); if (ec.has_value()) ender = ec->isEmpty(); - return sp->getInventory().isEmpty() && ender && sp->getOffhandSlot() == ItemStack::EMPTY_ITEM - && ActorEquipment::getArmorContainer(sp->getEntityContext()).isEmpty(); + return sp->simPlayer->getInventory().isEmpty() && ender && sp->simPlayer->getOffhandSlot() == ItemStack::EMPTY_ITEM + && ActorEquipment::getArmorContainer(sp->simPlayer->getEntityContext()).isEmpty(); } } // namespace sputils @@ -96,22 +100,22 @@ void SimPlayerManager::save() { std::filesystem::create_directories(dataDir); // for each simplayer for (auto& [name, sp] : this->mNameSimPlayerMap) { - if (!sp.simPlayer) { + if (!sp->simPlayer) { logger.debug("Save SimPlayer ({}) Data: SKIP", name); continue; } - sp.offlinePosX = sp.simPlayer->getFeetPos().x; - sp.offlinePosY = sp.simPlayer->getFeetPos().y; - sp.offlinePosZ = sp.simPlayer->getFeetPos().z; - sp.offlineDim = sp.simPlayer->getDimensionId(); - sp.offlineRotX = sp.simPlayer->getRotation().x; - sp.offlineRotY = sp.simPlayer->getRotation().y; - sp.offlineGameType = magic_enum::enum_name(sp.simPlayer->getPlayerGameType()); - sp.offlineEmptyInv = sputils::emptyInv(sp.simPlayer); - const auto& basePath = dataDir / sp.xuid; + sp->offlinePosX = sp->simPlayer->getFeetPos().x; + sp->offlinePosY = sp->simPlayer->getFeetPos().y; + sp->offlinePosZ = sp->simPlayer->getFeetPos().z; + sp->offlineDim = sp->simPlayer->getDimensionId(); + sp->offlineRotX = sp->simPlayer->getRotation().x; + sp->offlineRotY = sp->simPlayer->getRotation().y; + sp->offlineGameType = magic_enum::enum_name(sp->simPlayer->getPlayerGameType()); + sp->offlineEmptyInv = sputils::emptyInv(sp); + const auto& basePath = dataDir / sp->xuid; std::filesystem::create_directories(basePath); // save inventory - if (!sputils::saveSpNbt(sp.simPlayer, basePath)) + if (!sputils::saveSpNbt(sp, basePath)) logger.error("Failed to save SimPlayer ({}) NBT: cannot save data to {}", name, basePath); } // save self @@ -150,8 +154,8 @@ void SimPlayerManager::load() { file.close(); // for each simplayer for (auto& [name, sp] : this->mNameSimPlayerMap) { - int stat = sp.status; - sp.status = SimPlayerStatus::Offline; + int stat = sp->status; + sp->status = SimPlayerStatus::Offline; if (this->autojoin && stat != SimPlayerStatus::Offline) this->spawnSimPlayer(nullptr, name, {}, {}); } this->refreshSoftEnum(); @@ -166,10 +170,11 @@ std::string SimPlayerManager::listSimPlayer() { for (const auto& [name, sp] : this->mNameSimPlayerMap) retstr += "translate.simplayer.info.simplayer"_tr( name, - sp.xuid, - sp.ownerUuid, - magic_enum::enum_name(magic_enum::enum_cast(sp.status).value_or(SimPlayerStatus::Offline)), - boost::algorithm::join(sp.groups, ", ") + sp->xuid, + sp->ownerUuid, + magic_enum::enum_name(magic_enum::enum_cast(sp->status).value_or(SimPlayerStatus::Offline) + ), + boost::algorithm::join(sp->groups, ", ") ); return retstr; } @@ -190,12 +195,9 @@ std::pair SimPlayerManager::createGroup(Player* player, std:: using ll::i18n_literals::operator""_tr; if (this->mGroupNameMap.size() >= mod().getConfig().simPlayer.maxGroup) return {"translate.simplayer.error.toomanygroup"_tr(), false}; - if (this->mGroupNameMap.find(gname) == this->mGroupNameMap.end() - && this->mGroupAdminMap.find(gname) == this->mGroupAdminMap.end()) { - this->mGroupNameMap.emplace(std::make_pair(gname, std::unordered_set{})); - this->mGroupAdminMap.emplace( - std::make_pair(gname, std::unordered_set{player->getUuid().asString()}) - ); + if (!(this->mGroupNameMap.contains(gname)) && !(this->mGroupAdminMap.contains(gname))) { + this->mGroupNameMap.emplace(gname, std::unordered_set{}); + this->mGroupAdminMap.emplace(gname, std::unordered_set{player->getUuid().asString()}); this->refreshSoftEnum(); return {"translate.simplayer.success"_tr(), true}; } @@ -215,7 +217,7 @@ std::pair SimPlayerManager::deleteGroup(Player* player, std:: if (namesIt != this->mGroupNameMap.end()) { for (const auto& i : namesIt->second) { auto simIt = this->mNameSimPlayerMap.find(i); - if (simIt != this->mNameSimPlayerMap.end()) simIt->second.groups.erase(gname); + if (simIt != this->mNameSimPlayerMap.end()) simIt->second->groups.erase(gname); } } // delete group in mGroupNameMap @@ -244,11 +246,11 @@ SimPlayerManager::addSpToGroup(Player* player, std::string const& gname, std::st // check: admin auto uuid = player->getUuid().asString(); if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission - || (it->second.find(uuid) != it->second.end() && spIt->second.ownerUuid == uuid)) { + || (it->second.find(uuid) != it->second.end() && spIt->second->ownerUuid == uuid)) { // add group to SimPlayerInfo - auto gnameIt = spIt->second.groups.find(gname); - if (gnameIt != spIt->second.groups.end()) return {"translate.simplayer.error.exist"_tr(), false}; - spIt->second.groups.emplace(gname); + auto gnameIt = spIt->second->groups.find(gname); + if (gnameIt != spIt->second->groups.end()) return {"translate.simplayer.error.exist"_tr(), false}; + spIt->second->groups.emplace(gname); // add sp to mGroupNameMap auto nameIt = namesIt->second.find(spname); if (nameIt != namesIt->second.end()) return {"translate.simplayer.error.exist"_tr(), false}; @@ -273,11 +275,11 @@ SimPlayerManager::rmSpFromGroup(Player* player, std::string const& gname, std::s // check: admin auto uuid = player->getUuid().asString(); if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission - || (it->second.find(uuid) != it->second.end() && spIt->second.ownerUuid == uuid)) { + || (it->second.find(uuid) != it->second.end() && spIt->second->ownerUuid == uuid)) { // rm group from SimPlayerInfo - auto gnameIt = spIt->second.groups.find(gname); - if (gnameIt == spIt->second.groups.end()) return {"translate.simplayer.error.notfound"_tr(), false}; - spIt->second.groups.erase(gnameIt); + auto gnameIt = spIt->second->groups.find(gname); + if (gnameIt == spIt->second->groups.end()) return {"translate.simplayer.error.notfound"_tr(), false}; + spIt->second->groups.erase(gnameIt); // remove sp from mGroupNameMap auto nameIt = namesIt->second.find(spname); if (nameIt == namesIt->second.end()) return {"translate.simplayer.error.notfound"_tr(), false}; @@ -349,7 +351,7 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c // check: already exist auto spIt = this->mNameSimPlayerMap.find(spname); if (spIt != this->mNameSimPlayerMap.end()) { - if (spIt->second.status != SimPlayerStatus::Offline) return {"translate.simplayer.error.exist"_tr(), false}; + if (spIt->second->status != SimPlayerStatus::Offline) return {"translate.simplayer.error.exist"_tr(), false}; else rejoin = true; } else { spname = cfspmod.getConfig().simPlayer.namePrefix + name + cfspmod.getConfig().simPlayer.namePostfix; @@ -374,25 +376,28 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c if (rejoin) { auto* simPlayer = SimulatedPlayer::create( spname, - {spIt->second.offlinePosX, spIt->second.offlinePosY, spIt->second.offlinePosZ}, - spIt->second.offlineDim, + {spIt->second->offlinePosX, spIt->second->offlinePosY, spIt->second->offlinePosZ}, + spIt->second->offlineDim, serverNetworkHandler, - spIt->second.xuid + spIt->second->xuid ); if (!simPlayer) return {"translate.simplayer.error.cannotcreate"_tr(), false}; simPlayer->setPlayerGameType( - magic_enum::enum_cast(spIt->second.offlineGameType).value_or(GameType::WorldDefault) + magic_enum::enum_cast(spIt->second->offlineGameType).value_or(GameType::WorldDefault) ); simPlayer->teleport( - {spIt->second.offlinePosX, spIt->second.offlinePosY, spIt->second.offlinePosZ}, - spIt->second.offlineDim, - {spIt->second.offlineRotX, spIt->second.offlineRotY} + {spIt->second->offlinePosX, spIt->second->offlinePosY, spIt->second->offlinePosZ}, + spIt->second->offlineDim, + {spIt->second->offlineRotX, spIt->second->offlineRotY} + ); + spIt->second->status = SimPlayerStatus::Alive; + spIt->second->simPlayer = simPlayer; + spIt->second->scheduler = this->mScheduler; + spIt->second->taskid = 0; + sputils::loadSpNbt( + spIt->second, + CFSP::getInstance().getSelf().getDataDir() / "simplayer" / "data" / spIt->second->xuid ); - sputils::loadSpNbt(simPlayer, CFSP::getInstance().getSelf().getDataDir() / "simplayer" / spIt->second.xuid); - spIt->second.status = SimPlayerStatus::Alive; - spIt->second.simPlayer = simPlayer; - spIt->second.scheduler = this->mScheduler; - spIt->second.taskid = 0; } else { auto* simPlayer = SimulatedPlayer::create( spname, @@ -405,7 +410,7 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c simPlayer->setPlayerGameType(player->getPlayerGameType()); simPlayer->teleport(pos, player->getDimensionId(), rot); // add to map - const auto& info = SimPlayerInfo{ + this->mNameSimPlayerMap[spname] = boost::shared_ptr(new SimPlayerInfo{ spname, "-" + std::to_string(std::hash()(spname)), player->getUuid().asString(), @@ -418,13 +423,12 @@ SimPlayerManager::spawnSimPlayer(Player* player, std::string const& name, Vec3 c rot.x, rot.y, std::string{magic_enum::enum_name(player->getPlayerGameType())}, - sputils::emptyInv(simPlayer), + true, simPlayer, this->mScheduler, 0, 0 - }; - this->mNameSimPlayerMap[spname] = info; + }); this->mOwnerNameMap[player->getUuid().asString()].emplace(spname); } // add to softenum @@ -461,28 +465,28 @@ SimPlayerManager::despawnSimPlayer(Player* player, std::string const& spname, bo if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; // check: admin if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission || noCheck - || uuid == it->second.ownerUuid) { + || uuid == it->second->ownerUuid) { // check: offline - if (!noCheck && it->second.status == SimPlayerStatus::Offline) + if (!noCheck && it->second->status == SimPlayerStatus::Offline) return {"translate.simplayer.error.statuserror"_tr(), false}; // run - if (it->second.simPlayer) { - const auto& basePath = CFSP::getInstance().getSelf().getDataDir() / "simplayer" / it->second.xuid; - sputils::saveSpNbt(it->second.simPlayer, basePath); - it->second.offlinePosX = it->second.simPlayer->getFeetPos().x; - it->second.offlinePosY = it->second.simPlayer->getFeetPos().y; - it->second.offlinePosZ = it->second.simPlayer->getFeetPos().z; - it->second.offlineDim = it->second.simPlayer->getDimensionId(); - it->second.offlineRotX = it->second.simPlayer->getRotation().x; - it->second.offlineRotY = it->second.simPlayer->getRotation().y; - it->second.offlineGameType = magic_enum::enum_name(it->second.simPlayer->getPlayerGameType()); - it->second.offlineEmptyInv = sputils::emptyInv(it->second.simPlayer); - it->second.stop(); - it->second.simPlayer->simulateDisconnect(); - it->second.simPlayer = nullptr; + if (it->second->simPlayer) { + const auto& basePath = CFSP::getInstance().getSelf().getDataDir() / "simplayer" / "data" / it->second->xuid; + sputils::saveSpNbt(it->second, basePath); + it->second->offlinePosX = it->second->simPlayer->getFeetPos().x; + it->second->offlinePosY = it->second->simPlayer->getFeetPos().y; + it->second->offlinePosZ = it->second->simPlayer->getFeetPos().z; + it->second->offlineDim = it->second->simPlayer->getDimensionId(); + it->second->offlineRotX = it->second->simPlayer->getRotation().x; + it->second->offlineRotY = it->second->simPlayer->getRotation().y; + it->second->offlineGameType = magic_enum::enum_name(it->second->simPlayer->getPlayerGameType()); + it->second->offlineEmptyInv = sputils::emptyInv(it->second); + it->second->stop(); + it->second->simPlayer->simulateDisconnect(); + it->second->simPlayer = nullptr; }; // change status - it->second.status = SimPlayerStatus::Offline; + it->second->status = SimPlayerStatus::Offline; --this->mOnlineCount; // return return {"translate.simplayer.success"_tr(), true}; @@ -514,15 +518,18 @@ std::pair SimPlayerManager::rmSimPlayer(Player* player, std:: if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; // check: admin if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission || noCheck - || uuid == it->second.ownerUuid) { + || uuid == it->second->ownerUuid) { // check: offline - if (it->second.status != SimPlayerStatus::Offline) return {"translate.simplayer.error.statuserror"_tr(), false}; + if (it->second->status != SimPlayerStatus::Offline) + return {"translate.simplayer.error.statuserror"_tr(), false}; // check: empty inventory - if (!it->second.offlineEmptyInv) return {"translate.simplayer.error.notempty"_tr(), false}; + if (!it->second->offlineEmptyInv) return {"translate.simplayer.error.notempty"_tr(), false}; // remove - std::filesystem::remove_all(CFSP::getInstance().getSelf().getDataDir() / "simplayer" / it->second.xuid); - for (const auto& i : it->second.groups) this->mGroupNameMap[i].erase(spname); - this->mOwnerNameMap[it->second.ownerUuid].erase(it->first); + std::filesystem::remove_all( + CFSP::getInstance().getSelf().getDataDir() / "simplayer" / "data" / it->second->xuid + ); + for (const auto& i : it->second->groups) this->mGroupNameMap[i].erase(spname); + this->mOwnerNameMap[it->second->ownerUuid].erase(it->first); this->mNameSimPlayerMap.erase(it); this->refreshSoftEnum(); // return @@ -556,13 +563,13 @@ std::pair SimPlayerManager::respawnSimPlayer(Player* player, if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; // check: admin if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission || noCheck - || uuid == it->second.ownerUuid) { + || uuid == it->second->ownerUuid) { // check: dead - if (it->second.status != SimPlayerStatus::Dead) return {"translate.simplayer.error.statuserror"_tr(), false}; + if (it->second->status != SimPlayerStatus::Dead) return {"translate.simplayer.error.statuserror"_tr(), false}; // run - if (it->second.simPlayer) it->second.simPlayer->simulateRespawn(); + if (it->second->simPlayer) it->second->simPlayer->simulateRespawn(); // change status - it->second.status = SimPlayerStatus::Alive; + it->second->status = SimPlayerStatus::Alive; // return return {"translate.simplayer.success"_tr(), true}; } @@ -589,7 +596,13 @@ void SimPlayerManager::setDead(std::string const& spname) { auto it = this->mNameSimPlayerMap.find(spname); // check: simplayer if (it == this->mNameSimPlayerMap.end()) return; - it->second.status = SimPlayerStatus::Dead; + it->second->status = SimPlayerStatus::Dead; +} + +std::optional> +SimPlayerManager::fetchSimPlayer(std::string const& name) { + if (auto it = this->mNameSimPlayerMap.find(name); it != this->mNameSimPlayerMap.end()) return it->second; + else return std::nullopt; } SP_DEF(Stop, stop) @@ -606,9 +619,9 @@ std::pair SimPlayerManager::simPlayerSwap(Player* player, std auto it = this->mNameSimPlayerMap.find(spname); if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission - || uuid == it->second.ownerUuid) { - if (it->second.status != SimPlayerStatus::Alive) return {"translate.simplayer.error.statuserror"_tr(), false}; - if (it->second.simPlayer) it->second.swap(player); + || uuid == it->second->ownerUuid) { + if (it->second->status != SimPlayerStatus::Alive) return {"translate.simplayer.error.statuserror"_tr(), false}; + if (it->second->simPlayer) it->second->swap(player); return {"translate.simplayer.success"_tr(), true}; } return {"translate.simplayer.error.permissiondenied"_tr(), false}; @@ -618,7 +631,7 @@ SP_DEF_WA(Select, select, int) SP_DEF_TASK(Interact, interact) SP_DEF_TASK(Jump, jump) SP_DEF_TASK_WA(Use, useItem, int) -SP_DEF_TASK(Build, startBuild) +SP_DEF(Build, startBuild) SP_DEF_WA(LookAt, lookAt, Vec3 const&) SP_DEF_WA(MoveTo, moveTo, Vec3 const&) SP_DEF_WA(NavTo, navigateTo, Vec3 const&) @@ -635,11 +648,18 @@ std::pair SimPlayerManager::simPlayerScript( auto it = this->mNameSimPlayerMap.find(spname); if (it == this->mNameSimPlayerMap.end()) return {"translate.simplayer.error.notfound"_tr(), false}; if (player->getCommandPermissionLevel() >= mod().getConfig().simPlayer.adminPermission || noCheck - || uuid == it->second.ownerUuid) { - if (it->second.status != SimPlayerStatus::Alive) return {"translate.simplayer.error.statuserror"_tr(), false}; - if (!it->second.isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; + || uuid == it->second->ownerUuid) { + if (it->second->status != SimPlayerStatus::Alive) return {"translate.simplayer.error.statuserror"_tr(), false}; + if (!it->second->isFree()) return {"translate.simplayer.error.nonfree"_tr(), false}; // run script - if (it->second.simPlayer) return sputils::lua_api::execLuaScript(arg, interval, luaArg, it->second); + if (it->second->simPlayer) + return sputils::lua_api::execLuaScript( + arg, + interval, + luaArg, + it->second, + CFSP::getInstance().getSelf().getDataDir() / "simplayer" / "scripts" + ); return {"translate.simplayer.success"_tr(), true}; } return {"translate.simplayer.error.permissiondenied"_tr(), false}; diff --git a/src/cfsp/simplayer/luaapi/BlockInfo.h b/src/cfsp/simplayer/luaapi/BlockInfo.h index 2d7ec86..eff3c7f 100644 --- a/src/cfsp/simplayer/luaapi/BlockInfo.h +++ b/src/cfsp/simplayer/luaapi/BlockInfo.h @@ -1,6 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" #include "mc/nbt/CompoundTag.h" #include "mc/nbt/CompoundTagVariant.h" #include "mc/nbt/Tag.h" diff --git a/src/cfsp/simplayer/luaapi/BlockPos.h b/src/cfsp/simplayer/luaapi/BlockPos.h index d31dd77..4c5eb56 100644 --- a/src/cfsp/simplayer/luaapi/BlockPos.h +++ b/src/cfsp/simplayer/luaapi/BlockPos.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/src/cfsp/simplayer/luaapi/BlockSource.h b/src/cfsp/simplayer/luaapi/BlockSource.h index 06542b0..1036762 100644 --- a/src/cfsp/simplayer/luaapi/BlockSource.h +++ b/src/cfsp/simplayer/luaapi/BlockSource.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/src/cfsp/simplayer/luaapi/ItemInfo.h b/src/cfsp/simplayer/luaapi/ItemInfo.h index 5af8426..cb4c065 100644 --- a/src/cfsp/simplayer/luaapi/ItemInfo.h +++ b/src/cfsp/simplayer/luaapi/ItemInfo.h @@ -1,6 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" #include "mc/nbt/CompoundTag.h" #include "mc/nbt/CompoundTagVariant.h" #include "mc/nbt/Tag.h" diff --git a/src/cfsp/simplayer/luaapi/Level.h b/src/cfsp/simplayer/luaapi/Level.h index 7de8086..38f1c35 100644 --- a/src/cfsp/simplayer/luaapi/Level.h +++ b/src/cfsp/simplayer/luaapi/Level.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/src/cfsp/simplayer/luaapi/Log.h b/src/cfsp/simplayer/luaapi/Log.h index 51ad964..21424c1 100644 --- a/src/cfsp/simplayer/luaapi/Log.h +++ b/src/cfsp/simplayer/luaapi/Log.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lua.h" } diff --git a/src/cfsp/simplayer/luaapi/SimPlayer.cpp b/src/cfsp/simplayer/luaapi/SimPlayer.cpp index 408256b..7b7b2eb 100644 --- a/src/cfsp/simplayer/luaapi/SimPlayer.cpp +++ b/src/cfsp/simplayer/luaapi/SimPlayer.cpp @@ -5,7 +5,6 @@ #include "mc/world/level/BlockPos.h" #include "mc/world/level/BlockSource.h" #include -#include #include "cfsp/simplayer/luaapi/ItemInfo.h" #include "cfsp/simplayer/luaapi/Utils.h" @@ -23,144 +22,154 @@ namespace sputils::lua_api { LUAAPI(simplayer_getName) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushstring(L, (*spinfo)->getName().c_str()); + lua_pushstring(L, (**spinfo)->getName().c_str()); return 1; } LUAAPI(simplayer_getXuid) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushstring(L, (*spinfo)->getXuid().c_str()); + lua_pushstring(L, (**spinfo)->getXuid().c_str()); return 1; } LUAAPI(simplayer_getStatus) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushinteger(L, (*spinfo)->getStatus()); + lua_pushinteger(L, (**spinfo)->getStatus()); return 1; } LUAAPI(simplayer_getPos) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); Vec3* pos = (Vec3*)lua_newuserdata(L, sizeof(Vec3)); - *pos = (*spinfo)->getPos(); + *pos = (**spinfo)->getPos(); luaL_setmetatable(L, "vec3_mt"); return 1; } LUAAPI(simplayer_getFeetPos) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); Vec3* pos = (Vec3*)lua_newuserdata(L, sizeof(Vec3)); - *pos = (*spinfo)->getFeetPos(); + *pos = (**spinfo)->getFeetPos(); luaL_setmetatable(L, "vec3_mt"); return 1; } LUAAPI(simplayer_getStandingOn) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); BlockPos* pos = (BlockPos*)lua_newuserdata(L, sizeof(BlockPos)); - *pos = (*spinfo)->getStandingOn(); + *pos = (**spinfo)->getStandingOn(); luaL_setmetatable(L, "blockpos_mt"); return 1; } LUAAPI(simplayer_getRot) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); Vec2* pos = (Vec2*)lua_newuserdata(L, sizeof(Vec2)); - *pos = (*spinfo)->getRot(); + *pos = (**spinfo)->getRot(); luaL_setmetatable(L, "vec2_mt"); return 1; } LUAAPI(simplayer_getHealth) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushinteger(L, (*spinfo)->getHealth()); + lua_pushinteger(L, (**spinfo)->getHealth()); return 1; } LUAAPI(simplayer_getHunger) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushnumber(L, (*spinfo)->getHunger()); + lua_pushnumber(L, (**spinfo)->getHunger()); return 1; } LUAAPI(simplayer_sneaking) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); @@ -170,16 +179,17 @@ LUAAPI(simplayer_sneaking) { 2, std::string{"boolean expected, got " + std::string{lua_typename(L, lua_type(L, 2))}}.c_str() ); - lua_pushboolean(L, (*spinfo)->sneaking(lua_toboolean(L, 2))); + lua_pushboolean(L, (**spinfo)->sneaking(lua_toboolean(L, 2))); return 1; } LUAAPI(simplayer_swimming) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); @@ -189,107 +199,114 @@ LUAAPI(simplayer_swimming) { 2, std::string{"boolean expected, got " + std::string{lua_typename(L, lua_type(L, 2))}}.c_str() ); - (*spinfo)->swimming(lua_toboolean(L, 2)); + (**spinfo)->swimming(lua_toboolean(L, 2)); return 0; } LUAAPI(simplayer_attack) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->attack()); + lua_pushboolean(L, (**spinfo)->attack()); return 1; } LUAAPI(simplayer_chat) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); std::string msg = luaL_checkstring(L, 2); lua_settop(L, 0); - (*spinfo)->chat(msg); + (**spinfo)->chat(msg); return 0; } LUAAPI(simplayer_destroy) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->destroy()); + lua_pushboolean(L, (**spinfo)->destroy()); return 1; } LUAAPI(simplayer_dropSelectedItem) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->dropSelectedItem()); + lua_pushboolean(L, (**spinfo)->dropSelectedItem()); return 1; } LUAAPI(simplayer_dropInv) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->dropInv()); + lua_pushboolean(L, (**spinfo)->dropInv()); return 1; } LUAAPI(simplayer_runCmd) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); std::string cmd = luaL_checkstring(L, 2); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->runCmd(cmd)); + lua_pushboolean(L, (**spinfo)->runCmd(cmd)); return 1; } LUAAPI(simplayer_getBlockPosFromView) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - const auto& rst = (*spinfo)->getBlockPosFromView(); + const auto& rst = (**spinfo)->getBlockPosFromView(); BlockPos* pos = (BlockPos*)lua_newuserdata(L, sizeof(BlockPos)); *pos = rst.first; luaL_setmetatable(L, "blockpos_mt"); @@ -300,10 +317,11 @@ LUAAPI(simplayer_getBlockPosFromView) { LUAAPI(simplayer_searchInInvWithId) { int count = lua_gettop(L); if (count < 2 || count > 3) return luaL_error(L, "1 or 2 args expected (without \"self\")"); - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); @@ -311,17 +329,18 @@ LUAAPI(simplayer_searchInInvWithId) { int start = 0; if (count == 3) start = static_cast(luaL_checkinteger(L, 3)); lua_settop(L, 0); - lua_pushinteger(L, (*spinfo)->searchInInvWithId(id, start)); + lua_pushinteger(L, (**spinfo)->searchInInvWithId(id, start)); return 1; } LUAAPI(simplayer_searchInInvWithName) { int count = lua_gettop(L); if (count < 2 || count > 3) return luaL_error(L, "1 or 2 args expected (without \"self\")"); - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); @@ -329,193 +348,206 @@ LUAAPI(simplayer_searchInInvWithName) { int start = 0; if (count == 3) start = static_cast(luaL_checkinteger(L, 3)); lua_settop(L, 0); - lua_pushinteger(L, (*spinfo)->searchInInvWithName(name, start)); + lua_pushinteger(L, (**spinfo)->searchInInvWithName(name, start)); return 1; } LUAAPI(simplayer_selectSlot) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); int slot = static_cast(luaL_checkinteger(L, 2)); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->selectSlot(slot)); + lua_pushboolean(L, (**spinfo)->selectSlot(slot)); return 1; } LUAAPI(simplayer_select) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); int id = static_cast(luaL_checkinteger(L, 2)); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->select(id)); + lua_pushboolean(L, (**spinfo)->select(id)); return 1; } LUAAPI(simplayer_getItemFromInv) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); int slot = static_cast(luaL_checkinteger(L, 2)); lua_settop(L, 0); ItemInfo** info = (ItemInfo**)lua_newuserdata(L, sizeof(ItemInfo*)); - *info = new ItemInfo((*spinfo)->getItemFromInv(slot)); + *info = new ItemInfo((**spinfo)->getItemFromInv(slot)); luaL_setmetatable(L, "iteminfo_mt"); return 1; } LUAAPI(simplayer_interact) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->interact()); + lua_pushboolean(L, (**spinfo)->interact()); return 1; } LUAAPI(simplayer_jump) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->jump()); + lua_pushboolean(L, (**spinfo)->jump()); return 1; } LUAAPI(simplayer_useItem) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); int delay = static_cast(luaL_checkinteger(L, 2)); lua_settop(L, 0); - (*spinfo)->useItem(delay); + (**spinfo)->useItem(delay); return 0; } LUAAPI(simplayer_startBuild) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - (*spinfo)->startBuild(); + (**spinfo)->startBuild(); return 0; } LUAAPI(simplayer_lookAt) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); Vec3* pos = (Vec3*)luaL_checkudata(L, 2, "vec3_mt"); luaL_argcheck(L, pos != nullptr, 2, "invalid userdata"); lua_settop(L, 0); - (*spinfo)->lookAt(*pos); + (**spinfo)->lookAt(*pos); return 0; } LUAAPI(simplayer_moveTo) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); Vec3* pos = (Vec3*)luaL_checkudata(L, 2, "vec3_mt"); luaL_argcheck(L, pos != nullptr, 2, "invalid userdata"); lua_settop(L, 0); - (*spinfo)->moveTo(*pos); + (**spinfo)->moveTo(*pos); return 0; } LUAAPI(simplayer_navigateTo) { LUA_ARG_COUNT_CHECK_M(1) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); Vec3* pos = (Vec3*)luaL_checkudata(L, 2, "vec3_mt"); luaL_argcheck(L, pos != nullptr, 2, "invalid userdata"); lua_settop(L, 0); - (*spinfo)->navigateTo(*pos); + (**spinfo)->navigateTo(*pos); return 0; } LUAAPI(simplayer_isTaskFree) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - lua_pushboolean(L, (*spinfo)->isTaskFree()); + lua_pushboolean(L, (**spinfo)->isTaskFree()); return 1; } LUAAPI(simplayer_stopAction) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck( L, - (spinfo != nullptr) && ((*spinfo) != nullptr) && ((*spinfo)->simPlayer != nullptr), + (spinfo != nullptr) && ((*spinfo) != nullptr) && ((**spinfo) != nullptr) && ((**spinfo)->simPlayer != nullptr), 1, "invalid userdata" ); lua_settop(L, 0); - (*spinfo)->stopAction(); + (**spinfo)->stopAction(); return 0; } LUAAPI(simplayer_meta_gc) { LUA_ARG_COUNT_CHECK_M(0) - SimPlayerManager::SimPlayerInfo** spinfo = (SimPlayerManager::SimPlayerInfo**)luaL_checkudata(L, 1, "simplayer_mt"); + boost::shared_ptr** spinfo = + (boost::shared_ptr**)luaL_checkudata(L, 1, "simplayer_mt"); luaL_argcheck(L, (spinfo != nullptr) && ((*spinfo) != nullptr), 1, "invalid userdata"); lua_settop(L, 0); delete *spinfo; diff --git a/src/cfsp/simplayer/luaapi/SimPlayer.h b/src/cfsp/simplayer/luaapi/SimPlayer.h index 19aa60a..91edfc1 100644 --- a/src/cfsp/simplayer/luaapi/SimPlayer.h +++ b/src/cfsp/simplayer/luaapi/SimPlayer.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" @@ -77,9 +75,7 @@ int lua_api_simplayer_isTaskFree(lua_State*); int lua_api_simplayer_stopAction(lua_State*); -int lua_api_simplayer_meta_gc(lua_State*); - -extern const luaL_Reg lua_reg_simplayer_m[34]; +extern const luaL_Reg lua_reg_simplayer_m[33]; int lua_api_open_simplayer(lua_State*); diff --git a/src/cfsp/simplayer/luaapi/SpLuaApi.cpp b/src/cfsp/simplayer/luaapi/SpLuaApi.cpp index 62fd60f..cd24369 100644 --- a/src/cfsp/simplayer/luaapi/SpLuaApi.cpp +++ b/src/cfsp/simplayer/luaapi/SpLuaApi.cpp @@ -1,8 +1,8 @@ #include "cfsp/CFSP.h" -#include "cfsp/base/Macros.h" #include "cfsp/base/Mod.h" #include "cfsp/simplayer/CFSP.h" #include "mc/network/packet/TextPacket.h" +#include #include #include #include @@ -47,15 +47,16 @@ inline void broadcastErr(std::filesystem::path const& path, std::string const& n } std::pair execLuaScript( - std::string const& fileName, - int interval, - std::string const& luaArg, - SimPlayerManager::SimPlayerInfo& spinfo + std::string const& fileName, + int interval, + std::string const& luaArg, + boost::shared_ptr spinfo, + std::filesystem::path basePath ) { - auto path = CFSP::getInstance().getSelf().getDataDir() / "simplayer" / "scripts" / fileName; + auto path = basePath / fileName; // new state - std::shared_ptr L(luaL_newstate(), [](lua_State* L) { - if (L) lua_close(L); + std::shared_ptr L(luaL_newstate(), [](lua_State* state) { + if (state) lua_close(state); }); if (!L.get()) return {"Cannot open lua state", false}; // open libs @@ -74,7 +75,7 @@ std::pair execLuaScript( } // load BlockSource int* dimid = (int*)lua_newuserdata(L.get(), sizeof(int)); - *dimid = (int)spinfo.simPlayer->getDimensionId(); + *dimid = (int)spinfo->simPlayer->getDimensionId(); luaL_setmetatable(L.get(), "blocksource_mt"); lua_setglobal(L.get(), "BlockSource"); // load Level @@ -82,9 +83,10 @@ std::pair execLuaScript( luaL_setmetatable(L.get(), "level_mt"); lua_setglobal(L.get(), "Level"); // load SimPlayer - SimPlayerManager::SimPlayerInfo** luaspinfo = - (SimPlayerManager::SimPlayerInfo**)lua_newuserdata(L.get(), sizeof(SimPlayerManager::SimPlayerInfo*)); - *luaspinfo = new SimPlayerManager::SimPlayerInfo(spinfo); + boost::shared_ptr** luaspinfo = + (boost::shared_ptr**) + lua_newuserdata(L.get(), sizeof(boost::shared_ptr*)); + *luaspinfo = new boost::shared_ptr(spinfo); luaL_setmetatable(L.get(), "simplayer_mt"); lua_setglobal(L.get(), "SimPlayer"); // set require path @@ -128,21 +130,21 @@ std::pair execLuaScript( } else return {"function \"Tick\" did not return a boolean type value", false}; lua_settop(L.get(), 0); // run Tick in scheduler - spinfo.scriptid = spinfo.scheduler->add(interval, [L, path, spinfo](unsigned long long) { + spinfo->scriptid = spinfo->scheduler->add(interval, [L, path, spinfo](unsigned long long) { lua_settop(L.get(), 0); int fType = lua_getglobal(L.get(), "Tick"); if (fType != LUA_TFUNCTION) { - broadcastErr(path, spinfo.name, "\"Tick\" is not a function"); + broadcastErr(path, spinfo->name, "\"Tick\" is not a function"); return false; } int ret = lua_pcall(L.get(), 0, 1, 0); if (ret != LUA_OK) { - broadcastErr(path, spinfo.name, lua_tostring(L.get(), -1)); + broadcastErr(path, spinfo->name, lua_tostring(L.get(), -1)); return false; } if (lua_isboolean(L.get(), -1)) return static_cast(lua_toboolean(L.get(), -1)); else { - broadcastErr(path, spinfo.name, "function \"Tick\" did not return a boolean type value"); + broadcastErr(path, spinfo->name, "function \"Tick\" did not return a boolean type value"); return false; } }); diff --git a/src/cfsp/simplayer/luaapi/SpLuaApi.h b/src/cfsp/simplayer/luaapi/SpLuaApi.h index b17d83e..f389fb5 100644 --- a/src/cfsp/simplayer/luaapi/SpLuaApi.h +++ b/src/cfsp/simplayer/luaapi/SpLuaApi.h @@ -1,6 +1,8 @@ #pragma once #include "cfsp/simplayer/CFSP.h" +#include +#include #include #include @@ -8,8 +10,13 @@ namespace coral_fans::cfsp { namespace sputils::lua_api { -std::pair -execLuaScript(std::string const& fileName, int interval, std::string const&, SimPlayerManager::SimPlayerInfo& spinfo); +std::pair execLuaScript( + std::string const&, + int, + std::string const&, + boost::shared_ptr, + std::filesystem::path +); } diff --git a/src/cfsp/simplayer/luaapi/Utils.h b/src/cfsp/simplayer/luaapi/Utils.h index 385a592..9a170dd 100644 --- a/src/cfsp/simplayer/luaapi/Utils.h +++ b/src/cfsp/simplayer/luaapi/Utils.h @@ -1,11 +1,9 @@ #pragma once -#include "cfsp/base/Macros.h" #include "mc/nbt/CompoundTag.h" #include "mc/nbt/CompoundTagVariant.h" #include "mc/nbt/ListTag.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/src/cfsp/simplayer/luaapi/Vec2.h b/src/cfsp/simplayer/luaapi/Vec2.h index 27e1487..a33e8df 100644 --- a/src/cfsp/simplayer/luaapi/Vec2.h +++ b/src/cfsp/simplayer/luaapi/Vec2.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/src/cfsp/simplayer/luaapi/Vec3.h b/src/cfsp/simplayer/luaapi/Vec3.h index b395f7a..07dd45b 100644 --- a/src/cfsp/simplayer/luaapi/Vec3.h +++ b/src/cfsp/simplayer/luaapi/Vec3.h @@ -1,7 +1,5 @@ #pragma once -#include "cfsp/base/Macros.h" - extern "C" { #include "lauxlib.h" #include "lua.h" diff --git a/tooth.json b/tooth.json index d7e7206..5079e46 100644 --- a/tooth.json +++ b/tooth.json @@ -1,7 +1,7 @@ { "format_version": 2, "tooth": "CFSP", - "version": "1.0.0", + "version": "2.0.0", "info": { "name": "CFSP", "description": "CoralFans SimulatedPlayer", diff --git a/xmake.lua b/xmake.lua index 576b2fc..dc0d592 100644 --- a/xmake.lua +++ b/xmake.lua @@ -33,7 +33,7 @@ target("CFSP") -- Change this to your mod name. add_defines("VERSION=\"$(shell git describe --tags --abbrev=0 --always)\"") add_defines("COMMITID=\"$(shell git rev-parse HEAD)\"") add_files("src/**.cpp") - add_headerfiles("src/cfsp/simplayer/CFSP.h") + add_headerfiles("src/(cfsp/**.h)") add_includedirs("src") add_packages( "levilamina",