Files
src
build.gradle
settings.gradle
Code
7 489
src/main/java/com/kodari/elementalbows/BowAbilityManager.java
package com.kodari.elementalbows;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
public class BowAbilityManager {
private static final long COOLDOWN_MILLIS = 5000L;
private final BowManager bowManager;
private final Map<UUID, Long> cooldowns = new HashMap<>();
public BowAbilityManager(BowManager bowManager) {
this.bowManager = bowManager;
}
public void activate(Player player, ItemStack bow) {
ElementalBowType type = bowManager.getType(bow);
if (type == null) {
return;
}
long now = System.currentTimeMillis();
long availableAt = cooldowns.getOrDefault(player.getUniqueId(), 0L);
if (availableAt > now) {
long seconds = (availableAt - now + 999L) / 1000L;
player.sendMessage(ChatColor.RED + "Ability cooldown: " + seconds + "s");
return;
}
cooldowns.put(player.getUniqueId(), now + COOLDOWN_MILLIS);
switch (type) {
case FIRE -> fire(player);
case WATER -> water(player);
case EARTH -> earth(player);
case AIR -> air(player);
case ICE -> ice(player);
case NATURE -> nature(player);
case LIGHT -> light(player);
case SHADOW -> shadow(player);
case ARCANE -> arcane(player);
case STORM -> storm(player);
case POISON -> poison(player);
case VOID -> voidStep(player);
}
player.sendMessage(type.getColor() + type.getAbilityName() + ChatColor.GRAY + " activated.");
}
private void fire(Player player) {
for (LivingEntity entity : nearbyLiving(player, 5)) {
entity.setFireTicks(100);
entity.damage(4.0, player);
}
particle(player, "FLAME", 30);
sound(player, "ENTITY_GENERIC_EXPLODE");
}
private void water(Player player) {
player.setFireTicks(0);
player.setHealth(Math.min(player.getMaxHealth(), player.getHealth() + 6.0));
for (LivingEntity entity : nearbyLiving(player, 5)) {
entity.setFireTicks(0);
}
particle(player, "WATER_SPLASH", 30);
sound(player, "ENTITY_PLAYER_SPLASH");
}
private void earth(Player player) {
for (LivingEntity entity : nearbyLiving(player, 5)) {
entity.damage(3.0, player);
potion(entity, "SLOWNESS", 100, 1);
}
particle(player, "BLOCK", 20);
sound(player, "BLOCK_STONE_BREAK");
}
private void air(Player player) {
for (LivingEntity entity : nearbyLiving(player, 6)) {
entity.setVelocity(new Vector(0, 1.15, 0));
}
particle(player, "CLOUD", 35);
sound(player, "ENTITY_ENDER_DRAGON_FLAP");
}
private void ice(Player player) {
for (LivingEntity entity : nearbyLiving(player, 5)) {
entity.setFreezeTicks(Math.max(entity.getFreezeTicks(), 100));
entity.damage(2.0, player);
}
particle(player, "SNOWFLAKE", 30);
sound(player, "BLOCK_GLASS_BREAK");
}
private void nature(Player player) {
player.setHealth(Math.min(player.getMaxHealth(), player.getHealth() + 4.0));
potion(player, "REGENERATION", 120, 1);
potion(player, "SPEED", 120, 1);
particle(player, "VILLAGER_HAPPY", 25);
sound(player, "BLOCK_GRASS_BREAK");
}
private void light(Player player) {
for (LivingEntity entity : nearbyLiving(player, 8)) {
entity.setGlowing(true);
}
particle(player, "END_ROD", 35);
sound(player, "ENTITY_PLAYER_LEVELUP");
}
private void shadow(Player player) {
potion(player, "INVISIBILITY", 120, 1);
particle(player, "SMOKE_LARGE", 25);
sound(player, "ENTITY_ENDERMAN_AMBIENT");
}
private void arcane(Player player) {
Location target = targetLocation(player);
if (target != null) {
for (Entity entity : target.getWorld().getNearbyEntities(target, 3, 3, 3)) {
if (entity instanceof LivingEntity living && entity != player) {
living.damage(7.0, player);
}
}
particleAt(target, "ENCHANT", 45);
}
sound(player, "BLOCK_ENCHANTMENT_TABLE_USE");
}
private void storm(Player player) {
Location target = targetLocation(player);
if (target != null) {
target.getWorld().strikeLightning(target);
particleAt(target, "ELECTRIC_SPARK", 25);
}
sound(player, "ENTITY_LIGHTNING_BOLT_THUNDER");
}
private void poison(Player player) {
for (LivingEntity entity : nearbyLiving(player, 6)) {
potion(entity, "POISON", 100, 1);
}
particle(player, "WITCH", 35);
sound(player, "ENTITY_WITCH_THROW");
}
private void voidStep(Player player) {
Location destination = player.getLocation().clone().add(player.getLocation().getDirection().normalize().multiply(6));
player.teleport(destination);
for (LivingEntity entity : nearbyLiving(player, 3)) {
entity.damage(5.0, player);
}
particle(player, "PORTAL", 40);
sound(player, "ENTITY_ENDERMAN_TELEPORT");
}
private java.util.List<LivingEntity> nearbyLiving(Player player, double radius) {
java.util.List<LivingEntity> result = new java.util.ArrayList<>();
for (Entity entity : player.getNearbyEntities(radius, radius, radius)) {
if (entity instanceof LivingEntity living && entity != player) {
result.add(living);
}
}
return result;
}
private Location targetLocation(Player player) {
Block target = player.getTargetBlock(null, 30);
return target == null ? null : target.getLocation().add(0.5, 1.0, 0.5);
}
private void potion(LivingEntity entity, String name, int duration, int amplifier) {
XPotion.matchXPotion(name)
.map(potion -> potion.buildPotionEffect(duration, amplifier))
.ifPresent(entity::addPotionEffect);
}
private void particle(Player player, String name, int count) {
particleAt(player.getLocation(), name, count);
}
private void particleAt(Location location, String name, int count) {
XParticle.of(name).ifPresent(particle -> location.getWorld().spawnParticle(particle.get(), location, count));
}
private void sound(Player player, String name) {
XSound.matchXSound(name).ifPresent(sound -> sound.play(player));
}
}