Class EnchantRandomlyFunction

java.lang.Object
com.github.darksoulq.abyssallib.world.data.loot.LootFunction
com.github.darksoulq.abyssallib.world.data.loot.function.EnchantRandomlyFunction

public class EnchantRandomlyFunction extends LootFunction
A loot function that applies a random enchantment to the generated item.

If a list of enchantments is provided, the function will pick one at random from that list. If the list is empty, it will pick from all enchantments registered in the game that are compatible with the item being enchanted.

  • Field Details

  • Constructor Details

    • EnchantRandomlyFunction

      public EnchantRandomlyFunction(List<String> enchantments)
      Constructs a new EnchantRandomlyFunction.
      Parameters:
      enchantments - A list of NamespacedKey strings representing valid enchantments.
  • Method Details

    • apply

      public org.bukkit.inventory.ItemStack apply(org.bukkit.inventory.ItemStack stack, LootContext context)
      Applies a random enchantment to the provided ItemStack.

      This method handles both standard items (using DataComponentTypes.ENCHANTMENTS) and enchanted books (using DataComponentTypes.STORED_ENCHANTMENTS). It randomly selects a level between 1 and the enchantment's max level.

      Specified by:
      apply in class LootFunction
      Parameters:
      stack - The ItemStack generated by the loot pool.
      context - The LootContext providing the random source and environmental data.
      Returns:
      The modified ItemStack with a new enchantment applied.
    • getType

      public LootFunctionType<?> getType()
      Description copied from class: LootFunction
      Retrieves the specific type definition associated with this function.
      Specified by:
      getType in class LootFunction
      Returns:
      The LootFunctionType associated with this function.