11package dev .diamond .luafy .script .object .game ;
22
3+ import com .mojang .authlib .GameProfile ;
34import com .mojang .serialization .JsonOps ;
45import dev .diamond .luafy .autodoc .Argtypes ;
6+ import dev .diamond .luafy .autodoc .FunctionListBuilder ;
57import dev .diamond .luafy .lua .LuaTableBuilder ;
8+ import dev .diamond .luafy .lua .ScriptFunction ;
69import dev .diamond .luafy .registry .ScriptObjects ;
710import dev .diamond .luafy .script .LuaScript ;
811import dev .diamond .luafy .script .object .AbstractScriptObject ;
1114import net .minecraft .network .chat .ComponentSerialization ;
1215import net .minecraft .network .chat .MutableComponent ;
1316import net .minecraft .network .chat .contents .TranslatableContents ;
17+ import net .minecraft .network .chat .contents .objects .AtlasSprite ;
18+ import net .minecraft .network .chat .contents .objects .PlayerSprite ;
19+ import net .minecraft .resources .Identifier ;
20+ import net .minecraft .server .level .ServerPlayer ;
21+ import net .minecraft .world .item .component .ResolvableProfile ;
1422import org .luaj .vm2 .LuaTable ;
1523import org .luaj .vm2 .LuaValue ;
1624
1725import java .util .ArrayList ;
1826
19- public class TextComponentScriptObject extends AbstractScriptObject <MutableComponent > {
27+ public class TextComponentScriptObject extends AbstractScriptObject <Component > {
2028
2129 public static final String PROP_POINTER = "_ptr" ;
2230
2331 public static final String FUNC_SERIALISE = "serialise" ;
32+ public static final String FUNC_COLOR = "color" ;
33+ public static final String FUNC_FONT = "font" ;
34+ public static final String FUNC_EMBOLDEN = "bold" ;
35+ public static final String FUNC_ITALICISE = "italic" ;
36+ public static final String FUNC_UNDERLINE = "underline" ;
37+ public static final String FUNC_STRIKETHROUGH = "strikethrough" ;
38+ public static final String FUNC_OBFUSCATE = "obfuscated" ;
2439
25- public static final String FUNC_APPEND_LITERAL = "append_literal" ;
26- public static final String FUNC_APPEND_TRANSLATABLE = "append_translatable" ;
27- public static final String FUNC_APPEND_SPRITE = "append_sprite" ;
28- public static final String FUNC_APPEND_PLAYER_SPRITE = "append_sprite_player" ;
2940
3041 public TextComponentScriptObject () {
3142 super ("Text component." , doc -> {
3243 doc .addFunction (FUNC_SERIALISE , "Serialises this text component to a JSON string." , args -> {}, Argtypes .STRING );
44+ doc .addFunction (FUNC_COLOR , "Sets the text's color." , args -> {}, Argtypes .NIL );
45+ doc .addFunction (FUNC_FONT , "Sets the text's font." , args -> args .add ("font" , Argtypes .STRING , "Identifier of a Font to use." ), Argtypes .NIL );
46+ doc .addFunction (FUNC_EMBOLDEN , "Emboldens the text." , args -> args .add ("flag" , Argtypes .BOOLEAN , "If true, emboldens the text." ), Argtypes .NIL );
47+ doc .addFunction (FUNC_ITALICISE , "Italicises the text." , args -> args .add ("flag" , Argtypes .BOOLEAN , "If true, italicises the text." ), Argtypes .NIL );
48+ doc .addFunction (FUNC_UNDERLINE , "Underlines the text." , args -> args .add ("flag" , Argtypes .BOOLEAN , "If true, underlines the text." ), Argtypes .NIL );
49+ doc .addFunction (FUNC_STRIKETHROUGH , "Strikes through the text." , args -> args .add ("flag" , Argtypes .BOOLEAN , "If true, strikes through the text." ), Argtypes .NIL );
50+ doc .addFunction (FUNC_OBFUSCATE , "Obfuscates the text." , args -> args .add ("flag" , Argtypes .BOOLEAN , "If true, obfuscates the text." ), Argtypes .NIL );
51+
3352
34- doc .addFunction (FUNC_APPEND_LITERAL , "Appends as literal text." , args -> {
35- args .add ("string" , Argtypes .STRING , "Literal text." );
36- }, Argtypes .NIL );
37- doc .addFunction (FUNC_APPEND_TRANSLATABLE , "Appends as translatable text." , args -> {
38- args .add ("string" , Argtypes .STRING , "Translatable text." );
39- args .add ("args" , Argtypes .maybe (Argtypes .array (ScriptObjects .TEXT_COMPONENT )), "Components to pass as objects." );
40- }, Argtypes .NIL );
41- doc .addFunction (FUNC_APPEND_SPRITE , "Appends a sprite." , args -> {
42- args .add ("atlas" , Argtypes .STRING , "Sprite atlas." );
43- args .add ("id" , Argtypes .STRING , "Sprite identifer." );
44- }, Argtypes .NIL );
45- doc .addFunction (FUNC_APPEND_PLAYER_SPRITE , "Appends as a playerhead sprite." , args -> {
46- args .add ("username" , Argtypes .STRING , "Player username." );
47- }, Argtypes .NIL );
4853
4954 });
5055 }
5156
57+ public static void addStaticTextComponentBuilderMethods (FunctionListBuilder builder , LuaScript script ) {
58+ builder .add ("literal_text" , args -> {
59+ return LuaTableBuilder .provide (ScriptObjects .TEXT_COMPONENT , Component .literal (args .nextString ()), script );
60+ }, "Creates a literal text component." , args -> {
61+ args .add ("literal" , Argtypes .STRING , "Literal text." );
62+ }, ScriptObjects .TEXT_COMPONENT );
63+
64+ builder .add ("translatable_text" , args -> {
65+ String key = args .nextString ();
66+ ArrayList <Component > components = args .nextArray (v ->
67+ args .getScriptObject (ScriptObjects .TEXT_COMPONENT , v , script .getSource (), script ), new ArrayList <>());
68+
69+ return LuaTableBuilder .provide (
70+ ScriptObjects .TEXT_COMPONENT ,
71+ MutableComponent .create (
72+ new TranslatableContents (
73+ key , null , components .toArray ()
74+ )
75+ ), script
76+ );
77+ }, "Creates a translatable text component." , args -> {
78+ args .add ("translatable" , Argtypes .STRING , "Translation key" );
79+ args .add ("components" , Argtypes .maybe (Argtypes .array (ScriptObjects .TEXT_COMPONENT )), "Optional list of components to use as placeholders." );
80+ }, ScriptObjects .TEXT_COMPONENT );
81+
82+ builder .add ("player_sprite_text" , args -> {
83+ ServerPlayer player = args .nextScriptObject (ScriptObjects .PLAYER , script .getSource (), script );
84+ return LuaTableBuilder .provide (ScriptObjects .TEXT_COMPONENT , Component .object (
85+ new PlayerSprite (ResolvableProfile .createResolved (player .getGameProfile ()), true )
86+ ), script );
87+ }, "Creates a player sprite text component." , args -> {
88+ args .add ("player" , ScriptObjects .PLAYER , "Player to use." );
89+ }, ScriptObjects .TEXT_COMPONENT );
90+
91+ builder .add ("atlas_sprite_text" , args -> {
92+ Identifier atlas = Identifier .parse (args .nextString ());
93+ Identifier sprite = Identifier .parse (args .nextString ());
94+ return LuaTableBuilder .provide (ScriptObjects .TEXT_COMPONENT , Component .object (
95+ new AtlasSprite (atlas , sprite )
96+ ), script );
97+ }, "Creates a atlas-source sprite text component." , args -> {
98+ args .add ("atlas" , Argtypes .STRING , "Atlas to use." );
99+ args .add ("sprite" , Argtypes .STRING , "Sprite to use." );
100+ }, ScriptObjects .TEXT_COMPONENT );
101+
102+ builder .add ("compound_text" , args -> {
103+ MutableComponent compound = Component .empty ();
104+ ArrayList <Component > components = args .nextArray (v -> args .getScriptObject (ScriptObjects .TEXT_COMPONENT , v , script .getSource (), script ));
105+
106+ for (var comp : components ) {
107+ compound .append (comp );
108+ }
109+
110+ return LuaTableBuilder .provide (ScriptObjects .TEXT_COMPONENT , compound , script );
111+ }, "Creates a text component by concatenating several elements together." , args -> {
112+ args .add ("elements" , Argtypes .array (ScriptObjects .TEXT_COMPONENT ), "Elements to concatenate together." );
113+ }, ScriptObjects .TEXT_COMPONENT );
114+ }
115+
52116 @ Override
53- public void toTable (MutableComponent obj , LuaTableBuilder builder , LuaScript script ) {
117+ public void toTable (Component obj , LuaTableBuilder builder , LuaScript script ) {
54118
55119 // i probably could serialise this, but i think this would be faster
56120 builder .add (PROP_POINTER , script .addUnserializableData (obj ));
@@ -62,27 +126,13 @@ public void toTable(MutableComponent obj, LuaTableBuilder builder, LuaScript scr
62126 });
63127
64128
65- // append functions
66- builder .add (FUNC_APPEND_LITERAL , args -> {
67- obj .append (Component .literal (args .nextString ()));
68- return LuaValue .NIL ;
69- });
70-
71- builder .add (FUNC_APPEND_TRANSLATABLE , args -> {
72- String key = args .nextString ();
73- ArrayList <MutableComponent > components = args .nextArray (v ->
74- args .getScriptObject (ScriptObjects .TEXT_COMPONENT , v , script .getSource (), script ), new ArrayList <>());
75-
76- obj .append (MutableComponent .create (new TranslatableContents (key , null , components .toArray ())));
77- return LuaValue .NIL ;
78- });
79129
80130 }
81131
82132 @ Override
83- public MutableComponent toThing (LuaTable table , CommandSourceStack src , LuaScript script ) {
133+ public Component toThing (LuaTable table , CommandSourceStack src , LuaScript script ) {
84134 int ptr = table .get (PROP_POINTER ).toint ();
85- return script .getUnserializableData (ptr , MutableComponent .class );
135+ return script .getUnserializableData (ptr , Component .class );
86136 }
87137
88138 @ Override
0 commit comments