Files
src
Code
2 204
src/main/java/com/kodari/dragon/DragonPlugin.java
package com.kodari.dragon;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Fireball;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public final class DragonPlugin extends JavaPlugin implements Listener {
private final Set<UUID> dragons = new HashSet<>();
private final Map<UUID, Integer> attacks = new HashMap<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
@Override
public void onEnable() {
Bukkit.getPluginManager().registerEvents(this, this);
registerRecipes();
Bukkit.getScheduler().runTaskTimer(this, this::moveDragons, 1L, 1L);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Bu komutu yalnızca oyuncular kullanabilir.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("ejderha.olustur")) {
player.sendMessage(ChatColor.RED + "Bu komutu kullanma yetkin yok.");
return true;
}
Location location = player.getLocation().add(player.getLocation().getDirection().multiply(8)).add(0, 3, 0);
EnderDragon dragon = player.getWorld().spawn(location, EnderDragon.class);
dragon.setCustomName(ChatColor.DARK_PURPLE + "Kadim Ejderha");
dragon.setCustomNameVisible(true);
dragon.setAI(false);
dragon.setRemoveWhenFarAway(false);
dragons.add(dragon.getUniqueId());
player.sendMessage(ChatColor.LIGHT_PURPLE + "Ejderha oluşturuldu! Binmek için sağ tıkla, saldırı değiştirmek için F tuşuna bas.");
return true;
}
@EventHandler
public void onMount(org.bukkit.event.player.PlayerInteractEntityEvent event) {
if (!(event.getRightClicked() instanceof EnderDragon) || !dragons.contains(event.getRightClicked().getUniqueId())) return;
EnderDragon dragon = (EnderDragon) event.getRightClicked();
if (dragon.getPassengers().isEmpty()) {
event.setCancelled(true);
dragon.addPassenger(event.getPlayer());
event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Saldırı: Ateş topu. Sol tıkla saldır, F ile değiştir, Shift ile in.");
}
}
@EventHandler
public void onSwap(PlayerSwapHandItemsEvent event) {
if (!isRidingDragon(event.getPlayer())) return;
event.setCancelled(true);
UUID id = event.getPlayer().getUniqueId();
int attack = (attacks.getOrDefault(id, 0) + 1) % 3;
attacks.put(id, attack);
String[] names = {"Ateş topu", "Ejderha nefesi", "Kanat darbesi"};
event.getPlayer().sendMessage(ChatColor.LIGHT_PURPLE + "Saldırı: " + names[attack]);
}
@EventHandler
public void onAttack(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) return;
Player player = event.getPlayer();
if (!isRidingDragon(player)) return;
event.setCancelled(true);
long now = System.currentTimeMillis();
if (cooldowns.getOrDefault(player.getUniqueId(), 0L) > now) return;
cooldowns.put(player.getUniqueId(), now + 2000L);
EnderDragon dragon = (EnderDragon) player.getVehicle();
Vector direction = player.getLocation().getDirection().normalize();
int attack = attacks.getOrDefault(player.getUniqueId(), 0);
Location origin = dragon.getLocation().add(0, 2, 0).add(direction.clone().multiply(5));
if (attack == 0) {
Fireball fireball = dragon.getWorld().spawn(origin, Fireball.class);
fireball.setShooter(dragon);
fireball.setDirection(direction);
fireball.setYield(2F);
fireball.setIsIncendiary(false);
} else if (attack == 1) {
for (int i = 1; i <= 12; i++) {
Location point = origin.clone().add(direction.clone().multiply(i));
XParticle.of("DRAGON_BREATH").ifPresent(p -> point.getWorld().spawnParticle(p.get(), point, 8, 0.7, 0.7, 0.7, 0.01));
}
for (Entity entity : dragon.getNearbyEntities(18, 12, 18)) {
if (!(entity instanceof LivingEntity) || entity.equals(player) || entity.equals(dragon)) continue;
Vector offset = entity.getLocation().toVector().subtract(origin.toVector());
if (offset.lengthSquared() <= 144 && offset.lengthSquared() > 0 && offset.normalize().dot(direction) > 0.8) {
((LivingEntity) entity).damage(8, dragon);
}
}
} else {
XParticle.of("EXPLOSION").ifPresent(p -> dragon.getWorld().spawnParticle(p.get(), dragon.getLocation(), 12, 4, 2, 4, 0));
for (Entity entity : dragon.getNearbyEntities(8, 5, 8)) {
if (!(entity instanceof LivingEntity) || entity.equals(player) || entity.equals(dragon)) continue;
((LivingEntity) entity).damage(6, dragon);
Vector push = entity.getLocation().toVector().subtract(dragon.getLocation().toVector());
if (push.lengthSquared() > 0) entity.setVelocity(push.normalize().multiply(1.5).setY(0.6));
}
}
}
@EventHandler
public void onDeath(EntityDeathEvent event) {
if (!dragons.remove(event.getEntity().getUniqueId())) return;
ItemStack scales = scale();
scales.setAmount(24);
event.getDrops().add(scales);
}
private boolean isRidingDragon(Player player) {
Entity vehicle = player.getVehicle();
return vehicle instanceof EnderDragon && dragons.contains(vehicle.getUniqueId());
}
private void moveDragons() {
for (World world : Bukkit.getWorlds()) {
for (EnderDragon dragon : world.getEntitiesByClass(EnderDragon.class)) {
if (!dragons.contains(dragon.getUniqueId()) || dragon.getPassengers().isEmpty()) continue;
Entity passenger = dragon.getPassengers().get(0);
if (!(passenger instanceof Player)) continue;
Player player = (Player) passenger;
dragon.setVelocity(player.getLocation().getDirection().multiply(0.7));
}
}
}
private ItemStack item(String material, String name, String... lore) {
ItemStack item = XMaterial.matchXMaterial(material).map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("Eksik malzeme: " + material));
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(ChatColor.DARK_PURPLE + name);
if (lore.length > 0) meta.setLore(Arrays.asList(lore));
item.setItemMeta(meta);
return item;
}
private ItemStack scale() {
return item("DRAGON_BREATH", "Ejderha Pulu", ChatColor.GRAY + "Ejderha ekipmanı yapımında kullanılır.");
}
private void registerRecipes() {
recipe("dragon_sword", item("DIAMOND_SWORD", "Ejderha Kılıcı"), " S ", " S ", " B ");
recipe("dragon_helmet", item("DIAMOND_HELMET", "Ejderha Miğferi"), "SSS", "S S", " ");
recipe("dragon_chestplate", item("DIAMOND_CHESTPLATE", "Ejderha Göğüslüğü"), "S S", "SSS", "SSS");
recipe("dragon_leggings", item("DIAMOND_LEGGINGS", "Ejderha Pantolonu"), "SSS", "S S", "S S");
recipe("dragon_boots", item("DIAMOND_BOOTS", "Ejderha Botları"), " ", "S S", "S S");
}
private void recipe(String key, ItemStack result, String... shape) {
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, key), result);
recipe.shape(shape);
recipe.setIngredient('S', new RecipeChoice.ExactChoice(scale()));
if (key.equals("dragon_sword")) {
recipe.setIngredient('B', XMaterial.matchXMaterial("STICK").map(XMaterial::parseMaterial)
.orElseThrow(() -> new IllegalStateException("Çubuk malzemesi bulunamadı")));
}
Bukkit.addRecipe(recipe);
}
}