11package com .laytonsmith .core .functions ;
22
3+ import com .google .gson .Gson ;
4+ import com .google .gson .GsonBuilder ;
5+ import com .google .gson .JsonIOException ;
36import com .laytonsmith .PureUtilities .Common .StringUtils ;
47import com .laytonsmith .PureUtilities .Vector3D ;
58import com .laytonsmith .PureUtilities .Version ;
6366import com .laytonsmith .core .exceptions .CRE .CREBadEntityException ;
6467import com .laytonsmith .core .exceptions .CRE .CRECastException ;
6568import com .laytonsmith .core .exceptions .CRE .CREFormatException ;
69+ import com .laytonsmith .core .exceptions .CRE .CREIOException ;
6670import com .laytonsmith .core .exceptions .CRE .CREIllegalArgumentException ;
6771import com .laytonsmith .core .exceptions .CRE .CREInsufficientArgumentsException ;
6872import com .laytonsmith .core .exceptions .CRE .CREInvalidWorldException ;
@@ -6187,7 +6191,8 @@ public static class ptellraw extends AbstractFunction {
61876191
61886192 @ Override
61896193 public Class <? extends CREThrowable >[] thrown () {
6190- return new Class []{CRECastException .class };
6194+ return new Class []{CRECastException .class , CREFormatException .class , CREIOException .class ,
6195+ CREPlayerOfflineException .class };
61916196 }
61926197
61936198 @ Override
@@ -6202,15 +6207,31 @@ public Boolean runAsync() {
62026207
62036208 @ Override
62046209 public Mixed exec (Target t , Environment environment , Mixed ... args ) throws ConfigRuntimeException {
6210+ MCPlayer p = environment .getEnv (CommandHelperEnvironment .class ).GetPlayer ();
6211+ if (p == null ) {
6212+ throw new CREPlayerOfflineException ("ptellraw() requires player context. Consider tellraw()." , t );
6213+ }
62056214 String selector = "@s" ;
62066215 String json ;
6207- if (args .length == 1 ) {
6208- json = new DataTransformations .json_encode ().exec (t , environment , args [0 ]).val ();
6209- } else {
6210- selector = ArgumentValidation .getString (args [0 ], t );
6211- json = new DataTransformations .json_encode ().exec (t , environment , args [1 ]).val ();
6216+ try {
6217+ Gson gson = new GsonBuilder ().disableHtmlEscaping ().create ();
6218+ if (args .length == 1 ) {
6219+ json = gson .toJson (Construct .GetPOJO (args [0 ]));
6220+ } else {
6221+ selector = ArgumentValidation .getString (args [0 ], t );
6222+ json = gson .toJson (Construct .GetPOJO (args [1 ]));
6223+ }
6224+ } catch (ClassCastException ex ) {
6225+ throw new CRECastException (ex .getMessage (), t );
6226+ } catch (JsonIOException ex ) {
6227+ throw new CREIOException (ex .getMessage (), t );
6228+ }
6229+ try {
6230+ Static .getServer ().runasConsole ("minecraft:execute as " + p .getName () + " at @s"
6231+ + " run minecraft:tellraw " + selector + " " + json );
6232+ } catch (Exception ex ) {
6233+ throw new CREFormatException (ex .getMessage (), t , ex .getCause ());
62126234 }
6213- new Meta .sudo ().exec (t , environment , new CString ("/minecraft:tellraw " + selector + " " + json , t ));
62146235 return CVoid .VOID ;
62156236 }
62166237
@@ -6230,11 +6251,12 @@ public String docs() {
62306251 + " this simply passes the input to the command. The raw is passed in as a normal"
62316252 + " (possibly associative) array, and json encoded. No validation is done on the input,"
62326253 + " so the command may fail. If not provided, the selector defaults to @s. Do not use double quotes"
6233- + " (smart string) when providing the selector. See {{function|tellraw}} if you don't need player"
6234- + " context. ---- The specification of the array may change from version to version of Minecraft,"
6235- + " but is documented here https://minecraft.gamepedia.com/Commands#Raw_JSON_text."
6236- + " This function is simply written in terms of json_encode and sudo, and is otherwise equivalent"
6237- + " to sudo('/minecraft:tellraw ' . @selector . ' ' . json_encode(@raw))" ;
6254+ + " (smart string) when providing the selector. See {{function|tellraw}} if you don't need the @s"
6255+ + " selector with player context. ---- The specification of the array may change from version to"
6256+ + " version of Minecraft, but is documented here: https://minecraft.wiki/w/Text_component_format."
6257+ + " This function is roughly equivalent to"
6258+ + " runas('~console', '/execute as '.player().' at @s tellraw '.@selector.' '.json_encode(@raw))"
6259+ + " but uses the Gson serializer instead." ;
62386260 }
62396261
62406262 @ Override
@@ -6251,8 +6273,7 @@ public ExampleScript[] examples() throws ConfigCompileException {
62516273 new ExampleScript ("Advanced usage with embedded selectors." ,
62526274 "ptellraw('@a', array(\n "
62536275 + "\t array('selector': '@s'), // prints current player\n "
6254- + "\t array('text': ': Hello '),\n "
6255- + "\t array('selector': '@p') // prints receiving player\n "
6276+ + "\t array('text': ': Hello World!')\n "
62566277 + "));" ,
62576278 "<<Would output a message from the current player to all players.>>" ),
62586279 new ExampleScript ("Complex object" ,
0 commit comments