Blacksmith/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java
EpicKnarvik97 e39e70b690 Merge branch 'master' into dev
# Conflicts:
#	src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java
2023-11-12 19:40:51 +01:00

207 lines
6.9 KiB
Java

package net.knarcraft.blacksmith;
import net.citizensnpcs.api.CitizensAPI;
import net.knarcraft.blacksmith.command.BlackSmithConfigCommand;
import net.knarcraft.blacksmith.command.BlackSmithConfigTabCompleter;
import net.knarcraft.blacksmith.command.BlackSmithEditCommand;
import net.knarcraft.blacksmith.command.BlackSmithEditTabCompleter;
import net.knarcraft.blacksmith.command.PresetCommand;
import net.knarcraft.blacksmith.command.PresetTabCompleter;
import net.knarcraft.blacksmith.config.GlobalSettings;
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.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.PluginCommand;
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 java.io.File;
import java.util.logging.Level;
/**
* Blacksmith's main class
*/
public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private GlobalSettings config;
private static Translator translator;
private static StringFormatter stringFormatter;
/**
* Constructor required for MockBukkit
*/
@SuppressWarnings("unused")
public BlacksmithPlugin() {
super();
}
/**
* Constructor required for MockBukkit
*/
@SuppressWarnings("unused")
protected BlacksmithPlugin(JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, 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 BlacksmithPlugin getInstance() {
return instance;
}
/**
* Gets settings for the blacksmith plugin
*
* @return <p>Settings for the blacksmith plugin</p>
*/
public GlobalSettings getSettings() {
return config;
}
/**
* Reloads the configuration file from disk
*/
public void reload() {
this.reloadConfig();
config.load();
translator.loadLanguages(this.getDataFolder(), "en", this.getConfig().getString("language", "en"));
}
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static Translator getTranslator() {
return BlacksmithPlugin.translator;
}
/**
* Gets the string formatter to use for formatting
*
* @return <p>The string formatter to use</p>
*/
public static StringFormatter getStringFormatter() {
return BlacksmithPlugin.stringFormatter;
}
@Override
public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
}
@Override
public void onEnable() {
instance = this;
//Copy default config to disk
FileConfiguration fileConfiguration = this.getConfig();
fileConfiguration.options().copyDefaults(true);
this.saveDefaultConfig();
this.reloadConfig();
this.saveConfig();
//Load settings
config = new GlobalSettings(this);
config.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);
//Set up Vault integration
if (!setUpVault()) {
return;
}
//Register the blacksmith trait with Citizens
CitizensAPI.getTraitFactory().registerTrait(
net.citizensnpcs.api.trait.TraitInfo.create(BlacksmithTrait.class).withName("blacksmith"));
//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);
}
/**
* 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() {
//Register the blacksmith NPC edit main-command
PluginCommand blacksmithCommand = this.getCommand("blacksmith");
if (blacksmithCommand != null) {
blacksmithCommand.setExecutor(new BlackSmithEditCommand());
blacksmithCommand.setTabCompleter(new BlackSmithEditTabCompleter());
}
//Register the global config edit command
PluginCommand blacksmithConfigCommand = this.getCommand("blacksmithConfig");
if (blacksmithConfigCommand != null) {
blacksmithConfigCommand.setExecutor(new BlackSmithConfigCommand());
blacksmithConfigCommand.setTabCompleter(new BlackSmithConfigTabCompleter());
}
PluginCommand presetCommand = this.getCommand("preset");
if (presetCommand != null) {
presetCommand.setExecutor(new PresetCommand());
presetCommand.setTabCompleter(new PresetTabCompleter());
}
}
}