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)andregistryCast(BiFunction)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<byte[]>static final MinecraftHasher<Double>static final MinecraftHasher<Integer>static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot>static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemAttributeModifiers.EquipmentSlotGroup>static final MinecraftHasher<Float>static final MinecraftHasher<org.geysermc.mcprotocollib.auth.GameProfile.Property>static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos>static final MinecraftHasher<Integer>static final MinecraftHasher<int[]>static final MinecraftHasher<net.kyori.adventure.key.Key>static final MinecraftHasher<net.kyori.adventure.key.Key>static final MinecraftHasher<Long>static final MinecraftHasher<long[]>static final MinecraftHasher<org.cloudburstmc.nbt.NbtList<?>>static final MinecraftHasher<org.cloudburstmc.nbt.NbtMap>static final MinecraftHasher<org.cloudburstmc.math.vector.Vector3i>static final MinecraftHasher<Integer>static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.player.ResolvableProfile>static final MinecraftHasher<Short>static final MinecraftHasher<String>static final MinecraftHasher<net.kyori.adventure.key.Key>static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.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 aMinecraftHasherfrom aMinecraftHasherusingtypeExtractor, 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 aMinecraftHasher, and then uses that hasher to encodeMinecraftHasher.static <Type,First, Second>
MinecraftHasher<Type>either(MinecraftHasher<First> firstHasher, Function<Type, First> firstExtractor, MinecraftHasher<Second> secondHasher, Function<Type, Second> secondExtractor) Creates a hasher that hashesMinecraftHasherusing eitherfirstHasherto hashMinecraftHasher(obtained usingfirstExtractor), or usessecondHasherto hashMinecraftHasher(obtained viasecondExtractor).default MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.Filterable<Type>>Creates a hasher that wraps theMinecraftHasherin aFilterable.static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<EnumConstant>fromEnum()static <EnumConstant extends Enum<EnumConstant>>
MinecraftHasher<EnumConstant>Creates a hasher forMinecraftHashers that usestoNameto turn aMinecraftHasherinto 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 ofMinecraftHashers, 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 hashMinecraftHasherkeys andMinecraftHashervalues into a map, using thekeyHasherandvalueHasherrespectively.static <Type> MinecraftHasher<Type>mapBuilder(MapBuilder<Type> builder) Creates a hasher that uses aMapBuilder, which uses aMapHasherto hash aMinecraftHasherinto a map.static <Type> MinecraftHasher<Collection<Type>>mapSet(MinecraftHasher<Type> keyHasher, MinecraftHasher<Type> valueHasher) Creates a hasher that hashes a set ofMinecraftHasherentries into a map of keys and values, usingkeyHasherandvalueHasherrespectively.default <Casted> MinecraftHasher<Casted>registryCast(BiFunction<JavaRegistryProvider, Casted, Type> converter) Likecast(Function), but has access toJavaRegistryProvider.static <Type> MinecraftHasher<Type>unit()Creates a hasher that always encodes into an empty map.
-
Field Details
-
UNIT
static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.Unit> UNIT -
BYTE
-
SHORT
-
INT
-
LONG
-
FLOAT
-
DOUBLE
-
STRING
-
BOOL
-
BYTE_ARRAY
-
INT_ARRAY
-
LONG_ARRAY
-
NBT_MAP
-
NBT_LIST
-
POS
-
KEY
-
TAG
-
KEY_REMOVAL
-
UUID
-
GAME_PROFILE_PROPERTY
static final MinecraftHasher<org.geysermc.mcprotocollib.auth.GameProfile.Property> GAME_PROFILE_PROPERTY -
RESOLVABLE_PROFILE
static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.player.ResolvableProfile> RESOLVABLE_PROFILE -
RARITY
-
DYE_COLOR
-
EQUIPMENT_SLOT
static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.EquipmentSlot> EQUIPMENT_SLOT -
EQUIPMENT_SLOT_GROUP
static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.ItemAttributeModifiers.EquipmentSlotGroup> EQUIPMENT_SLOT_GROUP -
GLOBAL_POS
static final MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.entity.metadata.GlobalPos> GLOBAL_POS
-
-
Method Details
-
hash
-
list
Creates a hasher that hashes a list of objects this hasher hashes. -
cast
"Casts" this hasher to another hash a different object, with a converter method. The returned hasher takes aMinecraftHasher, converts it to aMinecraftHasherusing theconverter, and then hashes it.If a
JavaRegistryProviderobject is needed for conversion, useregistryCast(BiFunction).- Type Parameters:
Casted- the type of the new hasher.- Parameters:
converter- the converter function that converts aMinecraftHasherinto aMinecraftHasher.- See Also:
-
registryCast
default <Casted> MinecraftHasher<Casted> registryCast(BiFunction<JavaRegistryProvider, Casted, Type> converter) Likecast(Function), but has access toJavaRegistryProvider.- Type Parameters:
Casted- the type of the new hasher.- Parameters:
converter- the converter function.- See Also:
-
dispatch
default <Dispatched> MinecraftHasher<Dispatched> dispatch(Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> hashDispatch) -
dispatch
default <Dispatched> MinecraftHasher<Dispatched> dispatch(String typeKey, Function<Dispatched, Type> typeExtractor, Function<Type, MapBuilder<Dispatched>> mapDispatch) Creates a hasher that dispatches aMinecraftHasherfrom aMinecraftHasherusingtypeExtractor, 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.
Internally this simply delegates to and wraps
MapBuilder.dispatch(String, MinecraftHasher, Function, Function)inmapBuilder(MapBuilder), usingthisastypeHasher.- Type Parameters:
Dispatched- the type of the new hasher.- Parameters:
typeKey- the key to store theMinecraftHasherin.typeExtractor- the function that extracts aMinecraftHasherfrom aMinecraftHasher.mapDispatch- the function that provides aMapBuilderbased on aMinecraftHasher.- See Also:
-
filterable
default MinecraftHasher<org.geysermc.mcprotocollib.protocol.data.game.item.component.Filterable<Type>> filterable()Creates a hasher that wraps theMinecraftHasherin aFilterable. -
unit
Creates a hasher that always encodes into an empty map. -
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 ofMinecraftHashers.- 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 ofMinecraftHashers, and usestoNameto turn the constant into a string, which it then hashes.- Type Parameters:
EnumConstant- the enum.- Parameters:
values- the array ofMinecraftHashers.toName- the function that turns aMinecraftHasherinto 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 forMinecraftHashers that usestoNameto turn aMinecraftHasherinto a string, which it then hashes.- Type Parameters:
EnumConstant- the enum.- Parameters:
toName- the function that turns aMinecraftHasherinto a string.- See Also:
-
mapBuilder
Creates a hasher that uses aMapBuilder, which uses aMapHasherto hash aMinecraftHasherinto a map.- Type Parameters:
Type- the type to hash.- Parameters:
builder- the builder that creates a map from aMinecraftHasher.
-
map
static <K,V> MinecraftHasher<Map<K,V>> map(MinecraftHasher<K> keyHasher, MinecraftHasher<V> valueHasher) Creates a hasher that usesmapSet(MinecraftHasher, MinecraftHasher)to hashMinecraftHasherkeys andMinecraftHashervalues into a map, using thekeyHasherandvalueHasherrespectively.- Type Parameters:
K- the key type.V- the value type.- Parameters:
keyHasher- the hasher that hashes objects of typeMinecraftHasher.valueHasher- the hasher that hashes objects of typeMinecraftHasher.- See Also:
-
mapSet
static <Type> MinecraftHasher<Collection<Type>> mapSet(MinecraftHasher<Type> keyHasher, MinecraftHasher<Type> valueHasher) Creates a hasher that hashes a set ofMinecraftHasherentries 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.
- Type Parameters:
Type- the entry type.- Parameters:
keyHasher- the hasher that hashes aMinecraftHasherinto a key to be used in the map.valueHasher- the hasher that hashes aMinecraftHasherinto a value to be used in the map.- See Also:
-
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 hashesMinecraftHasherusing eitherfirstHasherto hashMinecraftHasher(obtained usingfirstExtractor), or usessecondHasherto hashMinecraftHasher(obtained viasecondExtractor).Specifically, the hasher first tries to use
firstExtractorto obtain aMinecraftHasherfromMinecraftHasher. If this returns a not-null value,firstHasheris used to hash the value.If the returned value is null, then
secondExtractoris used to obtain aMinecraftHasherfromMinecraftHasher. 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 hashMinecraftHasher.firstExtractor- the function used to obtain aMinecraftHasherfrom aMinecraftHasher.secondHasher- the hasher used to hashMinecraftHasher.secondExtractor- the function used to obtain aMinecraftHasherfrom aMinecraftHasher.
-
dispatch
Creates a hasher that dispatches aMinecraftHasherfrom aMinecraftHasher, and then uses that hasher to encodeMinecraftHasher.- Type Parameters:
Type- the type to hash.- Parameters:
hashDispatch- the function that returns aMinecraftHasherfrom aMinecraftHasher.
-