Files
src
build.gradle
Code
4 118
src/main/java/com/kodari/daniella/DaniellaPlugin.java
package com.kodari.daniella;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Husk;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class DaniellaPlugin extends JavaPlugin {
private static final String DANIella_TAG = "kodari_daniella";
@Override
public void onEnable() {
saveDefaultConfig();
long intervalTicks = Math.max(1L, getConfig().getLong("spawn-interval-seconds", 45L)) * 20L;
Bukkit.getScheduler().runTaskTimer(this, this::trySpawnDaniella, intervalTicks, intervalTicks);
}
private void trySpawnDaniella() {
Player[] players = Bukkit.getOnlinePlayers().toArray(new Player[0]);
if (players.length == 0) {
return;
}
Player target = players[ThreadLocalRandom.current().nextInt(players.length)];
World world = target.getWorld();
int maxPerWorld = getConfig().getInt("max-per-world", 3);
if (maxPerWorld <= 0 || countDaniellas(world) >= maxPerWorld) {
return;
}
int minimumDistance = Math.max(1, getConfig().getInt("minimum-distance", 24));
int maximumDistance = Math.max(minimumDistance, getConfig().getInt("maximum-distance", 64));
int attempts = Math.max(1, getConfig().getInt("spawn-attempts", 12));
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int attempt = 0; attempt < attempts; attempt++) {
double angle = random.nextDouble(0.0, Math.PI * 2.0);
double distance = random.nextDouble(minimumDistance, (double) maximumDistance + 1.0);
int x = (int) Math.floor(target.getLocation().getX() + Math.cos(angle) * distance);
int z = (int) Math.floor(target.getLocation().getZ() + Math.sin(angle) * distance);
int y = world.getHighestBlockYAt(x, z);
Location spawnLocation = new Location(world, x + 0.5, y + 1.0, z + 0.5);
if (!world.getWorldBorder().isInside(spawnLocation) || !isSafeSpawnLocation(world, x, y, z)) {
continue;
}
Husk daniella = (Husk) world.spawnEntity(spawnLocation, EntityType.HUSK);
daniella.setCustomName("Daniella");
daniella.setCustomNameVisible(true);
daniella.setPersistent(true);
daniella.setCanPickupItems(false);
daniella.addScoreboardTag(DANIella_TAG);
((Monster) daniella).setTarget(target);
return;
}
}
private boolean isSafeSpawnLocation(World world, int x, int y, int z) {
Block ground = world.getBlockAt(x, y, z);
Block feet = world.getBlockAt(x, y + 1, z);
Block head = world.getBlockAt(x, y + 2, z);
return ground.getType().isSolid() && !ground.isLiquid() && feet.isPassable() && head.isPassable();
}
private long countDaniellas(World world) {
return world.getEntities().stream()
.filter(entity -> entity.getType() == EntityType.HUSK)
.filter(Entity::isPersistent)
.filter(entity -> entity.getScoreboardTags().contains(DANIella_TAG))
.count();
}
}