160 lines
5.4 KiB
Java

package net.knarcraft.dynmapcitizens;
import net.knarcraft.dynmapcitizens.formatting.DynmapCitizensTranslatableMessage;
import net.knarcraft.dynmapcitizens.handler.VaultHandler;
import net.knarcraft.dynmapcitizens.handler.trait.BlacksmithHandler;
import net.knarcraft.dynmapcitizens.handler.trait.CitizensTraitHandler;
import net.knarcraft.dynmapcitizens.handler.trait.MinstrelHandler;
import net.knarcraft.dynmapcitizens.handler.trait.SentinelHandler;
import net.knarcraft.dynmapcitizens.handler.trait.quests.QuestsHandler;
import net.knarcraft.dynmapcitizens.settings.GlobalSettings;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapAPI;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
@SuppressWarnings("unused")
public final class DynmapCitizens extends JavaPlugin {
private static DynmapCitizens instance;
private static Translator translator;
private DynmapAPI dynmapAPIInstance;
private VaultHandler vaultHandler;
private GlobalSettings globalSettings;
private List<CitizensTraitHandler> traitHandlers;
@Override
public void onEnable() {
DynmapCitizens.instance = this;
//Initialize quest and dynmap APIs
PluginManager pluginManager = Bukkit.getPluginManager();
Plugin dynmapPlugin = pluginManager.getPlugin("dynmap");
if (!(dynmapPlugin instanceof DynmapAPI dynmapAPI) || dynmapAPI.getMarkerAPI() == null) {
this.getLogger().log(Level.SEVERE, "Could not initialize Dynmap");
this.onDisable();
return;
}
this.dynmapAPIInstance = dynmapAPI;
this.globalSettings = new GlobalSettings();
FileConfiguration configuration = this.getConfig();
this.saveDefaultConfig();
configuration.options().copyDefaults(true);
this.saveConfig();
this.reloadConfig();
configuration = this.getConfig();
this.globalSettings.load(configuration);
//Load all messages
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(DynmapCitizensTranslatableMessage.SENTINEL_DESCRIPTION);
translator.loadLanguages(this.getDataFolder(), "en");
//Initialize all enabled traits
initializeTraitHandlers(configuration);
//Schedule handlers to run periodically
Bukkit.getScheduler().runTaskTimer(this, () -> {
for (CitizensTraitHandler handler : this.traitHandlers) {
if (handler.isEnabled()) {
handler.updateMarkers();
}
}
}, 10 * 20, this.globalSettings.getUpdateAllMarkersDelay() * 20);
this.vaultHandler = new VaultHandler(this.getServer().getServicesManager());
}
@Override
public void onDisable() {
//TODO: Perhaps remove icons, just in case?
}
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static Translator getTranslator() {
return translator;
}
/**
* Gets the global settings for this plugin
*
* @return <p>The global settings for this plugin</p>
*/
public GlobalSettings getGlobalSettings() {
return this.globalSettings;
}
/**
* Gets the Vault handler to use for anything Vault-related
*
* @return <p>The Vault handler</p>
*/
public VaultHandler getVaultHandler() {
return this.vaultHandler;
}
/**
* Gets a reference to the Dynmap API
*
* @return <p>A reference to the Dynmap API</p>
*/
public DynmapAPI getDynmapAPI() {
return this.dynmapAPIInstance;
}
/**
* Gets an instance of this plugin
*
* @return <p>An instance of this plugin</p>
*/
public static DynmapCitizens getInstance() {
return DynmapCitizens.instance;
}
/**
* Initializes all trait handlers
*
* @param configuration <p>The configuration to read settings from</p>
*/
private void initializeTraitHandlers(FileConfiguration configuration) {
//Register all trait handlers
this.traitHandlers = new ArrayList<>();
this.traitHandlers.add(new BlacksmithHandler());
this.traitHandlers.add(new QuestsHandler());
this.traitHandlers.add(new SentinelHandler());
this.traitHandlers.add(new MinstrelHandler());
//Load and initialize all enabled trait handlers
for (CitizensTraitHandler handler : this.traitHandlers) {
//Load configuration values from the config file
handler.getSettings().load(configuration);
//Check if the handler is enabled in the config before doing anything
if (!handler.getSettings().isEnabled()) {
getLogger().log(Level.INFO, handler + " is disabled");
continue;
}
//Initialize, and update markers if successfully enabled
handler.initialize();
if (handler.isEnabled()) {
getLogger().log(Level.INFO, handler + " is enabled");
handler.updateMarkers();
}
}
}
}