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.blacksmith.GlobalBlacksmithSettings; import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings; import net.knarcraft.blacksmith.formatting.Translatable; import net.knarcraft.blacksmith.listener.NPCClickListener; import net.knarcraft.blacksmith.listener.PlayerListener; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.PlayerUsageManager; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait; import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.plugin.ConfigCommentPlugin; import net.knarcraft.knarlib.util.ConfigHelper; import net.knarcraft.knarlib.util.UpdateChecker; import org.bukkit.Bukkit; 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.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPluginLoader; import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.logging.Level; /** * Blacksmith's main class */ public class BlacksmithPlugin extends ConfigCommentPlugin { private static BlacksmithPlugin instance; private GlobalBlacksmithSettings blacksmithConfig; private GlobalScrapperSettings scrapperConfig; private static Translator translator; /** * 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
An instance of the blacksmith plugin
*/ public static @NotNull BlacksmithPlugin getInstance() { return instance; } /** * Gets settings for the blacksmith plugin's blacksmiths * * @returnSettings for the blacksmith plugin's blacksmith
*/ public @NotNull GlobalBlacksmithSettings getGlobalBlacksmithSettings() { return blacksmithConfig; } /** * Gets settings for the blacksmith plugin's scrapper * * @returnSettings for the blacksmith plugin's scrapper
*/ 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.getConfig().getString( "language", "en")); } /** * Gets the translator to use for translation * * @returnThe translator to use
*/ public static @NotNull Translator getTranslator() { return BlacksmithPlugin.translator; } @Override public void onDisable() { log(" v" + getDescription().getVersion() + " disabled."); } @Override public void onEnable() { instance = this; ConfigHelper.saveDefaults(this); //Migrate from an earlier configuration file syntax if necessary if (getConfig().getString("scrapper.defaults.dropItem") == null) { net.knarcraft.knarlib.util.ConfigHelper.migrateConfig(this); } initializeConfigurations(getConfig()); //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(); log(" 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); // Remove expired scrapper usage data Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> PlayerUsageManager.removeExpiredData( System.currentTimeMillis() - 3600000), 36000, 36000); } /** * Calls an event * * @param eventThe event to call
*/ public void callEvent(@NotNull Event event) { this.getServer().getPluginManager().callEvent(event); } /** * Prints an info message to the console * * @param messageThe message to print
*/ public static void log(@NotNull String message) { BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, message); } /** * Prints a warning message to the console * * @param messageThe message to print
*/ public static void warn(@NotNull String message) { BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, message); } /** * Prints an error message to the console * * @param messageThe message to print
*/ public static void error(@NotNull String message) { BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, message); } /** * Prints a debug message to the console * * @param messageThe message to print
*/ public static void debug(@NotNull String message) { BlacksmithPlugin.getInstance().getLogger().log(Level.FINE, message); } /** * Initializes custom configuration and translation * * @param fileConfigurationThe configuration file to get values from
*/ 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(Translatable.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(); } FormatBuilder.setStringFormatter(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 * * @returnTrue if Vault setup/integration succeeded
*/ private boolean setUpVault() { log("Setting Up Vault now...."); boolean canLoad = EconomyManager.setUp(getServer().getServicesManager()); if (!canLoad) { error("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()); } }