Skip to content

Instantly share code, notes, and snippets.

@fallenthereaper
Created December 16, 2023 15:29
Show Gist options
  • Save fallenthereaper/512d34e91015978ea8c705c6d89df489 to your computer and use it in GitHub Desktop.
Save fallenthereaper/512d34e91015978ea8c705c6d89df489 to your computer and use it in GitHub Desktop.
public interface DataSerializer<T> {
/**
* Static factory method to build a {@link DataSerializer} instance.
*
* @param bufferReader Function to read data from a packet buffer.
* @param bufferWriter Function to write data to a packet buffer.
* @param nbtReader Function to read data from NBT.
* @param nbtWriter Function to write data to NBT.
* @param id The key used for accessing
* @param <T> The type of data value.
* @return A new instance of {@code DataSerializer} with specified read and write functions.
*/
static <T> DataSerializer<T> builder(final Class<T> clazz,
final String id,
final FriendlyByteBuf.Reader<T> bufferReader,
final FriendlyByteBuf.Writer<T> bufferWriter,
final BiFunction<CompoundTag, String, T> nbtReader,
final TriConsumer<CompoundTag, T, String> nbtWriter) {
TypeToken<T> typeToken = new TypeToken<T>(DataSerializer.class) {};
return new DataSerializer<>() {
@Override
public Class<T> getTargetClass() {
return clazz;
}
@Override
public ResourceLocation getID() {
return new ResourceLocation(InsightAPI.MOD_ID, id);
}
@Override
public T readFromNBT(CompoundTag tag, String id) {
if (tag.contains(id)) {
return null;
}
return nbtReader.apply(tag, id);
}
@Override
public void writeToNBT(CompoundTag tag, T data, String id) {
nbtWriter.accept(tag, data, id);
}
@Override
public T readFromBuffer(FriendlyByteBuf buffer) {
return bufferReader.apply(buffer);
}
@Override
public void writeToBuffer(FriendlyByteBuf buffer, T data) {
bufferWriter.accept(buffer, data);
}
};
}
/**
* Creates a simple DataSerializer for an enum type.
*
* @param id The ID used for accessing the serializer.
* @param clazz The enum class.
* @param <T> The enum type.
* @return A DataSerializer for the specified enum type.
*/
static <T extends Enum<T>> DataSerializer<T> buildEnum(final String id, Class<T> clazz) {
T[] enumConstants = clazz.getEnumConstants();
if (enumConstants == null || enumConstants.length == 0) {
throw new IllegalArgumentException("Invalid enum class: " + clazz.getName());
}
return builder(clazz,id,
(buffer) -> buffer.readEnum(clazz),
FriendlyByteBuf::writeEnum,
(tag, tagId) -> enumConstants[tag.getInt(tagId)],
(tag, data, tagId) -> tag.putInt(tagId, data.ordinal())
);
}
static <T extends Enum<T>> DataSerializer<T> buildEnum(Class<T> clazz) {
return buildEnum(clazz.getName().toLowerCase(), clazz);
}
static <T> DataSerializer<Optional<T>> optional(
final Class<T> clazz,
final String id,
final FriendlyByteBuf.Reader<T> bufferReader,
final FriendlyByteBuf.Writer<T> bufferWriter,
final BiFunction<CompoundTag, String, T> nbtReader,
final TriConsumer<CompoundTag, T, String> nbtWriter) {
return builder(clazz, id,
bufferReader.asOptional(),
bufferWriter.asOptional(),
(tag, tagId) -> {
if (tag.contains(id)) {
return Optional.empty();
}
return Optional.ofNullable(nbtReader.apply(tag, id));
},
(tag, data, tagId) -> {
data.ifPresent(value -> nbtWriter.accept(tag, value, id));
}
);
}
ResourceLocation getID();
/**
* Reads the data value from NBT (Named Binary Tag).
*
* @param tag The NBT tag containing the serialized data.
* @param id The identifier for the specific data value within the NBT tag.
* @return The deserialized data value.
*/
T readFromNBT(CompoundTag tag, String id);
/**
* Writes the data value to NBT (Named Binary Tag).
*
* @param tag The NBT tag to write the serialized data.
* @param data The data value to serialize.
* @param id The identifier for the specific data value within the NBT tag.
*/
void writeToNBT(CompoundTag tag, T data, String id);
/**
* Reads the data value from a packet buffer.
*
* @param buffer The packet buffer containing the serialized data.
* @return The deserialized data value.
*/
T readFromBuffer(FriendlyByteBuf buffer);
/**
* Writes the data value to a packet buffer.
*
* @param buffer The packet buffer to write the serialized data.
* @param data The data value to serialize.
*/
void writeToBuffer(FriendlyByteBuf buffer, T data);
Class<T> getTargetClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment