Files
resource-pack
src
build.gradle
manifest.kod
settings.gradle
Code
41 820
src/main/java/com/kodari/warbound/AbilityEngine.java
package com.kodari.warbound;
import com.cryptomorin.xseries.particles.XParticle;
import com.cryptomorin.xseries.XPotion;
import com.cryptomorin.xseries.XSound;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Comparator;
public class AbilityEngine {
private final WarboundPlugin plugin;
private final PlayerDataManager dataManager;
public AbilityEngine(WarboundPlugin plugin, PlayerDataManager dataManager) {
this.plugin = plugin;
this.dataManager = dataManager;
}
public void cast(Player player, Warbound warbound, Ability ability) {
PlayerData data = dataManager.load(player.getUniqueId());
long now = System.currentTimeMillis();
String key = ability.getId();
long readyAt = data.getCooldowns().getOrDefault(key, 0L);
ConfigurationSection section = section(warbound, ability);
int cooldown = section.getInt("cooldown", ability.getCooldown());
if (readyAt > now) {
long seconds = Math.max(1, (readyAt - now + 999) / 1000);
plugin.sendMessage(player, "cooldown", "%seconds%", String.valueOf(seconds));
return;
}
data.getCooldowns().put(key, now + cooldown * 1000L);
dataManager.save(data);
double range = section.getDouble("range", ability.getRange());
double damage = section.getDouble("damage", ability.getDamage());
int duration = section.getInt("duration", ability.getDuration());
playEffects(player, section, ability);
execute(player, ability, range, damage, duration, section);
}
private ConfigurationSection section(Warbound warbound, Ability ability) {
return plugin.getConfig().getConfigurationSection("abilities." + warbound.getId() + "." + ability.getId());
}
private void playEffects(Player player, ConfigurationSection section, Ability ability) {
String particle = section.getString("particle", ability.getParticle());
int count = plugin.getConfig().getInt("settings.ability-particle-count", 24);
double offset = plugin.getConfig().getDouble("settings.ability-particle-offset", 0.35);
XParticle.of(particle).ifPresent(p -> player.getWorld().spawnParticle(p.get(), player.getLocation().add(0, 1, 0), count, offset, offset, offset, 0.02));
XSound.matchXSound(section.getString("sound", ability.getSound())).ifPresent(sound -> sound.play(player));
}
private void execute(Player player, Ability ability, double range, double damage, int duration, ConfigurationSection section) {
Ability.Action action = ability.getAction();
LivingEntity target = findTarget(player, range);
switch (action) {
case BURST, SHOCKWAVE, METEOR, DOMINION -> damageNearby(player, range, damage);
case DASH -> dash(player, range, damage);
case FREEZE, PRISON -> { if (target != null) { damage(target, player, damage); applyEffect(target, section, duration); } }
case LIGHTNING -> { if (target != null) { target.getWorld().strikeLightningEffect(target.getLocation()); damage(target, player, damage); applyEffect(target, section, duration); } }
case GUARD, SHIELD -> applyEffect(player, section, duration);
case BLAST, BEAM -> { if (target != null) { damage(target, player, damage); applyEffect(target, section, duration); } }
case KNOCKBACK -> { if (target != null) { damage(target, player, damage); target.setVelocity(player.getLocation().getDirection().normalize().multiply(1.4).setY(0.5)); applyEffect(target, section, duration); } }
case HEAL -> { player.setHealth(Math.min(player.getAttribute(org.bukkit.attribute.Attribute.MAX_HEALTH).getValue(), player.getHealth() + Math.max(2, damage == 0 ? 6 : damage))); applyEffect(player, section, duration); }
case TELEPORT -> teleport(player, range);
case VEIL -> applyEffect(player, section, duration);
case PULL -> { if (target != null) { damage(target, player, damage); target.setVelocity(player.getLocation().toVector().subtract(target.getLocation().toVector()).normalize().multiply(1.2)); applyEffect(target, section, duration); } }
case RIFT -> { damageNearby(player, range, damage); teleport(player, range / 2); }
case SWAP -> { if (target != null) { Location targetLocation = target.getLocation().clone(); target.teleport(player.getLocation()); player.teleport(targetLocation); damage(target, player, damage); applyEffect(target, section, duration); } }
case DRAIN -> { if (target != null) { damage(target, player, damage); player.setHealth(Math.min(player.getAttribute(org.bukkit.attribute.Attribute.MAX_HEALTH).getValue(), player.getHealth() + damage / 2)); applyEffect(target, section, duration); } }
case STRENGTH -> applyEffect(player, section, duration);
case FLIGHT -> { applyEffect(player, section, duration); XPotion.matchXPotion("SLOW_FALLING").map(x -> x.buildPotionEffect(duration * 20, 0)).ifPresent(player::addPotionEffect); }
case BREATH -> { damageNearby(player, range, damage); for (Entity entity : player.getNearbyEntities(range, range, range)) if (entity instanceof LivingEntity living && living != player) applyEffect(living, section, duration); }
}
}
private LivingEntity findTarget(Player player, double range) {
if (range <= 0) return null;
Vector direction = player.getEyeLocation().getDirection().normalize();
return player.getNearbyEntities(range, range, range).stream().filter(entity -> entity instanceof LivingEntity && entity != player)
.map(entity -> (LivingEntity) entity)
.filter(entity -> entity.getLocation().distanceSquared(player.getLocation()) <= range * range)
.filter(entity -> direction.dot(entity.getEyeLocation().toVector().subtract(player.getEyeLocation().toVector()).normalize()) > 0.45)
.min(Comparator.comparingDouble(entity -> entity.getLocation().distanceSquared(player.getLocation()))).orElse(null);
}
private void damageNearby(Player player, double range, double damage) {
if (damage <= 0) return;
for (Entity entity : player.getNearbyEntities(range, range, range)) if (entity instanceof LivingEntity living && living != player) damage(living, player, damage);
}
private void damage(LivingEntity target, Player source, double amount) { if (amount > 0) target.damage(amount, source); }
private void dash(Player player, double range, double damage) {
Vector direction = player.getLocation().getDirection().normalize();
player.setVelocity(direction.multiply(Math.min(2.5, Math.max(1, range / 3))).setY(0.35));
plugin.getServer().getScheduler().runTaskLater(plugin, () -> damageNearby(player, Math.min(4, range / 2), damage), 8L);
}
private void teleport(Player player, double range) {
Location destination = player.getLocation().clone().add(player.getLocation().getDirection().normalize().multiply(Math.max(2, Math.min(8, range))));
destination.setY(player.getWorld().getHighestBlockYAt(destination) + 1);
if (destination.getBlock().getType().isAir()) player.teleport(destination);
}
private void applyEffect(LivingEntity entity, ConfigurationSection section, int duration) {
String effect = section.getString("effect", "");
if (effect.isEmpty() || effect.equalsIgnoreCase("BURNING")) {
if (effect.equalsIgnoreCase("BURNING")) entity.setFireTicks(duration * 20);
return;
}
int amplifier = section.getInt("effect-amplifier", 0);
XPotion.matchXPotion(effect).map(potion -> potion.buildPotionEffect(duration * 20, amplifier)).ifPresent(entity::addPotionEffect);
}
}