Interface MinecraftHasher<Type>
- Type Parameters:
Type- the type this hasher hashes.
- All Known Subinterfaces:
RegistryHasher<DirectType>
- All Known Implementing Classes:
RegistryHasher.RegistryHasherWithDirectHasher
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
HashCode using a MinecraftHashEncoder.
Hashers have been implemented for common types, such as all Java primitives, units (encodes an empty map), NBT maps and lists,
Vector3i positions, Key resource locations and #-prefixed tags, UUIDs, and more types. Furthermore, in RegistryHasher
more hashers can be found for more specific for Minecraft types, and ComponentHasher.COMPONENT hashes Components.
Most hashers are not created by implementing them directly, rather, they are built on top of other hashers, using various methods to manipulate and map them. Here are some commonly used ones:
list()creates a hasher that hashes a list of the objects.cast(Function)create a new hasher that delegates to this hasher with a converter function.filterable()creates a hasher that hashes aFilterableinstance of the object.
On top of these there are more methods, as well as static helper methods in this class, to create hashers.
One worth pointing out is mapBuilder(MapBuilder), which hashes an object into a map-like structure. Read the documentation there, on MapBuilder, and on MapHasher on how to use it.
As of right now, hashers are used to create hash codes for data components, which Minecraft requires to be sent in inventory transactions. You'll find all the hashers for data components in
DataComponentHashers. In the future, hashers may be used elsewhere as well. If necessary, this system can even be refactored to write to different data structures, such as NBT or JSON files, as well.
When creating new hashers, please be sure to put them in the proper place:
- Hashers that hash very generic types, or types that are used broadly across Minecraft (like key, UUID, game profile, etc.) belong here in
MinecraftHasher. - Hashers that hash more specific types, are more complicated, or depend on a hasher in
RegistryHasher, belong inRegistryHasher. - Hashers that hash components, and are used nowhere else, belong in
DataComponentHashers.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final MinecraftHasher<Boolean> static final MinecraftHasher<Byte> static final MinecraftHasher<Double> static final MinecraftHasher<Integer> static final MinecraftHasher<EquipmentSlot> static final MinecraftHasher<ItemAttributeModifiers.EquipmentSlotGroup> static final MinecraftHasher<Float> static final MinecraftHasher<GameProfile> static final MinecraftHasher<GameProfile.Property> static final MinecraftHasher<GlobalPos> static final MinecraftHasher<Integer> static final MinecraftHasher<IntStream> static final MinecraftHasher<Consumable.ItemUseAnimation> static final MinecraftHasher<String> static final MinecraftHasher<String> static final MinecraftHasher<net.kyori.adventure.key.Key> static final MinecraftHasher<net.kyori.adventure.key.Key> static final MinecraftHasher<Long> static final MinecraftHasher<com.viaversion.nbt.mini.MNBT> static final MinecraftHasher<com.viaversion.nbt.tag.ListTag> static final MinecraftHasher<com.viaversion.nbt.tag.CompoundTag> static final MinecraftHasher<org.cloudburstmc.math.vector.Vector3i> static final MinecraftHasher<Integer> static final MinecraftHasher<Short> static final MinecraftHasher<String> static final MinecraftHasher<String> static final MinecraftHasher<Unit> static final MinecraftHasher<UUID> -
Method Summary
Modifier and TypeMethodDescriptiondefault <Casted> MinecraftHasher<Casted> "Casts" this hasher to another hash a different object, with a converter method.default <Dispatched>
MinecraftHasher<Dispatched> dispatch(String typeKey, Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> mapDispatch) Creates a hasher that dispatches aTypefrom aDispatchedusingtypeExtractor, puts this in thetypeKeykey in a map, and uses aMapBuilderprovided bymapDispatchto build the rest of the map.default <Dispatched>
MinecraftHasher<Dispatched> dispatch(Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> hashDispatch) static <Type> MinecraftHasher<Type> dispatch(Function<Type, MinecraftHasher<Type>> hashDispatch) Creates a hasher that dispatches aMinecraftHasherfrom aType, and then uses that hasher to encodeType.static <Type,First, Second>
MinecraftHasher<Type> either(MinecraftHasher<First> firstHasher, Function<Type, First> firstExtractor, MinecraftHasher<Second> secondHasher, Function<Type, Second> secondExtractor) default MinecraftHasher<Filterable<Type>> Creates a hasher that wraps theTypein aFilterable.static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<EnumConstant> fromEnum()static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<EnumConstant> Creates a hasher forEnumConstants that usestoNameto turn aEnumConstantinto a string, which it then hashes.static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<Integer> fromIdEnum(EnumConstant[] values) static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<Integer> fromIdEnum(EnumConstant[] values, Function<EnumConstant, String> toName) Creates a hasher that looks up an int in the array ofEnumConstants, and usestoNameto turn the constant into a string, which it then hashes.com.google.common.hash.HashCodehash(Type value, MinecraftHashEncoder encoder) static <Type> MinecraftHasher<Type> lazyInitialize(Supplier<MinecraftHasher<Type>> hasher) Lazily-initialises the given hasher usingSuppliers.memoize(com.google.common.base.Supplier).default MinecraftHasher<List<Type>> list()Creates a hasher that hashes a list of objects this hasher hashes.static <K,V> MinecraftHasher <Map<K, V>> map(MinecraftHasher<K> keyHasher, MinecraftHasher<V> valueHasher) Creates a hasher that usesmapSet(MinecraftHasher, MinecraftHasher)to hashKkeys andVvalues into a map, using thekeyHasherandvalueHasherrespectively.static <Type> MinecraftHasher<Type> mapBuilder(MapBuilder<Type> builder) static <Type> MinecraftHasher<Collection<Type>> mapSet(MinecraftHasher<Type> keyHasher, MinecraftHasher<Type> valueHasher) Creates a hasher that hashes a set ofTypeentries into a map of keys and values, usingkeyHasherandvalueHasherrespectively.
-
Field Details
-
UNIT
-
BYTE
-
SHORT
-
INT
-
LONG
-
FLOAT
-
DOUBLE
-
STRING
-
BOOL
-
INT_ARRAY
-
NBT_MAP
-
NBT_LIST
-
POS
-
MNBT
-
KEY
-
KYORI_KEY
-
TAG
-
KYORI_KEY_REMOVAL
-
KEY_REMOVAL
-
UUID
-
GAME_PROFILE_PROPERTY
-
GAME_PROFILE
-
RARITY
-
ITEM_USE_ANIMATION
-
DYE_COLOR
-
EQUIPMENT_SLOT
-
EQUIPMENT_SLOT_GROUP
-
GLOBAL_POS
-
-
Method Details
-
hash
-
list
Creates a hasher that hashes a list of objects this hasher hashes. -
cast
-
dispatch
default <Dispatched> MinecraftHasher<Dispatched> dispatch(Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> hashDispatch) - See Also:
-
dispatch
default <Dispatched> MinecraftHasher<Dispatched> dispatch(String typeKey, Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> mapDispatch) Creates a hasher that dispatches aTypefrom aDispatchedusingtypeExtractor, puts this in thetypeKeykey in a map, and uses aMapBuilderprovided bymapDispatchto build the rest of the map.This can be used to create hashers that hash an abstract type or interface into a map with different keys depending on the type.
- Type Parameters:
Dispatched- the type of the new hasher.- Parameters:
typeKey- the key to store theTypein.typeExtractor- the function that extracts aTypefrom aDispatched.mapDispatch- the function that provides aMapBuilderbased on aType.
-
filterable
Creates a hasher that wraps theTypein aFilterable. -
lazyInitialize
Lazily-initialises the given hasher usingSuppliers.memoize(com.google.common.base.Supplier). -
fromIdEnum
static <EnumConstant extends Enum<EnumConstant>> MinecraftHasher<Integer> fromIdEnum(EnumConstant[] values) UsesEnum.name()(lowercased) astoNamefunction infromIdEnum(Enum[], Function).Please be aware that you are using literal enum constants as string values here, meaning that if there is a typo in a constant, or a constant changes name, things may break. Use cautiously.
- Type Parameters:
EnumConstant- the enum.- Parameters:
values- the array ofEnumConstants.- See Also:
-
fromIdEnum
static <EnumConstant extends Enum<EnumConstant>> MinecraftHasher<Integer> fromIdEnum(EnumConstant[] values, Function<EnumConstant, String> toName) Creates a hasher that looks up an int in the array ofEnumConstants, and usestoNameto turn the constant into a string, which it then hashes.- Type Parameters:
EnumConstant- the enum.- Parameters:
values- the array ofEnumConstants.toName- the function that turns aEnumConstantinto a string.- See Also:
-
fromEnum
UsesEnum.name()(lowercased) astoNamefunction infromEnum(Function).Please be aware that you are using literal enum constants as string values here, meaning that if there is a typo in a constant, or a constant changes name, things may break. Use cautiously.
- Type Parameters:
EnumConstant- the enum.- See Also:
-
fromEnum
static <EnumConstant extends Enum<EnumConstant>> MinecraftHasher<EnumConstant> fromEnum(Function<EnumConstant, String> toName) Creates a hasher forEnumConstants that usestoNameto turn aEnumConstantinto a string, which it then hashes.- Type Parameters:
EnumConstant- the enum.- Parameters:
toName- the function that turns aEnumConstantinto a string.- See Also:
-
mapBuilder
- Type Parameters:
Type- the type to hash.- Parameters:
builder- the builder that creates a map from aType.
-
map
static <K,V> MinecraftHasher<Map<K,V>> map(MinecraftHasher<K> keyHasher, MinecraftHasher<V> valueHasher) Creates a hasher that usesmapSet(MinecraftHasher, MinecraftHasher)to hashKkeys andVvalues into a map, using thekeyHasherandvalueHasherrespectively. -
mapSet
static <Type> MinecraftHasher<Collection<Type>> mapSet(MinecraftHasher<Type> keyHasher, MinecraftHasher<Type> valueHasher) Creates a hasher that hashes a set ofTypeentries into a map of keys and values, usingkeyHasherandvalueHasherrespectively.Note that the key hasher is usually expected to hash into a string, but this is not necessarily a requirement. It may be once different encoders are used to encode to NBT or JSON, but this is not the case yet.
-
either
static <Type,First, MinecraftHasher<Type> eitherSecond> (MinecraftHasher<First> firstHasher, Function<Type, First> firstExtractor, MinecraftHasher<Second> secondHasher, Function<Type, Second> secondExtractor) Creates a hasher that hashesTypeusing eitherfirstHasherto hashFirst(obtained usingfirstExtractor), or usessecondHasherto hashSecond(obtained viasecondExtractor).Specifically, the hasher first tries to use
firstExtractorto obtain aFirstfromType. If this returns a not-null value,firstHasheris used to hash the value.If the returned value is null, then
secondExtractoris used to obtain aSecondfromType. This must never be null.secondHasheris then used to hash the value.Note:
secondExtractormust never return null iffirstExtractordoes!- Type Parameters:
Type- the type to hash.First- the first either type to hash.Second- the second either type to hash.- Parameters:
firstHasher- the hasher used to hashFirst.firstExtractor- the function used to obtain aFirstfrom aType.secondHasher- the hasher used to hashSecond.secondExtractor- the function used to obtain aSecondfrom aType.
-
dispatch
Creates a hasher that dispatches aMinecraftHasherfrom aType, and then uses that hasher to encodeType.- Type Parameters:
Type- the type to hash.- Parameters:
hashDispatch- the function that returns aMinecraftHasherfrom aType.
-