Skip to content

D | Creating Custom Foods

Sartaj Singh edited this page May 31, 2025 · 2 revisions

D | Creating Custom Foods


Table of Contents


1. Overview

FXItems lets you create custom edible items (foods) with unique hunger/saturation, advanced effects, custom models, and more.
Foods are defined using FXFoodDefinition and custom logic is added with FXFoodBehavior.


2. FXFoodDefinition: All Options & Parameters

Constructor

public FXFoodDefinition(
    String id,
    String displayName,
    Material material,
    int hunger,
    float saturation,
    boolean alwaysEdible,
    Integer customModelData,
    List<String> lore
)
Parameter Type Required Description
id String Yes Unique identifier for the food. Lowercase, no spaces recommended.
displayName String Yes Name shown in-game. Supports § color codes.
material Material Yes Minecraft material (Material.APPLE, etc.).
hunger int Yes How many hunger bars restored (vanilla: 1 = half a drumstick).
saturation float Yes Saturation modifier for the food.
alwaysEdible boolean No If true, can be eaten even at full hunger.
customModelData Integer (nullable) No For resource-pack custom models. Null for none.
lore List No Tooltip text. Color codes supported.

Notes:

  • You can omit customModelData and lore for simple foods.
  • alwaysEdible defaults to false if not set.

3. FXFoodBehavior: Custom Food Logic

Custom logic for foods (effects, abilities, events, etc.) is implemented in your own class that implements FXFoodBehavior.

Example Interface

public interface FXFoodBehavior {
    default void onEat(Player player, ItemStack food, FXFoodDefinition def) {}
    // Add more hooks as FXItems evolves!
}

Example Implementation

public class EnchantedAppleBehavior implements FXFoodBehavior {
    @Override
    public void onEat(Player player, ItemStack food, FXFoodDefinition def) {
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 200, 1));
        player.sendMessage("§dYou feel magically restored!");
    }
}
  • Only onEat is required for basic effects.
  • Extend with your own methods for more advanced effects (e.g., onConsume, onHeld, etc.).

4. Registering Your Food

4.1 Manual Registration

Registry registry = new RegistryAPI();
registry.registerFood(
    "enchanted_apple",
    new FXFoodDefinition(
        "enchanted_apple",
        "§dEnchanted Apple",
        Material.APPLE,
        8,        // hunger (4 drumsticks)
        12.8f,    // saturation
        true,     // alwaysEdible
        3,        // customModelData
        Arrays.asList("§7A magical apple.", "§bRestores health and grants buffs!")
    ),
    new EnchantedAppleBehavior()
);
  • Register in your plugin's onEnable() or a setup method.
  • You can manually create as many foods as you want, each with its own logic and appearance.

4.2 Auto-Registration

For large projects, auto-registration is recommended.

  1. Annotate your behavior class:

    @AutoRegister
    public class EnchantedAppleBehavior implements FXFoodBehavior { ... }
  2. In your plugin's main class:

    BehaviorAutoRegistrar.registerAll(this, "your.base.package");
  • All annotated behaviors in the package are discovered and registered.

5. Advanced Features

  • Custom Models: Use customModelData for resource pack integration.
  • Advanced Effects: Use onEat to apply any potion effect, run commands, or trigger custom events.
  • Lore: Add detailed lore for flavor and player guidance.
  • Always Edible: Useful for foods that should act like golden apples.
  • Synergy with Items: Foods can trigger item-based effects or interact with FXItems items (e.g., grant items on eat).

6. Real-World Examples

Basic Custom Food

FXFoodDefinition berryPie = new FXFoodDefinition(
    "berry_pie",
    "§dBerry Pie",
    Material.PUMPKIN_PIE,
    6,      // hunger
    9.6f,   // saturation
    false,  // alwaysEdible
    null,   // customModelData
    Arrays.asList("§7A delicious berry pie.")
);

registry.registerFood("berry_pie", berryPie, new FXFoodBehavior() {
    @Override
    public void onEat(Player player, ItemStack food, FXFoodDefinition def) {
        player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 120, 0));
        player.sendMessage("§aYou feel energized!");
    }
});

Food With Custom Model and Advanced Effects

FXFoodDefinition manaFruit = new FXFoodDefinition(
    "mana_fruit",
    "§9Mana Fruit",
    Material.APPLE,
    3,
    6f,
    true,
    7, // customModelData
    Arrays.asList("§7A fruit brimming with arcane energy.")
);

registry.registerFood("mana_fruit", manaFruit, new FXFoodBehavior() {
    @Override
    public void onEat(Player player, ItemStack food, FXFoodDefinition def) {
        // Use ManaUtils to recharge player's mana (see utility page)
        Utils utils = new UtilsAPI();
        utils.addMana(player, 50);
        player.sendMessage("§9Your mana is replenished!");
    }
});

7. Troubleshooting & FAQ

  • "My food isn't working":

    • Double-check your ID and registration code.
    • Make sure your behavior class is correct and implements FXFoodBehavior.
  • "No effect on eat":

    • Confirm your onEat is implemented and registered.
    • Check for errors in the server log.
  • "Custom model not showing":

    • Make sure your resource pack is loaded and matches the customModelData value.
  • "Can I make a food give items or trigger commands?"

    • Yes! In onEat, you can use Bukkit/Spigot API to do anything: give items, run commands, etc.

8. See Also


Clone this wiki locally