Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
27 1,729
src/main/java/com/kodari/profilemanager/ProfileManagerPlugin.java
package com.kodari.profilemanager;
import com.kodari.profilemanager.command.ProfileCommand;
import com.kodari.profilemanager.database.DatabaseManager;
import com.kodari.profilemanager.database.ProfileRepository;
import com.kodari.profilemanager.gui.ProfileGUIListener;
import com.kodari.profilemanager.gui.ProfileGUIManager;
import com.kodari.profilemanager.profile.ProfileAPI;
import com.kodari.profilemanager.profile.ProfileComponentRegistry;
import com.kodari.profilemanager.profile.ProfileListener;
import com.kodari.profilemanager.profile.ProfileService;
import com.kodari.profilemanager.reputation.ReputationManager;
import com.kodari.profilemanager.social.SocialInputManager;
import com.kodari.profilemanager.statistics.StatisticsManager;
import com.kodari.profilemanager.util.MessageUtil;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.sql.SQLException;
import java.util.concurrent.CompletionException;
public final class ProfileManagerPlugin extends JavaPlugin {
private YamlConfiguration messages;
private YamlConfiguration profileGuiConfig;
private YamlConfiguration socialGuiConfig;
private DatabaseManager database;
private ProfileService profileService;
private ProfileAPI api;
private StatisticsManager statistics;
private ProfileComponentRegistry components;
private ProfileGUIManager guis;
private SocialInputManager socialInput;
private ReputationManager reputation;
private ProfileListener profileListener;
private MessageUtil messageUtil;
private Object placeholderExpansion;
@Override
public void onEnable() {
saveDefaultConfig();
saveResourceIfMissing("messages.yml");
saveResourceIfMissing("guis/profile.yml");
saveResourceIfMissing("guis/socials.yml");
loadConfigurationFiles();
messageUtil = new MessageUtil(this);
try {
database = new DatabaseManager(this, getConfig().getString("database.file", "data.db"));
ProfileRepository repository = new ProfileRepository(database);
database.execute(() -> {
repository.initialize();
return null;
}).exceptionally(error -> {
getServer().getScheduler().runTask(this, () -> {
logDatabaseError("Could not initialize the SQLite schema", error);
getServer().getPluginManager().disablePlugin(this);
});
return null;
});
profileService = new ProfileService(database, repository);
} catch (SQLException | CompletionException exception) {
getLogger().severe("Could not initialize the SQLite database: " + exception.getMessage());
getServer().getPluginManager().disablePlugin(this);
return;
}
statistics = new StatisticsManager(this);
components = new ProfileComponentRegistry();
api = new ProfileAPI(profileService, statistics, components);
guis = new ProfileGUIManager(this);
socialInput = new SocialInputManager(this);
reputation = new ReputationManager(this, profileService);
profileListener = new ProfileListener(this);
getServer().getPluginManager().registerEvents(new ProfileGUIListener(guis), this);
getServer().getPluginManager().registerEvents(socialInput, this);
getServer().getPluginManager().registerEvents(profileListener, this);
registerPlaceholderExpansion();
ProfileCommand profileCommand = new ProfileCommand(this);
if (getCommand("profile") != null) {
getCommand("profile").setExecutor(profileCommand);
getCommand("profile").setTabCompleter(profileCommand);
}
profileListener.registerOnlinePlayers();
}
@Override
public void onDisable() {
unregisterPlaceholderExpansion();
if (profileListener != null) {
profileListener.persistActiveSessions();
}
if (database != null) {
database.close();
}
}
public void loadConfigurationFiles() {
messages = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "messages.yml"));
profileGuiConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "guis/profile.yml"));
socialGuiConfig = YamlConfiguration.loadConfiguration(new File(getDataFolder(), "guis/socials.yml"));
}
private void saveResourceIfMissing(String path) {
File file = new File(getDataFolder(), path);
if (!file.exists()) {
saveResource(path, false);
}
}
private void registerPlaceholderExpansion() {
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") == null) {
return;
}
try {
Class<?> expansionClass = Class.forName("com.kodari.profilemanager.placeholder.ProfilePlaceholderExpansion");
Object expansion = expansionClass.getConstructor(ProfileManagerPlugin.class).newInstance(this);
expansionClass.getMethod("register").invoke(expansion);
getServer().getPluginManager().registerEvents((Listener) expansion, this);
placeholderExpansion = expansion;
} catch (ReflectiveOperationException | ClassCastException exception) {
getLogger().warning("PlaceholderAPI integration could not be enabled: " + exception.getMessage());
}
}
private void unregisterPlaceholderExpansion() {
if (placeholderExpansion == null) {
return;
}
try {
placeholderExpansion.getClass().getMethod("unregister").invoke(placeholderExpansion);
} catch (ReflectiveOperationException exception) {
getLogger().warning("Could not unregister PlaceholderAPI expansion: " + exception.getMessage());
}
}
public String applyPlaceholderAPI(Player viewer, String text) {
if (getServer().getPluginManager().getPlugin("PlaceholderAPI") == null) {
return text;
}
try {
Class<?> placeholderApi = Class.forName("me.clip.placeholderapi.PlaceholderAPI");
return (String) placeholderApi.getMethod("setPlaceholders", org.bukkit.OfflinePlayer.class, String.class)
.invoke(null, viewer, text);
} catch (ReflectiveOperationException exception) {
getLogger().warning("Could not parse PlaceholderAPI text: " + exception.getMessage());
return text;
}
}
public YamlConfiguration getMessages() {
return messages;
}
public YamlConfiguration getProfileGuiConfig() {
return profileGuiConfig;
}
public YamlConfiguration getSocialGuiConfig() {
return socialGuiConfig;
}
public MessageUtil messages() {
return messageUtil;
}
public ProfileService profileService() {
return profileService;
}
public ProfileAPI api() {
return api;
}
public StatisticsManager statistics() {
return statistics;
}
public ProfileGUIManager guis() {
return guis;
}
public SocialInputManager socialInput() {
return socialInput;
}
public ReputationManager reputation() {
return reputation;
}
public void logDatabaseError(String action, Throwable error) {
Throwable cause = error instanceof CompletionException && error.getCause() != null ? error.getCause() : error;
getLogger().severe(action + ": " + cause.getMessage());
}
}