@@ -35,55 +35,57 @@ private LuaValue next() {
3535 return idx (this .argIdx ++);
3636 }
3737
38+ private <T > T optionalVal (LuaValue val , T def , Function <LuaValue , T > func ) {
39+ if (val .isnil ()) return def ;
40+ else return func .apply (val );
41+ }
3842
3943
40- public LuaTable getTable (LuaValue val , LuaTable def ) {
41- return val .opttable ( def );
44+ public LuaTable getTable (LuaValue val ) {
45+ return val .checktable ( );
4246 }
4347
44- public int getInt (LuaValue val , int def ) {
45- return val .optint ( def );
48+ public int getInt (LuaValue val ) {
49+ return val .checkint ( );
4650 }
4751
48- public float getFloat (LuaValue val , float def ) {
49- return (float ) val .optdouble ( def );
52+ public float getFloat (LuaValue val ) {
53+ return (float ) val .checkdouble ( );
5054 }
5155
52- public boolean getBoolean (LuaValue val , boolean def ) {
53- return val .optboolean ( def );
56+ public boolean getBoolean (LuaValue val ) {
57+ return val .checkboolean ( );
5458 }
5559
56- public String getString (LuaValue val , String def ) {
57- return MetamethodImpl .tostring (val . optstring ( LuaString . valueOf ( def )) );
60+ public String getString (LuaValue val ) {
61+ return MetamethodImpl .tostring (val );
5862 }
5963
60- public LuaValue getLuaValue (LuaValue val , LuaValue def ) {
61- return val . optvalue ( def ) ;
64+ public LuaValue getLuaValue (LuaValue val ) {
65+ return val ;
6266 }
6367
64- public LuaFunction getFunction (LuaValue val , LuaFunction def ) {
65- return val .optfunction ( def );
68+ public LuaFunction getFunction (LuaValue val ) {
69+ return val .checkfunction ( );
6670 }
6771
68- public <T > T getScriptObject (AbstractScriptObject <T > obj , LuaValue val , CommandSourceStack src , LuaScript script , T def ) {
69- LuaTable table = val .checktable ();
70- if (table .isnil ()) {
71- return def ;
72- } else {
73- return obj .toThing (table , src , script );
74- }
72+ public <T > T getScriptObject (AbstractScriptObject <T > obj , LuaValue val , CommandSourceStack src , LuaScript script ) {
73+ return obj .toThing (val .checktable (), src , script );
7574 }
7675
77- public <T > ArrayList <T > getArray (int idx , BiFunction <LuaValue , T , T > getter ) {
78- LuaTable table = getTable (idx );
76+ public <T > ArrayList <T > getArray (LuaValue val , Function <LuaValue , T > getter ) {
77+ LuaTable table = getTable (val );
7978 ArrayList <T > ts = new ArrayList <>();
8079
8180 for (LuaValue key : table .keys ()) {
8281 var luaValue = table .get (key );
82+ ts .add (getter .apply (luaValue ));
8383 }
8484
85+ return ts ;
8586 }
8687
88+ // nexts, no def
8789
8890 public LuaTable nextTable () {
8991 return getTable (next ());
@@ -117,38 +119,46 @@ public <T> T nextScriptObject(AbstractScriptObject<T> obj, CommandSourceStack sr
117119 return getScriptObject (obj , next (), src , script );
118120 }
119121
120- // defaults
122+ public <T > ArrayList <T > nextArray (Function <LuaValue , T > getter ) {
123+ return getArray (next (), getter );
124+ }
125+
126+ // nexts with defs
121127
122128 public LuaTable nextTable (LuaTable def ) {
123- return getTable (next (), def );
129+ return optionalVal (next (), def , this :: getTable );
124130 }
125131
126132 public int nextInt (int def ) {
127- return getInt (next (), def );
133+ return optionalVal (next (), def , this :: getInt );
128134 }
129135
130136 public float nextFloat (float def ) {
131- return getFloat (next (), def );
137+ return optionalVal (next (), def , this :: getFloat );
132138 }
133139
134140 public boolean nextBoolean (boolean def ) {
135- return getBoolean (next (), def );
141+ return optionalVal (next (), def , this :: getBoolean );
136142 }
137143
138144 public String nextString (String def ) {
139- return getString (next (), def );
145+ return optionalVal (next (), def , this :: getString );
140146 }
141147
142148 public LuaValue nextLuaValue (LuaValue def ) {
143- return getLuaValue (next (), def );
149+ return optionalVal (next (), def , this :: getLuaValue );
144150 }
145151
146152 public LuaFunction nextFunction (LuaFunction def ) {
147- return getFunction (next (), def );
153+ return optionalVal (next (), def , this :: getFunction );
148154 }
149155
150156 public <T > T nextScriptObject (AbstractScriptObject <T > obj , CommandSourceStack src , LuaScript script , T def ) {
151- return getScriptObject (obj , next (), src , script , def );
157+ return optionalVal (next (), def , val -> getScriptObject (obj , val , src , script ));
158+ }
159+
160+ public <T > ArrayList <T > nextArray (Function <LuaValue , T > getter , ArrayList <T > def ) {
161+ return optionalVal (next (), def , val -> getArray (val , getter ));
152162 }
153163
154164 }
0 commit comments