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

Commit

Permalink
feat: Complete the index form
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Mar 14, 2024
1 parent 462fa53 commit 57133b7
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Command/Command.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "Command.h"
#include "Config/Config.h"
#include "file/Config.h"

#include "form/index.h"
#include <ll/api/Logger.h>
Expand Down Expand Up @@ -163,7 +163,7 @@ bool regCommand() {
}
}>();

// tools talkas <Player> [msg]
// tools talkas <Player> <msg>
cmd.overload<Args_Kick>()
.text("talkas")
.required("player")
Expand Down
8 changes: 4 additions & 4 deletions src/Entry/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

namespace entry {
entry::entry() = default;
Expand All @@ -23,11 +23,11 @@ bool entry::load(ll::plugin::NativePlugin& self) {
mSelf = std::addressof(self);
auto& logger = getSelf().getLogger();

tools::config::loadConfig();

ll::i18n::load("plugins/LeviOPTools/lang");
ll::i18n::load(mSelf->getLangDir());
using ll::i18n_literals::operator""_tr;

tools::config::loadConfig();

logger.info("Autor: {}"_tr(PLUGIN_AUTHOR));
logger.info("Version: {}.{}.{} for Levilamina and BDS Protocol {}"_tr(
PLUGIN_VERSION_MAJOR,
Expand Down
80 changes: 75 additions & 5 deletions src/Form/index.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,93 @@
#include "Form/index.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 <mc/world/actor/player/Player.h>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
#include <optional>
#include <sys/stat.h>
#include <vector>


namespace tools::form {

using ll::i18n_literals::operator""_tr;
using string = std::string;
using SimpleForm = ll::form::SimpleForm;
using ModalForm = ll::form::ModalForm;
using CustomForm = ll::form::CustomForm;
using json = nlohmann::json;

struct Buttons {
string title;
string imageType;
string imageUrl;
string callbackType;
string callbackRun;
};
struct FormStruct {
string title;
string content;
std::vector<Buttons> buttons;
};

void from_json(const json& j, Buttons& b) {
j.at("title").get_to(b.title);
j.at("imageType").get_to(b.imageType);
j.at("imageUrl").get_to(b.imageUrl);
j.at("callbackType").get_to(b.callbackType);
j.at("callbackRun").get_to(b.callbackRun);
}
void from_json(const json& j, FormStruct& f) {
j.at("title").get_to(f.title);
j.at("content").get_to(f.content);
j.at("buttons").get_to(f.buttons);
}

void index(Player& player) {
SimpleForm fm;
fm.setTitle(PLUGIN_NAME);
fm.appendButton("call", [&](Player& player) { player.sendMessage("button 1"); });
fm.sendTo(player);
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>();

SimpleForm fm;
fm.setTitle(formStruct.title.empty() ? "" : formStruct.title);
fm.setContent(formStruct.content.empty() ? "" : formStruct.content);

for (const auto& button : formStruct.buttons) {

std::function<void(Player&)> buttonCallback = [&, button](Player& player) {
// 这里可以访问button变量
player.sendMessage("点击了按钮: " + button.title); // 举例
};

if (button.imageType.empty() || button.imageUrl.empty()) {
fm.appendButton(button.title, buttonCallback);
} else if (button.imageType == "url" || button.imageType == "path") {
fm.appendButton(button.title, button.imageUrl, button.imageType, buttonCallback);
} else {
logger.error(
"Build button failed, imageType value is invalid, allowed value \"url\" or \"path\", current value {}"_tr(
button.imageType
)
);
}
}

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());
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/Config/Config.cpp → src/file/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bool loadConfig() {
// init file
std::filesystem::path p = filePath;
if (!std::filesystem::exists(p)) {
logger.info("Saving default configurations"_tr());
logger.info("Saving default configurations");
writeConfig(cfg);
}
// loading
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions src/file/file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "file/file.h"
#include "nlohmann/json_fwd.hpp"
#include <fstream>
#include <optional>

namespace tools::file {

std::optional<nlohmann::json> loadJsonFile(const std::filesystem::path& path) {
try {
std::ifstream file(path);
if (file.good()) {
return nlohmann::json::parse(file);
}
return std::nullopt;
} catch (...) {
return std::nullopt;
}
return std::nullopt;
}


} // namespace tools::file
10 changes: 10 additions & 0 deletions src/file/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <nlohmann/json.hpp>
#include <optional>


namespace tools::file {

std::optional<nlohmann::json> loadJsonFile(const std::filesystem::path&);


}

0 comments on commit 57133b7

Please sign in to comment.