Skip to content

Commit

Permalink
Merge pull request #216 from Dragon-Seeker/1.20.3-RecordNullablity
Browse files Browse the repository at this point in the history
[1.20.3] Add Nullability within RecordEndec
  • Loading branch information
gliscowo authored Feb 17, 2024
2 parents 7c174ff + f933a8c commit df7b5a2
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @deprecated Moved to {@link io.wispforest.owo.serialization.annotations.SealedPolymorphic} for consistency. This
* annotation will keep working for the time being but eventually get removed
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Deprecated(forRemoval = true)
public @interface SealedPolymorphic {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.wispforest.owo.serialization.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Indicates to the {@link io.wispforest.owo.serialization.endec.RecordEndec} that this record component
* should be treated as nullable in serialization. Importantly, <b>this changes the serialized type of this
* component to an optional</b>
*/
@Target(ElementType.RECORD_COMPONENT)
@Retention(RetentionPolicy.RUNTIME)
public @interface NullableComponent {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.wispforest.owo.serialization.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SealedPolymorphic {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.Serializer;
import io.wispforest.owo.serialization.StructEndec;
import io.wispforest.owo.serialization.annotations.NullableComponent;
import org.apache.commons.lang3.mutable.MutableInt;

import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -44,9 +45,12 @@ public static <R extends Record> RecordEndec<R> create(Class<R> recordClass) {
var component = recordClass.getRecordComponents()[i];
var handle = lookup.unreflect(component.getAccessor());

var endec = (Endec<Object>) ReflectiveEndecBuilder.get(component.getGenericType());
if(component.isAnnotationPresent(NullableComponent.class)) endec = endec.nullableOf();

fields.add(new StructField<>(
component.getName(),
(Endec<Object>) ReflectiveEndecBuilder.get(component.getGenericType()),
endec,
instance -> getRecordEntry(instance, handle)
));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.wispforest.owo.serialization.endec;

import io.wispforest.owo.network.serialization.SealedPolymorphic;

import io.wispforest.owo.serialization.Deserializer;
import io.wispforest.owo.serialization.Endec;
import io.wispforest.owo.serialization.Serializer;
import io.wispforest.owo.serialization.StructEndec;
import io.wispforest.owo.serialization.annotations.SealedPolymorphic;
import io.wispforest.owo.serialization.format.nbt.NbtEndec;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
Expand Down Expand Up @@ -126,7 +127,7 @@ public static <T> Optional<Endec<T>> maybeGet(Class<T> clazz) {
serializer = (Endec<T>) Endec.forEnum((Class<? extends Enum>) clazz);
} else if (clazz.isArray()) {
serializer = (Endec<T>) ReflectiveEndecBuilder.createArrayEndec(clazz.getComponentType());
} else if (clazz.isAnnotationPresent(SealedPolymorphic.class)) {
} else if (clazz.isAnnotationPresent(io.wispforest.owo.network.serialization.SealedPolymorphic.class) || clazz.isAnnotationPresent(SealedPolymorphic.class)) {
serializer = (Endec<T>) ReflectiveEndecBuilder.createSealedSerializer(clazz);
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.wispforest.uwu.network;

import io.wispforest.owo.serialization.annotations.NullableComponent;

import java.util.List;

public record NullablePacket(@NullableComponent String name, @NullableComponent List<String> names) {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.wispforest.uwu.network;

import io.wispforest.owo.network.serialization.SealedPolymorphic;
import io.wispforest.owo.serialization.annotations.SealedPolymorphic;

@SealedPolymorphic
public sealed interface SealedTestClass permits SealedSubclassOne, SealedSubclassTwo {
Expand Down
16 changes: 16 additions & 0 deletions src/testmod/java/io/wispforest/uwu/network/UwuNetworkExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ public static void init() {
CHANNEL.registerServerbound(MaldingPacket.class, (message, access) -> {
access.player().sendMessage(Text.of(message.toString()), false);
});

CHANNEL.registerServerbound(NullablePacket.class, (message, access) -> {
if(message.name() == null && message.names() == null) {
access.player().sendMessage(Text.of("NULLABLITY FOR THE WIN"));
} else {
var text = Text.literal("");

text.append(Text.of(String.valueOf(message.name())));
text.append(Text.of(String.valueOf(message.names())));

access.player().sendMessage(text);
}
});
}

@Environment(EnvType.CLIENT)
Expand All @@ -52,6 +65,9 @@ public static void init() {

CHANNEL.clientHandle().send(new MaldingPacket(new DispatchedSubclassOne("base")));
CHANNEL.clientHandle().send(new MaldingPacket(new DispatchedSubclassTwo(20)));

CHANNEL.clientHandle().send(new NullablePacket(null, null));
CHANNEL.clientHandle().send(new NullablePacket("Weeee", null));
}
});
}
Expand Down

0 comments on commit df7b5a2

Please sign in to comment.