Added logger class that takes variables and replaces % with them
This commit is contained in:
parent
f09061f6b6
commit
784cf905fb
|
|
@ -7,6 +7,7 @@ import com.alttd.config.Config;
|
|||
import com.alttd.config.VillagerConfig;
|
||||
import com.alttd.config.WorthConfig;
|
||||
import com.alttd.events.VillagerInteract;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class VillagerUI extends JavaPlugin {
|
||||
|
|
@ -30,9 +31,9 @@ public class VillagerUI extends JavaPlugin {
|
|||
VillagerConfig.reload();
|
||||
WorthConfig.reload();
|
||||
Database.init();
|
||||
getLogger().info("--------------------------------------------------");
|
||||
getLogger().info("Villager UI started");
|
||||
getLogger().info("--------------------------------------------------");
|
||||
Logger.info("--------------------------------------------------");
|
||||
Logger.info("Villager UI started");
|
||||
Logger.info("--------------------------------------------------");
|
||||
}
|
||||
|
||||
private void registerEvents() {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import com.alttd.commands.subcommands.CommandCreateVillager;
|
|||
import com.alttd.commands.subcommands.CommandHelp;
|
||||
import com.alttd.commands.subcommands.CommandReload;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.util.Logger;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import org.bukkit.command.*;
|
||||
|
|
@ -27,7 +28,7 @@ public class CommandManager implements CommandExecutor, TabExecutor {
|
|||
if (command == null) {
|
||||
subCommands = null;
|
||||
miniMessage = null;
|
||||
villagerUI.getLogger().severe("Unable to find villager ui command.");
|
||||
Logger.severe("Unable to find villager ui command.");
|
||||
return;
|
||||
}
|
||||
command.setExecutor(this);
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.alttd.commands.database;
|
|||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.config.Config;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
|
@ -22,9 +23,9 @@ public class Database {
|
|||
connection = DriverManager.getConnection(url, Config.USERNAME, Config.PASSWORD);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
VillagerUI.getInstance().getLogger().severe("Connection to database failed!");
|
||||
Logger.severe("Connection to database failed!");
|
||||
connection = null;
|
||||
VillagerUI.getInstance().getLogger().severe("Shutting down VillagerUI");
|
||||
Logger.severe("Shutting down VillagerUI");
|
||||
Bukkit.getPluginManager().disablePlugin(VillagerUI.getInstance());
|
||||
return;
|
||||
}
|
||||
|
|
@ -33,16 +34,21 @@ public class Database {
|
|||
createUserPointsTable();
|
||||
}
|
||||
|
||||
static void createUserPointsTable() {
|
||||
private static void createUserPointsTable() {
|
||||
try {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS user_points(" +
|
||||
"UUID varchar 36 NOT NULL, " +
|
||||
"Points int NOT NULL, " +
|
||||
"PRIMARY KEY (UUID)" +
|
||||
"UUID varchar(36) NOT NULL, " +
|
||||
"points int NOT NULL, " +
|
||||
"villager_type varchar(128) NOT NULL, " +
|
||||
"PRIMARY KEY (UUID), " +
|
||||
"UNIQUE KEY (villager_type)" +
|
||||
")";
|
||||
connection.prepareStatement(sql).executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Logger.severe("Error while trying to create user point table");
|
||||
Logger.severe("Shutting down VillagerUI");
|
||||
Bukkit.getPluginManager().disablePlugin(VillagerUI.getInstance());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.alttd.config;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.util.Logger;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
|
|
@ -16,13 +17,11 @@ import java.lang.reflect.Modifier;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@SuppressWarnings({"unused", "SameParameterValue"})
|
||||
abstract class AbstractConfig {
|
||||
File file;
|
||||
YamlConfiguration yaml;
|
||||
Logger logger;
|
||||
|
||||
AbstractConfig(String filename) {
|
||||
init(new File(VillagerUI.getInstance().getDataFolder(), filename), filename);
|
||||
|
|
@ -35,12 +34,11 @@ abstract class AbstractConfig {
|
|||
private void init(File file, String filename) {
|
||||
this.file = file;
|
||||
this.yaml = new YamlConfiguration();
|
||||
this.logger = VillagerUI.getInstance().getLogger();
|
||||
try {
|
||||
yaml.load(file);
|
||||
} catch (IOException ignore) {
|
||||
} catch (InvalidConfigurationException ex) {
|
||||
logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
|
||||
Logger.severe(String.format("Could not load %s, please correct your syntax errors", filename));
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
yaml.options().copyDefaults(true);
|
||||
|
|
@ -56,7 +54,7 @@ abstract class AbstractConfig {
|
|||
} catch (InvocationTargetException ex) {
|
||||
throw new RuntimeException(ex.getCause());
|
||||
} catch (Exception ex) {
|
||||
logger.severe("Error invoking " + method);
|
||||
Logger.severe("Error invoking %.", method.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
@ -70,7 +68,7 @@ abstract class AbstractConfig {
|
|||
try {
|
||||
yaml.save(file);
|
||||
} catch (IOException ex) {
|
||||
logger.severe("Could not save " + file);
|
||||
Logger.severe("Could not save %.", file.toString());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.alttd.config;
|
|||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
|
@ -83,13 +84,13 @@ public final class Config extends AbstractConfig {
|
|||
VillagerType.clearVillagerTypes();
|
||||
ConfigurationSection configurationSection = config.getConfigurationSection("villager-types");
|
||||
if (configurationSection == null) {
|
||||
VillagerUI.getInstance().getLogger().warning("No villager types found in config.");
|
||||
Logger.warning("No villager types found in config.");
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> keys = configurationSection.getKeys(false);
|
||||
if (keys.isEmpty())
|
||||
VillagerUI.getInstance().getLogger().warning("No villager types found in config.");
|
||||
Logger.warning("No villager types found in config.");
|
||||
|
||||
keys.forEach(key -> {
|
||||
ConfigurationSection villagerType = configurationSection.getConfigurationSection(key);
|
||||
|
|
@ -114,7 +115,7 @@ public final class Config extends AbstractConfig {
|
|||
productsSection.getKeys(false).forEach(item -> {
|
||||
Material material = Material.getMaterial(item);
|
||||
if (material == null) {
|
||||
VillagerUI.getInstance().getLogger().warning("Invalid key in products -> " + item);
|
||||
Logger.warning("Invalid key in products -> " + item);
|
||||
return;
|
||||
}
|
||||
products.add(new ItemStack(material, productsSection.getInt(item)));
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.alttd.config;
|
|||
import com.alttd.VillagerUI;
|
||||
import com.alttd.objects.LoadedVillagers;
|
||||
import com.alttd.objects.VillagerType;
|
||||
import com.alttd.util.Logger;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
import java.util.Set;
|
||||
|
|
@ -33,7 +34,7 @@ public class VillagerConfig extends AbstractConfig {
|
|||
if (villagerType != null)
|
||||
LoadedVillagers.addLoadedVillager(UUID.fromString(key), villagerType);
|
||||
else
|
||||
VillagerUI.getInstance().getLogger().warning("Invalid config entry " + key + ".");
|
||||
Logger.warning("Invalid config entry %.", key);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.alttd.config;
|
|||
|
||||
import com.alttd.VillagerUI;
|
||||
import com.alttd.objects.Price;
|
||||
import com.alttd.util.Logger;
|
||||
import com.alttd.util.Utilities;
|
||||
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap;
|
||||
|
|
@ -42,7 +43,7 @@ public class WorthConfig extends AbstractConfig {
|
|||
for (String key : materials) {
|
||||
Material material = Material.getMaterial(key);
|
||||
if (material == null) {
|
||||
VillagerUI.getInstance().getLogger().warning("Invalid key in worth.yml -> " + key);
|
||||
Logger.warning("Invalid key in worth.yml: %.", key);
|
||||
continue;
|
||||
}
|
||||
prices.put(Material.getMaterial(key), new Price(Utilities.round(pointSection.getDouble(key), 2), Integer.parseInt(point)));
|
||||
|
|
|
|||
36
src/main/java/com/alttd/util/Logger.java
Normal file
36
src/main/java/com/alttd/util/Logger.java
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
package com.alttd.util;
|
||||
|
||||
import com.alttd.VillagerUI;
|
||||
|
||||
public class Logger {
|
||||
|
||||
static private final java.util.logging.Logger logger;
|
||||
|
||||
static {
|
||||
logger = VillagerUI.getInstance().getLogger();
|
||||
}
|
||||
|
||||
public static void info(String info, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
info = info.replaceFirst("%", variable);
|
||||
}
|
||||
logger.info(info);
|
||||
}
|
||||
|
||||
public static void warning(String warning, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
warning = warning.replaceFirst("%", variable);
|
||||
}
|
||||
logger.warning(warning);
|
||||
}
|
||||
|
||||
public static void severe(String severe, String... variables)
|
||||
{
|
||||
for (String variable : variables) {
|
||||
severe = severe.replaceFirst("%", variable);
|
||||
}
|
||||
logger.severe(severe);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user