Skip to content

Commit

Permalink
[endec] add CodecUtils.toEndecWithRegistries for making a combined co…
Browse files Browse the repository at this point in the history
…dec/packetcodec endec for registrybuf packetcodecs
  • Loading branch information
gliscowo committed Jul 28, 2024
1 parent 0bffb9c commit c1fe13e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minecraft_version=1.21
yarn_mappings=1.21+build.2
loader_version=0.15.11
# Mod Properties
mod_version=0.12.10
mod_version=0.12.11
maven_group=io.wispforest
archives_base_name=owo-lib
# Dependencies
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/io/wispforest/owo/serialization/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,32 @@ public static <T> Endec<T> toEndec(Codec<T> codec, PacketCodec<ByteBuf, T> packe
);
}

public static <T> Endec<T> toEndecWithRegistries(Codec<T> codec, PacketCodec<RegistryByteBuf, T> packetCodec) {
return Endec.of(
(ctx, serializer, value) -> {
if (serializer instanceof ByteBufSerializer<?>) {
var buffer = new RegistryByteBuf(PacketByteBufs.create(), ctx.requireAttributeValue(RegistriesAttribute.REGISTRIES).registryManager());
packetCodec.encode(buffer, value);

MinecraftEndecs.PACKET_BYTE_BUF.encode(ctx, serializer, buffer);
return;
}

var ops = RegistryOps.of(EdmOps.withContext(ctx), ctx.requireAttributeValue(RegistriesAttribute.REGISTRIES).infoGetter());
EdmEndec.INSTANCE.encode(ctx, serializer, codec.encodeStart(ops, value).getOrThrow(IllegalStateException::new));
},
(ctx, deserializer) -> {
if (deserializer instanceof ByteBufDeserializer) {
var buffer = MinecraftEndecs.PACKET_BYTE_BUF.decode(ctx, deserializer);
return packetCodec.decode(new RegistryByteBuf(buffer, ctx.requireAttributeValue(RegistriesAttribute.REGISTRIES).registryManager()));
}

var ops = RegistryOps.of(EdmOps.withContext(ctx), ctx.requireAttributeValue(RegistriesAttribute.REGISTRIES).infoGetter());
return codec.parse(ops, EdmEndec.INSTANCE.decode(ctx, deserializer)).getOrThrow(IllegalStateException::new);
}
);
}

/**
* Create an endec which serializes an instance of {@link Either}, using {@code first}
* for the left and {@code second} for the right variant
Expand Down
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) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private MinecraftEndecs() {}
});

public static final Endec<Identifier> IDENTIFIER = Endec.STRING.xmap(Identifier::of, Identifier::toString);
public static final Endec<ItemStack> ITEM_STACK = CodecUtils.toEndec(ItemStack.OPTIONAL_CODEC);
public static final Endec<ItemStack> ITEM_STACK = CodecUtils.toEndecWithRegistries(ItemStack.OPTIONAL_CODEC, ItemStack.OPTIONAL_PACKET_CODEC);
public static final Endec<Text> TEXT = CodecUtils.toEndec(TextCodecs.CODEC, TextCodecs.PACKET_CODEC);

public static final Endec<Vec3i> VEC3I = vectorEndec("Vec3i", Endec.INT, Vec3i::new, Vec3i::getX, Vec3i::getY, Vec3i::getZ);
Expand Down
4 changes: 2 additions & 2 deletions src/testmod/java/io/wispforest/uwu/FabledBananasClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.wispforest.endec.Endec;
import io.wispforest.endec.format.json.GsonDeserializer;
import io.wispforest.endec.format.json.GsonSerializer;
import io.wispforest.endec.format.gson.GsonDeserializer;
import io.wispforest.endec.format.gson.GsonSerializer;
import io.wispforest.endec.impl.StructEndecBuilder;
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
import net.minecraft.item.Item;
Expand Down
15 changes: 8 additions & 7 deletions src/testmod/java/io/wispforest/uwu/Uwu.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.logging.LogUtils;
import io.netty.buffer.Unpooled;
import io.wispforest.endec.format.json.GsonDeserializer;
import io.wispforest.endec.format.json.GsonEndec;
import io.wispforest.endec.format.json.GsonSerializer;
import io.wispforest.endec.format.gson.GsonDeserializer;
import io.wispforest.endec.format.gson.GsonEndec;
import io.wispforest.endec.format.gson.GsonSerializer;
import io.wispforest.endec.impl.StructEndecBuilder;
import io.wispforest.owo.Owo;
import io.wispforest.owo.config.ConfigSynchronizer;
Expand All @@ -30,6 +30,7 @@
import io.wispforest.endec.Endec;
import io.wispforest.endec.format.bytebuf.ByteBufSerializer;
import io.wispforest.owo.serialization.CodecUtils;
import io.wispforest.owo.serialization.RegistriesAttribute;
import io.wispforest.owo.serialization.endec.MinecraftEndecs;
import io.wispforest.owo.serialization.format.nbt.NbtDeserializer;
import io.wispforest.owo.serialization.format.nbt.NbtEndec;
Expand Down Expand Up @@ -345,7 +346,7 @@ public void onInitialize() {
JsonElement stackJsonData;

try {
stackJsonData = MinecraftEndecs.ITEM_STACK.encodeFully(GsonSerializer::of, handStack);
stackJsonData = MinecraftEndecs.ITEM_STACK.encodeFully(SerializationContext.attributes(RegistriesAttribute.of(context.getSource().getWorld().getRegistryManager())), GsonSerializer::of, handStack);
} catch (Exception exception){
LOGGER.info(exception.getMessage());
LOGGER.info((Arrays.toString(exception.getStackTrace())));
Expand All @@ -358,7 +359,7 @@ public void onInitialize() {
LOGGER.info("---");

try {
handStack = MinecraftEndecs.ITEM_STACK.decodeFully(GsonDeserializer::of, stackJsonData);
handStack = MinecraftEndecs.ITEM_STACK.decodeFully(SerializationContext.attributes(RegistriesAttribute.of(context.getSource().getWorld().getRegistryManager())), GsonDeserializer::of, stackJsonData);
} catch (Exception exception){
LOGGER.info(exception.getMessage());
LOGGER.info((Arrays.toString(exception.getStackTrace())));
Expand Down Expand Up @@ -467,9 +468,9 @@ public void onInitialize() {
try {
iterations("Endec", (buf) -> {
ItemStack stack = source.getPlayer().getStackInHand(Hand.MAIN_HAND);
buf.write(MinecraftEndecs.ITEM_STACK, stack);
buf.write(SerializationContext.attributes(RegistriesAttribute.of(context.getSource().getWorld().getRegistryManager())), MinecraftEndecs.ITEM_STACK, stack);

var stackFromByte = buf.read(MinecraftEndecs.ITEM_STACK);
var stackFromByte = buf.read(SerializationContext.attributes(RegistriesAttribute.of(context.getSource().getWorld().getRegistryManager())), MinecraftEndecs.ITEM_STACK);
});
} catch (Exception exception){
LOGGER.info(exception.getMessage());
Expand Down

0 comments on commit c1fe13e

Please sign in to comment.