@@ -60,9 +60,16 @@ impl<P: IntoScriptPluginParams> RunScriptCallback<P> {
6060 self
6161 }
6262
63+ fn handle_error ( res : & Result < ScriptValue , ScriptError > , guard : WorldGuard ) {
64+ if let Err ( err) = res {
65+ send_script_errors ( guard, [ err] ) ;
66+ }
67+ }
68+
6369 /// Run the command on the given context.
6470 ///
6571 /// Assumes this context matches the attachment for the command.
72+ /// Does not send the error as a message, this needs to be done explicitly by the caller.
6673 pub fn run_with_context (
6774 self ,
6875 guard : WorldGuard ,
@@ -109,6 +116,8 @@ impl<P: IntoScriptPluginParams> RunScriptCallback<P> {
109116 }
110117
111118 /// Equivalent to [`Self::run`], but usable in the case where you already have [`ScriptContext`] and [`ScriptCallbacks`] resources available.
119+ ///
120+ /// Does not send the error as a message, this needs to be done explicitly by the caller.
112121 pub fn run_with_contexts (
113122 self ,
114123 guard : WorldGuard ,
@@ -125,7 +134,6 @@ impl<P: IntoScriptPluginParams> RunScriptCallback<P> {
125134 )
126135 . with_script ( self . attachment . script ( ) . display ( ) )
127136 . with_language ( P :: LANGUAGE ) ;
128- send_script_errors ( guard, [ & err] ) ;
129137 return Err ( err) ;
130138 }
131139 } ;
@@ -135,19 +143,22 @@ impl<P: IntoScriptPluginParams> RunScriptCallback<P> {
135143
136144 /// Equivalent to running the command, but also returns the result of the callback.
137145 ///
138- /// The returned errors will NOT be sent as events or printed
139- pub fn run ( self , world : & mut World ) -> Result < ScriptValue , ScriptError > {
146+ /// The returned errors will NOT be sent as events or printed unless send errors is set to true
147+ pub fn run ( self , world : & mut World , send_errors : bool ) -> Result < ScriptValue , ScriptError > {
140148 let script_contexts = world. get_resource_or_init :: < ScriptContext < P > > ( ) . clone ( ) ;
141149 let script_callbacks = world. get_resource_or_init :: < ScriptCallbacks < P > > ( ) . clone ( ) ;
142150 let guard = WorldGuard :: new_exclusive ( world) ;
143- self . run_with_contexts ( guard, script_contexts, script_callbacks)
151+ let res = self . run_with_contexts ( guard. clone ( ) , script_contexts, script_callbacks) ;
152+ if send_errors && res. is_err ( ) {
153+ Self :: handle_error ( & res, guard) ;
154+ }
155+ res
144156 }
145157}
146158
147159impl < P : IntoScriptPluginParams > Command for RunScriptCallback < P > {
148160 fn apply ( self , world : & mut World ) {
149- // Internals handle this.
150- let _ = self . run ( world) ;
161+ let _ = self . run ( world, true ) ;
151162 }
152163}
153164
0 commit comments