Skip to content

CMatterImpl

CMatterImpl is the standard implementation class for the CMatter interface. Because it is defined as an open class, you can also create extended subclasses that add custom fields by inheriting from it.


open class CMatterImpl @JvmOverloads constructor(
override val name: String,
override val candidate: Set<Material>,
override val amount: Int = 1,
override val mass: Boolean = false,
override val predicates: Set<CMatterPredicate>? = defaultMatterPredicates()
) : CMatter
ParameterDefaultDescription
nameIdentifier name for the CMatter. Duplicates are allowed but unique names are recommended
candidateThe set of item types that are accepted
amount1Minimum number of items required
massfalseWhen true, passes with 1 or more items regardless of count, and is excluded from bulk-crafting quantity calculations
predicatesdefaultMatterPredicates()Additional validation logic set. When omitted, the default checkers for enchantments, stored enchantments, and potions are applied

Instead of calling the constructor directly, you can use the following factory methods for more concise code. All of them call isValidMatter() internally to perform validation.

Accepts multiple Material values and returns a CMatterImpl with them as the candidate. The name is a string formed by joining each Material name with -. Created with amount = 1, mass = false, and predicates = null.

// A CMatter that accepts stone or cobblestone
val stones = CMatterImpl.of(Material.STONE, Material.COBBLESTONE)
// Use a Tag to make all log variants candidates
val log = CMatterImpl.of(*Tag.LOGS.getValues().toTypedArray())

single(material) / multi(vararg materials)

Section titled “single(material) / multi(vararg materials)”

Both are aliases for of(). They are intended to be used interchangeably for readability.

val stone = CMatterImpl.single(Material.STONE) // 1 type
val stones = CMatterImpl.multi(Material.STONE, Material.COBBLESTONE) // multiple types

CMatterImpl.defaultMatterPredicates() returns a Set<CMatterPredicate> containing the three default checkers for enchantments, stored enchantments, and potions.

val predicates: Set<CMatterPredicate> = CMatterImpl.defaultMatterPredicates()
// = setOf(
// CEnchantMatterImpl.DEFAULT_ENCHANT_CHECKER,
// CEnchantmentStoreMatterImpl.DEFAULT_ENCHANT_STORE_CHECKER,
// CPotionMatterImpl.DEFAULT_POTION_CHECKER
// )

By passing this to the constructor argument of a subclass, you can manually build the same predicates set as the derived classes.


// Accept only stone (predicates = null)
val stone = CMatterImpl.of(Material.STONE)
// Require 8 or more stone
val stoneStack = CMatterImpl(
name = "stone-stack",
candidate = setOf(Material.STONE),
amount = 8
)
// Exclude lava bucket from quantity calculations
val lavaBucket = CMatterImpl(
name = "lava-bucket",
candidate = setOf(Material.LAVA_BUCKET),
mass = true
)

When you want to add custom validation on top of the default predicates, merge them with the result of defaultMatterPredicates().

val myChecker = CMatterPredicate { ctx ->
// Custom condition: the item's custom name must be "Special Stone"
ctx.input.itemMeta?.displayName()?.let { name ->
name == Component.text("Special Stone")
} ?: false
}
val specialStone = CMatterImpl(
name = "special-stone",
candidate = setOf(Material.STONE),
predicates = CMatterImpl.defaultMatterPredicates() + myChecker
)
// Example: inspect the custom model data of an item
class CustomModelMatter(
name: String,
candidate: Set<Material>,
val customModelData: Int
) : CMatterImpl(
name = name,
candidate = candidate,
predicates = CMatterImpl.defaultMatterPredicates() + CMatterPredicate { ctx ->
ctx.input.itemMeta?.hasCustomModelData() == true
&& ctx.input.itemMeta.customModelData == customModelData
}
)