Record Class ItemMatch
java.lang.Object
java.lang.Record
com.trevlar.menukit.api.element.ItemMatch
- Record Components:
stack- the rule's stack: the item, and forEXACTandCONTAINSits count and componentsmatch- how a stack is compared with itignored- component types left out of the comparison (none by default)countIgnored- whetherEXACTleaves the count out (false by default)
public record ItemMatch(net.minecraft.world.item.ItemStack stack, ItemMatch.Match match, Set<net.minecraft.core.component.DataComponentType<?>> ignored, boolean countIgnored)
extends Record
An item rule: a stack and how another stack has to match it. What an
ItemPicker slot holds, and the one comparison every mod uses, so none
writes its own.
// A rule the player set with an item picker, tested against a stack in play.
if (rule.matches(stack)) lock(slot);
// A plain stack and the mode the setting declares.
ItemMatch.matches(config.target, ItemMatch.Match.CONTAINS, stack);
The three modes
ItemMatch.Match.ITEM: the kind of item only. Count and components are ignored: any diamond sword is a diamond sword.ItemMatch.Match.EXACT: vanilla's own stack equality (ItemStack.matches(ItemStack, ItemStack)): the same item, the same count, and exactly the same components. A rule with no components set is a plain item, so an enchanted or renamed one does not match it. The count matters for an item that stacks; one that does not is always one.ItemMatch.Match.CONTAINS: the same item, and at least the components the rule sets; others may be there too, and the count is ignored. A rule with no components set matches any variant of the item. A component the rule removes (!foodin/givesyntax) must be absent.
EXACT says plain, CONTAINS says any.
Ignoring components, and the count
A rule can leave chosen component types out of the comparison, inEXACT
and CONTAINS alike: neither side's value for them is looked at. A lock on
"this exact sword" that should hold after the sword takes damage:
ItemMatch lock = new ItemMatch(sword, ItemMatch.Match.EXACT)
.ignoring(DataComponents.DAMAGE) // still this sword once it is worn
.ignoringCount(); // and a stack of 3 is still "this item"
ignoringCount() drops the count from EXACT (the count is not a
component, so it has its own call; CONTAINS and ITEM never compare
it). Both are saved with the rule by CODEC, and a rule saved without them
loads ignoring nothing, as before.
CONTAINS compares as vanilla's item predicates do
Each component the rule sets is compared the way an item predicate in a command or a datapack compares it (/clear @s diamond_sword[damage=0], and the
partial ~ form where vanilla has one):
enchantmentsandstored_enchantments: the stack has at least the rule's enchantments, each at the rule's level (vanilla'sEnchantmentsPredicate); it may have more.custom_data: the rule's tag is contained in the stack's (vanilla's NBT predicate,CustomData.matchedBy(CompoundTag)).- Any other component: equal, as
DataComponentExactPredicatecompares it.
An empty rule (ItemStack.EMPTY, or air) matches nothing, and an empty
stack matches no rule: an empty picker slot is not a rule.
Common code: it reads only the stacks, so a mod's server side can use it too.
The record copies its stack; treat stack() as read-only. Two rules are
equal when their modes are, their stacks match exactly (ItemStack.matches(ItemStack, ItemStack)),
and they ignore the same.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumHow a stack is compared with a rule's. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final com.mojang.serialization.Codec<ItemMatch> Saves a rule as{stack: <vanilla's stack>, match: "exact"}, plusignoring: ["minecraft:damage"]andignore_count: truewhen the rule has them (left out when it does not, so a rule saved before they existed loads as it was). -
Constructor Summary
ConstructorsConstructorDescriptionItemMatch(net.minecraft.world.item.ItemStack stack, ItemMatch.Match match) A rule that compares every component, and inEXACTthe count.ItemMatch(net.minecraft.world.item.ItemStack stack, ItemMatch.Match match, Set<net.minecraft.core.component.DataComponentType<?>> ignored, boolean countIgnored) Creates an instance of aItemMatchrecord class. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns the value of thecountIgnoredrecord component.booleanIndicates whether some other object is "equal to" this one.inthashCode()Returns a hash code value for this object.Set<net.minecraft.core.component.DataComponentType<?>> ignored()Returns the value of theignoredrecord component.ignoring(net.minecraft.core.component.DataComponentType<?>... types) This rule, also leavingtypesout of the comparison inEXACTandCONTAINS: neither the rule's value for them nor the candidate's is looked at.This rule, withEXACTalso leaving the count out (the other modes never compare it).match()Returns the value of thematchrecord component.booleanmatches(net.minecraft.world.item.ItemStack candidate) Whethercandidatematches this rule.static booleanmatches(net.minecraft.world.item.ItemStack rule, ItemMatch.Match match, net.minecraft.world.item.ItemStack candidate) Whethercandidatematches the ruleruleundermatch: the comparisonItemMatchdescribes, for a consumer that stores a plain stack and declares the mode itself.net.minecraft.world.item.ItemStackstack()Returns the value of thestackrecord component.toString()Returns a string representation of this record class.
-
Field Details
-
CODEC
Saves a rule as{stack: <vanilla's stack>, match: "exact"}, plusignoring: ["minecraft:damage"]andignore_count: truewhen the rule has them (left out when it does not, so a rule saved before they existed loads as it was). The stack goes through vanilla'sItemStack.OPTIONAL_CODEC, so encode it with the world's registry ops (enchantments are registry entries), as vanilla saves a stack.
-
-
Constructor Details
-
Method Details
-
ignoring
This rule, also leavingtypesout of the comparison inEXACTandCONTAINS: neither the rule's value for them nor the candidate's is looked at. Adds to any already ignored. -
ignoringCount
This rule, withEXACTalso leaving the count out (the other modes never compare it). -
matches
public boolean matches(net.minecraft.world.item.ItemStack candidate) Whethercandidatematches this rule. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
hashCode
-
toString
-
stack
-
match
-
ignored
-
countIgnored
public boolean countIgnored()Returns the value of thecountIgnoredrecord component.- Returns:
- the value of the
countIgnoredrecord component
-