Files
src
build.gradle
settings.gradle
Code
4 140
src/main/java/com/kodari/maceaura/MaceAuraPlugin.java
package com.kodari.maceaura;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
public final class MaceAuraPlugin extends JavaPlugin {
private Enchantment windBurst;
private XParticle dustParticle;
@Override
public void onEnable() {
windBurst = XEnchantment.matchXEnchantment("WIND_BURST")
.map(XEnchantment::getEnchant)
.orElse(null);
dustParticle = XParticle.of("DUST").orElse(null);
if (windBurst == null) {
getLogger().severe("Could not find the WIND_BURST enchantment; mace enchanting is disabled.");
}
if (dustParticle == null) {
getLogger().severe("Could not find the DUST particle; the red aura is disabled.");
}
getServer().getScheduler().runTaskTimer(this, this::updatePlayers, 0L, 10L);
}
@Override
public void onDisable() {
getServer().getScheduler().cancelTasks(this);
}
private void updatePlayers() {
for (Player player : Bukkit.getOnlinePlayers()) {
boolean carriesMace = false;
for (ItemStack item : player.getInventory().getContents()) {
if (isMace(item)) {
carriesMace = true;
applyWindBurst(item);
}
}
ItemStack offHand = player.getInventory().getItemInOffHand();
if (isMace(offHand)) {
carriesMace = true;
applyWindBurst(offHand);
}
if (carriesMace) {
showAura(player);
}
}
}
private boolean isMace(ItemStack item) {
return item != null && item.getType().name().equals("MACE");
}
private void applyWindBurst(ItemStack item) {
if (windBurst == null) {
return;
}
ItemMeta meta = item.getItemMeta();
if (meta == null || meta.getEnchantLevel(windBurst) >= 3) {
return;
}
meta.addEnchant(windBurst, 3, true);
item.setItemMeta(meta);
}
private void showAura(Player player) {
if (dustParticle == null) {
return;
}
Location center = player.getLocation().add(0, 1, 0);
for (int i = 0; i < 12; i++) {
double angle = (Math.PI * 2 * i) / 12;
Location point = center.clone().add(
Math.cos(angle) * 0.8,
Math.sin(angle) * 0.45,
Math.sin(angle) * 0.8
);
player.getWorld().spawnParticle(
dustParticle.get(),
point,
1,
0,
0,
0,
0,
new Particle.DustOptions(Color.RED, 1.0F)
);
}
}
}