Files
src
build.gradle
manifest.kod
settings.gradle
Code
24 3,129
src/main/java/com/zylos/clans/ZylosClans.java
package com.zylos.clans;
import com.zylos.clans.command.ClanCommand;
import com.zylos.clans.dialog.ClanDialogs;
import com.zylos.clans.hook.ClanExpansion;
import com.zylos.clans.listener.PlayerListener;
import com.zylos.clans.service.ClanChat;
import com.zylos.clans.service.ClanService;
import com.zylos.clans.service.EconomyBridge;
import com.zylos.clans.service.NameTagService;
import com.zylos.clans.service.RankRegistry;
import com.zylos.clans.service.VaultEconomy;
import com.zylos.clans.storage.ClanStorage;
import com.zylos.clans.util.Text;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;
import java.util.logging.Level;
import lombok.Getter;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public final class ZylosClans extends JavaPlugin {
@Getter private RankRegistry ranks;
@Getter private ClanStorage storage;
@Getter private ClanService clans;
@Getter private ClanChat clanChat;
@Getter private NameTagService nameTags;
@Getter private ClanDialogs dialogs;
private FileConfiguration messages;
private FileConfiguration sounds;
private Runnable expansionCleanup;
@Override
public void onEnable() {
try {
saveDefaultConfig();
if (!new File(getDataFolder(), "messages.yml").exists()) {
saveResource("messages.yml", false);
}
if (!new File(getDataFolder(), "sounds.yml").exists()) {
saveResource("sounds.yml", false);
}
loadAuxiliaryConfigurations();
ranks = new RankRegistry(this);
ranks.reload();
storage = new ClanStorage(this);
EconomyBridge economy = Bukkit.getPluginManager().isPluginEnabled("Vault")
? new VaultEconomy(this) : EconomyBridge.unavailable();
clans = new ClanService(this, storage, ranks, economy);
clans.reload();
clanChat = new ClanChat(this, storage);
clanChat.reload();
nameTags = new NameTagService(this);
dialogs = new ClanDialogs(this);
ClanCommand executor = new ClanCommand(this);
PluginCommand command = Objects.requireNonNull(getCommand("clan"), "clan command is missing from plugin.yml");
command.setExecutor(executor);
command.setTabCompleter(executor);
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
ClanExpansion expansion = new ClanExpansion(this);
if (expansion.register()) {
expansionCleanup = expansion::unregister;
} else {
getLogger().warning("PlaceholderAPI rejected the ZylosClans expansion.");
}
}
nameTags.start();
for (Player online : Bukkit.getOnlinePlayers()) {
clans.onJoin(online);
}
} catch (Exception | LinkageError ex) {
getLogger().log(Level.SEVERE, "Could not enable ZylosClans", ex);
getServer().getPluginManager().disablePlugin(this);
}
}
@Override
public void onDisable() {
if (nameTags != null) {
nameTags.stop();
}
if (expansionCleanup != null) {
expansionCleanup.run();
}
if (storage != null) {
try {
storage.close();
} catch (SQLException ex) {
getLogger().log(Level.SEVERE, "Could not close the clan database", ex);
}
}
}
public boolean reloadAll() {
try {
reloadConfig();
loadAuxiliaryConfigurations();
ranks.reload();
clans.reload();
clanChat.reload();
nameTags.refresh();
return true;
} catch (Exception ex) {
getLogger().log(Level.SEVERE, "Could not reload ZylosClans", ex);
return false;
}
}
private void loadAuxiliaryConfigurations() throws IOException, InvalidConfigurationException {
YamlConfiguration newMessages = new YamlConfiguration();
YamlConfiguration newSounds = new YamlConfiguration();
newMessages.load(new File(getDataFolder(), "messages.yml"));
newSounds.load(new File(getDataFolder(), "sounds.yml"));
messages = newMessages;
sounds = newSounds;
}
public Component rawMessage(String key, String... replacements) {
String template = messages.getString(key, "<red>Missing message: " + key + "</red>");
try {
return Text.render(template, replacements);
} catch (RuntimeException ex) {
getLogger().warning("Invalid MiniMessage in messages.yml for " + key + ": " + ex.getMessage());
return Text.text(template);
}
}
public Component message(String key, String... replacements) {
return rawMessage("prefix").append(rawMessage(key, replacements));
}
public void send(CommandSender recipient, String key, String... replacements) {
recipient.sendMessage(message(key, replacements));
}
public void play(Player player, String key) {
String sound = sounds.getString(key + ".sound", "");
if (sound == null || sound.isBlank()) {
return;
}
double configuredVolume = sounds.getDouble(key + ".volume", 1.0);
double configuredPitch = sounds.getDouble(key + ".pitch", 1.0);
if (!Double.isFinite(configuredVolume) || !Double.isFinite(configuredPitch)) {
return;
}
float volume = (float) Math.max(0.0, Math.min(10.0, configuredVolume));
float pitch = (float) Math.max(0.5, Math.min(2.0, configuredPitch));
player.playSound(player.getLocation(), sound, volume, pitch);
}
}