Skip to content

Commit

Permalink
Add copyOf and codecOf utility methods to NonNullList (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
gigaherz authored Dec 16, 2023
1 parent 84edb22 commit b1c7210
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions patches/net/minecraft/core/NonNullList.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--- a/net/minecraft/core/NonNullList.java
+++ b/net/minecraft/core/NonNullList.java
@@ -9,6 +_,28 @@
import org.apache.commons.lang3.Validate;

public class NonNullList<E> extends AbstractList<E> {
+
+ /**
+ * Neo: utility method to construct a Codec for a NonNullList
+ * @param entryCodec the codec to use for the elements
+ * @param <E> the element type
+ * @return a codec that encodes as a list, and decodes into NonNullList
+ */
+ public static <E> com.mojang.serialization.Codec<NonNullList<E>> codecOf(com.mojang.serialization.Codec<E> entryCodec) {
+ return entryCodec.listOf().xmap(NonNullList::copyOf, java.util.function.Function.identity());
+ }
+
+ /**
+ * Neo: utility method to construct an immutable NonNullList from a given collection
+ * @param entries the collection to make a copy of
+ * @param <E> the type of the elements in the list
+ * @return a new immutable NonNullList
+ * @throws NullPointerException if entries is null, or if it contains any nulls
+ */
+ public static <E> NonNullList<E> copyOf(java.util.Collection<? extends E> entries) {
+ return new NonNullList<>(List.copyOf(entries), null);
+ }
+
private final List<E> list;
@Nullable
private final E defaultValue;

0 comments on commit b1c7210

Please sign in to comment.