-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[endec] add CodecUtils.toEndecWithRegistries for making a combined co…
…dec/packetcodec endec for registrybuf packetcodecs
- Loading branch information
Showing
6 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/io/wispforest/owo/serialization/endec/DefaultedListEndec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.wispforest.owo.serialization.endec; | ||
|
||
import io.wispforest.endec.Endec; | ||
import io.wispforest.endec.impl.StructEndecBuilder; | ||
import net.minecraft.util.collection.DefaultedList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
public final class DefaultedListEndec { | ||
|
||
private DefaultedListEndec() {} | ||
|
||
public static <T> Endec<DefaultedList<T>> forSize(Endec<T> elementEndec, T defaultValue, int size) { | ||
return forSize(elementEndec, defaultValue, element -> Objects.equals(defaultValue, element), size); | ||
} | ||
|
||
public static <T> Endec<DefaultedList<T>> forSize(Endec<T> elementEndec, T defaultValue, Predicate<T> skipWhen, int size) { | ||
var entryEndec = StructEndecBuilder.of( | ||
elementEndec.fieldOf("element", s -> s.element), | ||
Endec.VAR_INT.fieldOf("idx", s -> s.idx), | ||
Entry::new | ||
); | ||
|
||
return entryEndec.listOf().xmap( | ||
entries -> { | ||
var list = DefaultedList.ofSize(size, defaultValue); | ||
entries.forEach(entry -> list.set(entry.idx, entry.element)); | ||
return list; | ||
}, elements -> { | ||
var entries = new ArrayList<Entry<T>>(); | ||
for (int i = 0; i < elements.size(); i++) { | ||
if (skipWhen.test(elements.get(i))) continue; | ||
entries.add(new Entry<>(elements.get(i), i)); | ||
} | ||
return entries; | ||
} | ||
); | ||
} | ||
|
||
private record Entry<T>(T element, int idx) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters