Skip to content

Commit 8398daf

Browse files
committed
refactor: fix some cmd shit
1 parent 9c93e77 commit 8398daf

File tree

6 files changed

+46
-50
lines changed

6 files changed

+46
-50
lines changed

src/legacy/api/CommandAPI.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "engine/GlobalShareData.h"
1414
#include "engine/LocalShareData.h"
1515
#include "ll/api/command/CommandRegistrar.h"
16+
#include "ll/api/event/EventBus.h"
17+
#include "ll/api/event/server/ServerStartedEvent.h"
1618
#include "ll/api/service/Bedrock.h"
1719
#include "ll/api/service/ServerInfo.h"
1820
#include "magic_enum.hpp"
@@ -41,6 +43,8 @@
4143
#include "mc/world/level/dimension/Dimension.h"
4244
#include "utils/Utils.h"
4345

46+
#include "lse/Plugin.h"
47+
4448
#include <filesystem>
4549
#include <string>
4650
#include <vector>
@@ -227,9 +231,7 @@ Local<Value> McClass::newCommand(const Arguments& args) {
227231
"applied except for setOverload!",
228232
name
229233
);
230-
return CommandClass::newCommand(
231-
const_cast<std::add_pointer_t<std::remove_cv_t<std::remove_pointer_t<decltype(instance)>>>>(instance)
232-
);
234+
return CommandClass::newCommand(instance);
233235
}
234236

235237
auto desc = args[1].toStr();
@@ -247,20 +249,13 @@ Local<Value> McClass::newCommand(const Arguments& args) {
247249
}
248250
}
249251
}
250-
if (ll::service::getCommandRegistry().has_value()) {
251-
auto command =
252-
DynamicCommand::createCommand(ll::service::getCommandRegistry().get(), name, desc, permission, flag);
253-
if (command) {
254-
if (!alias.empty()) {
255-
command->setAlias(alias);
256-
}
257-
return CommandClass::newCommand(std::move(command));
252+
auto command = DynamicCommand::createCommand(name, desc, permission, flag);
253+
if (command) {
254+
if (!alias.empty()) {
255+
command->setAlias(alias);
258256
}
259-
return Boolean::newBoolean(false);
257+
return CommandClass::newCommand(std::move(command));
260258
}
261-
lse::getSelfPluginInstance().getLogger().warn(
262-
"Server have not started yet, please don't use mc.newCommand() before server started."
263-
);
264259
return Boolean::newBoolean(false);
265260
}
266261
CATCH("Fail in newCommand!")
@@ -533,7 +528,19 @@ Local<Value> CommandClass::setup(const Arguments& args) {
533528
setCallback(args);
534529
}
535530
if (registered) return Boolean::newBoolean(true);
536-
return Boolean::newBoolean(DynamicCommand::setup(ll::service::getCommandRegistry(), std::move(uptr)));
531+
if (ll::service::getCommandRegistry()) {
532+
return Boolean::newBoolean(DynamicCommand::setup(ll::service::getCommandRegistry(), std::move(uptr)));
533+
} else {
534+
ll::event::EventBus::getInstance().emplaceListener<ll::event::ServerStartedEvent>(
535+
[pp = std::make_shared<decltype(uptr)>(std::move(uptr))](auto&&) mutable {
536+
DynamicCommand::setup(ll::service::getCommandRegistry(), std::move(*pp));
537+
},
538+
ll::event::EventPriority::Normal,
539+
lse::Plugin::current()
540+
);
541+
}
542+
543+
return Boolean::newBoolean(true);
537544
}
538545
CATCH("Fail in setup!")
539546
}

src/legacy/legacyapi/command/DynamicCommand.cpp

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,12 @@ void DynamicCommand::execute(CommandOrigin const& origin, CommandOutput& output)
595595
}
596596

597597
std::unique_ptr<class DynamicCommandInstance> DynamicCommand::createCommand(
598-
CommandRegistry& registry,
599598
std::string const& name,
600599
std::string const& description,
601600
CommandPermissionLevel permission,
602601
CommandFlag flag
603602
) {
604-
return DynamicCommandInstance::create(registry, name, description, permission, flag);
603+
return DynamicCommandInstance::create(name, description, permission, flag);
605604
}
606605

607606
DynamicCommandInstance const*
@@ -614,7 +613,6 @@ DynamicCommand::setup(CommandRegistry& registry, std::unique_ptr<class DynamicCo
614613
}
615614

