refactor: refactoring skeleton
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
#include "Cleaner.h"
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
bool isMatch(std::string& A, std::string& B) {
|
||||
// 如果B是正则表达式
|
||||
if (B.find_first_of(".*+?()[]{}|^$") != std::string::npos) {
|
||||
try {
|
||||
std::regex regex(B);
|
||||
return std::regex_match(A, regex);
|
||||
} catch (...) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return (A == B);
|
||||
}
|
||||
|
||||
bool shouldIgnore(GMLIB_Actor* ac) {
|
||||
if (ac->isMob() || ac->isItemActor()) {
|
||||
if (ac->isTame() || ac->isTrusting() || ac->getNameTag() != "") {
|
||||
return true;
|
||||
}
|
||||
auto nbt = ac->getNbt();
|
||||
if (nbt->getByte("ShowBottom") == 1) { // End Crystal Used Only, if has this tag 1b, it is modified by cleaner.
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ShouldClean(Actor* actor) {
|
||||
// Players
|
||||
auto en = (GMLIB_Actor*)actor;
|
||||
if (en->isPlayer() || shouldIgnore(en)) {
|
||||
return false;
|
||||
}
|
||||
auto type = en->getTypeName();
|
||||
auto tags = Config->getValue<std::vector<std::string>>({"IgnoreTags"}, {});
|
||||
for (auto& tag : tags) {
|
||||
if (en->hasTag(tag)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Items
|
||||
if (en->isItemActor()) {
|
||||
if (Config->getValue<bool>({"CleanItem", "Enabled"}, false)) {
|
||||
auto itac = (ItemActor*)en;
|
||||
if (itac->age() <= Config->getValue<int>({"CleanItem", "ExistTicks"}, 0)) {
|
||||
return false;
|
||||
}
|
||||
auto itemType = itac->item().getTypeName();
|
||||
auto whitelist = Config->getValue<std::vector<std::string>>({"CleanItem", "Whitelist"}, {});
|
||||
for (auto& key : whitelist) {
|
||||
if (isMatch(itemType, key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Mobs
|
||||
else if (en->isMob()) {
|
||||
if (Config->getValue<bool>({"CleanMobs", "Enabled"}, false)) {
|
||||
auto blacklist = Config->getValue<std::vector<std::string>>({"CleanMobs", "Blacklist"}, {});
|
||||
for (auto& key : blacklist) {
|
||||
if (isMatch(type, key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
auto whitelist = Config->getValue<std::vector<std::string>>({"CleanMobs", "Whitelist"}, {});
|
||||
for (auto& key : whitelist) {
|
||||
if (isMatch(type, key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (Config->getValue<bool>({"CleanMobs", "CleanMonstors"}, false)
|
||||
&& en->hasCategory(ActorCategory::Monster)) {
|
||||
return true;
|
||||
}
|
||||
if (Config->getValue<bool>({"CleanMobs", "CleanPeacefulMobs"}, false)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// Others
|
||||
else {
|
||||
if (Config->getValue<bool>({"CleanInanimate", "Enabled"}, false)) {
|
||||
auto blacklist = Config->getValue<std::vector<std::string>>({"CleanInanimate", "Blacklist"}, {});
|
||||
for (auto& key : blacklist) {
|
||||
if (isMatch(type, key)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int ExecuteClean() {
|
||||
int clean_count = 0;
|
||||
auto all_entities = GMLIB_Level::getLevel()->getAllEntities();
|
||||
for (auto entity : all_entities) {
|
||||
if (ShouldClean(entity)) {
|
||||
entity->despawn();
|
||||
clean_count++;
|
||||
}
|
||||
}
|
||||
return clean_count;
|
||||
}
|
||||
|
||||
int CountEntities() {
|
||||
int clean_count = 0;
|
||||
auto all_entities = GMLIB_Level::getLevel()->getAllEntities();
|
||||
for (auto entity : all_entities) {
|
||||
if (ShouldClean(entity)) {
|
||||
clean_count++;
|
||||
}
|
||||
}
|
||||
return clean_count;
|
||||
}
|
||||
|
||||
} // namespace Cleaner
|
||||
@@ -0,0 +1,108 @@
|
||||
#include "Cleaner.h"
|
||||
|
||||
namespace ConfigFile {
|
||||
|
||||
bool mConsoleLog = true;
|
||||
bool mAnnounce = true;
|
||||
bool mSendToast = true;
|
||||
|
||||
} // namespace ConfigFile
|
||||
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
using namespace ll::schedule;
|
||||
using namespace ll::chrono_literals;
|
||||
|
||||
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::ServerClock>> mAutoCleanTask;
|
||||
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::ServerClock>> mCheckCleanTask;
|
||||
|
||||
ServerTimeScheduler scheduler;
|
||||
|
||||
bool auto_clean_triggerred = false;
|
||||
|
||||
void CleanTask() {
|
||||
auto time = Config->getValue<int>({"Basic", "Notice1"}, 20);
|
||||
auto announce_time = Config->getValue<int>({"Basic", "Notice2"}, 5);
|
||||
auto time_1 = std::chrono::seconds::duration(time);
|
||||
auto time_2 = std::chrono::seconds::duration(time - announce_time);
|
||||
if (ConfigFile::mConsoleLog) {
|
||||
logger.info(tr("cleaner.output.count1", {S(time_1.count())}));
|
||||
}
|
||||
if (ConfigFile::mAnnounce) {
|
||||
Helper::broadcastMessage(tr("cleaner.output.count1", {S(time_1.count())}));
|
||||
}
|
||||
if (ConfigFile::mSendToast) {
|
||||
Helper::broadcastToast(tr("cleaner.output.count2", {S(announce_time)}));
|
||||
}
|
||||
scheduler.add<DelayTask>(time_2, [announce_time] {
|
||||
if (ConfigFile::mConsoleLog) {
|
||||
logger.info(tr("cleaner.output.count2", {S(announce_time)}));
|
||||
}
|
||||
if (ConfigFile::mAnnounce) {
|
||||
Helper::broadcastMessage(tr("cleaner.output.count2", {S(announce_time)}));
|
||||
}
|
||||
if (ConfigFile::mSendToast) {
|
||||
Helper::broadcastToast(tr("cleaner.output.count2", {S(announce_time)}));
|
||||
}
|
||||
});
|
||||
scheduler.add<DelayTask>(time_1, [] {
|
||||
auto count = ExecuteClean();
|
||||
if (ConfigFile::mConsoleLog) {
|
||||
logger.info(tr("cleaner.output.finish", {S(count)}));
|
||||
}
|
||||
if (ConfigFile::mAnnounce) {
|
||||
Helper::broadcastMessage(tr("cleaner.output.finish", {S(count)}));
|
||||
}
|
||||
if (ConfigFile::mSendToast) {
|
||||
Helper::broadcastToast(tr("cleaner.output.finish", {S(count)}));
|
||||
}
|
||||
auto_clean_triggerred = false;
|
||||
});
|
||||
}
|
||||
|
||||
void AutoCleanTask(int seconds) {
|
||||
auto time = std::chrono::seconds::duration(seconds);
|
||||
mAutoCleanTask = scheduler.add<RepeatTask>(time, [] { CleanTask(); });
|
||||
}
|
||||
|
||||
void CheckCleanTask(int max_entities, float min_tps) {
|
||||
mCheckCleanTask = scheduler.add<RepeatTask>(10s, [max_entities, min_tps] {
|
||||
auto count = CountEntities();
|
||||
if (auto_clean_triggerred == false) {
|
||||
if (count >= max_entities) {
|
||||
auto_clean_triggerred = true;
|
||||
if (ConfigFile::mConsoleLog) {
|
||||
logger.warn(tr("cleaner.output.triggerAutoCleanCount", {S(count)}));
|
||||
}
|
||||
if (ConfigFile::mAnnounce) {
|
||||
Helper::broadcastMessage(tr("cleaner.output.triggerAutoCleanCount", {S(count)}));
|
||||
}
|
||||
if (ConfigFile::mSendToast) {
|
||||
Helper::broadcastToast(tr("cleaner.output.triggerAutoCleanCount", {S(count)}));
|
||||
}
|
||||
CleanTask();
|
||||
} else if (GMLIB_Level::getLevel()->getServerAverageTps() <= min_tps) {
|
||||
auto_clean_triggerred = true;
|
||||
auto mspt = S(GMLIB_Level::getLevel()->getServerAverageTps());
|
||||
if (ConfigFile::mConsoleLog) {
|
||||
logger.warn(tr("cleaner.output.triggerAutoCleanCount", {mspt}));
|
||||
}
|
||||
if (ConfigFile::mAnnounce) {
|
||||
Helper::broadcastMessage(tr("cleaner.output.triggerAutoCleanCount", {mspt}));
|
||||
}
|
||||
if (ConfigFile::mSendToast) {
|
||||
Helper::broadcastToast(tr("cleaner.output.triggerAutoCleanCount", {mspt}));
|
||||
}
|
||||
CleanTask();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void stopAllTasks() {
|
||||
mCheckCleanTask->cancel();
|
||||
mAutoCleanTask->cancel();
|
||||
}
|
||||
|
||||
} // namespace Cleaner
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "Cleaner.h"
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
void loadCleaner() {
|
||||
ListenEvents();
|
||||
RegisterCommands();
|
||||
if (Config->getValue<bool>({"ScheduleClean", "Enabled"}, false)) {
|
||||
Cleaner::AutoCleanTask(Config->getValue<int>({"ScheduleClean", "CleanInterval"}, 3000));
|
||||
}
|
||||
if (Config->getValue<bool>({"AutoCleanCount", "Enabled"}, false)
|
||||
|| Config->getValue<bool>({"AutoCleanTPS", "Enabled"}, false)) {
|
||||
Cleaner::CheckCleanTask(
|
||||
Config->getValue<int>({"AutoCleanCount", "TriggerCount"}, 900),
|
||||
Config->getValue<int>({"AutoCleanTPS", "TriggerTPS"}, 15)
|
||||
);
|
||||
}
|
||||
ConfigFile::mItemDespawnTicks = Config->getValue<int>({"ItemDespawn", "DespawnTime"}, 6000);
|
||||
ConfigFile::mAnnounce = Config->getValue<bool>({"Basic", "SendBroadcast"}, true);
|
||||
ConfigFile::mConsoleLog = Config->getValue<bool>({"Basic", "ConsoleLog"}, true);
|
||||
ConfigFile::mSendToast = Config->getValue<bool>({"Basic", "SendToast"}, true);
|
||||
}
|
||||
|
||||
void unloadCleaner() {
|
||||
stopAllTasks();
|
||||
delete Config;
|
||||
delete Language;
|
||||
}
|
||||
|
||||
void reloadCleaner() {
|
||||
unloadCleaner();
|
||||
initPlugin();
|
||||
loadCleaner();
|
||||
}
|
||||
|
||||
} // namespace Cleaner
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
#include "Global.h"
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
extern int ExecuteClean();
|
||||
extern int CountEntities();
|
||||
|
||||
extern void AutoCleanTask(int seconds);
|
||||
extern void CheckCleanTask(int max_entities, float min_tps);
|
||||
extern void ListenEvents();
|
||||
extern void stopAllTasks();
|
||||
|
||||
} // namespace Cleaner
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "Cleaner.h"
|
||||
|
||||
namespace ConfigFile {
|
||||
|
||||
int mItemDespawnTicks = 3000;
|
||||
|
||||
} // namespace ConfigFile
|
||||
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
void setShouldIgnore(GMLIB_Actor* ac) {
|
||||
auto nbt = ac->getNbt();
|
||||
nbt->put("ShowBottom", ByteTag(1));
|
||||
ac->setNbt(*nbt);
|
||||
}
|
||||
|
||||
void ListenEvents() {
|
||||
auto& eventBus = ll::event::EventBus::getInstance();
|
||||
// ItemSpawnEvent
|
||||
eventBus.emplaceListener<GMLIB::Event::EntityEvent::ItemActorSpawnAfterEvent>(
|
||||
[](GMLIB::Event::EntityEvent::ItemActorSpawnAfterEvent& event) {
|
||||
auto& item = event.getItemActor();
|
||||
auto pl = (GMLIB_Player*)event.getSpawner();
|
||||
if (pl) {
|
||||
if (pl->isPlayer() && !pl->isAlive()) { // Death Drop
|
||||
auto ac = (GMLIB_Actor*)&item;
|
||||
setShouldIgnore(ac);
|
||||
return;
|
||||
}
|
||||
}
|
||||
item.lifeTime() = ConfigFile::mItemDespawnTicks;
|
||||
}
|
||||
);
|
||||
// MobTakeItemEvent
|
||||
eventBus.emplaceListener<GMLIB::Event::EntityEvent::MobPickupItemAfterEvent>(
|
||||
[](GMLIB::Event::EntityEvent::MobPickupItemAfterEvent& event) {
|
||||
auto mob = (GMLIB_Actor*)&event.self();
|
||||
setShouldIgnore(mob);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace Cleaner
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "Global.h"
|
||||
|
||||
namespace Helper {
|
||||
|
||||
void broadcastMessage(std::string msg) {
|
||||
TextPacket::createRawMessage(tr("cleaner.info.prefix") + msg).sendToClients();
|
||||
}
|
||||
|
||||
void broadcastToast(std::string msg) {
|
||||
auto pkt = ToastRequestPacket(tr("cleaner.info.prefix"), msg);
|
||||
pkt.sendToClients();
|
||||
}
|
||||
|
||||
} // namespace Helper
|
||||
Reference in New Issue
Block a user