P

make me a plugin so that the dragon egg cannot be put in ender chest and chest or any type of chest it can only be in inventory also whoever has that egg can remove it by getting killed by a player or with a command name as /removedragonegg . whoever having that egg will be having buffs that he will get all the enchanted god apple potion effects for all the time untill he has the egg

Kodari AI
KodariJul 3 at 4:04 pm 2

I’ll create a Dragon Egg protection and buff plugin.

DragonEggGuard
Protects the Dragon Egg by preventing it from being stored in any container (chests, ender chests, shulker boxes, etc.) — it can only exist in a player's inventory. The egg holder receives permanent god apple potion effects (Absorption IV, Regeneration II, Resistance I, Fire Resistance I) for as long as they possess it. The egg is removed upon player kill, and can also be removed with /removedragonegg.

Now I need to fix the EggListener constructor — I added DragonEggGuard parameter but the main class only passes the material. Let me update the registration in the main class.

All files created. Summary:

DragonEggGuard — 3 classes, 1 plugin.yml, 1 build.gradle

  • EggListener: Prevents the Dragon Egg from entering any container via click/drag/shift-click (InventoryClickEvent, InventoryDragEvent). Prevents placing the egg on the ground (PlayerInteractEvent). Removes the egg from drops and inventory when the holder is killed by a player (PlayerDeathEvent).

  • RemoveDragonEggCommand (/removedragonegg): Removes the Dragon Egg from the executing player’s inventory and clears their god apple effects.

  • Effect Scheduler (main class): Every 2 seconds, applies Absorption IV, Regeneration II, Resistance I, and Fire Resistance I to any online player carrying the egg. Effects auto-expire 5 seconds after the egg leaves the inventory.

  • Clear mechanics: Both death-by-player and the command call clearEggEffects() to strip the god apple buffs immediately.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
5 229
src/main/java/com/dragoneggguard/DragonEggGuard.java
package com.dragoneggguard;

import com.cryptomorin.xseries.XMaterial;
import com.dragoneggguard.commands.RemoveDragonEggCommand;
import com.dragoneggguard.listeners.EggListener;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.potion.PotionEffect;
import org.bukkit.scheduler.BukkitRunnable;

public class DragonEggGuard extends JavaPlugin {

    private Material eggMaterial;

    @Override
    public void onEnable() {
        eggMaterial = XMaterial.matchXMaterial("DRAGON_EGG").map(XMaterial::parseMaterial).orElse(Material.AIR);

        getServer().getPluginManager().registerEvents(new EggListener(eggMaterial, this), this);
        getCommand("removedragonegg").setExecutor(new RemoveDragonEggCommand(eggMaterial, this));

        startEffectTask();

        getLogger().info("DragonEggGuard enabled.");
    }

    @Override
    public void onDisable() {
        getLogger().info("DragonEggGuard disabled.");
    }

    private void startEffectTask() {
        new BukkitRunnable() {
            @Override
            public void run() {
                for (Player player : getServer().getOnlinePlayers()) {
                    if (player.getInventory().contains(eggMaterial)) {
                        applyEffects(player);
                    }
                }
            }
        }.runTaskTimer(this, 0L, 40L);
    }

    private void applyEffects(Player player) {
        XPotion.matchXPotion("ABSORPTION").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 3)));
        XPotion.matchXPotion("REGENERATION").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 1)));
        XPotion.matchXPotion("DAMAGE_RESISTANCE").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 0)));
        XPotion.matchXPotion("FIRE_RESISTANCE").ifPresent(xp -> player.addPotionEffect(xp.buildPotionEffect(100, 0)));
    }

    public void clearEggEffects(Player player) {
        XPotion.matchXPotion("ABSORPTION").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
        XPotion.matchXPotion("REGENERATION").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
        XPotion.matchXPotion("DAMAGE_RESISTANCE").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
        XPotion.matchXPotion("FIRE_RESISTANCE").map(XPotion::getPotionEffectType).ifPresent(player::removePotionEffect);
    }
}