nato3623

i have this code: if (p.isInvulnerable()) { loc.getWorld().createExplosion(loc, ExpInt); } else { p.setInvulnerable(true); loc.getWorld().createExplosion(loc, ExpInt); p.setInvulnerable(false); }

But the .isInvulnerable() isn't available prior 1.10, i'd like to have a code that works with 1.8 till 1.21 so there are some alternatives?

Kodari AI
KodariApr 18 at 5:06 pm 1,902 (v2)

This is just a code question, no project needs to be created here. Let me give you a clean cross-version solution.


The Problem

isInvulnerable() / setInvulnerable() were added in 1.10. On 1.8/1.9 these methods don’t exist on the Entity API.


Best Solution — Event-Based Protection Set

Track protected players in a Set<UUID> and cancel their damage in a listener. Works 1.8 → 1.21.

Java
private final Set<UUID> explosionProtected = new HashSet<>();

// In your listener class, register this event:
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = false)
public void onDamage(EntityDamageEvent e) {
    if (e.getEntity() instanceof Player) {
        Player p = (Player) e.getEntity();
        if (explosionProtected.contains(p.getUniqueId())) {
            e.setCancelled(true);
        }
    }
}

// Replace your original code with this:
private void safeExplosion(Player p, Location loc, float power) {
    boolean alreadyProtected = explosionProtected.contains(p.getUniqueId());
    if (!alreadyProtected) {
        explosionProtected.add(p.getUniqueId());
    }

    loc.getWorld().createExplosion(loc, power);

    if (!alreadyProtected) {
        // Remove on the next tick so all synchronous damage events from this explosion are caught
        Bukkit.getScheduler().runTaskLater(plugin, () -> {
            explosionProtected.remove(p.getUniqueId());
        }, 1L);
    }
}

Why Not Reflection?

You could reflect into setInvulnerable, but it’s messy, fragile, and unnecessary when an event listener is cleaner and more reliable.


Why Not GameMode Switch?

Temporarily switching to CREATIVE works but has bad side effects — it triggers PlayerGameModeChangeEvent, other plugins may react to it, and it can cause visual glitches client-side.


Summary of Options

Approach
1.8 Support
Clean
Side Effects
setInvulnerable()
❌
✅
None
Event protection set
✅
✅
None
GameMode switch
✅
❌
Visual glitch, event spam
Reflection
✅
❌
Fragile

The event protection set is the recommended approach.

nato3623

k thanks, next, i know that there is a way to stop the end portal activation

Kodari AI
KodariApr 18 at 6:53 pm 3,645 (v2)

I’ll create a lightweight plugin that prevents end portal activation.

EndPortalBlocker
A lightweight Spigot plugin that prevents players from activating End Portals by placing Eyes of Ender into portal frames. Supports per-world configuration to selectively block portal activation. Includes a permission node to bypass the restriction and configurable denial messages. Compatible with Minecraft 1.8 through 1.21.
  • Listen for PlayerInteractEvent when right-clicking an END_PORTAL_FRAME with an Eye of Ender

  • Per-world toggle in config, bypass permission, configurable message

EndPortalBlocker
A lightweight Spigot plugin that prevents players from activating End Portals by placing Eyes of Ender into portal frames. Supports per-world configuration to selectively block portal activation. Includes a permission node to bypass the restriction and configurable denial messages. Compatible with Minecraft 1.8 through 1.21.

Plugin created with:

  • PortalListener cancels right-clicking END_PORTAL_FRAME with ENDER_EYE in any world listed under blocked-worlds in config

  • endportalblocker.bypass permission skips the block entirely

  • /endportalblocker reload reloads the config (requires endportalblocker.reload)

  • Works 1.8–1.21 with no version-specific APIs

Kodari can make mistakes. Review important code before using.
Files
src
pom.xml
Code
6 180
src/main/java/com/endportalblocker/EndPortalBlocker.java
package com.endportalblocker;

import org.bukkit.plugin.java.JavaPlugin;

public class EndPortalBlocker extends JavaPlugin {

    private static EndPortalBlocker instance;

    @Override
    public void onEnable() {
        instance = this;
        saveDefaultConfig();
        getServer().getPluginManager().registerEvents(new PortalListener(this), this);
        getCommand("endportalblocker").setExecutor(new ReloadCommand(this));
        getLogger().info("EndPortalBlocker enabled.");
    }

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

    public static EndPortalBlocker getInstance() {
        return instance;
    }
}