Files
src
build.gradle
Code
5 460
src/main/java/com/kodari/elementalpaths/ElementalPathsPlugin.java
package com.kodari.elementalpaths;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public final class ElementalPathsPlugin extends JavaPlugin implements CommandExecutor {
private final Map<UUID, Element> playerElements = new HashMap<>();
private AbilityManager abilityManager;
@Override
public void onEnable() {
loadPlayerElements();
abilityManager = new AbilityManager(this);
getCommand("elements").setExecutor(this);
}
@Override
public void onDisable() {
getConfig().set("players", null);
for (Map.Entry<UUID, Element> entry : playerElements.entrySet()) {
getConfig().set("players." + entry.getKey(), entry.getValue().getKey());
}
saveConfig();
}
public Element getElement(Player player) {
return playerElements.get(player.getUniqueId());
}
private void loadPlayerElements() {
if (getConfig().getConfigurationSection("players") == null) {
return;
}
for (String id : getConfig().getConfigurationSection("players").getKeys(false)) {
try {
Element element = Element.fromKey(getConfig().getString("players." + id, ""));
if (element != null) {
playerElements.put(UUID.fromString(id), element);
}
} catch (IllegalArgumentException ignored) {
}
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) {
sender.sendMessage("This command can only be used by a player.");
return true;
}
if (args.length == 0) {
showHelp(player);
return true;
}
if (args[0].equalsIgnoreCase("choose")) {
if (args.length < 2) {
player.sendMessage(ChatColor.YELLOW + "Choose one: ember, tide, stone, or gale.");
return true;
}
Element element = Element.fromKey(args[1]);
if (element == null) {
player.sendMessage(ChatColor.RED + "Unknown element. Choose ember, tide, stone, or gale.");
return true;
}
playerElements.put(player.getUniqueId(), element);
player.sendMessage(ChatColor.GOLD + "Your path is now " + element.getDisplayName() + ".");
player.sendMessage(ChatColor.GRAY + "Abilities: " + String.join(", ", element.getAbilities()));
return true;
}
if (args[0].equalsIgnoreCase("abilities")) {
Element element = getElement(player);
if (element == null) {
player.sendMessage(ChatColor.YELLOW + "Choose an element first with /elements choose <element>.");
} else {
player.sendMessage(ChatColor.GOLD + element.getDisplayName() + " abilities: "
+ ChatColor.WHITE + String.join(", ", element.getAbilities()));
player.sendMessage(ChatColor.GRAY + "Cast one with /elements cast <ability>.");
}
return true;
}
if (args[0].equalsIgnoreCase("cast")) {
if (args.length < 2) {
player.sendMessage(ChatColor.YELLOW + "Usage: /elements cast <ability>");
return true;
}
Element element = getElement(player);
if (element == null) {
player.sendMessage(ChatColor.YELLOW + "Choose an element first with /elements choose <element>.");
return true;
}
abilityManager.cast(player, element, args[1].toLowerCase());
return true;
}
if (args[0].equalsIgnoreCase("info")) {
Element element = getElement(player);
player.sendMessage(element == null
? ChatColor.YELLOW + "You have not chosen an element."
: ChatColor.GOLD + "Your element: " + element.getDisplayName());
return true;
}
showHelp(player);
return true;
}
private void showHelp(Player player) {
player.sendMessage(ChatColor.GOLD + "Elemental Paths");
player.sendMessage(ChatColor.YELLOW + "/elements choose <ember|tide|stone|gale>");
player.sendMessage(ChatColor.YELLOW + "/elements abilities" + ChatColor.GRAY + " - list your abilities");
player.sendMessage(ChatColor.YELLOW + "/elements cast <ability>" + ChatColor.GRAY + " - use an ability");
player.sendMessage(ChatColor.YELLOW + "/elements info" + ChatColor.GRAY + " - show your element");
}
}