Skip to content

Commit

Permalink
chore: add command and form
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Mar 12, 2024
1 parent 27ec316 commit fb09799
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
81 changes: 81 additions & 0 deletions src/Command/Command.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "Command.h"
#include "Config/Config.h"

#include "form/index.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <ll/api/Logger.h>
#include <ll/api/command/Command.h>
#include <ll/api/command/CommandHandle.h>
#include <ll/api/command/CommandRegistrar.h>
#include <ll/api/plugin/NativePlugin.h>
#include <ll/api/service/Bedrock.h>
#include <ll/api/utils/HashUtils.h>
#include <mc/entity/utilities/ActorType.h>
#include <mc/server/commands/CommandOrigin.h>
#include <mc/server/commands/CommandOriginType.h>
#include <mc/server/commands/CommandOutput.h>
#include <mc/server/commands/CommandParameterOption.h>
#include <mc/server/commands/CommandPermissionLevel.h>
#include <mc/server/commands/CommandRegistry.h>
#include <mc/server/commands/CommandSelector.h>
#include <mc/world/actor/Actor.h>
#include <mc/world/actor/player/Player.h>
#include <memory>
#include <numeric>
#include <stdexcept>
#include <string>
#include <utility>


namespace tools::command {

// clang-format off
#define CHECK_COMMAND_TYPE(currentType, targetType) \
if (currentType != targetType) { \
std::string targetTypeStr; \
switch (targetType) { \
case CommandOriginType::Player: targetTypeStr = "players"; break; \
case CommandOriginType::CommandBlock: targetTypeStr = "command blocks"; break; \
case CommandOriginType::MinecartCommandBlock: targetTypeStr = "minecart command blocks"; break; \
case CommandOriginType::DevConsole: targetTypeStr = "the developer console"; break; \
case CommandOriginType::Test: targetTypeStr = "test origins"; break; \
case CommandOriginType::AutomationPlayer: targetTypeStr = "automation players"; break; \
case CommandOriginType::ClientAutomation: targetTypeStr = "client automation"; break; \
case CommandOriginType::DedicatedServer: targetTypeStr = "dedicated servers"; break; \
case CommandOriginType::Entity: targetTypeStr = "entities"; break; \
case CommandOriginType::Virtual: targetTypeStr = "virtual origins"; break; \
case CommandOriginType::GameArgument: targetTypeStr = "game argument origins"; break; \
case CommandOriginType::EntityServer: targetTypeStr = "entity servers"; break; \
case CommandOriginType::Precompiled: targetTypeStr = "precompiled origins"; break; \
case CommandOriginType::GameDirectorEntityServer: targetTypeStr = "game director entity servers"; break; \
case CommandOriginType::Scripting: targetTypeStr = "scripting origins"; break; \
case CommandOriginType::ExecuteContext: targetTypeStr = "execute contexts"; break; \
default: targetTypeStr = "unknown"; \
} \
return output.error("This command is available to [" + targetTypeStr + "] only!"); \
}
// clang-format on

bool regCommand() {
auto& cmd = ll::command::CommandRegistrar::getInstance().getOrCreateCommand(
config::cfg.command.commandName,
config::cfg.command.commandDescription
);

// tools
cmd.overload().execute<[&](CommandOrigin const& origin, CommandOutput& output) {
CHECK_COMMAND_TYPE(origin.getOriginType(), CommandOriginType::Player);
Actor* entity = origin.getEntity();
if (entity) {
auto& player = *static_cast<Player*>(entity);
tools::form::index(player);
}
}>();

return true;
}

} // namespace tools::command
7 changes: 7 additions & 0 deletions src/Command/Command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


namespace tools::command {

bool regCommand();

}
4 changes: 2 additions & 2 deletions src/Config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace tools::config {
struct Configs {
int version = 1;
struct Command {
std::string commandName;
std::string commandDescription;
std::string commandName = "toosl";
std::string commandDescription = "LeviOPTools";
} command;
};

Expand Down
6 changes: 6 additions & 0 deletions src/Entry/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <ll/api/plugin/NativePlugin.h>
#include <memory>

// my files
#include "Command/Command.h"
#include "Config/Config.h"

namespace entry {

entry::entry() = default;
Expand All @@ -19,6 +23,7 @@ bool entry::load(ll::plugin::NativePlugin& self) {
getSelf().getLogger().info("loading...");

// Code for loading the plugin goes here.
tools::config::loadConfig();

return true;
}
Expand All @@ -27,6 +32,7 @@ bool entry::enable() {
getSelf().getLogger().info("enabling...");

// Code for enabling the plugin goes here.
tools::command::regCommand();

return true;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Form/index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <ll/api/form/SimpleForm.h>

#include "Form/index.h"
#include "mc/world/actor/player/Player.h"

namespace tools::form {

void index(Player& player) {
using SimpleForm = ll::form::SimpleForm;

SimpleForm fm;
fm.setTitle("default");
fm.appendButton("call", [&](Player& player) { player.sendMessage("button 1"); });
fm.sendTo(player, [](Player& player, int id) {
player.sendMessage("send"); // def
});
}


} // namespace tools::form
8 changes: 8 additions & 0 deletions src/Form/index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "mc/world/actor/player/Player.h"

namespace tools::form {

void index(Player& player);


} // namespace tools::form

0 comments on commit fb09799

Please sign in to comment.