Files
src
build.gradle
Code
3 192
src/main/java/com/kodari/kingbreaker/KingBreakerPlugin.java
package com.kodari.kingbreaker;
import com.cryptomorin.xseries.XEnchantment;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vex;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.RayTraceResult;
public final class KingBreakerPlugin extends JavaPlugin implements Listener {
private static final long DEMON_COOLDOWN_MILLIS = 10_000L;
private static final List<String> ENCHANTMENTS = List.of(
"AQUA_AFFINITY", "BANE_OF_ARTHROPODS", "BINDING_CURSE", "BLAST_PROTECTION",
"BREACH", "CHANNELING", "DENSITY", "DEPTH_STRIDER", "EFFICIENCY", "FEATHER_FALLING",
"FIRE_ASPECT", "FIRE_PROTECTION", "FLAME", "FORTUNE", "FROST_WALKER", "IMPALING",
"INFINITY", "KNOCKBACK", "LOOTING", "LOYALTY", "LUCK_OF_THE_SEA", "LURE", "MENDING",
"MULTISHOT", "PIERCING", "POWER", "PROJECTILE_PROTECTION", "PROTECTION", "PUNCH",
"QUICK_CHARGE", "RESPIRATION", "RIPTIDE", "SHARPNESS", "SILK_TOUCH", "SMITE",
"SOUL_SPEED", "SWEEPING_EDGE", "SWIFT_SNEAK", "THORNS", "UNBREAKING", "VANISHING_CURSE",
"WIND_BURST"
);
private final Map<UUID, Long> demonCooldowns = new HashMap<>();
private NamespacedKey itemKey;
@Override
public void onEnable() {
itemKey = new NamespacedKey(this, "king_breaker");
Bukkit.getPluginManager().registerEvents(this, this);
registerRecipe();
}
private void registerRecipe() {
Material mace = XMaterial.matchXMaterial("MACE").map(XMaterial::parseMaterial).orElse(null);
Material dragonEgg = XMaterial.matchXMaterial("DRAGON_EGG").map(XMaterial::parseMaterial).orElse(null);
if (mace == null || dragonEgg == null) {
getLogger().severe("KING BREAKER requires a server version with mace and dragon egg materials.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "king_breaker"), createKingBreaker(mace));
recipe.shape("M M", " E ");
recipe.setIngredient('M', mace);
recipe.setIngredient('E', dragonEgg);
Bukkit.addRecipe(recipe);
}
private ItemStack createKingBreaker(Material mace) {
ItemStack item = new ItemStack(mace);
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return item;
}
meta.setDisplayName(ChatColor.GOLD.toString() + ChatColor.BOLD + "KING BREAKER");
meta.setLore(List.of(
ChatColor.DARK_RED + "Demon's Wrath",
ChatColor.GRAY + "Sneak + right-click to summon a demon."
));
for (String enchantmentName : ENCHANTMENTS) {
XEnchantment.matchXEnchantment(enchantmentName)
.ifPresent(enchantment -> meta.addEnchant(enchantment.getEnchant(), 10, true));
}
meta.getPersistentDataContainer().set(itemKey, PersistentDataType.BYTE, (byte) 1);
item.setItemMeta(meta);
return item;
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND
|| (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) {
return;
}
Player player = event.getPlayer();
if (!player.isSneaking() || !isKingBreaker(event.getItem())) {
return;
}
event.setCancelled(true);
long now = System.currentTimeMillis();
long cooldownUntil = demonCooldowns.getOrDefault(player.getUniqueId(), 0L);
if (now < cooldownUntil) {
player.sendMessage(ChatColor.RED + "The demon is resting for "
+ ((cooldownUntil - now + 999) / 1000) + "s.");
return;
}
LivingEntity target = findTarget(player);
if (target == null) {
player.sendMessage(ChatColor.DARK_RED + "Aim at a creature to call the demon.");
return;
}
Vex demon = player.getWorld().spawn(player.getLocation().add(0, 1, 0), Vex.class);
demon.setCustomName(ChatColor.DARK_RED + "Demon");
demon.setCustomNameVisible(true);
demon.setGlowing(true);
demon.setTarget(target);
XParticle.of("SOUL_FIRE_FLAME").ifPresent(particle ->
player.getWorld().spawnParticle(particle.get(), demon.getLocation(), 30, 0.45, 0.45, 0.45, 0.02));
XSound.matchXSound("ENTITY_VEX_CHARGE").ifPresent(sound -> sound.play(player));
demonCooldowns.put(player.getUniqueId(), now + DEMON_COOLDOWN_MILLIS);
Bukkit.getScheduler().runTaskLater(this, demon::remove, 100L);
player.sendMessage(ChatColor.DARK_RED + "The demon attacks your target!");
}
private boolean isKingBreaker(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return false;
}
ItemMeta meta = item.getItemMeta();
return meta != null && meta.getPersistentDataContainer().has(itemKey, PersistentDataType.BYTE);
}
private LivingEntity findTarget(Player player) {
RayTraceResult rayTrace = player.getWorld().rayTraceEntities(
player.getEyeLocation(),
player.getEyeLocation().getDirection(),
24,
0.5,
entity -> entity instanceof LivingEntity && entity != player
);
if (rayTrace != null && rayTrace.getHitEntity() instanceof LivingEntity livingEntity) {
return livingEntity;
}
return player.getNearbyEntities(18, 18, 18).stream()
.filter(entity -> entity instanceof Monster)
.map(entity -> (Monster) entity)
.min(Comparator.comparingDouble(entity -> entity.getLocation().distanceSquared(player.getLocation())))
.orElse(null);
}
}