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/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..35151d5 --- /dev/null +++ b/src/EventAPI.cpp @@ -0,0 +1,127 @@ +#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) { + Call( + ev.getRealName(), + ev.getUuid().asString(), + ev.getServerAuthXuid(), + ev.getClientAuthXuid() + ); + } + ); + 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}; + Call(&ev.getItem(), (Actor*)&ev.getItemActor(), lsePos, ev.getSpawner()); + } + ); + 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; + } + 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