feat: adapt LeviLamin 1.1.1 & GMLIB 1.0.0-rc.3

This commit is contained in:
killcerr
2025-03-22 23:32:42 +08:00
Unverified
parent f85416b785
commit 8729d71133
9 changed files with 99 additions and 73 deletions
+13 -8
View File
@@ -1,4 +1,5 @@
#include "Cleaner.h"
#include "mc/world/actor/provider/SynchedActorDataAccess.h"
namespace Cleaner {
@@ -15,9 +16,11 @@ bool isMatch(std::string& A, std::string& B) {
return (A == B);
}
bool shouldIgnore(GMLIB_Actor* ac) {
if (ac->isMob() || ac->isItemActor()) {
if (ac->isTame() || ac->isTrusting() || ac->getNameTag() != "" || ac->hasTag("cleaner:ignore")) {
bool isTrust(Actor* ac) { return SynchedActorDataAccess::getActorFlag(ac->getEntityContext(), ::ActorFlags::Trusting); }
bool shouldIgnore(gmlib::world::actor::GMActor* ac) {
if (ac->hasCategory(::ActorCategory::Mob) || ac->hasCategory(::ActorCategory::Item)) {
if (ac->isTame() || isTrust(ac) || ac->getNameTag() != "" || ac->hasTag("cleaner:ignore")) {
return true;
}
}
@@ -27,7 +30,7 @@ bool shouldIgnore(GMLIB_Actor* ac) {
bool ShouldClean(Actor* actor) {
// Players
auto& config = Cleaner::Entry::getInstance().getConfig();
auto en = (GMLIB_Actor*)actor;
auto en = (gmlib::world::actor::GMActor*)actor;
if (en->isPlayer() || shouldIgnore(en)) {
return false;
}
@@ -38,7 +41,7 @@ bool ShouldClean(Actor* actor) {
}
}
// Items
if (en->isItemActor()) {
if (en->hasCategory(::ActorCategory::Item)) {
if (config.CleanItem.Enabled) {
auto itac = (ItemActor*)en;
if (itac->age() <= config.CleanItem.ExistTicks) {
@@ -56,7 +59,7 @@ bool ShouldClean(Actor* actor) {
return false;
}
// Mobs
else if (en->isMob()) {
else if (en->hasCategory(::ActorCategory::Mob)) {
if (config.CleanMobs.Enabled) {
auto blacklist = config.CleanMobs.BlackList;
for (auto& key : blacklist) {
@@ -94,8 +97,9 @@ bool ShouldClean(Actor* actor) {
}
int ExecuteClean() {
auto level = ll::service::getLevel();
int clean_count = 0;
auto all_entities = GMLIB_Level::getLevel()->getRuntimeActorList();
auto all_entities = level->getRuntimeActorList();
for (auto entity : all_entities) {
if (ShouldClean(entity)) {
entity->despawn();
@@ -106,8 +110,9 @@ int ExecuteClean() {
}
int CountEntities() {
auto level = ll::service::getLevel();
int clean_count = 0;
auto all_entities = GMLIB_Level::getLevel()->getRuntimeActorList();
auto all_entities = level->getRuntimeActorList();
for (auto* entity : all_entities) {
if (ShouldClean(entity)) {
clean_count++;
+13 -11
View File
@@ -10,11 +10,11 @@ static std::shared_ptr<bool> mCleanTaskTPS = std::make_shared<bool>(false);
bool auto_clean_triggerred = false;
void CleanTask() {
auto& config = Cleaner::Entry::getInstance().getConfig();
auto time = config.Basic.Notice1;
auto announce_time = config.Basic.Notice2;
auto time_1 = std::chrono::seconds::duration(time);
auto time_2 = std::chrono::seconds::duration(time - announce_time);
auto& config = Cleaner::Entry::getInstance().getConfig();
auto time = config.Basic.Notice1;
auto announce_time = config.Basic.Notice2;
std::chrono::seconds time_1(time);
std::chrono::seconds time_2(time - announce_time);
if (config.Basic.ConsoleLog) {
ll::io::LoggerRegistry::getInstance().getOrCreate("Cleaner")->info(
tr("cleaner.output.count1", {S(time_1.count())})
@@ -59,7 +59,7 @@ void CleanTask() {
}
void AutoCleanTask(int seconds) {
auto time = std::chrono::seconds::duration(seconds);
std::chrono::seconds time(seconds);
*mAutoCleanTask = true;
ll::coro::keepThis([time]() -> ll::coro::CoroTask<> {
while (true) {
@@ -72,7 +72,7 @@ void AutoCleanTask(int seconds) {
}
void CleanTaskCount(int max_entities) {
auto& config = Cleaner::Entry::getInstance().getConfig();
auto& config = Cleaner::Entry::getInstance().getConfig();
*mCleanTaskCount = true;
ll::coro::keepThis([max_entities, &config]() -> ll::coro::CoroTask<> {
while (true) {
@@ -83,7 +83,9 @@ void CleanTaskCount(int max_entities) {
if (count >= max_entities) {
auto_clean_triggerred = true;
if (config.Basic.ConsoleLog) {
ll::io::LoggerRegistry::getInstance().getOrCreate("Cleaner")->warn(tr("cleaner.output.triggerAutoCleanCount", {S(count)}));
ll::io::LoggerRegistry::getInstance().getOrCreate("Cleaner")->warn(
tr("cleaner.output.triggerAutoCleanCount", {S(count)})
);
}
if (config.Basic.SendBroadcast) {
Helper::broadcastMessage(tr("cleaner.output.triggerAutoCleanCount", {S(count)}));
@@ -100,16 +102,16 @@ void CleanTaskCount(int max_entities) {
}
void CleanTaskTPS(float min_tps) {
auto& config = Cleaner::Entry::getInstance().getConfig();
auto& config = Cleaner::Entry::getInstance().getConfig();
*mCleanTaskTPS = true;
ll::coro::keepThis([min_tps, &config]() -> ll::coro::CoroTask<> {
while (true) {
co_await 10s;
if (*mCleanTaskTPS == false) co_return;
if (auto_clean_triggerred == false) {
if (GMLIB_Level::getLevel()->getServerAverageTps() <= min_tps) {
if (gmlib::world::GMLevel::getInstance()->getServerAverageTps() <= min_tps) {
auto_clean_triggerred = true;
auto mspt = S(GMLIB_Level::getLevel()->getServerAverageTps());
auto mspt = S(gmlib::world::GMLevel::getInstance()->getServerAverageTps());
if (config.Basic.ConsoleLog) {
ll::io::LoggerRegistry::getInstance().getOrCreate("Cleaner")->warn(
tr("cleaner.output.triggerAutoCleanTps", {mspt})
+23 -28
View File
@@ -1,44 +1,39 @@
#include "Cleaner.h"
namespace Cleaner {
void setShouldIgnore(GMLIB_Actor* ac) { ac->addTag("cleaner:ignore"); }
void setShouldIgnore(gmlib::world::actor::GMActor* ac) { ac->addTag("cleaner:ignore"); }
void ListenEvents() {
auto& eventBus = ll::event::EventBus::getInstance();
auto& config = Cleaner::Entry::getInstance().getConfig();
// ItemSpawnEvent
eventBus.emplaceListener<GMLIB::Event::EntityEvent::ItemActorSpawnAfterEvent>(
[&config](GMLIB::Event::EntityEvent::ItemActorSpawnAfterEvent& event) {
auto& item = event.getItemActor();
if (event.getSpawner().has_value()) {
auto pl = (GMLIB_Player*)event.getSpawner().as_ptr();
if (pl->isPlayer() && !pl->isAlive()) { // Death Drop
auto ac = (GMLIB_Actor*)&item;
setShouldIgnore(ac);
eventBus.emplaceListener<ila::mc::SpawnItemActorAfterEvent>([&config](ila::mc::SpawnItemActorAfterEvent& event) {
auto& item = event.itemActor();
if (event.spawner()) {
auto pl = (gmlib::world::actor::GMPlayer*)event.spawner();
if (pl->isPlayer() && !pl->isAlive()) { // Death Drop
auto ac = (gmlib::world::actor::GMActor*)&item;
setShouldIgnore(ac);
return;
}
}
if (config.ItemDespawn.Enabled) {
item->lifeTime() = config.ItemDespawn.DespawnTime;
auto list = config.ItemDespawn.WhiteList;
auto type = item->item().getTypeName();
for (auto& key : list) {
if (isMatch(type, key)) {
return;
}
}
if (config.ItemDespawn.Enabled) {
item.lifeTime() = config.ItemDespawn.DespawnTime;
auto list = config.ItemDespawn.WhiteList;
auto type = item.item().getTypeName();
for (auto& key : list) {
if (isMatch(type, key)) {
return;
}
}
item.lifeTime() = config.ItemDespawn.DespawnTime;
}
item->lifeTime() = config.ItemDespawn.DespawnTime;
}
);
});
// MobTakeItemEvent
eventBus.emplaceListener<GMLIB::Event::EntityEvent::MobPickupItemAfterEvent>(
[](GMLIB::Event::EntityEvent::MobPickupItemAfterEvent& event) {
auto mob = (GMLIB_Actor*)&event.self();
setShouldIgnore(mob);
}
);
eventBus.emplaceListener<ila::mc::ActorPickupItemAfterEvent>([](ila::mc::ActorPickupItemAfterEvent& event) {
auto mob = (gmlib::world::actor::GMActor*)&event.self();
setShouldIgnore(mob);
});
}
} // namespace Cleaner
+2 -2
View File
@@ -3,11 +3,11 @@
namespace Helper {
void broadcastMessage(std::string_view msg) {
GMLIB_Level::getInstance()->broadcast(tr("cleaner.info.prefix") + std::string(msg));
gmlib::world::GMLevel::getInstance()->broadcast(tr("cleaner.info.prefix") + std::string(msg));
}
void broadcastToast(std::string_view msg) {
GMLIB_Level::getInstance()->broadcastToast(tr("cleaner.info.prefix"), msg);
gmlib::world::GMLevel::getInstance()->broadcastToast(tr("cleaner.info.prefix"), msg);
}
} // namespace Helper