Reworked classe.
This commit is contained in:
parent
f526c803fa
commit
b317749ed0
10 changed files with 145 additions and 38 deletions
11
.classpath
Normal file
11
.classpath
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="lib" path="libs/craftbukkit-1060.jar"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/bin/
|
||||
17
.project
Normal file
17
.project
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>tenkumaLib</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
5
.settings/org.eclipse.jdt.core.prefs
Normal file
5
.settings/org.eclipse.jdt.core.prefs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
|
|
@ -1,13 +1,31 @@
|
|||
package gd.rf.adrianvictor.lib;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class Color {
|
||||
public String formatColors(String message) {
|
||||
return message.replaceAll("&([0-9a-fk-or])", "§$1");
|
||||
public static String formatColors(String message) {
|
||||
return message.replaceAll("&", "§");
|
||||
}
|
||||
|
||||
public Object[] ignoreColors(String message) {
|
||||
public static Object[] ignoreColors(String message) {
|
||||
String parsed = message.replaceAll("&([0-9a-fk-or])", "");
|
||||
Boolean changed = parsed == message;
|
||||
Boolean changed = parsed.equals(message);
|
||||
return new Object[] {parsed, changed};
|
||||
}
|
||||
|
||||
public static String rainbow(String message) {
|
||||
ChatColor[] rainbowColors = { ChatColor.RED, ChatColor.GOLD, ChatColor.YELLOW, ChatColor.GREEN, ChatColor.AQUA, ChatColor.BLUE, ChatColor.LIGHT_PURPLE };
|
||||
int colorIndex = 0;
|
||||
String coloredMessage = "";
|
||||
|
||||
for (char c : message.toCharArray()) {
|
||||
ChatColor currentColor = rainbowColors[colorIndex];
|
||||
coloredMessage += currentColor + String.valueOf(c);
|
||||
colorIndex++;
|
||||
if (colorIndex >= rainbowColors.length) {
|
||||
colorIndex = 0;
|
||||
}
|
||||
}
|
||||
return coloredMessage;
|
||||
}
|
||||
}
|
||||
|
|
@ -11,16 +11,17 @@ import java.nio.file.Files;
|
|||
/**
|
||||
* Utility class for managing plugin configuration files.
|
||||
* <p>
|
||||
* This class extends {@link Configuration} to provide custom methods for loading, saving, and managing
|
||||
* This class extends {@link ConfigurationEx} to provide custom methods for loading, saving, and managing
|
||||
* configuration files. It automatically handles the creation of parent directories and copies default configuration
|
||||
* files from the plugin's resources if they do not exist.
|
||||
* <p>
|
||||
* <b>Note:</b> This class allows for flexible management of multiple configuration files, specified by their file name.
|
||||
*/
|
||||
public class Configuration extends Configuration {
|
||||
public class ConfigurationEx extends Configuration {
|
||||
|
||||
private final File configFile;
|
||||
private final String pluginName;
|
||||
private Log logger;
|
||||
JavaPlugin plugin;
|
||||
|
||||
/**
|
||||
* Constructs a new instance of {@code ConfigUtil}.
|
||||
|
|
@ -28,10 +29,11 @@ public class Configuration extends Configuration {
|
|||
* @param plugin the plugin instance using this configuration utility
|
||||
* @param fileName the name of the configuration file to manage (e.g., "config.yml", "settings.yml")
|
||||
*/
|
||||
public Configuration(JavaPlugin plugin, String fileName) {
|
||||
super(new File(plugin.getDataFolder(), fileName));
|
||||
public ConfigurationEx(JavaPlugin _plugin, String fileName, Log _logger) {
|
||||
super(new File(_plugin.getDataFolder(), fileName));
|
||||
plugin = _plugin;
|
||||
logger = _logger;
|
||||
this.configFile = new File(plugin.getDataFolder(), fileName);
|
||||
this.pluginName = plugin.getDescription().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -54,7 +56,7 @@ public class Configuration extends Configuration {
|
|||
try {
|
||||
super.load();
|
||||
} catch (Exception e) {
|
||||
Logger.severe(String.format("[%s] Failed to load config '%s': %s", pluginName, configFile.getName(), e.getMessage()));
|
||||
logger.severe(String.format("Failed to load config '%s': %s", configFile.getName(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -67,7 +69,7 @@ public class Configuration extends Configuration {
|
|||
try {
|
||||
Files.createDirectories(configFile.getParentFile().toPath());
|
||||
} catch (IOException e) {
|
||||
Logger.severe(String.format("[%s] Failed to generate default config directory: %s", pluginName, e.getMessage()));
|
||||
logger.severe(String.format("Failed to generate default config directory: %s", e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,19 +82,23 @@ public class Configuration extends Configuration {
|
|||
* Logs an error if the default configuration file cannot be found or copied.
|
||||
*/
|
||||
private void copyDefaultConfig() {
|
||||
// Adjust the path to ensure it's correct for your JAR structure
|
||||
String resourcePath = "/" + configFile.getName();
|
||||
// Load the config from the JAR directly (it is located at the root level)
|
||||
String resourcePath = "/" + configFile.getName(); // Root path of JAR
|
||||
|
||||
try (InputStream input = getClass().getResourceAsStream(resourcePath)) {
|
||||
try (InputStream input = plugin.getClass().getResourceAsStream(resourcePath)) {
|
||||
if (input == null) {
|
||||
Logger.severe(String.format("[%s] Default config '%s' wasn't found.", pluginName, configFile.getName()));
|
||||
logger.severe(String.format("Default config '%s' wasn't found in the JAR.", configFile.getName()));
|
||||
return;
|
||||
}
|
||||
|
||||
Files.copy(input, configFile.toPath());
|
||||
Logger.info(String.format("[%s] Default config '%s' generated successfully.", pluginName, configFile.getName()));
|
||||
if (Files.exists(configFile.toPath())) {
|
||||
logger.info(String.format("Default config '%s' generated successfully.", configFile.getName()));
|
||||
} else {
|
||||
logger.warning("We tried to generate the default config file, but it was not found even after the creation. Maybe your permissions are broken?");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Logger.severe(String.format("[%s] Failed to generate default config '%s': %s", pluginName, configFile.getName(), e.getMessage()));
|
||||
logger.severe(String.format("Failed to generate default config '%s': %s", configFile.getName(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -104,9 +110,9 @@ public class Configuration extends Configuration {
|
|||
public void loadConfig() {
|
||||
try {
|
||||
this.load();
|
||||
Logger.info(String.format("[%s] Config '%s' loaded successfully.", pluginName, configFile.getName()));
|
||||
logger.info(String.format("Config '%s' loaded successfully.", configFile.getName()));
|
||||
} catch (Exception e) {
|
||||
Logger.severe(String.format("[%s] Failed to load config '%s': %s", pluginName, configFile.getName(), e.getMessage()));
|
||||
logger.severe(String.format("Failed to load config '%s': %s", configFile.getName(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -118,9 +124,9 @@ public class Configuration extends Configuration {
|
|||
public void saveConfig() {
|
||||
try {
|
||||
this.save();
|
||||
Logger.info(String.format("[%s] Config '%s' saved successfully.", pluginName, configFile.getName()));
|
||||
logger.info(String.format("Config '%s' saved successfully.", configFile.getName()));
|
||||
} catch (Exception e) {
|
||||
Logger.severe(String.format("[%s] Failed to save config '%s': %s", pluginName, configFile.getName(), e.getMessage()));
|
||||
logger.severe(String.format("Failed to save config '%s': %s", configFile.getName(), e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,28 +1,39 @@
|
|||
package gd.rf.adrianvictor.lib;
|
||||
import static org.bukkit.Bukkit.getServer;
|
||||
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Log {
|
||||
public static void info(String message) {
|
||||
getServer().getLogger().info(message);
|
||||
JavaPlugin plugin;
|
||||
PluginDescriptionFile pdf;
|
||||
|
||||
public Log(JavaPlugin _plugin) {
|
||||
plugin = _plugin;
|
||||
pdf = plugin.getDescription();
|
||||
}
|
||||
|
||||
public static void infoc(String message) {
|
||||
getServer().getLogger().info(message);
|
||||
public void info(String message) {
|
||||
getServer().getLogger().info("[" + pdf.getName() + "] " + message);
|
||||
}
|
||||
|
||||
public static void warning(String message) {
|
||||
getServer().getLogger().warning(message);
|
||||
public void infoc(String message) {
|
||||
getServer().getLogger().info("[" + pdf.getName() + "] " + Color.formatColors(message));
|
||||
}
|
||||
|
||||
public static void warningc(String message) {
|
||||
getServer().getLogger().warning(message);
|
||||
public void warning(String message) {
|
||||
getServer().getLogger().warning("[" + pdf.getName() + "] " + message);
|
||||
}
|
||||
|
||||
public static void severe(String message) {
|
||||
getServer().getLogger().severe(message);
|
||||
public void warningc(String message) {
|
||||
getServer().getLogger().warning("[" + pdf.getName() + "] " + Color.formatColors(message));
|
||||
}
|
||||
|
||||
public static void severec(String message) {
|
||||
getServer().getLogger().severe(message);
|
||||
public void severe(String message) {
|
||||
getServer().getLogger().severe("[" + pdf.getName() + "] " + message);
|
||||
}
|
||||
|
||||
public void severec(String message) {
|
||||
getServer().getLogger().severe("[" + pdf.getName() + "] " + Color.formatColors(message));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,7 +2,17 @@ package gd.rf.adrianvictor.lib;
|
|||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Main extends JavaPlugin {
|
||||
public void onLoad() {
|
||||
Log.info(this + " is loading.");
|
||||
|
||||
Log logger;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
logger.info("is being disabled.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
logger = new Log(this);
|
||||
logger.info("is loading.");
|
||||
}
|
||||
}
|
||||
8
src/gd/rf/adrianvictor/lib/PlayerEx.java
Normal file
8
src/gd/rf/adrianvictor/lib/PlayerEx.java
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
package gd.rf.adrianvictor.lib;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PlayerEx {
|
||||
public static void strikeLightning(Player player) {
|
||||
player.getWorld().strikeLightning(player.getLocation());
|
||||
}
|
||||
}
|
||||
20
src/gd/rf/adrianvictor/lib/Text.java
Normal file
20
src/gd/rf/adrianvictor/lib/Text.java
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package gd.rf.adrianvictor.lib;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class Text {
|
||||
public static String generateRandomString(int length) {
|
||||
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
||||
Random random = new Random();
|
||||
StringBuilder randomString = new StringBuilder(length);
|
||||
|
||||
for (int i = 0; i < length; i++) {
|
||||
int randomIndex = random.nextInt(characters.length());
|
||||
randomString.append(characters.charAt(randomIndex));
|
||||
}
|
||||
|
||||
return randomString.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue