99import com .laytonsmith .core .constructs .CInt ;
1010import com .laytonsmith .core .constructs .CString ;
1111import com .laytonsmith .core .constructs .Target ;
12+ import com .laytonsmith .core .constructs .generics .GenericParameters ;
1213import com .laytonsmith .core .environments .Environment ;
1314import com .laytonsmith .core .exceptions .CRE .CRECastException ;
1415import com .laytonsmith .core .exceptions .CRE .CREFormatException ;
1516import com .laytonsmith .core .natives .interfaces .Mixed ;
1617
18+ import java .util .function .BiFunction ;
1719import java .util .function .Function ;
1820
1921/**
2022 * Minecraft NBT types, with functions to convert to and from MethodScript constructs.
2123 */
2224public enum MCTagType {
2325 BYTE (
24- (Mixed v ) -> ArgumentValidation .getInt8 (v , v .getTarget ()),
26+ (Mixed v , Environment env ) -> ArgumentValidation .getInt8 (v , v .getTarget (), env ),
2527 (Byte v ) -> new CInt (((Number ) v ).longValue (), Target .UNKNOWN )),
2628 BYTE_ARRAY (
27- (Mixed v ) -> {
28- CArray array = ArgumentValidation .getArray (v , v .getTarget ());
29+ (Mixed v , Environment env ) -> {
30+ CArray array = ArgumentValidation .getArray (v , v .getTarget (), env );
2931 if (array .isAssociative ()) {
3032 throw new CRECastException ("Expected byte array to not be associative." , v .getTarget ());
3133 }
32- byte [] bytes = new byte [(int ) array .size ()];
34+ byte [] bytes = new byte [(int ) array .size (env )];
3335 int i = 0 ;
3436 for (Mixed m : array ) {
35- bytes [i ++] = ArgumentValidation .getInt8 (m , m .getTarget ());
37+ bytes [i ++] = ArgumentValidation .getInt8 (m , m .getTarget (), env );
3638 }
3739 return bytes ;
3840 },
3941 (byte [] array ) -> {
40- CArray r = new CArray (Target .UNKNOWN );
42+ CArray r = new CArray (Target .UNKNOWN , null , null );
4143 for (int i : array ) {
42- r .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN );
44+ r .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN , null );
4345 }
4446 return r ;
4547 }),
4648 DOUBLE (
47- (Mixed v ) -> ArgumentValidation .getDouble (v , v .getTarget ()),
49+ (Mixed v , Environment env ) -> ArgumentValidation .getDouble (v , v .getTarget (), env ),
4850 (Double v ) -> new CDouble (v , Target .UNKNOWN )),
4951 FLOAT (
50- (Mixed v ) -> ArgumentValidation .getDouble32 (v , v .getTarget ()),
52+ (Mixed v , Environment env ) -> ArgumentValidation .getDouble32 (v , v .getTarget (), env ),
5153 (Float v ) -> new CDouble (v .doubleValue (), Target .UNKNOWN )),
5254 INTEGER (
53- (Mixed v ) -> ArgumentValidation .getInt32 (v , v .getTarget ()),
55+ (Mixed v , Environment env ) -> ArgumentValidation .getInt32 (v , v .getTarget (), env ),
5456 (Integer v ) -> new CInt (((Number ) v ).longValue (), Target .UNKNOWN )),
5557 INTEGER_ARRAY (
56- (Mixed v ) -> {
57- CArray array = ArgumentValidation .getArray (v , v .getTarget ());
58+ (Mixed v , Environment env ) -> {
59+ CArray array = ArgumentValidation .getArray (v , v .getTarget (), env );
5860 if (array .isAssociative ()) {
5961 throw new CRECastException ("Expected integer array to not be associative." , v .getTarget ());
6062 }
61- int [] ints = new int [(int ) array .size ()];
63+ int [] ints = new int [(int ) array .size (env )];
6264 int i = 0 ;
6365 for (Mixed m : array ) {
64- ints [i ++] = ArgumentValidation .getInt32 (m , m .getTarget ());
66+ ints [i ++] = ArgumentValidation .getInt32 (m , m .getTarget (), env );
6567 }
6668 return ints ;
6769 },
6870 (int [] array ) -> {
6971 CArray r = new CArray (Target .UNKNOWN );
7072 for (int i : array ) {
71- r .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN );
73+ r .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN , null );
7274 }
7375 return r ;
7476 }),
7577 LONG (
76- (Mixed v ) -> ArgumentValidation .getInt (v , v .getTarget ()),
78+ (Mixed v , Environment env ) -> ArgumentValidation .getInt (v , v .getTarget (), env ),
7779 (Long v ) -> new CInt (((Number ) v ).longValue (), Target .UNKNOWN )),
7880 LONG_ARRAY (
79- (Mixed v ) -> {
80- CArray array = ArgumentValidation .getArray (v , v .getTarget ());
81+ (Mixed v , Environment env ) -> {
82+ CArray array = ArgumentValidation .getArray (v , v .getTarget (), env );
8183 if (array .isAssociative ()) {
8284 throw new CRECastException ("Expected long array to not be associative." , v .getTarget ());
8385 }
84- long [] longs = new long [(int ) array .size ()];
86+ long [] longs = new long [(int ) array .size (env )];
8587 int i = 0 ;
8688 for (Mixed m : array ) {
87- longs [i ++] = ArgumentValidation .getInt (m , m .getTarget ());
89+ longs [i ++] = ArgumentValidation .getInt (m , m .getTarget (), env );
8890 }
8991 return longs ;
9092 },
9193 (long [] array ) -> {
9294 CArray ret = new CArray (Target .UNKNOWN );
9395 for (long i : array ) {
94- ret .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN );
96+ ret .push (new CInt (i , Target .UNKNOWN ), Target .UNKNOWN , null );
9597 }
9698 return ret ;
9799 }),
98100 SHORT (
99- (Mixed v ) -> ArgumentValidation .getInt16 (v , v .getTarget ()),
101+ (Mixed v , Environment env ) -> ArgumentValidation .getInt16 (v , v .getTarget (), env ),
100102 (Short v ) -> new CInt (((Number ) v ).longValue (), Target .UNKNOWN )),
101103 STRING (
102- (Mixed v ) -> v .val (),
104+ (Mixed v , Environment env ) -> v .val (),
103105 (String v ) -> new CString (v , Target .UNKNOWN )),
104106 TAG_CONTAINER (
105- (Mixed v ) -> {
107+ (Mixed v , Environment env ) -> {
106108 throw new UnsupportedOperationException ();
107109 },
108110 (MCTagContainer v ) -> {
109111 throw new UnsupportedOperationException ();
110112 }),
111113 TAG_CONTAINER_ARRAY (
112- (Mixed v ) -> {
114+ (Mixed v , Environment env ) -> {
113115 throw new UnsupportedOperationException ();
114116 },
115117 (MCTagContainer [] v ) -> {
116118 throw new UnsupportedOperationException ();
117119 });
118120
119- private final Function conversion ;
121+ private final BiFunction conversion ;
120122 private final Function construction ;
121123
122- <T extends Mixed , Z > MCTagType (Function < T , Z > conversion , Function <Z , T > construction ) {
124+ <T extends Mixed , Z > MCTagType (BiFunction < T , Environment , Z > conversion , Function <Z , T > construction ) {
123125 this .conversion = conversion ;
124126 this .construction = construction ;
125127 }
@@ -148,30 +150,30 @@ public Object convert(MCTagContainer container, Mixed value, Environment env) {
148150 throw new CREFormatException ("Expected tag container array to be associative." , value .getTarget ());
149151 }
150152 for (String key : containerArray .stringKeySet ()) {
151- Mixed possibleArray = containerArray .get (key , value .getTarget ());
153+ Mixed possibleArray = containerArray .get (key , value .getTarget (), env );
152154 if (!possibleArray .isInstanceOf (CArray .TYPE , null , env )) {
153155 throw new CREFormatException ("Expected tag entry to be an array." , possibleArray .getTarget ());
154156 }
155157 CArray entryArray = (CArray ) possibleArray ;
156158 if (!entryArray .isAssociative ()) {
157159 throw new CREFormatException ("Expected tag array to be associative." , entryArray .getTarget ());
158160 }
159- Mixed entryType = entryArray .get ("type" , entryArray .getTarget ());
160- Mixed entryValue = entryArray .get ("value" , entryArray .getTarget ());
161+ Mixed entryType = entryArray .get ("type" , entryArray .getTarget (), env );
162+ Mixed entryValue = entryArray .get ("value" , entryArray .getTarget (), env );
161163 MCTagType tagType ;
162164 try {
163165 tagType = MCTagType .valueOf (entryType .val ());
164166 } catch (IllegalArgumentException ex ) {
165167 throw new CREFormatException ("Tag type is not valid: " + entryType .val (), entryType .getTarget ());
166168 }
167169 Object tagValue ;
168- if (tagType == MCTagType .TAG_CONTAINER ) {
169- tagValue = tagType .convert (container .newContainer (), entryValue , env );
170- } else if (tagType == TAG_CONTAINER_ARRAY ) {
171- tagValue = tagType .convert (container , entryValue , env );
172- } else {
170+ if (null == tagType ) {
173171 tagValue = tagType .convert (container , entryValue , env );
174- }
172+ } else tagValue = switch (tagType ) {
173+ case TAG_CONTAINER -> tagType .convert (container .newContainer (), entryValue , env );
174+ case TAG_CONTAINER_ARRAY -> tagType .convert (container , entryValue , env );
175+ default -> tagType .convert (container , entryValue , env );
176+ };
175177 try {
176178 container .set (StaticLayer .GetConvertor ().GetNamespacedKey (key ), tagType , tagValue );
177179 } catch (ClassCastException ex ) {
@@ -189,14 +191,14 @@ public Object convert(MCTagContainer container, Mixed value, Environment env) {
189191 if (array .isAssociative ()) {
190192 throw new CREFormatException ("Expected tag container array to not be associative." , array .getTarget ());
191193 }
192- MCTagContainer [] containers = new MCTagContainer [(int ) array .size ()];
194+ MCTagContainer [] containers = new MCTagContainer [(int ) array .size (env )];
193195 int i = 0 ;
194196 for (Mixed possibleContainer : array ) {
195197 containers [i ++] = (MCTagContainer ) TAG_CONTAINER .convert (container .newContainer (), possibleContainer , env );
196198 }
197199 return containers ;
198200 }
199- return conversion .apply (value );
201+ return conversion .apply (value , env );
200202 }
201203
202204 /**
@@ -208,20 +210,20 @@ public Object convert(MCTagContainer container, Mixed value, Environment env) {
208210 public Mixed construct (Object value ) throws ClassCastException {
209211 if (this == TAG_CONTAINER ) {
210212 MCTagContainer container = (MCTagContainer ) value ;
211- CArray containerArray = CArray .GetAssociativeArray (Target .UNKNOWN );
213+ CArray containerArray = CArray .GetAssociativeArray (Target .UNKNOWN , null , null );
212214 for (MCNamespacedKey key : container .getKeys ()) {
213- CArray entry = CArray .GetAssociativeArray (Target .UNKNOWN );
215+ CArray entry = CArray .GetAssociativeArray (Target .UNKNOWN , null , null );
214216 MCTagType type = container .getType (key );
215- entry .set ("type" , type .name (), Target .UNKNOWN );
216- entry .set ("value" , type .construct (container .get (key , type )), Target .UNKNOWN );
217- containerArray .set (key .toString (), entry , Target .UNKNOWN );
217+ entry .set ("type" , type .name (), Target .UNKNOWN , null );
218+ entry .set ("value" , type .construct (container .get (key , type )), Target .UNKNOWN , null );
219+ containerArray .set (key .toString (), entry , Target .UNKNOWN , null );
218220 }
219221 return containerArray ;
220222 } else if (this == TAG_CONTAINER_ARRAY ) {
221223 MCTagContainer [] containers = (MCTagContainer []) value ;
222- CArray array = new CArray (Target .UNKNOWN , containers .length );
224+ CArray array = new CArray (Target .UNKNOWN , containers .length , ( GenericParameters ) null , ( Environment ) null );
223225 for (MCTagContainer container : containers ) {
224- array .push (TAG_CONTAINER .construct (container ), Target .UNKNOWN );
226+ array .push (TAG_CONTAINER .construct (container ), Target .UNKNOWN , null );
225227 }
226228 return array ;
227229 }
0 commit comments