Interface EnumMapDispatchHasher<DistinctType,ValueType>

Type Parameters:
DistinctType - the distinct type used to indicate the implementation of a EnumMapDispatchHasher
ValueType - the abstract, base type to hash
All Known Implementing Classes:
ConsumeEffectType, NbtComponentType, ObjectContentsType

public interface EnumMapDispatchHasher<DistinctType,ValueType>
Utility interface with utility methods for creating MapBuilders for a scenario in which there is an abstract type A (EnumMapDispatchHasher), indicated by a typeKey of EnumMapDispatchHasher, and for each implementation of A, a MapBuilder exists, hashing an instance of the implementation. This interface builds on the MapBuilder.dispatch(String, MinecraftHasher, Function, Function) method to achieve this.

Essentially, implementations of this interface, when grouped together in e.g. an enum, function as a fancy named map from instances of A to their respective hashers.

A common way of implementing this is as follows:

  1. Create an enum, implementing EnumMapDispatchHasher, with the enum as EnumMapDispatchHasher and the base type A as EnumMapDispatchHasher.
  2. Add a clazz field to the enum, of type Class<? extends A>, and a mapBuilder field, of type MapBuilder<? extends A>.
    • The clazz field is used to recognise an implementation of A and find an instance of EnumMapDispatchHasher for a certain instance of an implementation of A.

      It is therefore important that the clazz field is unique for all constants in the enum.

    • The mapBuilder field is then used to hash that implementation of A.
    • You can use a typed constructor like this to ensure that the type of clazz is the same as mapBuilder, and to provide proper typing information for the mapBuilder:

      <T extends A> MyExampleEnum(Class<T> clazz, MapBuilder<T> builder)

      In which T is an implementation of A, and A is the base type to hash.

  3. For each implementation of A, add a constant to that enum, specifying the clazz of that implementation, and a mapBuilder for it.

    The name of the constant will be used to encode the value of the typeKey, so be sure it is right and matches Java edition!

  4. The methods of EnumMapDispatchHasher are to be implemented as follows:
  5. Finally, create the MapBuilder with the dispatch(Supplier) or the dispatch(String, Supplier) method, using MyExampleEnum::values as hashersSupplier.

Example implementations can be seen in ConsumeEffectType, NbtComponentType, and ObjectContentsType. If a MinecraftHasher is required instead of a MapBuilder, use MinecraftHasher.mapBuilder(MapBuilder) to wrap the MapBuilder into a hasher.

The implementation described above uses the enum itself as EnumMapDispatchHasher for A, and then uses dispatch(String, Supplier), which uses MinecraftHasher.fromEnum() to encode the enum constant for typeKey. This may not always be wanted behaviour, and this interface does allow using a different EnumMapDispatchHasher. In that case, the hasher for the EnumMapDispatchHasher has to be given, using the dispatch(MinecraftHasher, Supplier) or the dispatch(String, MinecraftHasher, Supplier) method.

See Also: