Files
src
Code
2 145
src/main/java/com/kodari/blades/CustomBlades.java
package com.kodari.blades;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
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.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
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.event.player.PlayerQuitEvent;
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 CustomBlades extends JavaPlugin implements Listener {
private NamespacedKey weaponKey;
private final Map<UUID, Map<String, Long>> cooldowns = new HashMap<>();
@Override
public void onEnable() {
weaponKey = new NamespacedKey(this, "weapon");
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players can receive weapons.");
return true;
}
Player player = (Player) sender;
if (!player.hasPermission("customblades.give")) {
player.sendMessage(ChatColor.RED + "You don't have permission to do that.");
return true;
}
if (args.length != 1 || !(args[0].equalsIgnoreCase("cutlass")
|| args[0].equalsIgnoreCase("knife") || args[0].equalsIgnoreCase("red"))) {
player.sendMessage(ChatColor.YELLOW + "Usage: /blade <cutlass|knife|red>");
return true;
}
String type = args[0].toLowerCase(java.util.Locale.ROOT);
String material = type.equals("knife") ? "IRON_SWORD" : "DIAMOND_SWORD";
ItemStack item = XMaterial.matchXMaterial(material).map(XMaterial::parseItem).orElse(null);
if (item == null) {
player.sendMessage(ChatColor.RED + "This weapon isn't available on this server version.");
return true;
}
ItemMeta meta = item.getItemMeta();
String name = type.equals("cutlass") ? "&6Captain's Cutlass" : type.equals("knife") ? "&aVenom Knife" : "&cCrimson Blade";
String ability = type.equals("cutlass") ? "&7Right-click: Tidal Sweep" : type.equals("knife") ? "&7Right-click: Venom Rush" : "&7Right-click: Crimson Burst";
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
meta.setLore(java.util.Arrays.asList(ChatColor.translateAlternateColorCodes('&', ability), ChatColor.DARK_GRAY + "A custom weapon"));
meta.getPersistentDataContainer().set(weaponKey, PersistentDataType.STRING, type);
item.setItemMeta(meta);
Map<Integer, ItemStack> leftovers = player.getInventory().addItem(item);
for (ItemStack leftover : leftovers.values()) player.getWorld().dropItemNaturally(player.getLocation(), leftover);
player.sendMessage(ChatColor.GREEN + "Weapon received: " + ChatColor.translateAlternateColorCodes('&', name));
return true;
}
@EventHandler
public void onUse(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND || (event.getAction() != Action.RIGHT_CLICK_AIR
&& event.getAction() != Action.RIGHT_CLICK_BLOCK)) return;
ItemStack item = event.getItem();
if (item == null || !item.hasItemMeta()) return;
String type = item.getItemMeta().getPersistentDataContainer().get(weaponKey, PersistentDataType.STRING);
if (type == null || !(type.equals("cutlass") || type.equals("knife") || type.equals("red"))) return;
event.setCancelled(true);
Player player = event.getPlayer();
long now = System.currentTimeMillis();
Map<String, Long> playerCooldowns = cooldowns.computeIfAbsent(player.getUniqueId(), id -> new HashMap<>());
long remaining = playerCooldowns.getOrDefault(type, 0L) - now;
if (remaining > 0) {
player.sendTitle(ChatColor.RED + "Cooling down", ChatColor.GRAY + String.format(java.util.Locale.ROOT, "%.1fs remaining", remaining / 1000.0), 0, 20, 5);
return;
}
int seconds = type.equals("cutlass") ? 12 : type.equals("knife") ? 10 : 16;
playerCooldowns.put(type, now + seconds * 1000L);
Location center = player.getLocation();
if (type.equals("knife")) {
Vector direction = center.getDirection().setY(0);
if (direction.lengthSquared() > 0) direction.normalize().multiply(1.3);
direction.setY(0.25);
player.setVelocity(direction);
XPotion.matchXPotion("SPEED").map(p -> p.buildPotionEffect(100, 1)).ifPresent(player::addPotionEffect);
for (Entity entity : player.getNearbyEntities(3, 2, 3)) {
if (entity instanceof LivingEntity && entity != player && center.distanceSquared(entity.getLocation()) <= 9) {
LivingEntity target = (LivingEntity) entity;
target.damage(3.0, player);
XPotion.matchXPotion("POISON").map(p -> p.buildPotionEffect(60, 0)).ifPresent(target::addPotionEffect);
}
}
particles(player, "HAPPY_VILLAGER", 35);
player.sendTitle(ChatColor.GREEN + "VENOM RUSH", ChatColor.GRAY + "Speed and poison", 5, 30, 10);
} else {
double radius = type.equals("cutlass") ? 4.0 : 5.0;
for (Entity entity : player.getNearbyEntities(radius, 3, radius)) {
if (!(entity instanceof LivingEntity) || entity == player || center.distanceSquared(entity.getLocation()) > radius * radius) continue;
LivingEntity target = (LivingEntity) entity;
target.damage(type.equals("cutlass") ? 6.0 : 8.0, player);
Vector push = target.getLocation().toVector().subtract(center.toVector()).setY(0);
if (push.lengthSquared() > 0) target.setVelocity(push.normalize().multiply(0.8).setY(0.3));
}
particles(player, type.equals("cutlass") ? "SWEEP_ATTACK" : "FLAME", 35);
player.sendTitle(type.equals("cutlass") ? ChatColor.GOLD + "TIDAL SWEEP" : ChatColor.RED + "CRIMSON BURST", ChatColor.GRAY + "Cooldown: " + seconds + "s", 5, 30, 10);
}
}
private void particles(Player player, String name, int count) {
XParticle.of(name).ifPresent(p -> player.getWorld().spawnParticle(p.get(), player.getLocation().add(0, 1, 0), count, 1.2, 0.5, 1.2, 0.05));
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
cooldowns.remove(event.getPlayer().getUniqueId());
}
}

