Skip to content

Enchant CMatter Implementations

The following three classes are provided as CMatter implementations for inspecting enchantments.

ClassTarget
CEnchantMatterImplEnchantments directly applied to an item
CEnchantmentStoreMatterImplEnchantments stored in an item (primarily enchanted books)
CEnchantBothMatterImplBoth directly applied and stored enchantments

None of these are subclasses of CMatterImpl; each is an independent class implementing the CEnchantMatter / CEnchantmentStoreMatter interfaces respectively. They are defined as open class, so they can be extended through inheritance.


open class CEnchantMatterImpl @JvmOverloads constructor(
override val name: String,
override val candidate: Set<Material>,
override val enchantComponents: Set<CEnchantComponent>,
override val amount: Int = 1,
override val mass: Boolean = false,
override val predicates: Set<CMatterPredicate>? = CMatterImpl.defaultMatterPredicates()
) : CEnchantMatter
ParameterDefaultDescription
nameIdentifier name for the CMatter
candidateThe set of item types that are accepted
enchantComponentsThe set of enchantment conditions required
amount1Minimum number of items required
massfalseWhen true, excluded from quantity calculations
predicatesdefaultMatterPredicates()Additional validation logic set

CEnchantMatterImpl.DEFAULT_ENCHANT_CHECKER is the default CMatterPredicate that inspects enchantments directly applied to an item. Because it is included in CMatterImpl.defaultMatterPredicates(), it becomes active automatically when predicates is omitted.

Processing flow:

  1. If the input item is Air, return true
  2. If CMatter cannot be cast to CEnchantMatter, return true (coexistence with other CMatter types)
  3. If enchantComponents is empty, return true
  4. If the item has no enchantments at all, return false
  5. Verify that all CEnchantComponent entries in enchantComponents pass enchantBaseCheck()

enchantBaseCheck(enchants, required) is an internal utility function that is also reused by CEnchantmentStoreMatterImpl.

internal fun enchantBaseCheck(
enchants: Map<Enchantment, Int>,
required: CEnchantComponent
): Boolean {
return when (required.strict) {
CEnchantComponent.Strict.ONLY_ENCHANT -> enchants.containsKey(required.enchantment)
CEnchantComponent.Strict.STRICT -> {
enchants.getOrDefault(required.enchantment, -1) == required.level
}
}
}

With ONLY_ENCHANT, only the type of enchantment is checked and the level does not matter. With STRICT, both the enchantment type and level must match exactly.

// Diamond pickaxe with Efficiency (any level)
val efficientPickaxe = CEnchantMatterImpl(
name = "efficient-pickaxe",
candidate = setOf(Material.DIAMOND_PICKAXE),
enchantComponents = setOf(
CEnchantComponent(
level = 1,
enchantment = Enchantment.EFFICIENCY,
strict = CEnchantComponent.Strict.ONLY_ENCHANT
)
)
)
// Diamond pickaxe with Unbreaking III (exactly III)
val durablePickaxe = CEnchantMatterImpl(
name = "durable-pickaxe",
candidate = setOf(Material.DIAMOND_PICKAXE),
enchantComponents = setOf(
CEnchantComponent(
level = 3,
enchantment = Enchantment.UNBREAKING,
strict = CEnchantComponent.Strict.STRICT
)
)
)
// Multiple enchantment conditions (all conditions must be satisfied)
val powerfulSword = CEnchantMatterImpl(
name = "powerful-sword",
candidate = setOf(Material.DIAMOND_SWORD),
enchantComponents = setOf(
CEnchantComponent(1, Enchantment.SHARPNESS, CEnchantComponent.Strict.ONLY_ENCHANT),
CEnchantComponent(3, Enchantment.UNBREAKING, CEnchantComponent.Strict.STRICT)
)
)

open class CEnchantmentStoreMatterImpl @JvmOverloads constructor(
override val name: String,
override val candidate: Set<Material>,
override val storedEnchantComponents: Set<CEnchantComponent>,
override val amount: Int = 1,
override val mass: Boolean = false,
override val predicates: Set<CMatterPredicate>? = CMatterImpl.defaultMatterPredicates()
) : CEnchantmentStoreMatter

This is structurally symmetric to CEnchantMatterImpl, but differs in that the field name is storedEnchantComponents and it inspects stored enchantments held by EnchantmentStorageMeta rather than enchantments directly applied to the item.

CEnchantmentStoreMatterImpl.DEFAULT_ENCHANT_STORE_CHECKER is the default CMatterPredicate that inspects stored enchantments. It is included in CMatterImpl.defaultMatterPredicates().

Processing flow:

  1. If the input item is Air, return true
  2. If CMatter cannot be cast to CEnchantmentStoreMatter, return true
  3. If storedEnchantComponents is empty, return true
  4. If the item’s meta is not EnchantmentStorageMeta, return false
  5. Retrieve storedEnchants and verify that all storedEnchantComponents pass enchantBaseCheck()
// Enchanted book storing Efficiency III (exactly III)
val efficiencyBook = CEnchantmentStoreMatterImpl(
name = "efficiency-book",
candidate = setOf(Material.ENCHANTED_BOOK),
storedEnchantComponents = setOf(
CEnchantComponent(
level = 3,
enchantment = Enchantment.EFFICIENCY,
strict = CEnchantComponent.Strict.STRICT
)
),
mass = true // Non-stackable like buckets, so mass = true is appropriate
)
// Enchanted book storing Sharpness (any level)
val sharpnessBook = CEnchantmentStoreMatterImpl(
name = "sharpness-book",
candidate = setOf(Material.ENCHANTED_BOOK),
storedEnchantComponents = setOf(
CEnchantComponent(
level = 1,
enchantment = Enchantment.SHARPNESS,
strict = CEnchantComponent.Strict.ONLY_ENCHANT
)
)
)

open class CEnchantBothMatterImpl @JvmOverloads constructor(
override val name: String,
override val candidate: Set<Material>,
override val enchantComponents: Set<CEnchantComponent>,
override val storedEnchantComponents: Set<CEnchantComponent>,
override val amount: Int = 1,
override val mass: Boolean = false,
override val predicates: Set<CMatterPredicate>? = CMatterImpl.defaultMatterPredicates()
) : CEnchantMatter, CEnchantmentStoreMatter

This implements both CEnchantMatter and CEnchantmentStoreMatter. Because both DEFAULT_ENCHANT_CHECKER and DEFAULT_ENCHANT_STORE_CHECKER are included in defaultMatterPredicates(), both directly applied and stored enchantments are checked together.

// Diamond sword or enchanted book:
// if a sword, checks for Sharpness applied directly; if an enchanted book, checks for stored Sharpness
val sharpnessMatter = CEnchantBothMatterImpl(
name = "sharpness-matter",
candidate = setOf(Material.DIAMOND_SWORD, Material.ENCHANTED_BOOK),
enchantComponents = setOf(
CEnchantComponent(1, Enchantment.SHARPNESS, CEnchantComponent.Strict.ONLY_ENCHANT)
),
storedEnchantComponents = setOf(
CEnchantComponent(1, Enchantment.SHARPNESS, CEnchantComponent.Strict.ONLY_ENCHANT)
)
)

FieldTypeDescription
levelIntThe required enchantment level
enchantmentEnchantmentThe required enchantment type
strictCEnchantComponent.StrictThe strictness of the check
Strict valueDescription
ONLY_ENCHANTThe type only needs to match (level is ignored)
STRICTBoth the type and level must match exactly