616615
std::unique_ptr<class DynamicCommandInstance> DynamicCommand::createCommand(
617-
CommandRegistry& registry,
618616
std::string const& name,
619617
std::string const& description,
620618
std::unordered_map<std::string, std::vector<std::string>>&& enums,
@@ -624,7 +622,7 @@ std::unique_ptr<class DynamicCommandInstance> DynamicCommand::createCommand(
624622
CommandPermissionLevel permission,
625623
CommandFlag flag
626624
) {
627-
auto command = createCommand(registry, name, description, permission, flag);
625+
auto command = createCommand(name, description, permission, flag);
628626
if (!command) return std::unique_ptr<class DynamicCommandInstance>();
629627
for (auto& [desc, values] : enums) {
630628
command->setEnum(desc, std::move(values));
@@ -647,9 +645,9 @@ void DynamicCommand::updateAvailableCommands(CommandRegistry& registry) {
647645
registry.serializeAvailableCommands().sendToClients();
648646
}
649647

650-
DynamicCommandInstance const* DynamicCommand::getInstance() const { return getInstance(getCommandName()); }
648+
DynamicCommandInstance* DynamicCommand::getInstance() const { return getInstance(getCommandName()); }
651649

652-
DynamicCommandInstance const* DynamicCommand::getInstance(std::string const& commandName) {
650+
DynamicCommandInstance* DynamicCommand::getInstance(std::string const& commandName) {
653651
auto iter = dynamicCommandInstances.find(commandName);
654652
if (iter == dynamicCommandInstances.end()) return nullptr;
655653
else return iter->second.get();
@@ -681,7 +679,6 @@ static std::unique_ptr<Command> commandBuilder(uintptr_t t) {
681679
}
682680

683681
DynamicCommandInstance::DynamicCommandInstance(
684-
CommandRegistry& registry,
685682
std::string const& name,
686683
std::string const& description,
687684
CommandPermissionLevel permission,
@@ -691,25 +688,17 @@ DynamicCommandInstance::DynamicCommandInstance(
691688
description_(std::make_unique<std::string>(description)),
692689
permission_(permission),
693690
flag_(flag),
694-
registry(registry),
695691
builder(std::make_unique<ll::memory::NativeClosure<std::unique_ptr<Command>>>(commandBuilder, (uintptr_t)this)) {}
696692

697693
DynamicCommandInstance::~DynamicCommandInstance() = default;
698694

699695
std::unique_ptr<DynamicCommandInstance> DynamicCommandInstance::create(
700-
CommandRegistry& registry,
701696
std::string const& name,
702697
std::string const& description,
703698
CommandPermissionLevel permission,
704699
CommandFlag flag
705700
) {
706-
if (registry.findCommand(name)) {
707-
logger.error("Command \"{}\" already exists", name);
708-
return nullptr;
709-
}
710-
return std::unique_ptr<DynamicCommandInstance>(
711-
new DynamicCommandInstance(registry, name, description, permission, flag)
712-
);
701+
return std::unique_ptr<DynamicCommandInstance>(new DynamicCommandInstance(name, description, permission, flag));
713702
}
714703

715704
bool DynamicCommandInstance::addOverload(std::vector<DynamicCommand::ParameterData>&& params) {
@@ -886,10 +875,10 @@ std::string DynamicCommandInstance::setSoftEnum(std::string const& name, std::ve
886875
softEnums.emplace(name, values);
887876
} else {
888877
if (!ll::command::CommandRegistrar::getInstance().hasSoftEnum(name)) {
889-
registry.addSoftEnum(name, values);
890-
return name;
878+
ll::command::CommandRegistrar::getInstance().tryRegisterSoftEnum(name, values);
879+
} else {
880+
ll::command::CommandRegistrar::getInstance().setSoftEnumValues(name, values);
891881
}
892-
CommandSoftEnumRegistry(registry).updateSoftEnum(SoftEnumUpdateType::Replace, name, values);
893882
}
894883
return name;
895884
}
@@ -904,10 +893,10 @@ bool DynamicCommandInstance::addSoftEnumValues(std::string const& name, std::vec
904893
}
905894
} else {
906895
if (!ll::command::CommandRegistrar::getInstance().hasSoftEnum(name)) {
907-
registry.addSoftEnum(name, values);
908-
return true;
896+
ll::command::CommandRegistrar::getInstance().tryRegisterSoftEnum(name, values);
897+
} else {
898+
ll::command::CommandRegistrar::getInstance().addSoftEnumValues(name, values);
909899
}
910-
CommandSoftEnumRegistry(registry).updateSoftEnum(SoftEnumUpdateType::Add, name, values);
911900
}
912901
return true;
913902
}
@@ -924,7 +913,7 @@ bool DynamicCommandInstance::removeSoftEnumValues(std::string const& name, std::
924913
}
925914
return false;
926915
} else {
927-
CommandSoftEnumRegistry(registry).updateSoftEnum(SoftEnumUpdateType::Remove, name, values);
916+
ll::command::CommandRegistrar::getInstance().removeSoftEnumValues(name, values);
928917
}
929918
return true;
930919
}

src/legacy/legacyapi/command/DynamicCommand.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,12 @@ class DynamicCommand : public Command {
522522
/*1*/ virtual void execute(class CommandOrigin const& origin, class CommandOutput& output) const;
523523

524524
static std::unique_ptr<class DynamicCommandInstance> createCommand(
525-
CommandRegistry& registry,
526525
std::string const& name,
527526
std::string const& description,
528527
CommandPermissionLevel permission = CommandPermissionLevel::GameDirectors,
529528
CommandFlag flag = CommandFlagValue::NotCheat
530529
);
531530
static std::unique_ptr<class DynamicCommandInstance> createCommand(
532-
CommandRegistry& registry,
533531
std::string const& name,
534532
std::string const& description,
535533
std::unordered_map<std::string, std::vector<std::string>>&& enums,
@@ -571,9 +569,9 @@ class DynamicCommand : public Command {
571569

572570
static void updateAvailableCommands(CommandRegistry& registry);
573571

574-
DynamicCommandInstance const* getInstance() const;
572+
DynamicCommandInstance* getInstance() const;
575573

576-
static DynamicCommandInstance const* getInstance(std::string const& commandName);
574+
static DynamicCommandInstance* getInstance(std::string const& commandName);
577575
};
578576

579577
/**
@@ -600,7 +598,6 @@ class DynamicCommandInstance {
600598
std::unique_ptr<std::string> description_;
601599
CommandPermissionLevel permission_;
602600
CommandFlag flag_;
603-
CommandRegistry& registry;
604601

605602
public:
606603
std::unique_ptr<ll::memory::NativeClosure<std::unique_ptr<Command>>> builder;
@@ -626,7 +623,6 @@ class DynamicCommandInstance {
626623
friend class DynamicCommand;
627624

628625
DynamicCommandInstance(
629-
CommandRegistry& registry,
630626
std::string const& name,
631627
std::string const& description,
632628
CommandPermissionLevel permission,
@@ -638,7 +634,6 @@ class DynamicCommandInstance {
638634
virtual ~DynamicCommandInstance();
639635

640636
static std::unique_ptr<DynamicCommandInstance> create(
641-
CommandRegistry& registry,
642637
std::string const& name,
643638
std::string const& description,
644639
CommandPermissionLevel permission = CommandPermissionLevel::GameDirectors,
@@ -749,7 +744,6 @@ inline DynamicCommandInstance const* DynamicCommand::setup(
749744
return setup(
750745
registry,
751746
createCommand(
752-
registry,
753747
name,
754748
description,
755749
std::move(enums),

src/legacy/main/BuiltinCommands.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ bool ProcessDebugEngine(const std::string& cmd) {
4949

5050
void RegisterDebugCommand() {
5151
auto command = DynamicCommand::createCommand(
52-
ll::service::getCommandRegistry(),
5352
LLSE_DEBUG_CMD,
5453
"Debug LegacyScriptEngine",
5554
CommandPermissionLevel::Owner

src/lse/Plugin.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#include "Plugin.h"
22

3+
#include "Entry.h"
4+
#include "legacy/engine/EngineOwnData.h"
5+
36
#include <ll/api/plugin/Manifest.h>
47

58
namespace lse {
69

710
Plugin::Plugin(const ll::plugin::Manifest& manifest) : ll::plugin::Plugin(manifest) {}
811

12+
std::shared_ptr<ll::plugin::Plugin> Plugin::current() {
13+
return lse::getPluginManager().getPlugin(ENGINE_OWN_DATA()->pluginName);
14+
}
915
} // namespace lse

src/lse/Plugin.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
namespace lse {
99

1010
class Plugin : public ll::plugin::Plugin {
11+
friend PluginManager;
12+
1113
public:
1214
Plugin(const ll::plugin::Manifest& manifest);
1315

14-
private:
15-
friend PluginManager;
16+
static std::shared_ptr<ll::plugin::Plugin> current();
1617
};
1718
} // namespace lse

0 commit comments

Comments
 (0)