Files
BlacksmithVisuals/src/main/java/net/knarcraft/blacksmithvisuals/BlacksmithVisuals.java
EpicKnarvik97 b094d34c62
All checks were successful
KnarCraft/BlacksmithVisuals/pipeline/head This commit looks good
Adds debug output and a new working position
2024-11-27 12:20:01 +01:00

141 lines
4.3 KiB
Java

package net.knarcraft.blacksmithvisuals;
import net.knarcraft.blacksmithvisuals.command.PlayTestSoundCommand;
import net.knarcraft.blacksmithvisuals.command.ReloadCommand;
import net.knarcraft.blacksmithvisuals.command.SetNPCPositionCommand;
import net.knarcraft.blacksmithvisuals.listener.BlacksmithListener;
import net.knarcraft.blacksmithvisuals.manager.ConfigurationManager;
import net.knarcraft.blacksmithvisuals.manager.NPCDataManager;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.util.logging.Level;
/**
* The blacksmith visual main class
*/
@SuppressWarnings("unused")
public final class BlacksmithVisuals extends JavaPlugin {
private static BlacksmithVisuals instance;
private ConfigurationManager configurationManager;
private NPCDataManager npcDataManager;
private boolean debug = false;
@Override
public void onEnable() {
BlacksmithVisuals.instance = this;
//Copy default config to disk, and add missing configuration values
this.saveDefaultConfig();
this.getConfig().options().copyDefaults(true);
this.reloadConfig();
this.saveConfig();
this.debug = this.getConfig().getBoolean("debug", false);
try {
this.configurationManager = new ConfigurationManager(this.getConfig());
} catch (InvalidConfigurationException exception) {
this.getLogger().log(Level.SEVERE, "Could not properly load the configuration file. " +
"Please check it for errors!");
this.onDisable();
return;
}
try {
this.npcDataManager = NPCDataManager.load();
} catch (IOException e) {
this.getLogger().log(Level.SEVERE, "Could not properly load the data.yml file. " +
"Please check it for errors!");
this.onDisable();
return;
}
getServer().getPluginManager().registerEvents(new BlacksmithListener(this.configurationManager), this);
registerCommand("reload", new ReloadCommand());
registerCommand("setNPCPosition", new SetNPCPositionCommand());
registerCommand("playTestSound", new PlayTestSoundCommand());
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
/**
* Logs a debug message
*
* @param message <p>The debug message to log</p>
*/
public static void debug(@NotNull String message) {
BlacksmithVisuals instance = getInstance();
if (instance.debug) {
instance.getLogger().log(Level.INFO, "[Debug] " + message);
} else {
instance.getLogger().log(Level.FINE, message);
}
}
/**
* Reloads the configuration file
*/
public void reload() {
reloadConfig();
saveConfig();
try {
this.configurationManager.load(getConfig());
} catch (InvalidConfigurationException exception) {
this.getLogger().log(Level.SEVERE, "Could not properly load the configuration file. " +
"Please check it for errors!");
this.onDisable();
}
}
/**
* Registers a command
*
* @param commandName <p>The name of the command</p>
* @param executor <p>The executor to bind to the command</p>
*/
private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor executor) {
PluginCommand command = this.getCommand(commandName);
if (command != null) {
command.setExecutor(executor);
}
}
/**
* Gets the NPC data manager
*
* @return <p>The NPC data manager</p>
*/
@NotNull
public NPCDataManager getNpcDataManager() {
return this.npcDataManager;
}
/**
* Gets the configuration manager
*
* @return <p>The configuration manager</p>
*/
@NotNull
public ConfigurationManager getConfigurationManager() {
return this.configurationManager;
}
/**
* Gets an instance of this plugin
*
* @return <p>An instance of this plugin</p>
*/
@NotNull
public static BlacksmithVisuals getInstance() {
return instance;
}
}