mixson

Mixson Codecs

Although Mixson only supports .json files by default, MixsonCodecs can be created so that the user can handle custom resource types.

The default codec Mixson uses is the JSON_ELEMENT_CODEC field in the Mixson class.

The MixsonCodec interface is defined as follows:

import java.io.ByteArrayOutputStream;interface MixsonCodec<T> {
T deserialize(Resource resource) throws IOException;
Resource serialize(Resource associatedResource, T file);
ByteArrayOutputStream serializeOutputFile(T file) throws IOException;
String extensionAndDot();
}

The deserialize method will take in the resource Minecraft finds and will convert it into whatever value the codec is designed for.

The serialize method converts the given custom resource and turns it back into a Minecraft resource. An associatedResource is also provided as context for serialization.

The serializeOutputFile converts the custom resource into a string to be written to a file for debugging in the EXPORT debug mode.

the extensionAndDot method returns the extension of the file the codec is targeting with a dot (.).

MixsonCodec provides two methods as syntax sugar for creating instances:

static <T> MixsonCodec<T> of(String extension, ResourceDeserializer<T> deserializer, ResourceSerializer<T> serializer, Function<T, String> serializeOutputFile)
static <T> MixsonCodec<T> create(String extension, ResourceDeserializer<T> deserializer, ResourceSerializer<T> serializer, ResourceExporter<T> serializeOutputFile)

Each argument matches the definition of a method in the interface.

Inline

This codec will create a JsonObject for event handling:

MixsonCodec<JsonObject> JSON_OBJECT_CODEC = MixsonCodec.of("json", resource -> Mixson.JSON_ELEMENT_CODEC.deserialize(resource).getAsJsonObject(), Mixson.JSON_ELEMENT_CODEC::serialize, JsonObject::toString);

This example event will process the resource 'models/item/diamond' with the new codec:

Mixson.registerModificationEvent(
JSON_OBJECT_CODEC,
"models/item/diamond",
"test event",
(context) -> { // EventContext<JsonObject>
JsonObject elem = context.getFile();
JsonObject textures = elem.getAsJsonObject("textures");
textures.addProperty("layer0", "minecraft:item/netherite_scrap");
}
);

APT

The codec wanting to be used will need to be returned by a function that Mixson can call to obtain the value. The method will also need to be annotated with @Codec.

@Codec is defined as follows:

public @interface Codec {
String value();
}

The value should be an identifier that the @MixsonEvent annotation can share to link the two.

The method used for getting the codec should take in no values and return MixsonEvent<T> such that T is the desired type.

@Codec("codecId")
private static Mixson<JsonObject> getCustomCodec() {
return MixsonCodec.of("json", resource -> Mixson.JSON_ELEMENT_CODEC.deserialize(resource).getAsJsonObject(), Mixson.JSON_ELEMENT_CODEC::serialize, JsonObject::toString);
}

The event then needs to pass the id value given to the @Codec annotation to the @MixsonEvent annotation.

@MixsonEvent(value = "models/item/diamond", codec = "codecId")
private static void changeDiamondTexture(EventContext<JsonObject> context) {
context.getFile().getAsJsonObject("textures").addProperty("layer0", "item/netherite_scrap");
}