Compare commits

...

7 Commits

11 changed files with 263 additions and 17 deletions
+137 -3
View File
@@ -16,6 +16,7 @@ const GMLIB_API = {
setPlayerNbt: ll.import("GMLIB_API", "setPlayerNbt"), setPlayerNbt: ll.import("GMLIB_API", "setPlayerNbt"),
setPlayerNbtTags: ll.import("GMLIB_API", "setPlayerNbtTags"), setPlayerNbtTags: ll.import("GMLIB_API", "setPlayerNbtTags"),
resourcePackTranslate: ll.import("GMLIB_API", "resourcePackTranslate"), resourcePackTranslate: ll.import("GMLIB_API", "resourcePackTranslate"),
getResourcePackI18nLanguage: ll.import("GMLIB_API", "getResourcePackI18nLanguage"),
chooseResourcePackI18nLanguage: ll.import("GMLIB_API", "chooseResourcePackI18nLanguage"), chooseResourcePackI18nLanguage: ll.import("GMLIB_API", "chooseResourcePackI18nLanguage"),
setEducationFeatureEnabled: ll.import("GMLib_ServerAPI", "setEducationFeatureEnabled"), setEducationFeatureEnabled: ll.import("GMLib_ServerAPI", "setEducationFeatureEnabled"),
registerAbilityCommand: ll.import("GMLib_ServerAPI", "registerAbilityCommand"), registerAbilityCommand: ll.import("GMLib_ServerAPI", "registerAbilityCommand"),
@@ -34,6 +35,7 @@ const GMLIB_API = {
getExperimentTranslatedName: ll.import("GMLIB_API", "getExperimentTranslatedName"), getExperimentTranslatedName: ll.import("GMLIB_API", "getExperimentTranslatedName"),
getExperimentEnabled: ll.import("GMLib_ModAPI", "getExperimentEnabled"), getExperimentEnabled: ll.import("GMLib_ModAPI", "getExperimentEnabled"),
setExperimentEnabled: ll.import("GMLib_ModAPI", "setExperimentEnabled"), setExperimentEnabled: ll.import("GMLib_ModAPI", "setExperimentEnabled"),
unregisterRecipe: ll.import("GMLIB_API", "unregisterRecipe"),
registerExperimentsRequire: ll.import("GMLib_ModAPI", "registerExperimentsRequire"), registerExperimentsRequire: ll.import("GMLib_ModAPI", "registerExperimentsRequire"),
registerStoneCutterRecipe: ll.import("GMLib_ModAPI", "registerStoneCutterRecipe"), registerStoneCutterRecipe: ll.import("GMLib_ModAPI", "registerStoneCutterRecipe"),
registerSmithingTrimRecipe: ll.import("GMLib_ModAPI", "registerSmithingTrimRecipe"), registerSmithingTrimRecipe: ll.import("GMLib_ModAPI", "registerSmithingTrimRecipe"),
@@ -87,7 +89,8 @@ const GMLIB_API = {
setWorldSpawn: ll.import("GMLIB_API", "setWorldSpawn"), setWorldSpawn: ll.import("GMLIB_API", "setWorldSpawn"),
getPlayerSpawnPoint: ll.import("GMLIB_API", "getPlayerSpawnPoint"), getPlayerSpawnPoint: ll.import("GMLIB_API", "getPlayerSpawnPoint"),
setPlayerSpawnPoint: ll.import("GMLIB_API", "setPlayerSpawnPoint"), setPlayerSpawnPoint: ll.import("GMLIB_API", "setPlayerSpawnPoint"),
clearPlayerSpawnPoint: ll.import("GMLIB_API", "clearPlayerSpawnPoint") clearPlayerSpawnPoint: ll.import("GMLIB_API", "clearPlayerSpawnPoint"),
setCustomPackPath: ll.import("GMLIB_API", "setCustomPackPath")
} }
const FloatingTextList = []; const FloatingTextList = [];
@@ -380,10 +383,18 @@ class Minecraft {
return GMLIB_API.throwEntity(entity, proj, speed = 2, offset = 3); return GMLIB_API.throwEntity(entity, proj, speed = 2, offset = 3);
} }
static chooseResourcePackI18nLanguage(language) { static getServerLanguage() {
return GMLIB_API.getResourcePackI18nLanguage(language);
}
static setServerLanguage(language) {
return GMLIB_API.chooseResourcePackI18nLanguage(language); return GMLIB_API.chooseResourcePackI18nLanguage(language);
} }
static setCustomPackPath(path) {
GMLIB_API.setCustomPackPath(path);
}
static resourcePackTranslate(key, params = []) { static resourcePackTranslate(key, params = []) {
return GMLIB_API.resourcePackTranslate(key, params); return GMLIB_API.resourcePackTranslate(key, params);
} }
@@ -394,6 +405,10 @@ class Recipes {
throw new Error("Static class cannot be instantiated"); throw new Error("Static class cannot be instantiated");
} }
static unregisterRecipe(recipeId) {
return GMLIB_API.unregisterRecipe(recipeId);
}
static registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount) { static registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount) {
return GMLIB_API.registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount); return GMLIB_API.registerStoneCutterRecipe(recipeId, inputName, inputAux, outputName, outputAux, outputCount);
} }
@@ -465,6 +480,53 @@ class Experiments {
} }
} }
class Version {
constructor(major, minor, patch) {
this.mMajor = major;
this.mMinor = minor;
this.mPatch = patch;
}
toString(prefix = true) {
let result = `${this.mMajor}.${this.mMinor}.${this.mPatch}`;
if (prefix) {
result = "v" + result;
}
return result;
}
toArray() {
return [this.mMajor, this.mMinor, this.mPatch];
}
valueOf() {
return 100000000 * this.mMajor + 10000 * this.mMinor + this.mPatch;
}
static fromString(string) {
if (typeof string === 'string' || string instanceof String) {
let pattern = /^v?\d+\.\d+\.\d+$/;
if (pattern.test(string)) {
let regex = /\d+/g;
let numbers = string.match(regex);
let array = numbers.slice(0, 3).map(Number);
return new Version(array[0], array[1], array[2]);
}
}
return null;
}
static fromArray(array) {
if (Array.isArray(array) && array.length == 3) {
let isNumber = array.every(element => typeof element == "number");
if (isNumber) {
return new Version(array[0], array[1], array[2])
}
}
return null;
}
}
class GMLIB { class GMLIB {
constructor() { constructor() {
throw new Error("Static class cannot be instantiated"); throw new Error("Static class cannot be instantiated");
@@ -649,6 +711,7 @@ class JsonConfig {
constructor(path, defultValue = {}) { constructor(path, defultValue = {}) {
this.mData = defultValue; this.mData = defultValue;
this.mPath = path; this.mPath = path;
this.init();
} }
init() { init() {
@@ -688,6 +751,74 @@ class JsonConfig {
} }
} }
class JsonLanguage extends JsonConfig {
constructor(path, defultValue = {}) {
super(path, defultValue);
}
translate(key, data = []) {
let result = this.get(key);
if (result == null) {
return key;
}
data.forEach((val, index) => {
let old = `{${index + 1}}`;
result = result.split(old).join(val);
});
return result;
}
}
class JsonI18n {
constructor(path, localLangCode = "en_US") {
if (!path.endsWith("/") && !path.endsWith("\\")) {
path = path + "/";
}
this.mPath = path;
this.mLangCode = localLangCode;
this.mAllLanguages = {};
this.mDefaultLangCode = "en_US";
this.loadAllLanguages();
}
loadAllLanguages() {
let exist_list = File.getFilesList(this.mPath);
exist_list.forEach((name) => {
let code = name.replace(".json", "");
let path = this.mPath + name;
let language = new JsonLanguage(path);
this.mAllLanguages[code] = language;
});
}
loadLanguage(langCode, defaultData = {}) {
let langPath = this.mPath;
langPath = langPath + langCode + ".json";
let language = new JsonLanguage(langPath, defaultData);
this.mAllLanguages[langCode] = language;
}
chooseLanguage(langCode) {
this.mLangCode = langCode;
}
setDefaultLanguage(langCode) {
this.mDefaultLangCode = langCode;
}
translate(key, data = [], langCode = this.mLangCode) {
let language = this.mAllLanguages[langCode];
let result = language.translate(key, data);
if (result == key) {
let language = this.mAllLanguages[this.mDefaultLangCode];
if (language) {
result = language.translate(key, data);
}
}
return result;
}
};
module.exports = { module.exports = {
StaticFloatingText, StaticFloatingText,
DynamicFloatingText, DynamicFloatingText,
@@ -696,5 +827,8 @@ module.exports = {
Experiments, Experiments,
GMLIB, GMLIB,
Scoreboard, Scoreboard,
JsonConfig JsonConfig,
JsonLanguage,
JsonI18n,
Version
}; };
+2 -1
View File
@@ -1,7 +1,8 @@
{ {
"name": "${pluginName}", "name": "${pluginName}",
"entry": "${pluginFile}", "entry": "${pluginFile}",
"version": "0.9.4", "version": "0.10.0",
"author": "GroupMountain",
"type": "native", "type": "native",
"dependencies": [ "dependencies": [
{ {
+15 -2
View File
@@ -14,6 +14,16 @@ ActorUniqueID parseScriptUniqueID(std::string uniqueId) {
} }
void Export_Compatibility_API() { void Export_Compatibility_API() {
RemoteCall::exportAs("GMLIB_API", "unregisterRecipe", [](std::string id) -> bool {
auto level = GMLIB_Level::getInstance();
if (!level) {
return false;
}
return GMLIB::Mod::CustomRecipe::unregisterRecipe(id);
});
RemoteCall::exportAs("GMLIB_API", "setCustomPackPath", [](std::string path) -> void {
GMLIB::Mod::CustomPacks::addCustomPackPath(path);
});
RemoteCall::exportAs("GMLIB_API", "getServerMspt", []() -> float { RemoteCall::exportAs("GMLIB_API", "getServerMspt", []() -> float {
auto level = GMLIB_Level::getInstance(); auto level = GMLIB_Level::getInstance();
if (!level) { if (!level) {
@@ -148,8 +158,8 @@ void Export_Compatibility_API() {
return false; return false;
}); });
RemoteCall::exportAs("GMLIB_API", "isVersionMatched", [](int a, int b, int c) -> bool { RemoteCall::exportAs("GMLIB_API", "isVersionMatched", [](int a, int b, int c) -> bool {
auto version = SemVersion(a, b, c, "", ""); auto version = GMLIB::Version(a, b, c, "", "");
return LIB_VERSION.satisfies(version); return version >= LIB_VERSION;
}); });
RemoteCall::exportAs("GMLIB_API", "getVersion_LRCA", []() -> std::string { return LIB_VERSION.asString(); }); RemoteCall::exportAs("GMLIB_API", "getVersion_LRCA", []() -> std::string { return LIB_VERSION.asString(); });
RemoteCall::exportAs("GMLIB_API", "getVersion_GMLIB", []() -> std::string { RemoteCall::exportAs("GMLIB_API", "getVersion_GMLIB", []() -> std::string {
@@ -163,6 +173,9 @@ void Export_Compatibility_API() {
RemoteCall::exportAs("GMLIB_API", "chooseResourcePackI18nLanguage", [](std::string code) -> void { RemoteCall::exportAs("GMLIB_API", "chooseResourcePackI18nLanguage", [](std::string code) -> void {
I18n::chooseLanguage(code); I18n::chooseLanguage(code);
}); });
RemoteCall::exportAs("GMLIB_API", "getResourcePackI18nLanguage", []() -> std::string {
return I18n::getCurrentLanguage()->getFullLanguageCode();
});
RemoteCall::exportAs("GMLIB_API", "getPlayerPosition", [](std::string uuid) -> std::pair<BlockPos, int> { RemoteCall::exportAs("GMLIB_API", "getPlayerPosition", [](std::string uuid) -> std::pair<BlockPos, int> {
auto uid = mce::UUID::fromString(uuid); auto uid = mce::UUID::fromString(uuid);
auto pos = GMLIB_Player::getPlayerPosition(uid); auto pos = GMLIB_Player::getPlayerPosition(uid);
+28 -6
View File
@@ -105,14 +105,13 @@ void Export_Event_API() {
); );
return true; return true;
} }
case doHash("onTextSend"): { case doHash("onEntityChangeDim"): {
auto Call = RemoteCall::importAs<bool(std::string author, std::string message)>(eventName, eventId); auto Call = RemoteCall::importAs<bool(Actor * entity, int toDimId)>(eventName, eventId);
eventBus->emplaceListener<GMLIB::Event::PacketEvent::TextPacketSendBeforeEvent>( eventBus->emplaceListener<GMLIB::Event::EntityEvent::ActorChangeDimensionBeforeEvent>(
[Call](GMLIB::Event::PacketEvent::TextPacketSendBeforeEvent& ev) { [Call](GMLIB::Event::EntityEvent::ActorChangeDimensionBeforeEvent& ev) {
auto pkt = ev.getPacket();
bool result = true; bool result = true;
try { try {
result = Call(pkt.mAuthor, pkt.mMessage); result = Call(&ev.self(), ev.getToDimensionId());
} catch (...) {} } catch (...) {}
if (!result) { if (!result) {
ev.cancel(); ev.cancel();
@@ -152,6 +151,29 @@ void Export_Event_API() {
); );
return true; return true;
} }
case doHash("onMobHurted"): {
auto Call = RemoteCall::importAs<bool(Actor * mob, Actor * source, float damage, int cause)>(
eventName,
eventId
);
eventBus->emplaceListener<GMLIB::Event::EntityEvent::MobHurtAfterEvent>(
[Call](GMLIB::Event::EntityEvent::MobHurtAfterEvent& ev) {
auto& damageSource = ev.getSource();
Actor* source = nullptr;
if (damageSource.isEntitySource()) {
auto uniqueId = damageSource.getDamagingEntityUniqueID();
source = ll::service::getLevel()->fetchEntity(uniqueId);
if (source->getOwner()) {
source = source->getOwner();
}
}
try {
Call(&ev.self(), source, ev.getDamage(), (int)damageSource.getCause());
} catch (...) {}
}
);
return true;
}
default: default:
return false; return false;
} }
+1 -1
View File
@@ -4,7 +4,7 @@
#include <RemoteCallAPI.h> #include <RemoteCallAPI.h>
#define PLUGIN_NAME "GMLIB-LRCA" #define PLUGIN_NAME "GMLIB-LRCA"
#define LIB_VERSION SemVersion(0, 9, 4, "", "") #define LIB_VERSION GMLIB::Version(0, 10, 0)
extern ll::Logger logger; extern ll::Logger logger;
@@ -0,0 +1,53 @@
/**
*
* @name GMLIB_LRCA-Plugin-Template
* @brief plugin template for GMLIB-LegacyRemoteCallApi
*
* @copyright Copyright (c) 2024 GroupMountain
* GMLIB-LegacyRemoteCallApi <https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/>
*
*/
const PluginInfo = {
Name: "GMLIB_LRCA-Plugin-Template",
Author: "your name",
Version: "0.0.1"
}
// 导入 API
const { JsonConfig, JsonI18n } = require('./GMLIB-LegacyRemoteCallApi/lib/GMLIB_API-JS');
// 默认配置文件
const defaultConfig = {
"language": "zh_CN"
};
// 默认语言文件
const zh_CN = {
"consolelog.loaded": "加载成功!",
"consolelog.info": "插件作者: {1}, 当前版本: v{2}"
};
// 初始化配置文件
const config = new JsonConfig(`./plugins/${PluginInfo.Name}/config/config.json`, defaultConfig);
// 初始化 I18n
const I18n = new JsonI18n(`./plugins/${PluginInfo.Name}/language/`, config.get("language"));
I18n.loadLanguage("zh_CN", zh_CN); // 升级语言文件
I18n.setDefaultLanguage("zh_CN"); // 设置I18n无法翻译时采用的默认语言
// I18n快捷翻译函数
function tr(key, data = []) {
return I18n.translate(key, data);
}
// 监听服务器启动
// 在无特殊说明的情况下,一切 MCAPI 都应该在服务器启动后访问。
mc.listen("onServerStarted", () => {
// 你的代码
// 打印插件信息
logger.info(tr("consolelog.loaded"));
logger.info(tr("consolelog.info", [PluginInfo.Author, PluginInfo.Version]));
});
@@ -0,0 +1,3 @@
{
"language": "zh_CN"
}
@@ -0,0 +1,4 @@
{
"consolelog.loaded": "加载成功!",
"consolelog.info": "插件作者: {1}, 当前版本: v{2}"
}
@@ -0,0 +1,16 @@
{
"entry": "GMLIB_LRCA-Plugin-Template.js",
"name": "GMLIB_LRCA-Plugin-Template",
"type": "lse-quickjs",
"version": "0.0.1",
"author": "Your Name",
"description": "plugin description",
"dependencies": [
{
"name": "legacy-script-engine-quickjs"
},
{
"name": "GMLIB-LegacyRemoteCallApi"
}
]
}
+3 -3
View File
@@ -1,7 +1,7 @@
{ {
"format_version": 2, "format_version": 2,
"tooth": "github.com/GroupMountain/GMLIB-LegacyRemoteCallApi", "tooth": "github.com/GroupMountain/GMLIB-LegacyRemoteCallApi",
"version": "0.9.4", "version": "0.10.0",
"info": { "info": {
"name": "GMLIB-LegacyRemoteCallApi", "name": "GMLIB-LegacyRemoteCallApi",
"description": "Legacy RemoteCall API for GMLIB", "description": "Legacy RemoteCall API for GMLIB",
@@ -14,9 +14,9 @@
"library" "library"
] ]
}, },
"asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.9.4/GMLIB-LegacyRemoteCallApi-windows-x64.zip", "asset_url": "https://github.com/GroupMountain/GMLIB-LegacyRemoteCallApi/releases/download/v0.10.0/GMLIB-LegacyRemoteCallApi-windows-x64.zip",
"dependencies": { "dependencies": {
"github.com/GroupMountain/GMLIB": ">=0.9.6" "github.com/GroupMountain/GMLIB": ">=0.10.0"
}, },
"files": { "files": {
"place": [ "place": [