4444import java .nio .IntBuffer ;
4545import java .nio .LongBuffer ;
4646import java .util .ArrayList ;
47- import java .util .Arrays ;
47+ import java .util .Iterator ;
4848import java .util .List ;
4949import java .util .Map .Entry ;
5050import java .util .function .BiConsumer ;
@@ -99,12 +99,18 @@ public <U> U convertTo(DynamicOps<U> outOps, LinTag<?> input) {
9999 case LinLongTag tag -> outOps .createLong (tag .valueAsLong ());
100100 case LinFloatTag tag -> outOps .createFloat (tag .valueAsFloat ());
101101 case LinDoubleTag tag -> outOps .createDouble (tag .valueAsDouble ());
102- case LinByteArrayTag tag -> outOps .createByteList (tag .view ());
103102 case LinStringTag tag -> outOps .createString (tag .value ());
104103 case LinListTag <?> tag -> convertList (outOps , tag );
105104 case LinCompoundTag tag -> convertMap (outOps , tag );
106- case LinIntArrayTag tag -> outOps .createIntList (Arrays .stream (tag .value ()));
107- case LinLongArrayTag tag -> outOps .createLongList (Arrays .stream (tag .value ()));
105+ case LinByteArrayTag tag -> outOps .createByteList (tag .view ());
106+ case LinIntArrayTag tag -> {
107+ IntBuffer view = tag .view ();
108+ yield outOps .createIntList (IntStream .range (0 , view .limit ()).map (view ::get ));
109+ }
110+ case LinLongArrayTag tag -> {
111+ LongBuffer view = tag .view ();
112+ yield outOps .createLongList (IntStream .range (0 , view .limit ()).mapToLong (view ::get ));
113+ }
108114 };
109115 }
110116
@@ -293,21 +299,27 @@ public DataResult<LinTag<?>> mergeToMap(LinTag<?> map, MapLike<LinTag<?>> values
293299 return DataResult .error (() -> "mergeToMap called with non-map: " + map , map );
294300 }
295301 LinCompoundTag .Builder output = builderFrom (map );
296- List <LinTag <?>> missed = new ArrayList <>();
302+ List <LinTag <?>> missed = null ;
303+ Iterator <Pair <LinTag <?>, LinTag <?>>> entries = values .entries ().iterator ();
297304 try {
298- values .entries ().forEach (entry -> {
305+ while (entries .hasNext ()) {
306+ Pair <LinTag <?>, LinTag <?>> entry = entries .next ();
299307 LinTag <?> key = entry .getFirst ();
300308 if (key instanceof LinStringTag stringKey ) {
301309 output .put (stringKey .value (), entry .getSecond ());
302310 } else {
311+ if (missed == null ) {
312+ missed = new ArrayList <>();
313+ }
303314 missed .add (key );
304315 }
305- });
316+ }
306317 } catch (IllegalArgumentException e ) {
307318 return DataResult .error (e ::getMessage );
308319 }
309- if (!missed .isEmpty ()) {
310- return DataResult .error (() -> "some keys are not strings: " + missed , output .build ());
320+ if (missed != null ) {
321+ List <LinTag <?>> missedKeys = missed ;
322+ return DataResult .error (() -> "some keys are not strings: " + missedKeys , output .build ());
311323 }
312324 return DataResult .success (output .build ());
313325 }
@@ -368,10 +380,18 @@ public DataResult<Stream<LinTag<?>>> getStream(LinTag<?> input) {
368380 IntStream .range (0 , values .limit ()).mapToObj (i -> LinByteTag .of (values .get (i )))
369381 );
370382 }
371- case LinIntArrayTag tag ->
372- DataResult .success (Arrays .stream (tag .value ()).mapToObj (value -> (LinTag <?>) LinIntTag .of (value )));
373- case LinLongArrayTag tag ->
374- DataResult .success (Arrays .stream (tag .value ()).mapToObj (value -> (LinTag <?>) LinLongTag .of (value )));
383+ case LinIntArrayTag tag -> {
384+ IntBuffer values = tag .view ();
385+ yield DataResult .success (
386+ IntStream .range (0 , values .limit ()).mapToObj (i -> (LinTag <?>) LinIntTag .of (values .get (i )))
387+ );
388+ }
389+ case LinLongArrayTag tag -> {
390+ LongBuffer values = tag .view ();
391+ yield DataResult .success (
392+ IntStream .range (0 , values .limit ()).mapToObj (i -> (LinTag <?>) LinLongTag .of (values .get (i )))
393+ );
394+ }
375395 default -> DataResult .error (() -> "Not a list" );
376396 };
377397 }
@@ -395,7 +415,8 @@ public LinTag<?> createByteList(ByteBuffer input) {
395415 @ Override
396416 public DataResult <IntStream > getIntStream (LinTag <?> input ) {
397417 if (input instanceof LinIntArrayTag tag ) {
398- return DataResult .success (Arrays .stream (tag .value ()));
418+ IntBuffer view = tag .view ();
419+ return DataResult .success (IntStream .range (0 , view .limit ()).map (view ::get ));
399420 }
400421 return DynamicOps .super .getIntStream (input );
401422 }
@@ -408,7 +429,8 @@ public LinTag<?> createIntList(IntStream input) {
408429 @ Override
409430 public DataResult <LongStream > getLongStream (LinTag <?> input ) {
410431 if (input instanceof LinLongArrayTag tag ) {
411- return DataResult .success (Arrays .stream (tag .value ()));
432+ LongBuffer view = tag .view ();
433+ return DataResult .success (IntStream .range (0 , view .limit ()).mapToLong (view ::get ));
412434 }
413435 return DynamicOps .super .getLongStream (input );
414436 }
0 commit comments