Files
src
build.gradle
settings.gradle
Code
5 227
src/main/java/com/kodari/dragonlairs/DragonLairs.java
package com.kodari.dragonlairs;
import com.cryptomorin.xseries.XAttribute;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.world.ChunkPopulateEvent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.Locale;
import java.util.Random;
public final class DragonLairs extends JavaPlugin implements Listener {
private NamespacedKey dragonKey;
@Override
public void onEnable() {
saveDefaultConfig();
dragonKey = new NamespacedKey(this, "dragon_type");
getServer().getPluginManager().registerEvents(this, this);
long interval = Math.max(20L, getConfig().getLong("dragon-ability-interval-ticks", 100L));
getServer().getScheduler().runTaskTimer(this, this::useAbilities, interval, interval);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can use this command.");
return true;
}
Player player = (Player) sender;
if (args.length != 1) return false;
String type = args[0].toLowerCase(Locale.ROOT);
if (type.equals("lair")) {
createLair(player.getLocation().getBlock().getLocation(), new Random());
player.sendMessage(ChatColor.GREEN + "Created a dragon lair entrance.");
return true;
}
if (!type.equals("fire") && !type.equals("ice") && !type.equals("storm") && !type.equals("queen")) return false;
spawnDragon(player.getLocation().add(0, 8, 0), type);
player.sendMessage(ChatColor.GREEN + "Spawned a " + type + " dragon.");
return true;
}
private void spawnDragon(Location location, String type) {
EnderDragon dragon = location.getWorld().spawn(location, EnderDragon.class);
dragon.getPersistentDataContainer().set(dragonKey, PersistentDataType.STRING, type);
dragon.setCustomName(ChatColor.DARK_PURPLE + (type.equals("queen") ? "Queen Dragon" : Character.toUpperCase(type.charAt(0)) + type.substring(1) + " Dragon"));
dragon.setCustomNameVisible(true);
dragon.setRemoveWhenFarAway(false);
double health = type.equals("queen") ? 6000.0 : 300.0;
XAttribute.of("max_health").ifPresent(attribute -> {
AttributeInstance instance = dragon.getAttribute(attribute.get());
if (instance != null) {
instance.setBaseValue(health);
dragon.setHealth(Math.min(health, instance.getValue()));
}
});
}
@EventHandler
public void onDamage(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof EnderDragon)) return;
EnderDragon dragon = (EnderDragon) event.getDamager();
if ("queen".equals(dragon.getPersistentDataContainer().get(dragonKey, PersistentDataType.STRING))) {
event.setDamage(15.0);
}
}
@EventHandler
public void onDragonExplosion(EntityExplodeEvent event) {
if (event.getEntity() instanceof EnderDragon && event.getEntity().getPersistentDataContainer().has(dragonKey, PersistentDataType.STRING)) {
event.blockList().clear();
}
}
private void useAbilities() {
for (World world : getServer().getWorlds()) {
for (EnderDragon dragon : world.getEntitiesByClass(EnderDragon.class)) {
String type = dragon.getPersistentDataContainer().get(dragonKey, PersistentDataType.STRING);
if (type == null || dragon.isDead()) continue;
Player target = null;
double best = 24 * 24;
for (Player player : world.getPlayers()) {
if (player.isDead() || !player.getGameMode().equals(org.bukkit.GameMode.SURVIVAL) && !player.getGameMode().equals(org.bukkit.GameMode.ADVENTURE)) continue;
double distance = player.getLocation().distanceSquared(dragon.getLocation());
if (distance < best) {
best = distance;
target = player;
}
}
if (target == null) continue;
if (type.equals("storm")) {
world.strikeLightningEffect(target.getLocation());
target.damage(8.0, dragon);
} else {
breathe(dragon, target, type.equals("ice"));
}
}
}
}
private void breathe(EnderDragon dragon, Player target, boolean ice) {
Location start = dragon.getLocation().add(0, 2, 0);
Location end = target.getEyeLocation();
Vector direction = end.toVector().subtract(start.toVector());
int steps = Math.max(1, (int) (direction.length() * 2));
direction.multiply(1.0 / steps);
XParticle.of(ice ? "SNOWFLAKE" : "FLAME").ifPresent(particle -> {
for (int i = 0; i <= steps; i++) {
Location point = start.clone().add(direction.clone().multiply(i));
if (point.getBlock().getType().isSolid()) break;
dragon.getWorld().spawnParticle(particle.get(), point, 3, 0.25, 0.25, 0.25, 0.01);
}
});
if (!target.hasLineOfSight(dragon)) return;
target.damage(ice ? 6.0 : 8.0, dragon);
if (ice) {
XPotion.matchXPotion("SLOWNESS").map(potion -> potion.buildPotionEffect(100, 1)).ifPresent(target::addPotionEffect);
target.setFreezeTicks(Math.max(target.getFreezeTicks(), 100));
} else {
target.setFireTicks(Math.max(target.getFireTicks(), 100));
}
}
@EventHandler
public void onChunkPopulate(ChunkPopulateEvent event) {
World world = event.getWorld();
if (!getConfig().getBoolean("lair-generation.enabled", true) || world.getEnvironment() != World.Environment.NORMAL) return;
long seed = world.getSeed() ^ ((long) event.getChunk().getX() * 341873128712L) ^ ((long) event.getChunk().getZ() * 132897987541L);
Random random = new Random(seed);
double chance = Math.max(0.0, Math.min(1.0, getConfig().getDouble("lair-generation.chance-per-chunk", 0.001)));
if (random.nextDouble() >= chance) return;
int x = event.getChunk().getX() * 16 + 8;
int z = event.getChunk().getZ() * 16 + 8;
int y = world.getHighestBlockYAt(x, z);
if (y < world.getMinHeight() + 8 || y + 5 >= world.getMaxHeight()) return;
Block surface = world.getBlockAt(x, y, z);
if (!surface.getType().isSolid()) return;
createLair(surface.getLocation().add(0, 1, 0), random);
}
private void createLair(Location entrance, Random random) {
World world = entrance.getWorld();
int x = entrance.getBlockX(), y = entrance.getBlockY(), z = entrance.getBlockZ();
if (y - 6 < world.getMinHeight() || y + 5 >= world.getMaxHeight()) return;
org.bukkit.Material stone = XMaterial.matchXMaterial("COBBLESTONE").map(XMaterial::parseMaterial).orElse(null);
org.bukkit.Material air = XMaterial.matchXMaterial("AIR").map(XMaterial::parseMaterial).orElse(null);
if (stone == null || air == null) return;
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
if (dx * dx + dz * dz > 10) continue;
for (int dy = -5; dy <= 3; dy++) {
Block block = world.getBlockAt(x + dx, y + dy, z + dz);
if (dy == -5 || dx * dx + dz * dz >= 8 || dy == 3) block.setType(stone, false);
else block.setType(air, false);
}
}
}
for (int dy = 0; dy <= 4; dy++) {
world.getBlockAt(x - 2, y + dy, z - 3).setType(stone, false);
world.getBlockAt(x + 2, y + dy, z - 3).setType(stone, false);
}
for (int dx = -2; dx <= 2; dx++) world.getBlockAt(x + dx, y + 4, z - 3).setType(stone, false);
for (int dx = -1; dx <= 1; dx++) {
for (int dy = 0; dy <= 2; dy++) world.getBlockAt(x + dx, y + dy, z - 3).setType(air, false);
}
String[] types = {"fire", "ice", "storm"};
String type = types[random.nextInt(types.length)];
Location spawn = new Location(world, x + 0.5, y + 8, z + 0.5);
getServer().getScheduler().runTask(this, () -> spawnDragon(spawn, type));
}
}