Files
src
build.gradle
manifest.kod
README.md
settings.gradle
Code
26 2,880
src/main/java/com/kodari/animedungeonrpg/AnimeDungeonRPGPlugin.java
package com.kodari.animedungeonrpg;
import com.kodari.animedungeonrpg.inventory.gui.GUIListener;
import com.kodari.animedungeonrpg.inventory.gui.GUIManager;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public final class AnimeDungeonRPGPlugin extends JavaPlugin {
private final Map<String, YamlConfiguration> rpgConfigs = new HashMap<>();
private final Map<String, File> rpgConfigFiles = new HashMap<>();
private Database database;
private RpgService rpgService;
private GuiService guiService;
private GUIManager guiManager;
private Economy economy;
private NamespacedKey monsterKey;
private NamespacedKey bossKey;
private NamespacedKey npcKey;
@Override
public void onEnable() {
saveDefaultConfig();
for (String name : new String[]{"ranks", "monsters", "dungeons", "quests", "skills"}) {
File file = new File(getDataFolder(), name + ".yml");
if (!file.exists()) {
saveResource(name + ".yml", false);
}
rpgConfigFiles.put(name, file);
}
reloadRpgConfigs();
monsterKey = new NamespacedKey(this, "monster-id");
bossKey = new NamespacedKey(this, "boss-id");
npcKey = new NamespacedKey(this, "npc-type");
setupEconomy();
try {
database = new Database(new File(getDataFolder(), "players.db"));
} catch (SQLException | ClassNotFoundException exception) {
getLogger().severe("Could not open the player SQLite database: " + exception.getMessage());
Bukkit.getPluginManager().disablePlugin(this);
return;
}
rpgService = new RpgService(this, database);
guiManager = new GUIManager();
guiService = new GuiService(this, rpgService, guiManager);
getServer().getScheduler().runTaskTimer(this, rpgService::regenerateMana, 1200L, 1200L);
getServer().getPluginManager().registerEvents(new GUIListener(guiManager), this);
getServer().getPluginManager().registerEvents(new RpgListener(this, rpgService, guiService), this);
getCommand("anime").setExecutor(new AnimeCommand(this, rpgService, guiService));
getCommand("animerpgadmin").setExecutor(new AdminCommand(guiService));
for (Player player : Bukkit.getOnlinePlayers()) {
rpgService.loadProfile(player);
}
getLogger().info("AnimeDungeonRPG enabled with SQLite player storage.");
}
@Override
public void onDisable() {
if (rpgService != null) {
rpgService.saveAll();
}
if (database != null) {
try {
database.close();
} catch (SQLException exception) {
getLogger().warning("Could not close SQLite cleanly: " + exception.getMessage());
}
}
}
private void setupEconomy() {
if (!getConfig().getBoolean("economy.use-vault", false)) {
return;
}
if (getServer().getPluginManager().getPlugin("Vault") == null) {
getLogger().warning("Vault economy was requested but Vault is not installed; using the built-in Gold wallet.");
return;
}
RegisteredServiceProvider<Economy> registration = getServer().getServicesManager().getRegistration(Economy.class);
if (registration != null) {
economy = registration.getProvider();
}
if (economy == null) {
getLogger().warning("Vault has no economy provider; using the built-in Gold wallet.");
}
}
public void reloadRpgConfigs() {
for (Map.Entry<String, File> entry : rpgConfigFiles.entrySet()) {
rpgConfigs.put(entry.getKey(), YamlConfiguration.loadConfiguration(entry.getValue()));
}
}
public void saveRpgConfig(String name) throws IOException {
YamlConfiguration config = rpgConfigs.get(name);
File file = rpgConfigFiles.get(name);
if (config != null && file != null) {
config.save(file);
}
}
public YamlConfiguration rpgConfig(String name) {
return rpgConfigs.get(name);
}
public File rpgConfigFile(String name) {
return rpgConfigFiles.get(name);
}
public RpgService rpg() {
return rpgService;
}
public Economy economy() {
return economy;
}
public NamespacedKey monsterKey() {
return monsterKey;
}
public NamespacedKey bossKey() {
return bossKey;
}
public NamespacedKey npcKey() {
return npcKey;
}
public static String color(String text) {
return ChatColor.translateAlternateColorCodes('&', text == null ? "" : text);
}
}