1+ import 'dart:async' ;
12import 'dart:convert' ;
23import 'dart:io' ;
34
@@ -15,8 +16,12 @@ typedef PluginSendEventCallback =
1516 );
1617
1718mixin ServerInterface {
18- void process (WorldEvent event, {bool force = false , required String plugin});
19- void sendEvent (
19+ Future <void > process (
20+ WorldEvent event, {
21+ bool force = false ,
22+ required String plugin,
23+ });
24+ Future <void > sendEvent (
2025 PlayableWorldEvent event, {
2126 Channel target = kAnyChannel,
2227 required String plugin,
@@ -52,7 +57,7 @@ final class PluginSystem {
5257 (c) => LuauPlugin (code: code, callback: c),
5358 pluginServer,
5459 onPrint: (e) => server.print (e, name),
55- pluginId : () => name,
60+ location : ItemLocation . fromString ( name) ,
5661 ),
5762 );
5863 }
@@ -108,8 +113,11 @@ final class PluginSystem {
108113}
109114
110115abstract class PluginServerInterface {
111- void process (WorldEvent event, {bool force = false });
112- void sendEvent (PlayableWorldEvent event, {Channel target = kAnyChannel});
116+ Future <void > process (WorldEvent event, {bool force = false });
117+ Future <void > sendEvent (
118+ PlayableWorldEvent event, {
119+ Channel target = kAnyChannel,
120+ });
113121 WorldState get state;
114122 List <int > get players;
115123}
@@ -121,14 +129,14 @@ class _PluginServerInterfaceImpl implements PluginServerInterface {
121129 _PluginServerInterfaceImpl (this .server, this .pluginName);
122130
123131 @override
124- void process (WorldEvent event, {bool force = false }) {
125- server.process (event, force: force, plugin: pluginName);
126- }
132+ Future <void > process (WorldEvent event, {bool force = false }) =>
133+ server.process (event, force: force, plugin: pluginName);
127134
128135 @override
129- void sendEvent (PlayableWorldEvent event, {Channel target = kAnyChannel}) {
130- server.sendEvent (event, target: target, plugin: pluginName);
131- }
136+ Future <void > sendEvent (
137+ PlayableWorldEvent event, {
138+ Channel target = kAnyChannel,
139+ }) => server.sendEvent (event, target: target, plugin: pluginName);
132140
133141 @override
134142 WorldState get state => server.state;
@@ -164,25 +172,23 @@ final class RustSetonixPlugin extends SetonixPlugin {
164172 RustPlugin Function (PluginCallback ) builder,
165173 PluginServerInterface server, {
166174 void Function (String )? onPrint,
167- String Function () ? pluginId ,
175+ ItemLocation ? location ,
168176 }) async {
169- final callback = PluginCallback .default_ ();
170- if (onPrint != null ) {
171- callback.changeOnPrint (onPrint: onPrint);
172- }
173- callback.changeProcessEvent (
174- processEvent: (eventSerizalized, force) {
175- final event = WorldEventMapper .fromJson (eventSerizalized);
176- server.process (event, force: force ?? false );
177+ final processEventController = StreamController <(String , bool ?)>();
178+
179+ final callback = PluginCallback (
180+ onPrint: onPrint ?? (s) {},
181+ processEvent: (eventSerizalized, force) async {
182+ processEventController.add ((eventSerizalized, force));
177183 },
178- );
179- callback.changeSendEvent (
180184 sendEvent: (eventSerizalized, target) {
181- final event = PlayableWorldEventMapper .fromJson (eventSerizalized);
182- server.sendEvent (event, target: target ?? kAnyChannel);
185+ try {
186+ final event = PlayableWorldEventMapper .fromJson (eventSerizalized);
187+ server.sendEvent (event, target: target ?? kAnyChannel);
188+ } catch (e) {
189+ print ("Error sending event from plugin: $e " );
190+ }
183191 },
184- );
185- callback.changeStateFieldAccess (
186192 stateFieldAccess: (field) {
187193 final state = server.state;
188194 return switch (field) {
@@ -193,18 +199,30 @@ final class RustSetonixPlugin extends SetonixPlugin {
193199 StateFieldAccess .tableName => jsonEncode (state.tableName),
194200 StateFieldAccess .players => jsonEncode (server.players),
195201 StateFieldAccess .teamMembers => jsonEncode (state.teamMembers),
196- StateFieldAccess .pluginId => jsonEncode (
197- pluginId? .call () ?? 'unknown' ,
202+ StateFieldAccess .game => jsonEncode (location? .namespace ?? 'unknown' ),
203+ StateFieldAccess .namespace => jsonEncode (
204+ location? .namespace ?? 'unknown' ,
198205 ),
199206 };
200207 },
201- );
202- callback.changeTableAccess (
203208 tableAccess: (tableName) {
204209 final table = server.state.data.getTable (tableName ?? '' );
205210 return table? .toJson () ?? "{}" ;
206211 },
207212 );
213+ processEventController.stream.listen ((message) async {
214+ final (eventSerizalized, force) = message;
215+ final smallMsg = eventSerizalized.substring (0 , 20 );
216+ try {
217+ onPrint? .call ("Process event called from plugin: $smallMsg " );
218+ final event = WorldEventMapper .fromJson (eventSerizalized);
219+ await server.process (event, force: force ?? false );
220+ } catch (e) {
221+ onPrint? .call ("Error processing event from plugin: $e " );
222+ } finally {
223+ onPrint? .call ("Finished processing event from plugin $smallMsg " );
224+ }
225+ });
208226 final plugin = builder (callback);
209227 final instance = RustSetonixPlugin ._(server, plugin);
210228 instance.eventSystem.on < WorldEvent > ((e) async {
0 commit comments