From 87a671899a9d91fecc963323f2a44fc959c0c559 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 <116721335+Tsubasa6848@users.noreply.github.com> Date: Fri, 5 Jan 2024 22:13:21 +0800 Subject: [PATCH] feat: add more feature --- src/Cleaner.cpp | 2 +- src/Global.h | 2 ++ src/ItemDespawn.cpp | 42 +++++++++++++++++++++++++ src/Mspt.cpp | 2 ++ src/Plugin.cpp | 6 ++-- src/RegCommand.cpp | 77 ++++++++++++++++++++++++++++++--------------- 6 files changed, 103 insertions(+), 28 deletions(-) create mode 100644 src/ItemDespawn.cpp diff --git a/src/Cleaner.cpp b/src/Cleaner.cpp index 33dcd82..a34730a 100644 --- a/src/Cleaner.cpp +++ b/src/Cleaner.cpp @@ -112,7 +112,7 @@ void AutoCleanTask(int seconds) { void CheckCleanTask(int max_entities, float min_tps) { check_clean_task = scheduler.add(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!"); diff --git a/src/Global.h b/src/Global.h index b00812a..c610d84 100644 --- a/src/Global.h +++ b/src/Global.h @@ -7,6 +7,7 @@ extern void RegisterCommands(); extern void UnregisterCommands(); static std::shared_ptr> auto_clean_task; static std::shared_ptr> 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(); } \ No newline at end of file diff --git a/src/ItemDespawn.cpp b/src/ItemDespawn.cpp new file mode 100644 index 0000000..a779777 --- /dev/null +++ b/src/ItemDespawn.cpp @@ -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); +} \ No newline at end of file diff --git a/src/Mspt.cpp b/src/Mspt.cpp index a40b6f0..09b83dd 100644 --- a/src/Mspt.cpp +++ b/src/Mspt.cpp @@ -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 diff --git a/src/Plugin.cpp b/src/Plugin.cpp index a55e571..e6514a2 100644 --- a/src/Plugin.cpp +++ b/src/Plugin.cpp @@ -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; diff --git a/src/RegCommand.cpp b/src/RegCommand.cpp index b40d071..9d477e7 100644 --- a/src/RegCommand.cpp +++ b/src/RegCommand.cpp @@ -9,31 +9,61 @@ void RegVoteCommand(CommandRegistry& registry) { CommandOrigin const& origin, CommandOutput& output, std::unordered_map& 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& 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& result) { - // reload & config + if (result["despawn"].isSet && result["target"].isSet) { + auto ens = result["target"].get>(); + 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(); + 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(); + 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& result) { - auto ens = result["target"].get>(); - 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"); } \ No newline at end of file