Skip to content
This repository has been archived by the owner on Jan 20, 2025. It is now read-only.

Commit

Permalink
feat: complete index form callback
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Mar 14, 2024
1 parent 57133b7 commit 243c96b
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/Api/Global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "mc/world/actor/player/Player.h"
#include <string>

namespace tools::api {

bool runCmd(Player& player, const std::string&);

}
29 changes: 29 additions & 0 deletions src/Api/runcmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "api/Global.h"
#include <ll/api/service/Bedrock.h>
#include <ll/api/service/ServerInfo.h>
#include <ll/api/service/Service.h>
#include <ll/api/service/ServiceManager.h>
#include <mc/common/wrapper/optional_ref.h>
#include <mc/server/commands/CommandContext.h>
#include <mc/server/commands/MinecraftCommands.h>
#include <mc/server/commands/PlayerCommandOrigin.h>
#include <mc/world/Minecraft.h>
#include <mc/world/actor/player/Player.h>
#include <mc/world/level/Command.h>


namespace tools::api {

bool runCmd(Player& player, const std::string& cmd) {
try {
CommandContext context =
CommandContext(cmd, std::make_unique<PlayerCommandOrigin>(PlayerCommandOrigin(player)));
ll::service::getMinecraft()->getCommands().executeCommand(context);
return true;
} catch (...) {
return false;
}
}


} // namespace tools::api
2 changes: 1 addition & 1 deletion src/Command/Command.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Command.h"
#include "file/Config.h"

#include "form/index.h"
#include "form/Global.h"
#include <ll/api/Logger.h>
#include <ll/api/command/Command.h>
#include <ll/api/command/CommandHandle.h>
Expand Down
2 changes: 2 additions & 0 deletions src/Entry/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// my files
#include "Command/Command.h"
#include "file/Config.h"
#include "form/Global.h"

namespace entry {
entry::entry() = default;
Expand Down Expand Up @@ -57,6 +58,7 @@ bool entry::enable() {

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

return true;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Form/Global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "mc/world/actor/player/Player.h"
#include <filesystem>
#include <functional>
#include <string>
#include <unordered_map>

namespace tools::form {

void index(Player&, std::filesystem::path);
void index(Player&);

extern std::unordered_map<std::string, std::function<void(Player&)>> mapping;

bool initMapping();

} // namespace tools::form
41 changes: 32 additions & 9 deletions src/Form/index.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#include "Form/index.h"
#include "Form/Global.h"
#include "api/Global.h"
#include "entry/Entry.h"
#include "entry/PluginInfo.h"
#include "file/file.h"

#include <fstream>

#include <functional>
#include <ll/api/form/CustomForm.h>
#include <ll/api/form/ModalForm.h>
#include <ll/api/form/SimpleForm.h>
#include <ll/api/i18n/I18n.h>
#include <ll/api/utils/HashUtils.h>
#include <mc/world/actor/player/Player.h>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
Expand Down Expand Up @@ -51,10 +53,9 @@ void from_json(const json& j, FormStruct& f) {
j.at("buttons").get_to(f.buttons);
}

void index(Player& player) {
void index(Player& player, std::filesystem::path filePath) {
auto& mSelf = entry::entry::getInstance().getSelf();
auto& logger = mSelf.getLogger();
auto filePath = mSelf.getPluginDir() / "form" / "index.json";
auto jsonData = tools::file::loadJsonFile(filePath);
if (jsonData) {
FormStruct formStruct = jsonData->get<FormStruct>();
Expand All @@ -66,8 +67,27 @@ void index(Player& player) {
for (const auto& button : formStruct.buttons) {

std::function<void(Player&)> buttonCallback = [&, button](Player& player) {
// 这里可以访问button变量
player.sendMessage("点击了按钮: " + button.title); // 举例
using namespace ll::hash_utils;
using namespace ll::hash_literals;
switch (doHash(button.callbackType)) {
case "cmd"_h:
tools::api::runCmd(player, button.callbackRun);
break;
case "form"_h:
index(player, mSelf.getPluginDir() / "form" / button.callbackRun);
break;
case "function"_h:
if (mapping.find(button.callbackRun) != mapping.end()) {
return mapping[button.callbackRun](player);
}
logger.error("Unsupported function parameters: {}"_tr(button.callbackRun));
player.sendMessage("The plugin error is due to the error in the console"_tr());
break;
default:
logger.error("Unsupported callbackType: {}"_tr(button.callbackType));
player.sendMessage("The plugin error is due to the error in the console"_tr());
break;
}
};

if (button.imageType.empty() || button.imageUrl.empty()) {
Expand All @@ -85,10 +105,13 @@ void index(Player& player) {

fm.sendTo(player);
} else {
player.sendMessage("Plugin error, unable to read or find file \"{}\""_tr(filePath));
logger.error("The plugin error is due to the error in the console"_tr());
player.sendMessage("The plugin error is due to the error in the console"_tr());
logger.error("Plugin error, unable to read or find file \"{}\""_tr(filePath));
}
}

void index(Player& player) {
auto filePath = entry::entry::getInstance().getSelf().getPluginDir() / "form" / "index.json";
return index(player, filePath);
}

} // namespace tools::form
8 changes: 0 additions & 8 deletions src/Form/index.h

This file was deleted.

19 changes: 19 additions & 0 deletions src/Form/mapping.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "form/Global.h"
#include "mc/world/actor/player/Player.h"
#include <functional>
#include <string>
#include <unordered_map>

namespace tools::form {

std::unordered_map<std::string, std::function<void(Player&)>> mapping;

void test(Player& player) { player.sendMessage("test"); }

bool initMapping() {
mapping["test"] = test;
return true;
}


} // namespace tools::form

0 comments on commit 243c96b

Please sign in to comment.