Skip to content

D | Creating Custom Foods

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

Creating Custom Foods

FXItems supports custom food items with unique effects, hunger, saturation, and recipes.


Step 1: Create the Food Class

Recommended location: com.noctify.Custom.Foods

Example: MagicApple

public class MagicApple {
    // Required: item definition
    public static ItemStack createItem() {
        ItemStack apple = new ItemStack(Material.APPLE);
        ItemMeta meta = apple.getItemMeta();
        meta.setDisplayName("§bMagic Apple");
        meta.setLore(List.of("§7Restores health and grants power!"));
        apple.setItemMeta(meta);
        return apple;
    }
    // Required: food values
    public static int getHungerPoints() { return 6; }
    public static float getSaturationPoints() { return 9.6f; }
    // Required: potion effects
    public static List<PotionEffect> getEffects() {
        return List.of(
           new PotionEffect(PotionEffectType.REGENERATION, 200, 1),
           new PotionEffect(PotionEffectType.ABSORPTION, 400, 1)
        );
    }
    // Optional: Custom shaped recipe
    public static ShapedRecipe getRecipe(Plugin plugin, NamespacedKey key) {
        ShapedRecipe recipe = new ShapedRecipe(key, createItem());
        recipe.shape("GGG", "GAG", "GGG");
        recipe.setIngredient('G', Material.GOLD_INGOT);
        recipe.setIngredient('A', Material.APPLE);
        return recipe;
    }
}

Step 2: Register the Food

In FoodRegistry:

registerFood(plugin, "MagicApple", MagicApple.class, null);

The fourth parameter is for a custom behavior class (see below).


Step 3: Custom Eating Behavior

If you want special logic (for example, play a sound or trigger lightning when eaten), write a Listener:

public class MagicAppleBehavior implements Listener {
    public MagicAppleBehavior(Plugin plugin) {}
    @EventHandler
    public void onEat(PlayerItemConsumeEvent event) {
        if (!CustomItemUtils.isCustomItem(event.getItem(), Material.APPLE, "§bMagic Apple")) return;
        Player player = event.getPlayer();
        player.getWorld().strikeLightningEffect(player.getLocation());
    }
}

Register using:

registerFood(plugin, "MagicApple", MagicApple.class, MagicAppleBehavior.class);

Step 4: Crafting & Testing

  1. Register your food. Reload server.
  2. Craft using the recipe or give with /fxgive <player> MagicApple.
  3. Eat and confirm hunger/saturation/effects and custom behavior.

Advanced: Custom Effects

You can add any number of potion effects, play sounds, spawn particles, etc., in your behavior class.


Full Checklist for Custom Foods

  • Food class with createItem(), getHungerPoints(), getSaturationPoints(), getEffects()
  • Optional: getRecipe()
  • Register in FoodRegistry
  • Optional: Custom eating behavior in OtherBehaviors package

Next: Creating Custom Commands (Step-by-Step)

Clone this wiki locally