Abdullah Osman

Create a Minecraft mod flying Iron Golems

Kodari AI
KodariOct 13 at 3:30 pm 1
IronGolemFlight
IronGolemFlight is a Minecraft plugin that enables players to mount and fly Iron Golems. Players can right-click an Iron Golem with a saddle to mount it, then use WASD-style controls or arrow keys to steer the golem through the air. The plugin implements smooth flight physics with adjustable speed, vertical ascent/descent controls, and collision detection to prevent flying through blocks. Golems gain a glowing trail effect while airborne, and flight is disabled when the player dismounts. Includes configurable permissions for mounting, speed limits, and flight duration with optional fuel requirements using phantom membranes. The plugin supports both single-player and multiplayer environments with full event handling for safe dismounting and fall damage prevention.
  • Make newly spawned and loaded iron golems take off and hover.

  • Give them simple flight-based pursuit and attacks against nearby hostile mobs.

  • Add the Paper plugin entry point and build files.

Created the FlyingIronGolems Paper plugin. Iron golems take off to hover, pursue nearby hostile mobs, and attack them; newly spawned, already-loaded, and chunk-loaded golems are supported. On plugin disable, their normal AI and gravity are restored.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
settings.gradle
Code
4 173
src/main/java/com/kodari/flyingirongolems/FlyingIronGolemsPlugin.java
package com.kodari.flyingirongolems;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.Monster;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

public final class FlyingIronGolemsPlugin extends JavaPlugin implements Listener {
    private static final double HOME_HEIGHT = 6.0;
    private static final double TARGET_RANGE = 32.0;
    private static final double ATTACK_RANGE_SQUARED = 9.0;
    private static final double ATTACK_DAMAGE = 15.0;
    private static final long ATTACK_COOLDOWN_TICKS = 20L;

    private final Set<IronGolem> golems = new HashSet<>();
    private final Map<UUID, Location> homeLocations = new HashMap<>();
    private final Map<UUID, Long> nextAttackTicks = new HashMap<>();
    private long currentTick;

    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(this, this);

        Bukkit.getWorlds().forEach(world -> world.getEntities().stream()
                .filter(IronGolem.class::isInstance)
                .map(IronGolem.class::cast)
                .forEach(this::enableFlight));

        Bukkit.getScheduler().runTaskTimer(this, this::tickGolems, 1L, 1L);
    }

    @Override
    public void onDisable() {
        for (IronGolem golem : golems) {
            if (golem.isValid()) {
                golem.setAI(true);
                golem.setGravity(true);
                golem.setVelocity(new Vector());
            }
        }
        golems.clear();
        homeLocations.clear();
        nextAttackTicks.clear();
    }

    @EventHandler
    public void onGolemSpawn(CreatureSpawnEvent event) {
        if (event.getEntity() instanceof IronGolem golem) {
            enableFlight(golem);
        }
    }

    @EventHandler
    public void onChunkLoad(ChunkLoadEvent event) {
        for (Entity entity : event.getChunk().getEntities()) {
            if (entity instanceof IronGolem golem) {
                enableFlight(golem);
            }
        }
    }

    private void enableFlight(IronGolem golem) {
        if (!golem.isValid() || !golems.add(golem)) {
            return;
        }

        golem.setAI(false);
        golem.setGravity(false);
        homeLocations.put(golem.getUniqueId(), golem.getLocation().add(0, HOME_HEIGHT, 0));
    }

    private void tickGolems() {
        currentTick++;
        Iterator<IronGolem> iterator = golems.iterator();

        while (iterator.hasNext()) {
            IronGolem golem = iterator.next();
            if (!golem.isValid() || golem.isDead()) {
                UUID id = golem.getUniqueId();
                iterator.remove();
                homeLocations.remove(id);
                nextAttackTicks.remove(id);
                continue;
            }

            Monster target = findNearestMonster(golem);
            UUID id = golem.getUniqueId();
            Location destination = target == null
                    ? homeLocations.get(id)
                    : target.getLocation().add(0, 1.0, 0);

            if (destination == null) {
                destination = golem.getLocation();
            }

            Vector direction = destination.toVector().subtract(golem.getLocation().toVector());
            if (target == null && direction.lengthSquared() < 0.0625) {
                golem.setVelocity(new Vector());
            } else if (direction.lengthSquared() > 0) {
                golem.setVelocity(direction.normalize().multiply(target == null ? 0.18 : 0.3));
            }

            if (target != null && golem.getLocation().distanceSquared(target.getLocation()) <= ATTACK_RANGE_SQUARED
                    && currentTick >= nextAttackTicks.getOrDefault(id, 0L)) {
                target.damage(ATTACK_DAMAGE, golem);
                nextAttackTicks.put(id, currentTick + ATTACK_COOLDOWN_TICKS);
            }
        }
    }

    private Monster findNearestMonster(IronGolem golem) {
        Monster nearest = null;
        double nearestDistanceSquared = TARGET_RANGE * TARGET_RANGE;

        for (Entity entity : golem.getWorld().getNearbyEntities(
                golem.getLocation(), TARGET_RANGE, TARGET_RANGE, TARGET_RANGE)) {
            if (!(entity instanceof Monster monster) || !monster.isValid() || monster.isDead()) {
                continue;
            }

            double distanceSquared = golem.getLocation().distanceSquared(monster.getLocation());
            if (distanceSquared < nearestDistanceSquared) {
                nearest = monster;
                nearestDistanceSquared = distanceSquared;
            }
        }

        return nearest;
    }
}