diff --git a/lib/BEPlaceholderAPI-JS.js b/lib/BEPlaceholderAPI-JS.js index 45284e6..50ba212 100644 --- a/lib/BEPlaceholderAPI-JS.js +++ b/lib/BEPlaceholderAPI-JS.js @@ -21,17 +21,17 @@ class PAPI { static registerPlayerPlaceholder(func, PluginName, PAPIName) { ll.export(func, PluginName, func.getName()); - PlaceholderAPI.registerPlayerPlaceholderAPI(PluginName, func.getName(), PAPIName); + return PlaceholderAPI.registerPlayerPlaceholderAPI(PluginName, func.getName(), PAPIName); } static registerServerPlaceholder(func, PluginName, PAPIName) { ll.export(func, PluginName, func.getName()); - PlaceholderAPI.registerServerPlaceholderAPI(PluginName, func.getName(), PAPIName); + return PlaceholderAPI.registerServerPlaceholderAPI(PluginName, func.getName(), PAPIName); } static registerStaticPlaceholder(func, PluginName, PAPIName, UpdateInterval = 50) { ll.export(func, PluginName, func.getName()); - PlaceholderAPI.registerStaticPlaceholderAPI(PluginName, func.getName(), PAPIName, UpdateInterval); + return PlaceholderAPI.registerStaticPlaceholderAPI(PluginName, func.getName(), PAPIName, UpdateInterval); } static getValue(key) { diff --git a/lib/EventAPI-JS.js b/lib/EventAPI-JS.js new file mode 100644 index 0000000..0fc9189 --- /dev/null +++ b/lib/EventAPI-JS.js @@ -0,0 +1,29 @@ +const CallEvent = ll.import("GMLIB_API", "callCustomEvent"); + +let NextEventId = 0; + +function getNextEventId() { + NextEventId++; + return "GMLIB_Event_" + NextEventId; +} + +class Event { + constructor() { + throw new Error("Static class cannot be instantiated"); + } + + static listen(event, callback) { + let eventId = getNextEventId(); + ll.export(callback, event, eventId); + let result = CallEvent(event, eventId); + if (!result) { + logger.error(`Cannot listen event "${event}"!`); + logger.error(`Event "${event}" No Found!`); + } + return result; + } +} + +module.exports = { + Event +}; \ No newline at end of file diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 77654b7..ab5d461 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -15,6 +15,8 @@ const GMLIB_API = { getPlayerNbt: ll.import("GMLIB_API", "getPlayerNbt"), setPlayerNbt: ll.import("GMLIB_API", "setPlayerNbt"), setPlayerNbtTags: ll.import("GMLIB_API", "setPlayerNbtTags"), + resourcePackTranslate: ll.import("GMLIB_API", "resourcePackTranslate"), + chooseResourcePackI18nLanguage: ll.import("GMLIB_API", "chooseResourcePackI18nLanguage"), setEducationFeatureEnabled: ll.import("GMLib_ServerAPI", "setEducationFeatureEnabled"), registerAbilityCommand: ll.import("GMLib_ServerAPI", "registerAbilityCommand"), setEnableAchievement: ll.import("GMLib_ServerAPI", "setEnableAchievement"), @@ -300,6 +302,14 @@ class Minecraft { static throwEntity(entity, proj, speed = 2, offset = 3) { return GMLIB_API.throwEntity(entity, proj, speed = 2, offset = 3); } + + static chooseResourcePackI18nLanguage(language) { + return GMLIB_API.chooseResourcePackI18nLanguage(language); + } + + static resourcePackTranslate(key, params = []) { + return GMLIB_API.resourcePackTranslate(key, params); + } } class Recipes { diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 9a544ee..729172f 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -133,4 +133,12 @@ void Export_Compatibility_API() { RemoteCall::exportAs("GMLIB_API", "getVersion_GMLIB", []() -> std::string { return GMLIB::Version::getLibVersionString(); }); + RemoteCall::exportAs( + "GMLIB_API", + "resourcePackTranslate", + [](std::string key, std::vector params) -> std::string { return I18n::get(key, params); } + ); + RemoteCall::exportAs("GMLIB_API", "chooseResourcePackI18nLanguage", [](std::string code) -> void { + I18n::chooseLanguage(code); + }); } \ No newline at end of file diff --git a/src/Entry.cpp b/src/Entry.cpp index 82da79a..c786469 100644 --- a/src/Entry.cpp +++ b/src/Entry.cpp @@ -20,6 +20,7 @@ auto load(ll::plugin::NativePlugin& self) -> bool { Export_Legacy_GMLib_ServerAPI(); Export_Compatibility_API(); ExportPAPI(); + Export_Event_API(); logger.info("GMLIB-LegacyRemoteCallApi Loaded!"); logger.info("Version: {}", LIB_VERSION.asString()); logger.info("Author: GroupMountain"); diff --git a/src/EventAPI.cpp b/src/EventAPI.cpp new file mode 100644 index 0000000..a4c1e25 --- /dev/null +++ b/src/EventAPI.cpp @@ -0,0 +1,162 @@ +#include "Global.h" +using namespace ll::hash_utils; + +void Export_Event_API() { + auto eventBus = &ll::event::EventBus::getInstance(); + RemoteCall::exportAs( + "GMLIB_API", + "callCustomEvent", + [eventBus](std::string eventName, std::string eventId) -> bool { + if (RemoteCall::hasFunc(eventName, eventId)) { + switch (do_hash(eventName)) { + case do_hash("onClientLogin"): { + auto Call = RemoteCall::importAs< + bool(std::string realName, std::string uuid, std::string serverXuid, std::string clientXuid)>( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](GMLIB::Event::PacketEvent::ClientLoginAfterEvent& ev) { + try { + Call( + ev.getRealName(), + ev.getUuid().asString(), + ev.getServerAuthXuid(), + ev.getClientAuthXuid() + ); + } catch (...) {} + } + ); + return true; + } + case do_hash("onWeatherChange"): { + auto Call = + RemoteCall::importAs( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](GMLIB::Event::LevelEvent::WeatherUpdateBeforeEvent& ev) { + bool result = true; + try { + result = Call( + ev.getLightningLevel(), + ev.getRainLevel(), + ev.getLightningLastTick(), + ev.getRainingLastTick() + ); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case do_hash("onMobPick"): { + auto Call = RemoteCall::importAs(eventName, eventId); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::MobPickupItemBeforeEvent& ev) { + bool result = true; + try { + result = Call(&ev.self(), (Actor*)&ev.getItemActor()); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case do_hash("onItemTrySpawn"): { + auto Call = RemoteCall::importAs< + bool(const ItemStack* item, std::pair position, Actor* spawner)>(eventName, eventId); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::ItemActorSpawnBeforeEvent& ev) { + auto pos = ev.getPosition(); + auto dimid = ev.getBlockSource().getDimensionId().id; + std::pair lsePos = {pos, dimid}; + bool result = true; + try { + result = Call(&ev.getItem(), lsePos, ev.getSpawner()); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case do_hash("onItemSpawned"): { + auto Call = RemoteCall::importAs< + bool(const ItemStack* item, Actor* itemActor, std::pair position, Actor* spawner)>( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::ItemActorSpawnAfterEvent& ev) { + auto pos = ev.getPosition(); + auto dimid = ev.getBlockSource().getDimensionId().id; + std::pair lsePos = {pos, dimid}; + try { + Call(&ev.getItem(), (Actor*)&ev.getItemActor(), lsePos, ev.getSpawner()); + } catch (...) {} + } + ); + return true; + } + case do_hash("onTextSend"): { + auto Call = RemoteCall::importAs(eventName, eventId); + eventBus->emplaceListener( + [Call](GMLIB::Event::PacketEvent::TextPacketSendBeforeEvent& ev) { + auto pkt = ev.getPacket(); + bool result = true; + try { + result = Call(pkt.mAuthor, pkt.mMessage); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case do_hash("onLeaveBed"): { + auto Call = RemoteCall::importAs(eventName, eventId); + eventBus->emplaceListener( + [Call](GMLIB::Event::PlayerEvent::PlayerStopSleepBeforeEvent& ev) { + bool result = true; + try { + result = Call(&ev.self()); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case do_hash("onDeathMessage"): { + auto Call = RemoteCall::importAs, Actor * dead)>( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](GMLIB::Event::EntityEvent::DeathMessageAfterEvent& ev) { + auto msg = ev.getDeathMessage(); + auto source = ev.getDamageSource(); + try { + Call(msg.first, msg.second, &ev.self()); + } catch (...) {} + } + ); + return true; + } + default: + return false; + } + } + return false; + } + ); +} \ No newline at end of file diff --git a/src/Global.h b/src/Global.h index c1fa162..2938e1c 100644 --- a/src/Global.h +++ b/src/Global.h @@ -11,4 +11,5 @@ extern ll::Logger logger; extern void Export_Legacy_GMLib_ModAPI(); extern void Export_Legacy_GMLib_ServerAPI(); extern void Export_Compatibility_API(); -extern void ExportPAPI(); \ No newline at end of file +extern void ExportPAPI(); +extern void Export_Event_API(); \ No newline at end of file