Files
src
pom.xml
README.md
Code
19 1,692
src/main/java/com/kodari/elementalbending/ElementalBendingPlugin.java
package com.kodari.elementalbending;
import com.kodari.elementalbending.ability.AbilityCatalog;
import com.kodari.elementalbending.ability.AbilityService;
import com.kodari.elementalbending.ability.CooldownService;
import com.kodari.elementalbending.api.ElementalBendingAPI;
import com.kodari.elementalbending.command.ElementCommand;
import com.kodari.elementalbending.data.PlayerStore;
import com.kodari.elementalbending.element.ElementRegistry;
import com.kodari.elementalbending.listener.BendingListener;
import com.kodari.elementalbending.model.ElementInfo;
import com.kodari.elementalbending.progression.ProgressionService;
import com.kodari.elementalbending.util.Text;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException;
import java.util.List;
public final class ElementalBendingPlugin extends JavaPlugin {
private ElementRegistry elements;
private AbilityCatalog abilities;
private CooldownService cooldowns;
private PlayerStore store;
private ProgressionService progression;
private AbilityService abilityService;
private ElementalBendingAPI api;
@Override
public void onEnable() {
saveDefaultConfig();
elements = new ElementRegistry();
abilities = new AbilityCatalog();
cooldowns = new CooldownService();
try {
store = new PlayerStore(this);
} catch (SQLException exception) {
getLogger().severe("Could not initialize player database: " + exception.getMessage());
getServer().getPluginManager().disablePlugin(this);
return;
}
progression = new ProgressionService(this, store, elements, abilities);
abilityService = new AbilityService(this, abilities, elements, progression, cooldowns);
api = new ElementalBendingAPI(this);
getServer().getPluginManager().registerEvents(new BendingListener(this), this);
ElementCommand command = new ElementCommand(this);
getCommand("element").setExecutor(command);
getCommand("element").setTabCompleter(command);
getServer().getScheduler().runTaskTimer(this, () -> {
if (!getConfig().getBoolean("control.show-cooldowns-in-action-bar", true)) return;
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers()) {
var profile = progression.getOrCreate(player.getUniqueId());
var selected = abilities.get(profile.getSelectedAbility());
if (selected == null) continue;
long remaining = abilityService.cooldownRemaining(player.getUniqueId(), selected.id());
if (remaining > 0) {
player.sendActionBar(Text.color("&c" + selected.name() + " &7- &f" + String.format(java.util.Locale.US, "%.1f", remaining / 1000.0) + "s"));
}
}
}, 20L, 20L);
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers()) {
progression.getOrCreate(player.getUniqueId());
}
getLogger().info("ElementalBending enabled with " + abilities.all().size() + " built-in abilities.");
}
@Override
public void onDisable() {
if (store != null) {
for (org.bukkit.entity.Player player : Bukkit.getOnlinePlayers()) {
if (progression != null) progression.unload(player.getUniqueId());
}
try {
store.close();
} catch (SQLException exception) {
getLogger().warning("Could not close player database cleanly: " + exception.getMessage());
}
}
}
public boolean isWorldEnabled(String worldName) {
List<String> enabledWorlds = getConfig().getStringList("enabled-worlds");
return enabledWorlds.isEmpty() || enabledWorlds.stream().anyMatch(name -> name.equalsIgnoreCase(worldName));
}
public void playAwakening(org.bukkit.entity.Player player, ElementInfo element) {
String configuredSound = getConfig().getString("elements." + element.id() + ".sound", element.sound());
XSound.matchXSound(configuredSound).ifPresent(sound -> sound.play(player));
XSound.matchXSound("ENTITY_PLAYER_LEVELUP").ifPresent(sound -> sound.play(player));
}
public void playLevelUp(org.bukkit.entity.Player player) {
XSound.matchXSound("ENTITY_PLAYER_LEVELUP").ifPresent(sound -> sound.play(player));
}
public void spawnElementParticles(org.bukkit.entity.Player player, ElementInfo element, int amount) {
Location location = player.getLocation().add(0, 1, 0);
int count = Math.min(amount, getConfig().getInt("anti-exploit.maximum-particle-count", 48));
String particleName = getConfig().getString("elements." + element.id() + ".particle", element.particle());
XParticle.of(particleName).ifPresent(particle -> player.getWorld().spawnParticle(particle.get(), location, Math.max(1, count)));
}
public ElementRegistry getElements() { return elements; }
public AbilityCatalog getAbilities() { return abilities; }
public CooldownService getCooldowns() { return cooldowns; }
public ProgressionService getProgression() { return progression; }
public AbilityService getAbilityService() { return abilityService; }
public ElementalBendingAPI getAPI() { return api; }
}