Blacksmith/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java
EpicKnarvik97 d7bac6d08f
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
Fixes missing config values not loading properly until a reload
2024-05-05 18:38:46 +02:00

295 lines
11 KiB
Java

package net.knarcraft.blacksmith;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.trait.TraitFactory;
import net.citizensnpcs.api.trait.TraitInfo;
import net.knarcraft.blacksmith.command.PresetCommand;
import net.knarcraft.blacksmith.command.PresetTabCompleter;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithConfigCommand;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithConfigTabCompleter;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithEditCommand;
import net.knarcraft.blacksmith.command.blacksmith.BlackSmithEditTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter;
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.listener.NPCClickListener;
import net.knarcraft.blacksmith.listener.PlayerListener;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.trait.ScrapperTrait;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.util.UpdateChecker;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
/**
* Blacksmith's main class
*/
public class BlacksmithPlugin extends JavaPlugin {
private static final String CONFIG_FILE_NAME = "config.yml";
private static BlacksmithPlugin instance;
private GlobalBlacksmithSettings blacksmithConfig;
private GlobalScrapperSettings scrapperConfig;
private static Translator translator;
private static StringFormatter stringFormatter;
private FileConfiguration configuration;
/**
* Constructor required for MockBukkit
*/
@SuppressWarnings("unused")
public BlacksmithPlugin() {
super();
}
/**
* Constructor required for MockBukkit
*/
@SuppressWarnings("unused")
protected BlacksmithPlugin(@NotNull JavaPluginLoader loader, @NotNull PluginDescriptionFile descriptionFile,
@NotNull File dataFolder, @NotNull File file) {
super(loader, descriptionFile, dataFolder, file);
}
/**
* Gets an instance of the Blacksmith plugin
*
* @return <p>An instance of the blacksmith plugin</p>
*/
public static @NotNull BlacksmithPlugin getInstance() {
return instance;
}
/**
* Gets settings for the blacksmith plugin's blacksmiths
*
* @return <p>Settings for the blacksmith plugin's blacksmith</p>
*/
public @NotNull GlobalBlacksmithSettings getGlobalBlacksmithSettings() {
return blacksmithConfig;
}
/**
* Gets settings for the blacksmith plugin's scrapper
*
* @return <p>Settings for the blacksmith plugin's scrapper</p>
*/
public @NotNull GlobalScrapperSettings getGlobalScrapperSettings() {
return scrapperConfig;
}
/**
* Reloads the configuration file from disk
*/
public void reload() {
this.reloadConfig();
blacksmithConfig.load();
scrapperConfig.load();
translator.loadLanguages(this.getDataFolder(), "en", this.getConfiguration().getString(
"language", "en"));
}
/**
* Gets the raw configuration
*
* @return <p>The raw configuration</p>
*/
@NotNull
public FileConfiguration getConfiguration() {
return this.configuration;
}
/**
* Gets the string formatter to use for formatting
*
* @return <p>The string formatter to use</p>
*/
public static @NotNull StringFormatter getStringFormatter() {
return BlacksmithPlugin.stringFormatter;
}
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static @NotNull Translator getTranslator() {
return BlacksmithPlugin.translator;
}
@Override
public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
}
@Override
public void onEnable() {
instance = this;
//Copy default config to disk, and add missing configuration values
this.saveDefaultConfig();
this.getConfig().options().copyDefaults(true);
this.reloadConfig();
this.saveConfig();
//Migrate from an earlier configuration file syntax if necessary
if (getConfiguration().getString("scrapper.defaults.dropItem") == null) {
ConfigHelper.migrateConfig(this.getDataFolder().getPath().replaceAll("\\\\", "/"),
getConfiguration());
this.reloadConfig();
}
initializeConfigurations(getConfiguration());
//Set up Vault integration
if (!setUpVault()) {
return;
}
//Register the blacksmith trait with Citizens
TraitFactory traitFactory = CitizensAPI.getTraitFactory();
traitFactory.registerTrait(TraitInfo.create(BlacksmithTrait.class).withName("blacksmith"));
traitFactory.registerTrait(TraitInfo.create(ScrapperTrait.class).withName("scrapper"));
//Register all commands
registerCommands();
//Register all listeners
registerListeners();
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
//Alert about an update in the console
UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=105938",
() -> this.getDescription().getVersion(), null);
}
@Override
public void reloadConfig() {
super.reloadConfig();
this.configuration = new StargateYamlConfiguration();
this.configuration.options().copyDefaults(true);
try {
this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException | InvalidConfigurationException exception) {
getLogger().log(Level.SEVERE, "Unable to load the configuration! Message: " + exception.getMessage());
}
}
@Override
public void saveConfig() {
super.saveConfig();
try {
this.configuration.save(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException exception) {
getLogger().log(Level.SEVERE, "Unable to save the configuration! Message: " + exception.getMessage());
}
}
/**
* Initializes custom configuration and translation
*
* @param fileConfiguration <p>The configuration file to get values from</p>
*/
private void initializeConfigurations(@NotNull FileConfiguration fileConfiguration) {
//Load settings
blacksmithConfig = new GlobalBlacksmithSettings(this);
blacksmithConfig.load();
scrapperConfig = new GlobalScrapperSettings(this);
scrapperConfig.load();
//Prepare the translator
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(this.getDataFolder(), "en",
fileConfiguration.getString("language", "en"));
PluginDescriptionFile description = this.getDescription();
String prefix;
if (description.getPrefix() == null) {
prefix = "Blacksmith";
} else {
prefix = description.getPrefix();
}
BlacksmithPlugin.stringFormatter = new StringFormatter(prefix, translator);
// This reload is necessary to get values just added to the configuration for some reason
this.reload();
}
/**
* Tries to set up Vault
*
* @return <p>True if Vault setup/integration succeeded</p>
*/
private boolean setUpVault() {
getLogger().log(Level.INFO, "Setting Up Vault now....");
boolean canLoad = EconomyManager.setUp(getServer().getServicesManager(), getLogger());
if (!canLoad) {
getLogger().log(Level.SEVERE, "Vault Integration Failed....");
getServer().getPluginManager().disablePlugin(this);
return false;
}
return true;
}
/**
* Registers all listeners used by this plugin
*/
private void registerListeners() {
PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new PlayerListener(), this);
pluginManager.registerEvents(new NPCClickListener(), this);
}
/**
* Registers all commands used by this plugin
*/
private void registerCommands() {
registerCommand("blacksmith", new BlackSmithEditCommand(), new BlackSmithEditTabCompleter());
registerCommand("blacksmithConfig", new BlackSmithConfigCommand(), new BlackSmithConfigTabCompleter());
registerCommand("scrapper", new ScrapperEditCommand(), new ScrapperEditTabCompleter());
registerCommand("scrapperConfig", new ScrapperConfigCommand(), new ScrapperConfigTabCompleter());
registerCommand("preset", new PresetCommand(), new PresetTabCompleter());
}
/**
* Registers a command
*
* @param commandName <p>The name of the command</p>
* @param executor <p>The executor to bind to the command</p>
* @param tabCompleter <p>The tab completer to bind to the command, or null</p>
*/
private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor executor,
@Nullable TabCompleter tabCompleter) {
PluginCommand command = this.getCommand(commandName);
if (command != null) {
command.setExecutor(executor);
if (tabCompleter != null) {
command.setTabCompleter(tabCompleter);
}
}
}
}