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;
}
}