Files
src
build.gradle
settings.gradle
Code
4 349
src/main/java/com/kodari/arsenal/SoulArsenal.java
package com.kodari.arsenal;
import com.cryptomorin.xseries.XMaterial;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Arrow;
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.entity.PlayerDeathEvent;
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.util.Vector;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public final class SoulArsenal extends JavaPlugin implements Listener, CommandExecutor, TabCompleter {
private static final String[] ABILITY_NAMES = {"Quickdraw", "Shockwave", "Cataclysm"};
private final Map<UUID, Integer> kills = new HashMap<>();
private final Map<UUID, Integer> selectedAbility = new HashMap<>();
private final Map<UUID, Long> cooldowns = new HashMap<>();
private NamespacedKey weaponKey;
private File playersFile;
private YamlConfiguration playersData;
@Override
public void onEnable() {
weaponKey = new NamespacedKey(this, "weapon");
playersFile = new File(getDataFolder(), "players.yml");
playersData = YamlConfiguration.loadConfiguration(playersFile);
for (String id : playersData.getKeys(false)) {
try {
kills.put(UUID.fromString(id), playersData.getInt(id));
} catch (IllegalArgumentException ignored) {
getLogger().warning("Ignoring invalid player UUID in players.yml: " + id);
}
}
getServer().getPluginManager().registerEvents(this, this);
getCommand("arsenal").setExecutor(this);
getCommand("arsenal").setTabCompleter(this);
}
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player killer = event.getEntity().getKiller();
if (killer == null) {
return;
}
int previous = getKills(killer.getUniqueId());
int total = previous + 1;
kills.put(killer.getUniqueId(), total);
playersData.set(killer.getUniqueId().toString(), total);
savePlayerData();
killer.sendMessage(color("&aKill recorded: &f" + total + "&a."));
int oldUnlocked = Math.min(3, previous / 2);
int newlyUnlocked = Math.min(3, total / 2);
if (newlyUnlocked > oldUnlocked) {
killer.sendMessage(color("&6Ability unlocked: &e" + ABILITY_NAMES[newlyUnlocked - 1]
+ "&6! Hold a custom weapon and sneak + right-click to select abilities."));
}
}
@EventHandler
public void onWeaponInteract(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 weaponId = item.getItemMeta().getPersistentDataContainer().get(weaponKey, PersistentDataType.STRING);
Weapon weapon = Weapon.fromId(weaponId);
if (weapon == null) {
return;
}
event.setCancelled(true);
Player player = event.getPlayer();
int unlocked = unlockedAbilities(player.getUniqueId());
if (unlocked == 0) {
player.sendMessage(color("&cGet 2 kills to unlock your first weapon ability."));
return;
}
if (player.isSneaking()) {
int next = selectedAbility.getOrDefault(player.getUniqueId(), 1) % unlocked + 1;
selectedAbility.put(player.getUniqueId(), next);
player.sendMessage(color("&eSelected ability: &6" + ABILITY_NAMES[next - 1]));
return;
}
int ability = selectedAbility.getOrDefault(player.getUniqueId(), 1);
if (ability > unlocked) {
ability = 1;
selectedAbility.put(player.getUniqueId(), ability);
}
activateAbility(player, weapon, ability);
}
private void activateAbility(Player player, Weapon weapon, int ability) {
long now = System.currentTimeMillis();
long readyAt = cooldowns.getOrDefault(player.getUniqueId(), 0L);
if (now < readyAt) {
player.sendMessage(color("&cAbility cooldown: &f" + ((readyAt - now + 999) / 1000) + "s"));
return;
}
cooldowns.put(player.getUniqueId(), now + 4000L);
if (weapon.isRanged()) {
int arrows = ability == 1 ? 3 : ability == 2 ? 5 : 7;
fireVolley(player, arrows);
} else if (ability == 1) {
Vector dash = player.getLocation().getDirection().normalize().multiply(1.45);
dash.setY(weapon == Weapon.MACE ? 0.8 : Math.max(0.25, dash.getY()));
player.setVelocity(dash);
} else {
double radius = ability == 2 ? 4.5 : 6.0;
double damage = ability == 2 ? 5.0 : 8.0;
Location center = player.getLocation();
for (org.bukkit.entity.Entity entity : player.getNearbyEntities(radius, radius, radius)) {
if (!(entity instanceof LivingEntity target) || target == player || target.isDead()) {
continue;
}
target.damage(damage, player);
Vector push = target.getLocation().toVector().subtract(center.toVector());
if (push.lengthSquared() > 0) {
target.setVelocity(push.normalize().multiply(ability == 2 ? 0.8 : 1.2).setY(0.35));
}
}
}
player.sendMessage(color("&bUsed &f" + ABILITY_NAMES[ability - 1] + "&b!"));
}
private void fireVolley(Player player, int count) {
Location origin = player.getEyeLocation();
Vector direction = origin.getDirection().normalize();
for (int i = 0; i < count; i++) {
double angle = (i - (count - 1) / 2.0) * 0.08;
Arrow arrow = player.getWorld().spawnArrow(origin, direction.clone().rotateAroundY(angle), 2.4f, 0.0f);
arrow.setShooter(player);
arrow.setCritical(true);
}
}
private ItemStack createWeapon(Weapon weapon) {
ItemStack item = XMaterial.matchXMaterial(weapon.material)
.map(XMaterial::parseItem)
.orElseGet(() -> XMaterial.matchXMaterial("DIAMOND_SWORD").map(XMaterial::parseItem).orElse(null));
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(color("&6&l" + weapon.displayName));
List<String> lore = new ArrayList<>();
lore.add(color("&7A soulbound custom weapon"));
lore.add(color("&e2 kills &7- " + ABILITY_NAMES[0]));
lore.add(color("&e4 kills &7- " + ABILITY_NAMES[1]));
lore.add(color("&e6 kills &7- " + ABILITY_NAMES[2]));
lore.add(color("&8Right-click to use; sneak + right-click to cycle."));
meta.setLore(lore);
meta.getPersistentDataContainer().set(weaponKey, PersistentDataType.STRING, weapon.id);
item.setItemMeta(meta);
return item;
}
private int getKills(UUID playerId) {
return kills.getOrDefault(playerId, 0);
}
private int unlockedAbilities(UUID playerId) {
return Math.min(3, getKills(playerId) / 2);
}
private void savePlayerData() {
try {
if (!playersFile.getParentFile().exists() && !playersFile.getParentFile().mkdirs()) {