Files
src
Code
2 185
src/main/java/de/kodari/zauberstab/ZauberstabPlugin.java
package de.kodari.zauberstab;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.SmallFireball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class ZauberstabPlugin extends JavaPlugin implements Listener {
private final Map<UUID, Integer> selectedAbilities = new HashMap<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
private NamespacedKey staffKey;
private static final String[] ABILITIES = {"Feuerball", "Blitzschlag", "Arkanstoß"};
@Override
public void onEnable() {
staffKey = new NamespacedKey(this, "zauberstab");
getServer().getPluginManager().registerEvents(this, this);
getCommand("zauberstab").setExecutor((sender, command, label, args) -> {
if (!(sender instanceof Player player)) {
sender.sendMessage("Dieser Befehl ist nur im Spiel verfügbar.");
return true;
}
if (!player.hasPermission("zauberstab.use")) {
player.sendMessage("§cDafür hast du keine Berechtigung.");
return true;
}
player.getInventory().addItem(createStaff());
player.sendMessage("§d✦ §5Du hast den §dArkanen Stab §5erhalten!");
return true;
});
}
private ItemStack createStaff() {
ItemStack staff = XMaterial.matchXMaterial("BLAZE_ROD").map(XMaterial::parseItem).orElse(null);
if (staff == null) {
throw new IllegalStateException("BLAZE_ROD konnte nicht geladen werden.");
}
ItemMeta meta = staff.getItemMeta();
meta.setDisplayName("§d✦ §5Arkaner Stab §d✦");
meta.setLore(java.util.List.of(
"§7Rechtsklick: Attacke wirken",
"§7Schleichen + Rechtsklick: Attacke wechseln",
"§8Ein Fokus für uralte Magie"
));
meta.getPersistentDataContainer().set(staffKey, PersistentDataType.BYTE, (byte) 1);
staff.setItemMeta(meta);
return staff;
}
private boolean isStaff(ItemStack item) {
return item != null && item.hasItemMeta()
&& item.getItemMeta().getPersistentDataContainer().has(staffKey, PersistentDataType.BYTE);
}
@EventHandler
public void onStaffUse(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND || !isStaff(event.getItem())) {
return;
}
Action action = event.getAction();
if (action != Action.RIGHT_CLICK_AIR && action != Action.RIGHT_CLICK_BLOCK) {
return;
}
event.setCancelled(true);
Player player = event.getPlayer();
if (player.isSneaking()) {
int next = (selectedAbilities.getOrDefault(player.getUniqueId(), 0) + 1) % ABILITIES.length;
selectedAbilities.put(player.getUniqueId(), next);
player.sendMessage("§d✦ §5Ausgewählt: §d" + ABILITIES[next]);
XSound.matchXSound("BLOCK_AMETHYST_BLOCK_CHIME").ifPresent(sound -> sound.play(player));
return;
}
long now = System.currentTimeMillis();
long readyAt = cooldowns.getOrDefault(player.getUniqueId(), 0L);
if (now < readyAt) {
player.sendMessage("§7Der Stab lädt sich noch auf …");
return;
}
cooldowns.put(player.getUniqueId(), now + 1400L);
switch (selectedAbilities.getOrDefault(player.getUniqueId(), 0)) {
case 0 -> castFireball(player);
case 1 -> castLightning(player);
default -> castArcaneBurst(player);
}
}
private void castFireball(Player player) {
Vector direction = player.getLocation().getDirection().normalize();
SmallFireball fireball = player.getWorld().spawn(
player.getEyeLocation().add(direction.clone().multiply(0.8)), SmallFireball.class);
fireball.setShooter(player);
fireball.setVelocity(direction.multiply(1.5));
fireball.setIsIncendiary(false);
fireball.setYield(0.0F);
XParticle.of("FLAME").ifPresent(particle -> player.getWorld().spawnParticle(
particle.get(), fireball.getLocation(), 12, 0.15, 0.15, 0.15, 0.02));
XSound.matchXSound("ENTITY_BLAZE_SHOOT").ifPresent(sound -> sound.play(player));
}
private void castLightning(Player player) {
Location target = player.getLocation().add(player.getLocation().getDirection().multiply(8));
player.getWorld().strikeLightningEffect(target);
for (Entity entity : player.getWorld().getNearbyEntities(target, 3.5, 3.5, 3.5)) {
if (entity instanceof LivingEntity living && entity != player) {
living.damage(6.0, player);
}
}
XSound.matchXSound("ENTITY_LIGHTNING_BOLT_THUNDER").ifPresent(sound -> sound.play(player));
}
private void castArcaneBurst(Player player) {
Location center = player.getLocation();
XParticle.of("WITCH").ifPresent(particle -> player.getWorld().spawnParticle(
particle.get(), center.clone().add(0, 1, 0), 36, 2.2, 1.0, 2.2, 0.08));
for (Entity entity : player.getWorld().getNearbyEntities(center, 4.5, 3.0, 4.5)) {
if (entity instanceof LivingEntity living && entity != player) {
living.damage(4.0, player);
Vector push = living.getLocation().toVector().subtract(center.toVector());
if (push.lengthSquared() > 0) {
living.setVelocity(push.normalize().multiply(1.0).setY(0.45));
}
}
}
XSound.matchXSound("ENTITY_ILLUSIONER_CAST_SPELL").ifPresent(sound -> sound.play(player));
}
@EventHandler
public void onSwordHit(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player player)) {
return;
}
ItemStack weapon = player.getInventory().getItemInMainHand();
if (weapon.getType().name().endsWith("_SWORD")) {
Location center = event.getEntity().getLocation().add(0, 0.8, 0);
XParticle.of("SWEEP_ATTACK").ifPresent(particle -> {
for (int i = 0; i < 7; i++) {
double angle = Math.PI * i / 6.0;
Location point = center.clone().add(Math.cos(angle) * 0.9, Math.sin(angle) * 0.45, 0);
player.getWorld().spawnParticle(particle.get(), point, 1, 0, 0, 0, 0);
}
});
XParticle.of("CRIT").ifPresent(particle -> player.getWorld().spawnParticle(
particle.get(), center, 8, 0.35, 0.35, 0.35, 0.08));
XSound.matchXSound("ENTITY_PLAYER_ATTACK_SWEEP").ifPresent(sound -> sound.play(player));
}
}
}