Skip to content

Commit

Permalink
Create logging module
Browse files Browse the repository at this point in the history
Create plugin configuration file to retrieve items from pom.xml
  • Loading branch information
IanTapply22 committed Jun 7, 2024
1 parent 9a73abb commit 6087654
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,10 @@
<version>24.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.7</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package net.jeqo.bloons.configuration;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.FileReader;
import java.io.IOException;

/**
* A class that contains configurations and information regarding the plugin
*/
public class PluginConfiguration {
// The developer credits for the plugin, displayed on startup
public static final String DEVELOPER_CREDITS = "Jeqo and Gucci Fox";

/**
* Get the version of the plugin from the pom.xml file
* @return The version of the plugin
* @throws IOException If the file cannot be read
* @throws XmlPullParserException If the file cannot be parsed
*/
public static String getVersion() throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
return model.getVersion();
}

/**
* Get the name of the plugin from the pom.xml file
* @return The name of the plugin
* @throws IOException If the file cannot be read
* @throws XmlPullParserException If the file cannot be parsed
*/
public static String getName() throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
return model.getName();
}

/**
* Get the description of the plugin from the pom.xml file
* @return The description of the plugin
* @throws IOException If the file cannot be read
* @throws XmlPullParserException If the file cannot be parsed
*/
public static String getDescription() throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
return model.getDescription();
}

/**
* Gets the website URL of the plugin from the pom.xml file
* @return The website URL of the plugin
* @throws IOException If the file cannot be read
* @throws XmlPullParserException If the file cannot be parsed
*/
public static String getURL() throws IOException, XmlPullParserException {
MavenXpp3Reader reader = new MavenXpp3Reader();
Model model = reader.read(new FileReader("pom.xml"));
return model.getUrl();
}
}
84 changes: 84 additions & 0 deletions src/main/java/net/jeqo/bloons/logger/Logger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.jeqo.bloons.logger;

import net.jeqo.bloons.configuration.PluginConfiguration;
import org.bukkit.Bukkit;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.IOException;

/**
* A utility class intended to log messages easily to the Bukkit console
*/
public class Logger {
/**
* Log a message to the console with STDOUT
* @param message The message to log
*/
public static void logWithSTDOUT(String message) {
System.out.println(message);
}

/**
* Log a message to the console
* @param level The logging level
* @param message The message to log
*/
public static void log(LoggingLevel level, String message) {
Bukkit.getServer().getConsoleSender().sendMessage(level.getColor() + "[" + level.getName() + "] " + message);
}

/**
* Logs a message to the console with the WARNING level
* @param message The message to log
*/
public static void logWarning(String message) {
log(LoggingLevel.WARNING, message);
}

/**
* Logs a message to the console with the INFO level
* @param message The message to log
*/
public static void logInfo(String message) {
log(LoggingLevel.INFO, message);
}

/**
* Logs a message to the console with the ERROR level
* @param message The message to log
*/
public static void logError(String message) {
log(LoggingLevel.ERROR, message);
}

/**
* Logs a message to the console with the DEBUG level
* @param message The message to log
*/
public static void logDebug(String message) {
log(LoggingLevel.DEBUG, message);
}

/**
* Logs an initialization message to the Bukkit console
*/
public static void logInitialization() throws XmlPullParserException, IOException {
log(LoggingLevel.INFO, "Initializing" + PluginConfiguration.getName() + " plugin...");
}

/**
* Logs a startup message to the Bukkit console containing plugin information
*/
public static void logStartup() throws XmlPullParserException, IOException {
log(LoggingLevel.INFO, PluginConfiguration.getName() + "plugin has initialized");
log(LoggingLevel.INFO, "Version: " + PluginConfiguration.getVersion());
log(LoggingLevel.INFO, "Developers: " + PluginConfiguration.DEVELOPER_CREDITS);
}

/**
* Logs a shutdown message to the Bukkit console
*/
public static void logShutdown() throws XmlPullParserException, IOException {
log(LoggingLevel.INFO, PluginConfiguration.getName() + "plugin has been shutdown gracefully");
}
}
29 changes: 29 additions & 0 deletions src/main/java/net/jeqo/bloons/logger/LoggingLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.jeqo.bloons.logger;

import lombok.Getter;
import net.kyori.adventure.text.format.NamedTextColor;

/**
* The different levels of logging that can be used in the plugin
* Contains the name and color of the logging level to use in the Bukkit console
*/
@Getter
public enum LoggingLevel {
WARNING("WARNING", NamedTextColor.RED),
INFO("INFO", NamedTextColor.YELLOW),
ERROR("ERROR", NamedTextColor.DARK_RED),
DEBUG("DEBUG", NamedTextColor.WHITE);

private final String name;
private final NamedTextColor color;

/**
* Create a new logging level
* @param name The name of the logging level to use in the console
* @param color The Minecraft chat color of the logging level
*/
LoggingLevel(String name, NamedTextColor color) {
this.name = name;
this.color = color;
}
}
5 changes: 5 additions & 0 deletions src/main/java/net/jeqo/bloons/logger/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Logger
This module is a custom logger designed to easily log messages to both STDOUT
and to the Bukkit server that is currently running this plugin. It efficiently and easily
works by allowing you to select a logging level and the text you want to push
to the source of the logging.

0 comments on commit 6087654

Please sign in to comment.