feat: add more feature

This commit is contained in:
Tsubasa6848
2024-01-05 22:13:21 +08:00
Unverified
parent 57f70fac0c
commit 87a671899a
6 changed files with 103 additions and 28 deletions
+1 -1
View File
@@ -112,7 +112,7 @@ void AutoCleanTask(int seconds) {
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 (auto_clean_triggerred == false) {
if (count >= max_entities) {
auto_clean_triggerred = true;
logger.warn("Too many entities! Auto started clean task!");
+2
View File
@@ -7,6 +7,7 @@ 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;
extern int item_despawn_time;
namespace Cleaner {
extern void CleanTask(int time, int announce_time);
@@ -18,4 +19,5 @@ namespace TPS {
extern void CaculateTPS();
extern float getCurrentTps();
extern float getAverageTps();
extern float getMspt();
}
+42
View File
@@ -0,0 +1,42 @@
#include "Global.h"
int item_despawn_time = 3000;
LL_AUTO_TYPED_INSTANCE_HOOK(
ItemSpawnEventHook,
ll::memory::HookPriority::Normal,
Spawner,
"?spawnItem@Spawner@@QEAAPEAVItemActor@@AEAVBlockSource@@AEBVItemStack@@PEAVActor@@AEBVVec3@@H@Z",
ItemActor*,
class BlockSource& region,
class ItemStack const& item,
class Actor* spawner,
class Vec3 const& pos,
int throwTime
) {
auto itac = origin(region, item, spawner, pos, throwTime);
if (itac) {
if (spawner) {
if (spawner->isType(ActorType::Player)) {
if (!spawner->isAlive()) {
// Death Drop
return itac;
}
}
}
itac->mLifeTime = item_despawn_time;
}
return itac;
}
LL_AUTO_TYPED_INSTANCE_HOOK(
ItemTakeEventHook,
ll::memory::HookPriority::Normal,
Actor,
"?pickUpItem@Actor@@QEAAXAEAVItemActor@@H@Z",
void,
class ItemActor& itemActor, int count
) {
logger.warn("pickup item {}", this->getTypeName());
return origin(itemActor,count);
}
+2
View File
@@ -51,4 +51,6 @@ float getCurrentTps() { return mspt <= 50 ? 20 : (float)(1000.0 / mspt); }
float getAverageTps() { return average_tps; }
float getMspt() { return mspt; }
} // namespace TPS
+4 -2
View File
@@ -9,7 +9,9 @@ Plugin::Plugin(ll::plugin::NativePlugin& self) : mSelf(self) {
}
bool Plugin::enable() {
//TPS::CaculateTPS();
RegisterCommands();
item_despawn_time = 300;
Cleaner::AutoCleanTask(60);
Cleaner::CheckCleanTask(20, 15);
logger.info("Cleaner Loaded!");
@@ -21,8 +23,8 @@ bool Plugin::enable() {
bool Plugin::disable() {
logger.info("Disabling Cleaner...");
// Code for disabling the plugin goes here.
auto_clean_task->cancel();
check_clean_task->cancel();
//auto_clean_task->cancel();
//check_clean_task->cancel();
UnregisterCommands();
logger.info("Cleaner Disabled!");
return true;
+52 -25
View File
@@ -9,31 +9,61 @@ void RegVoteCommand(CommandRegistry& registry) {
CommandOrigin const& origin,
CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>& result) {
// vote clean
// vote clean command
});
DynamicCommand::setup(registry, std::move(command));
}
void RegCleanCommand(CommandRegistry& registry) {
auto command = DynamicCommand::createCommand(registry, "clean", "Clean entities", CommandPermissionLevel::Any);
command->addOverload();
void RegCleanerCommand(CommandRegistry& registry) {
auto command = DynamicCommand::createCommand(
registry,
"cleaner",
"Cleaner plugin settings",
CommandPermissionLevel::GameDirectors
);
command->setEnum("Despawn", {"despawn"});
command->setEnum("Action", {"tps", "clean", "reload", "mspt"});
command->setEnum("Despawntime", {"despawntime"});
command->mandatory("action", ParamType::Enum, "Action", CommandParameterOption::EnumAutocompleteExpansion);
command
->mandatory("despawntime", ParamType::Enum, "Despawntime", CommandParameterOption::EnumAutocompleteExpansion);
command->mandatory("despawn", ParamType::Enum, "Despawn", CommandParameterOption::EnumAutocompleteExpansion);
command->mandatory("ticks", ParamType::Int);
command->mandatory("target", ParamType::Actor);
command->addOverload({"action"});
command->addOverload({"despawntime", "ticks"});
command->addOverload({"despawn", "target"});
command->setCallback([](DynamicCommand const& cmd,
CommandOrigin const& origin,
CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>& result) {
Cleaner::CleanTask(15, 7);
});
DynamicCommand::setup(registry, std::move(command));
}
void RegConfigCommand(CommandRegistry& registry) {
auto command = DynamicCommand::createCommand(registry, "cleaner", "Cleaner plugin settings", CommandPermissionLevel::Any);
command->addOverload();
command->setCallback([](DynamicCommand const& cmd,
CommandOrigin const& origin,
CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>& result) {
// reload & config
if (result["despawn"].isSet && result["target"].isSet) {
auto ens = result["target"].get<std::vector<Actor*>>();
if (ens.size() == 0) {
return output.error("No matched targets found!");
}
for (auto en : ens) {
en->despawn();
}
return output.success("Successfully despawned {} entities", ens.size());
} else if (result["action"].isSet) {
auto act = result["action"].get<std::string>();
if (act == "tps") {
return output.success("current tps {}, average tps {}", TPS::getCurrentTps(), TPS::getAverageTps());
} else if (act == "mspt") {
return output.success("mspt {}", TPS::getMspt());
} else if (act == "clean") {
output.success("Clean task started!");
Cleaner::CleanTask(20, 5);
return;
} else if (act == "reload") {
output.success("Reloading Cleaner ...");
return output.success("Cleaner Reloaded!");
}
} else if (result["despawntime"].isSet && result["ticks"].isSet) {
item_despawn_time = result["ticks"].get<int>();
return output.success("Successfully set deapawn time to {}", item_despawn_time);
}
});
DynamicCommand::setup(registry, std::move(command));
}
@@ -47,22 +77,19 @@ void RegDespawnCommand(CommandRegistry& registry) {
CommandOrigin const& origin,
CommandOutput& output,
std::unordered_map<std::string, DynamicCommand::Result>& result) {
auto ens = result["target"].get<std::vector<Actor*>>();
for (auto en : ens) {
en->despawn();
}
});
DynamicCommand::setup(registry, std::move(command));
}
void RegisterCommands() {
auto registry = ll::service::bedrock::getCommandRegistry();
RegCleanCommand(registry);
RegDespawnCommand(registry);
RegCleanerCommand(registry);
RegVoteCommand(registry);
}
void UnregisterCommands() {
auto registry = ll::service::bedrock::getCommandRegistry();
registry->unregisterCommand("clean");
registry->unregisterCommand("despawn");
registry->unregisterCommand("cleaner");
registry->unregisterCommand("voteclean");
}