Files
src
build.gradle
settings.gradle
Code
4 249
src/main/java/com/kodari/netheritegodsword/NetheriteGodSwordPlugin.java
package com.kodari.netheritegodsword;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.World;
import org.bukkit.damage.DamageSource;
import org.bukkit.damage.DamageType;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
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;
public final class NetheriteGodSwordPlugin extends JavaPlugin implements Listener {
private static final long SONIC_BOOM_COOLDOWN_MILLIS = 5_000L;
private static final double SONIC_BOOM_RANGE = 15.0;
private final Map<UUID, Long> sonicBoomCooldowns = new HashMap<>();
private final Set<UUID> applyingAbilityDamage = new HashSet<>();
private NamespacedKey godSwordKey;
@Override
public void onEnable() {
godSwordKey = new NamespacedKey(this, "god_sword");
getServer().getPluginManager().registerEvents(this, this);
}
public ItemStack createGodSword() {
ItemStack sword = XMaterial.matchXMaterial("NETHERITE_SWORD")
.map(XMaterial::parseItem)
.orElseThrow(() -> new IllegalStateException("NETHERITE_SWORD is unavailable"));
ItemMeta meta = sword.getItemMeta();
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "" + ChatColor.BOLD + "Netherite God Sword");
meta.getPersistentDataContainer().set(godSwordKey, PersistentDataType.BYTE, (byte) 1);
sword.setItemMeta(meta);
return sword;
}
private boolean isGodSword(ItemStack item) {
if (item == null || item.getType().isAir()
|| !XMaterial.matchXMaterial(item.getType()).name().equals("NETHERITE_SWORD")) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta != null
&& meta.getPersistentDataContainer().has(godSwordKey, PersistentDataType.BYTE);
}
@EventHandler
public void onRightClick(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND
|| (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)
|| !isGodSword(event.getItem())) {
return;
}
Player player = event.getPlayer();
long now = System.currentTimeMillis();
long cooldownUntil = sonicBoomCooldowns.getOrDefault(player.getUniqueId(), 0L);
if (cooldownUntil > now) {
double secondsLeft = (cooldownUntil - now) / 1000.0;
player.sendMessage(ChatColor.RED + "Sonic Boom is on cooldown for "
+ String.format(java.util.Locale.US, "%.1f", secondsLeft) + "s.");
return;
}
launchSonicBoom(player);
sonicBoomCooldowns.put(player.getUniqueId(), now + SONIC_BOOM_COOLDOWN_MILLIS);
}
private void launchSonicBoom(Player player) {
Location start = player.getEyeLocation();
Vector direction = start.getDirection().normalize();
LivingEntity target = findSonicBoomTarget(player, start, direction);
double travelDistance = SONIC_BOOM_RANGE;
if (target != null) {
Vector targetCenter = target.getLocation().add(0, target.getHeight() * 0.5, 0).toVector();
travelDistance = Math.min(SONIC_BOOM_RANGE, targetCenter.distance(start.toVector()));
}
World world = player.getWorld();
playSound(world, start, "ENTITY_WARDEN_SONIC_BOOM", 1.0f, 1.0f);
double trailDistance = travelDistance;
XParticle.of("SCULK_SOUL").ifPresent(particle -> {
for (double distance = 0.5; distance < trailDistance; distance += 0.5) {
Location point = start.clone().add(direction.clone().multiply(distance));
world.spawnParticle(particle.get(), point, 1, 0, 0, 0, 0);
}
});
if (target == null) {
return;
}
Location impact = target.getLocation().add(0, target.getHeight() * 0.5, 0);
XParticle.of("SONIC_BOOM").ifPresent(particle ->
world.spawnParticle(particle.get(), impact, 1, 0, 0, 0, 0));
playSound(world, impact, "ENTITY_WARDEN_SONIC_BOOM", 1.0f, 1.0f);
DamageSource source = DamageSource.builder(DamageType.SONIC_BOOM)
.withCausingEntity(player)
.withDirectEntity(player)
.build();
applyAbilityDamage(target, sonicBoomDamage(world), source);
}
private LivingEntity findSonicBoomTarget(Player player, Location start, Vector direction) {
LivingEntity closestTarget = null;
double closestDistance = SONIC_BOOM_RANGE + 1.0;
for (Entity entity : player.getWorld().getNearbyEntities(start,
SONIC_BOOM_RANGE, SONIC_BOOM_RANGE, SONIC_BOOM_RANGE)) {
if (!(entity instanceof LivingEntity living) || living.equals(player) || living.isDead()) {
continue;
}
Vector toTarget = living.getLocation()
.add(0, living.getHeight() * 0.5, 0)
.toVector()
.subtract(start.toVector());
double distanceAlongRay = toTarget.dot(direction);
if (distanceAlongRay < 0 || distanceAlongRay > SONIC_BOOM_RANGE
|| distanceAlongRay >= closestDistance) {
continue;
}
double distanceFromRay = toTarget.subtract(direction.clone().multiply(distanceAlongRay)).length();
if (distanceFromRay <= 0.9) {
closestTarget = living;
closestDistance = distanceAlongRay;
}
}
return closestTarget;
}
private double sonicBoomDamage(World world) {
return switch (world.getDifficulty().name()) {
case "EASY" -> 6.0;
case "HARD" -> 15.0;
case "PEACEFUL" -> 0.0;
default -> 10.0;
};
}
private void playSound(World world, Location location, String soundName, float volume, float pitch) {
XSound.matchXSound(soundName).ifPresent(sound ->
world.playSound(location, sound.get(), volume, pitch));
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerAttack(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player attacker)
|| !(event.getEntity() instanceof Player target)
|| !attacker.isSneaking()
|| !isGodSword(attacker.getInventory().getItemInMainHand())
|| applyingAbilityDamage.contains(target.getUniqueId())) {
return;
}
double unmitigatedDamage = event.getDamage();
event.setCancelled(true);
breakEquippedArmor(target);
consumeHandTotems(target);
DamageSource source = DamageSource.builder(DamageType.PLAYER_ATTACK)
.withCausingEntity(attacker)
.withDirectEntity(attacker)
.build();
applyAbilityDamage(target, unmitigatedDamage, source);
}
private void breakEquippedArmor(Player target) {
target.getInventory().setHelmet(null);
target.getInventory().setChestplate(null);
target.getInventory().setLeggings(null);
target.getInventory().setBoots(null);
}
private void consumeHandTotems(Player target) {
if (isTotem(target.getInventory().getItemInMainHand())) {
target.getInventory().setItemInMainHand(null);
}
if (isTotem(target.getInventory().getItemInOffHand())) {
target.getInventory().setItemInOffHand(null);