#"Abstract" Codec

19 messages · Page 1 of 1 (latest)

knotty lake
#

I'm working on migrating some of my code from raw JSON parsing to Codecs, the way how I have done the code kind of brings me some issues with that approach

I'm also changing how the config looks, the 1st image is the current config and the 2nd one is the new planned one

roles and type are common to all objects in entries, execute and on_join are optional, and it's content varies depending on the type
each type has it's class, and it's own codec.

I wanted to have a Codec in BaseEntry, the abstract super class, that gives the correct child class from Codec#parse and gives the full JSON object from Codec#encode

BaseEntry needs the roles when initialised, currently I also take the type too, so it can be encoded back

I can also change the config structure, but my goal is to have it easier for users to understand

So I want to know if there is a way to have one codec to do the stuff. I also want to allow one to use the child class' codec to encode and decode

in other words, BaseEntry.CODEC.parse gives me a child of BaseEntry and (for example) CommandEntry.CODEC.encode to give me

{
  "roles": ["744941527545020468"],
  "type": "autowhitelist:team",
  "on_join": true,
  "execute": {
    "on_add": "give {player} diamond 1",
    "on_remove": "scoreboard players set cool_score {player} 0"
  }
}
#

This is another config layout I'm considering

keen flax
#

If you're looking for each type having a different codec, you're probably looking for a dispatch codec.

knotty lake
#

I have looked into that, but got a bit confused looking at usages

#

specially bc something like this isn't really possible as it is an abstract class

keen flax
#

Would an example help maybe? I can grab one

#
public abstract class SpellConsequence {
    public static final Codec<SpellConsequence> CODEC = ConsequenceFactory.FACTORY_CODEC.dispatch("type", SpellConsequence::getFactory, ConsequenceFactory::getCodec);
...
}

The factory is a "type," which also has a codec. Anything that has that factory will use that codec. Repo

#

This also helped. Yes it's for forge, but DFU apparently isn't obfuscated.
https://forge.gemwire.uk/wiki/Codecs#Registry_Dispatch

Codecs are a serialization tool from mojang's DataFixerUpper library. Codecs are used alongside DynamicOps to allow objects to be serialized to different formats and back, such as JSON or NBT. While the DynamicOps describes the format the object is to be serialized to, the Codec describes the manner in which the object is to be serialized; a sin...

knotty lake
#

Thanks, I'll look into it

#

I imagine I still have to include the other common fields in the children codecs right?
the dispatch seems to just deal with the type

keen flax
#

Yeah

#

At least as far as I know

knotty lake
knotty lake
#

That's an issue

#

Input does not contain a key []: MapLike[{ "type": "autowhitelist:team", "roles": [ "744941527545020468" ], "execute": { "team": "team1" } }]

#

nvm that's on me

sharp bough
knotty lake
sharp bough
#

and how would you use it since it can't instantiate that class?