feat: clean entities
This commit is contained in:
+40
-24
@@ -3,17 +3,21 @@
|
||||
using namespace ll::schedule;
|
||||
using namespace ll::chrono_literals;
|
||||
|
||||
GameTimeScheduler scheduler;
|
||||
GameTimeAsyncScheduler scheduler;
|
||||
bool auto_clean_triggerred = false;
|
||||
|
||||
namespace Cleaner {
|
||||
|
||||
bool isDeathDrop(ItemActor* itac) {
|
||||
// Todo
|
||||
//
|
||||
return false;
|
||||
}
|
||||
|
||||
bool shouldIgnoreMob(Mob* mob) {
|
||||
// Todo
|
||||
bool shouldIgnoreMob(Actor* ac) {
|
||||
if (ac->isTame() || ac->isTrusting() || ac->getNameTag() != "") {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -22,10 +26,6 @@ bool ShouldClean(Actor* en) {
|
||||
if (en->isType(ActorType::Player)) {
|
||||
return false;
|
||||
}
|
||||
// Tag
|
||||
if (en->hasTag("testtag")) {
|
||||
// Todo
|
||||
}
|
||||
auto type = en->getTypeName();
|
||||
// Items
|
||||
if (en->hasCategory(ActorCategory::Item)) {
|
||||
@@ -36,7 +36,6 @@ bool ShouldClean(Actor* en) {
|
||||
}
|
||||
auto itemType = itac->item().getTypeName();
|
||||
if (true) { // Todo Config WhiteList
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -46,14 +45,15 @@ bool ShouldClean(Actor* en) {
|
||||
// Mobs
|
||||
else if (en->hasCategory(ActorCategory::Mob)) {
|
||||
if (true) { // Todo Config Toggle
|
||||
auto mob = (Mob*)en;
|
||||
if (shouldIgnoreMob(mob)) {
|
||||
if (shouldIgnoreMob(en)) {
|
||||
return false;
|
||||
}
|
||||
// Test
|
||||
return true;
|
||||
if (true) { // Todo WhiteList
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -63,6 +63,7 @@ bool ShouldClean(Actor* en) {
|
||||
if (true) { // BlackList
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -91,24 +92,39 @@ int CountEntities() {
|
||||
return clean_count;
|
||||
}
|
||||
|
||||
using namespace ll::schedule;
|
||||
|
||||
using namespace ll::chrono_literals;
|
||||
|
||||
void CleanTask() {
|
||||
// Todo Schedule
|
||||
logger.info("clean will in {}", 20s);
|
||||
scheduler.add<DelayTask>(20s, [] {
|
||||
void CleanTask(int time, int announce_time) {
|
||||
auto time_1 = std::chrono::seconds::duration(time);
|
||||
auto time_2 = std::chrono::seconds::duration(time - announce_time);
|
||||
logger.info("clean will in {}", time_1);
|
||||
scheduler.add<DelayTask>(time_2, [announce_time] { logger.info("clean will in {}", announce_time); });
|
||||
scheduler.add<DelayTask>(time_1, [] {
|
||||
auto count = ExecuteClean();
|
||||
logger.info("Successfully cleaned {} entities", count);
|
||||
auto_clean_triggerred = false;
|
||||
});
|
||||
}
|
||||
|
||||
void AutoCleanTask() {
|
||||
auto_clean_task->cancel();
|
||||
auto_clean_task = scheduler.add<RepeatTask>(1min, [] {
|
||||
CleanTask();
|
||||
void AutoCleanTask(int seconds) {
|
||||
auto time = std::chrono::seconds::duration(seconds);
|
||||
auto_clean_task = scheduler.add<RepeatTask>(time, [] { CleanTask(20, 5); });
|
||||
}
|
||||
|
||||
void CheckCleanTask(int max_entities, float min_tps) {
|
||||
check_clean_task = scheduler.add<RepeatTask>(10s, [max_entities, min_tps] {
|
||||
auto count = CountEntities();
|
||||
if (!auto_clean_triggerred) {
|
||||
if (count >= max_entities) {
|
||||
auto_clean_triggerred = true;
|
||||
logger.warn("Too many entities! Auto started clean task!");
|
||||
CleanTask(20, 5);
|
||||
} else if (TPS::getAverageTps() <= min_tps) {
|
||||
auto_clean_triggerred = true;
|
||||
logger.warn("TPS too low! Auto started clean task!");
|
||||
CleanTask(20, 5);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
} // namespace Cleaner
|
||||
+10
-2
@@ -6,8 +6,16 @@ extern ll::Logger logger;
|
||||
extern void RegisterCommands();
|
||||
extern void UnregisterCommands();
|
||||
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::GameTimeClock>> auto_clean_task;
|
||||
static std::shared_ptr<ll::schedule::task::Task<ll::chrono::GameTimeClock>> check_clean_task;
|
||||
|
||||
namespace Cleaner {
|
||||
extern void CleanTask();
|
||||
extern void AutoCleanTask();
|
||||
extern void CleanTask(int time, int announce_time);
|
||||
extern void AutoCleanTask(int seconds);
|
||||
extern void CheckCleanTask(int max_entities, float min_tps);
|
||||
}
|
||||
|
||||
namespace TPS {
|
||||
extern void CaculateTPS();
|
||||
extern float getCurrentTps();
|
||||
extern float getAverageTps();
|
||||
}
|
||||
+54
-1
@@ -1 +1,54 @@
|
||||
#include "Global.h"
|
||||
#include "Global.h"
|
||||
|
||||
typedef std::chrono::high_resolution_clock timer_clock;
|
||||
#define TIMER_START auto start = timer_clock::now();
|
||||
#define TIMER_END \
|
||||
auto elapsed = timer_clock::now() - start; \
|
||||
long long timeReslut = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
|
||||
|
||||
uint ticks = 0;
|
||||
float average_tps = 0;
|
||||
double mspt = 0;
|
||||
bool culculate_mspt = false;
|
||||
std::list<ushort> avrtps = {};
|
||||
|
||||
LL_AUTO_TYPED_INSTANCE_HOOK(LevelTickHook, ll::memory::HookPriority::Normal, Level, "?tick@Level@@UEAAXXZ", void) {
|
||||
ticks++;
|
||||
TIMER_START
|
||||
origin();
|
||||
TIMER_END
|
||||
culculate_mspt = true;
|
||||
if (culculate_mspt) {
|
||||
mspt = (double)timeReslut / 1000;
|
||||
culculate_mspt = false;
|
||||
}
|
||||
}
|
||||
|
||||
namespace TPS {
|
||||
|
||||
void CaculateTPS() {
|
||||
std::thread([] {
|
||||
while (true) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
culculate_mspt = true;
|
||||
avrtps.push_back(ticks);
|
||||
ticks = 0;
|
||||
if (avrtps.size() >= 60) {
|
||||
avrtps.clear();
|
||||
continue;
|
||||
}
|
||||
uint ticks_minute = 0;
|
||||
for (auto i : avrtps) {
|
||||
ticks_minute = ticks_minute + i;
|
||||
}
|
||||
float res = (float)ticks_minute / ((float)avrtps.size());
|
||||
average_tps = res >= 20 ? 20 : res;
|
||||
}
|
||||
}).detach();
|
||||
}
|
||||
|
||||
float getCurrentTps() { return mspt <= 50 ? 20 : (float)(1000.0 / mspt); }
|
||||
|
||||
float getAverageTps() { return average_tps; }
|
||||
|
||||
} // namespace TPS
|
||||
|
||||
+3
-1
@@ -10,7 +10,8 @@ Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) {
|
||||
|
||||
bool Plugin::enable() {
|
||||
RegisterCommands();
|
||||
Cleaner::AutoCleanTask();
|
||||
Cleaner::AutoCleanTask(60);
|
||||
Cleaner::CheckCleanTask(20, 15);
|
||||
logger.info("Cleaner Loaded!");
|
||||
logger.info("Author: Tsubasa6848");
|
||||
logger.info("Repository: https://github.com/GroupMountain/Cleaner");
|
||||
@@ -21,6 +22,7 @@ bool Plugin::disable() {
|
||||
logger.info("Disabling Cleaner...");
|
||||
// Code for disabling the plugin goes here.
|
||||
auto_clean_task->cancel();
|
||||
check_clean_task->cancel();
|
||||
UnregisterCommands();
|
||||
logger.info("Cleaner Disabled!");
|
||||
return true;
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ void RegCleanCommand(CommandRegistry& registry) {
|
||||
CommandOrigin const& origin,
|
||||
CommandOutput& output,
|
||||
std::unordered_map<std::string, DynamicCommand::Result>& result) {
|
||||
Cleaner::CleanTask();
|
||||
Cleaner::CleanTask(15, 7);
|
||||
});
|
||||
DynamicCommand::setup(registry, std::move(command));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user