generated from GroupMountain/GMLIB-Mod-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tsubasa6848
committed
Mar 4, 2024
1 parent
68e192e
commit dc2819e
Showing
8 changed files
with
257 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include "Global.h" | ||
|
||
struct FloatingTextParam { | ||
enum class GUI { gui } gui; | ||
enum class Reload { reload } reload; | ||
enum class Create { createstatic } createstatic; | ||
enum class Static { createdynamic } createdynamic; | ||
DimensionType dimension; | ||
CommandPositionFloat position; | ||
std::string text; | ||
int seconds; | ||
}; | ||
|
||
void RegisterCommand() { | ||
auto& cmd = ll::command::CommandRegistrar::getInstance().getOrCreateCommand( | ||
"floatingtext", | ||
tr("command.floatingtext.description"), | ||
CommandPermissionLevel::GameDirectors | ||
); | ||
ll::service::getCommandRegistry()->registerAlias("floatingtext", "ft"); | ||
cmd.overload<FloatingTextParam>() | ||
.required("reload") | ||
.execute<[](CommandOrigin const& origin, CommandOutput& output, FloatingTextParam const& param) { | ||
removeAllFloatingTexts(); | ||
initFloatingTexts(); | ||
auto count = getFloatingTextCount(); | ||
return output.success(tr("command.floatingtext.reload", {S(count.first), S(count.second)})); | ||
}>(); | ||
cmd.overload<FloatingTextParam>() | ||
.required("gui") | ||
.execute<[](CommandOrigin const& origin, CommandOutput& output, FloatingTextParam const& param) { | ||
auto type = origin.getOriginType(); | ||
if (type == CommandOriginType::Player) { | ||
// todo | ||
return output.success(tr("command.todo")); | ||
} | ||
return output.error(tr("command.error.invalidCommandOrigin")); | ||
}>(); | ||
cmd.overload<FloatingTextParam>() | ||
.required("createstatic") | ||
.required("text") | ||
.required("position") | ||
.required("dimension") | ||
.execute<[](CommandOrigin const& origin, CommandOutput& output, FloatingTextParam const& param) { | ||
auto text = param.text; | ||
auto pos = param.position.mOffset; | ||
auto dimid = param.dimension; | ||
createStaticFloatingText(text, pos, dimid); | ||
auto dimName = VanillaDimensions::toString(param.dimension); | ||
return output.success(tr("command.createStatic.success", {dimName, pos.toString()})); | ||
}>(); | ||
cmd.overload<FloatingTextParam>() | ||
.required("createdynamic") | ||
.required("text") | ||
.required("position") | ||
.required("dimension") | ||
.required("seconds") | ||
.execute<[](CommandOrigin const& origin, CommandOutput& output, FloatingTextParam const& param) { | ||
auto text = param.text; | ||
auto pos = param.position.mOffset; | ||
auto dimid = param.dimension; | ||
auto update = param.seconds; | ||
createDynamicFloatingText(text, pos, dimid, update); | ||
auto dimName = VanillaDimensions::toString(param.dimension); | ||
return output.success(tr("command.createDynamic.success", {dimName, pos.toString(), S(update)})); | ||
}>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
#include "Global.h" | ||
|
||
std::string defaultConfig = R"({ | ||
"language": "en_US" | ||
})"; | ||
|
||
std::string defaultStaticFile = R"([ | ||
{ | ||
"Text": "Example Static Text 1", | ||
"Position": { | ||
"x": -5, | ||
"y": 90, | ||
"z": -5, | ||
"dim": 0 | ||
} | ||
}, | ||
{ | ||
"Text": "Example\nStatic\nText 2", | ||
"Position": { | ||
"x": -5, | ||
"y": 95, | ||
"z": -5, | ||
"dim": 0 | ||
} | ||
} | ||
])"; | ||
|
||
std::string defaultDynamicFile = R"([ | ||
{ | ||
"Text": "Player Name: %player_realname%\nServer Mspt: %server_mspt%", | ||
"UpdateRate": 1, | ||
"Position": { | ||
"x": 5, | ||
"y": 90, | ||
"z": 5, | ||
"dim": 0 | ||
} | ||
}, | ||
{ | ||
"Text": "Player Ping: %player_ping%ms\nServer Tps: %server_tps%", | ||
"UpdateRate": 2, | ||
"Position": { | ||
"x": 5, | ||
"y": 95, | ||
"z": 5, | ||
"dim": 0 | ||
} | ||
} | ||
])"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
#pragma once | ||
#include <ll/api/Logger.h> | ||
// #include <include_all.h> | ||
#include <include_all.h> | ||
|
||
#define PLUGIN_NAME "plugin" | ||
#define PLUGIN_NAME "FloatingText" | ||
#define S(x) std::to_string(x) | ||
|
||
extern ll::Logger logger; | ||
extern ll::Logger logger; | ||
|
||
extern void removeAllFloatingTexts(); | ||
extern void initConfig(); | ||
extern void initFloatingTexts(); | ||
extern void RegisterCommand(); | ||
|
||
extern std::pair<int, int> getFloatingTextCount(); | ||
|
||
extern void createStaticFloatingText(std::string& text, Vec3& pos, int dimId); | ||
extern void createDynamicFloatingText(std::string& text, Vec3& pos, int dimId, int update); | ||
|
||
extern std::string tr(std::string key, std::vector<std::string> data = {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include "Config.h" | ||
#include "Global.h" | ||
#include "Language.h" | ||
|
||
GMLIB::Files::JsonConfig* Config = nullptr; | ||
GMLIB::Files::I18n::LangI18n* Language = nullptr; | ||
int commandPermissionLevel = 4; | ||
|
||
nlohmann::json StaticFloatingTextList; | ||
nlohmann::json DynamicFloatingTextList; | ||
|
||
std::unordered_map<int64, GMLIB::Server::FloatingText*> RuntimeFloatingTexts; | ||
|
||
void initConfig() { | ||
Config = new GMLIB::Files::JsonConfig("./plugins/FloatingText/config/config.json", defaultConfig); | ||
Config->init(); | ||
std::string langPath = "./plugins/FloatingText/language/"; | ||
std::string languageCode = Config->getValue<std::string>({"language"}, "en_US"); | ||
Language = new GMLIB::Files::I18n::LangI18n(langPath, languageCode); | ||
Language->updateOrCreateLanguage("en_US", defaultLanguage_en_US); | ||
Language->updateOrCreateLanguage("zh_CN", defaultLanguage_zh_CN); | ||
Language->loadAllLanguages(); | ||
Language->chooseLanguage(languageCode); | ||
} | ||
|
||
void initFloatingTexts() { | ||
StaticFloatingTextList = | ||
GMLIB::Files::JsonFile::initJson("./plugins/FloatingText/config/StaticFloatingText.json", defaultStaticFile); | ||
for (auto& config : StaticFloatingTextList) { | ||
auto text = GMLIB::Files::JsonFile::getValue<std::string>(config, {"Text"}, ""); | ||
auto x = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "x"}, 0); | ||
auto y = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "y"}, 0); | ||
auto z = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "z"}, 0); | ||
auto dimid = GMLIB::Files::JsonFile::getValue<int>(config, {"Position", "dim"}, 0); | ||
Vec3 pos{x, y, z}; | ||
auto ft = new GMLIB::Server::StaticFloatingText(text, pos, dimid, true); | ||
// Static | ||
RuntimeFloatingTexts[ft->getRuntimeID()] = ft; | ||
} | ||
DynamicFloatingTextList = | ||
GMLIB::Files::JsonFile::initJson("./plugins/FloatingText/config/DynamicFloatingText.json", defaultDynamicFile); | ||
for (auto& config : DynamicFloatingTextList) { | ||
auto text = GMLIB::Files::JsonFile::getValue<std::string>(config, {"Text"}, ""); | ||
auto x = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "x"}, 0); | ||
auto y = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "y"}, 0); | ||
auto z = GMLIB::Files::JsonFile::getValue<float>(config, {"Position", "z"}, 0); | ||
auto dimid = GMLIB::Files::JsonFile::getValue<int>(config, {"Position", "dim"}, 0); | ||
auto update = GMLIB::Files::JsonFile::getValue<int>(config, {"UpdateRate"}, 1); | ||
Vec3 pos{x, y, z}; | ||
auto ft = new GMLIB::Server::DynamicFloatingText(text, pos, dimid, update, true); | ||
// Dynamic | ||
RuntimeFloatingTexts[ft->getRuntimeID()] = ft; | ||
} | ||
} | ||
|
||
void removeAllFloatingTexts() { | ||
for (auto& key : RuntimeFloatingTexts) { | ||
delete key.second; | ||
} | ||
} | ||
|
||
std::pair<int, int> getFloatingTextCount() { return {StaticFloatingTextList.size(), DynamicFloatingTextList.size()}; } | ||
|
||
std::string tr(std::string key, std::vector<std::string> data) { return Language->translate(key, data); } | ||
|
||
void createStaticFloatingText(std::string& text, Vec3& pos, int dimId) { | ||
nlohmann::json json{ | ||
{"Text", text }, | ||
{"Position", {{"x", pos.x}, {"y", pos.y}, {"z", pos.z}, {"dim", dimId}}} | ||
}; | ||
auto ft = new GMLIB::Server::StaticFloatingText(text, pos, dimId, true); | ||
StaticFloatingTextList.push_back(json); | ||
std::string path = "./plugins/FloatingText/config/StaticFloatingText.json"; | ||
GMLIB::Files::JsonFile::writeFile(path, StaticFloatingTextList); | ||
RuntimeFloatingTexts[ft->getRuntimeID()] = ft; | ||
} | ||
|
||
void createDynamicFloatingText(std::string& text, Vec3& pos, int dimId, int update) { | ||
nlohmann::json json{ | ||
{"Text", text }, | ||
{"UpdateRate", update }, | ||
{"Position", {{"x", pos.x}, {"y", pos.y}, {"z", pos.z}, {"dim", dimId}}} | ||
}; | ||
auto ft = new GMLIB::Server::DynamicFloatingText(text, pos, dimId, update, true); | ||
DynamicFloatingTextList.push_back(json); | ||
std::string path = "./plugins/FloatingText/config/DynamicFloatingText.json"; | ||
GMLIB::Files::JsonFile::writeFile(path, DynamicFloatingTextList); | ||
RuntimeFloatingTexts[ft->getRuntimeID()] = ft; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
#include "Global.h" | ||
|
||
std::string defaultLanguage_en_US = R"( | ||
command.floatingtext.reload=已重载全部悬浮字!\n当前加载了 %1$s 个静态悬浮字和 %2$s 个动态悬浮字。 | ||
command.floatingtext.description=创建悬浮字 | ||
command.error.invalidCommandOrigin=This command can only be executed by the player! | ||
command.createStatic.success=已成功在 %1$s 的 %2$s 创建静态悬浮字。 | ||
command.createDynamic.success=已成功在 %1$s 的 %2$s 创建更新周期为 %3$ss 的动态悬浮字。 | ||
)"; | ||
|
||
std::string defaultLanguage_zh_CN = R"( | ||
command.floatingtext.reload=已重载全部悬浮字!\n当前加载了 %1$s 个静态悬浮字和 %2$s 个动态悬浮字。 | ||
command.floatingtext.description=创建悬浮字 | ||
command.error.invalidCommandOrigin=该命令只能由玩家或控制台执行! | ||
command.createStatic.success=已成功在 %1$s 的 %2$s 创建静态悬浮字。 | ||
command.createDynamic.success=已成功在 %1$s 的 %2$s 创建更新周期为 %3$ss 的动态悬浮字。 | ||
)"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters