Improves console logging
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-07-29 18:32:08 +02:00
parent f3372a13a5
commit 0993fbe15f
15 changed files with 81 additions and 74 deletions

View File

@ -142,7 +142,7 @@ public class BlacksmithPlugin extends JavaPlugin {
@Override
public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
log(" v" + getDescription().getVersion() + " disabled.");
}
@Override
@ -178,7 +178,7 @@ public class BlacksmithPlugin extends JavaPlugin {
//Register all listeners
registerListeners();
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " enabled.");
log(" v" + getDescription().getVersion() + " enabled.");
//Alert about an update in the console
UpdateChecker.checkForUpdate(this, "https://api.spigotmc.org/legacy/update.php?resource=105938",
@ -193,7 +193,7 @@ public class BlacksmithPlugin extends JavaPlugin {
try {
this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException | InvalidConfigurationException exception) {
getLogger().log(Level.SEVERE, "Unable to load the configuration! Message: " + exception.getMessage());
error("Unable to load the configuration! Message: " + exception.getMessage());
}
}
@ -203,7 +203,7 @@ public class BlacksmithPlugin extends JavaPlugin {
try {
this.configuration.save(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException exception) {
getLogger().log(Level.SEVERE, "Unable to save the configuration! Message: " + exception.getMessage());
error("Unable to save the configuration! Message: " + exception.getMessage());
}
}
@ -216,6 +216,42 @@ public class BlacksmithPlugin extends JavaPlugin {
this.getServer().getPluginManager().callEvent(event);
}
/**
* Prints an info message to the console
*
* @param message <p>The message to print</p>
*/
public static void log(@NotNull String message) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, message);
}
/**
* Prints a warning message to the console
*
* @param message <p>The message to print</p>
*/
public static void warn(@NotNull String message) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, message);
}
/**
* Prints an error message to the console
*
* @param message <p>The message to print</p>
*/
public static void error(@NotNull String message) {
BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, message);
}
/**
* Prints a debug message to the console
*
* @param message <p>The message to print</p>
*/
public static void debug(@NotNull String message) {
BlacksmithPlugin.getInstance().getLogger().log(Level.FINE, message);
}
/**
* Initializes custom configuration and translation
*
@ -253,10 +289,10 @@ public class BlacksmithPlugin extends JavaPlugin {
* @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());
log("Setting Up Vault now....");
boolean canLoad = EconomyManager.setUp(getServer().getServicesManager());
if (!canLoad) {
getLogger().log(Level.SEVERE, "Vault Integration Failed....");
error("Vault Integration Failed....");
getServer().getPluginManager().disablePlugin(this);
return false;
}

View File

@ -20,7 +20,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage;
@ -127,8 +126,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
//Change the setting
Settings<L> settings = trait.getTraitSettings();
if (settings == null) {
BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, "Settings for a CustomTrait has not " +
"been initialized! Please inform the developer!");
BlacksmithPlugin.error("Settings for a CustomTrait has not been initialized! Please inform the developer!");
return false;
}
settings.changeValue(setting, newValue);
@ -154,8 +152,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
@NotNull CommandSender sender) {
Settings<L> settings = trait.getTraitSettings();
if (settings == null) {
BlacksmithPlugin.getInstance().getLogger().log(Level.SEVERE, "Settings for a CustomTrait has not " +
"been initialized! Please inform the developer!");
BlacksmithPlugin.error("Settings for a CustomTrait has not been initialized! Please inform the developer!");
return;
}

View File

@ -12,7 +12,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
/**
* A representation of the presets for different kinds of smiths
@ -133,8 +132,8 @@ public enum SmithPreset {
} catch (IllegalArgumentException exception) {
/* This case means that either the preset or the filter given is invalid, and thus the preset string should
be ignored to prevent any problems. */
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, String.format("The smith preset %s is " +
"invalid, and will be ignored. Please fix it!", possiblePreset));
BlacksmithPlugin.warn(String.format("The smith preset %s is invalid, and will be ignored. Please fix it!",
possiblePreset));
return "";
}

View File

@ -16,7 +16,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
* A class which keeps track of all Blacksmith settings/config values for one NPC
@ -384,8 +383,7 @@ public class BlacksmithNPCSettings implements TraitSettings<BlacksmithSetting> {
if (enchantment != null) {
enchantmentBlockList.add(enchantment);
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid enchantment");
BlacksmithPlugin.warn("Unable to verify " + item + " as a valid enchantment");
}
}
return enchantmentBlockList;

View File

@ -19,7 +19,6 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
* A class which keeps track of all default blacksmith NPC settings and all global blacksmith settings
@ -344,15 +343,14 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
ConfigurationSection enchantmentCostNode = fileConfiguration.getConfigurationSection(
getBase(BlacksmithSetting.ENCHANTMENT_COST.getPath()));
if (enchantmentCostNode == null) {
instance.getLogger().log(Level.WARNING, "Could not load enchantment prices. because the " +
"configuration section doesn't exist");
BlacksmithPlugin.warn("Could not load enchantment prices. because the configuration section doesn't exist");
return;
}
Map<String, String> relevantKeys = getRelevantKeys(enchantmentCostNode);
for (String key : relevantKeys.keySet()) {
String enchantmentName = relevantKeys.get(key);
Enchantment enchantment = InputParsingHelper.matchEnchantment(enchantmentName);
instance.getLogger().log(Level.WARNING, "loadEnchantmentPrices " + enchantmentName);
BlacksmithPlugin.warn("loadEnchantmentPrices " + enchantmentName);
setItemPrice(this.enchantmentCosts, enchantmentName, enchantment, enchantmentCostNode.getDouble(key));
}
}
@ -366,8 +364,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
ConfigurationSection basePerDurabilityPriceNode = fileConfiguration.getConfigurationSection(
getBase(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getPath()));
if (basePerDurabilityPriceNode == null) {
instance.getLogger().log(Level.WARNING, "Could not load per durability prices. because the " +
"configuration section doesn't exist");
BlacksmithPlugin.warn("Could not load per durability prices. because the configuration section doesn't exist");
return;
}
Map<String, String> relevantKeys = getRelevantKeys(basePerDurabilityPriceNode);
@ -394,8 +391,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
ConfigurationSection basePriceNode = fileConfiguration.getConfigurationSection(
getBase(BlacksmithSetting.BASE_PRICE.getPath()));
if (basePriceNode == null) {
instance.getLogger().log(Level.WARNING, "Could not load base prices, because the configuration " +
"section doesn't exist");
BlacksmithPlugin.warn("Could not load base prices, because the configuration section doesn't exist");
return;
}
Map<String, String> relevantKeys = getRelevantKeys(basePriceNode);
@ -442,7 +438,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
if (item != null) {
prices.put(item, price);
} else {
instance.getLogger().log(Level.WARNING, "Unable to find a material/enchantment matching " + itemName);
BlacksmithPlugin.warn("Unable to find a material/enchantment matching " + itemName);
}
}

View File

@ -16,7 +16,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
/**
* A class which keeps track of all default scrapper NPC settings and all global scrapper settings
@ -251,8 +250,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
for (String trashSalvageInfo : allTrashSalvage) {
// Ignore invalid lines
if (!trashSalvageInfo.contains(":")) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, String.format("The trash salvage " +
"configuration line %s is invalid", trashSalvageInfo));
BlacksmithPlugin.warn(String.format("The trash salvage configuration line %s is invalid", trashSalvageInfo));
continue;
}

View File

@ -12,8 +12,6 @@ import org.bukkit.event.Listener;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.logging.Level;
/**
* A listener for detecting and handling a Blacksmith being right-clicked
*/
@ -37,7 +35,7 @@ public class NPCClickListener implements Listener {
*/
private void handleNPCClick(@NotNull NPCRightClickEvent event, @Nullable CustomTrait<?> customTrait) {
if (customTrait == null) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Could not get trait from NPC!");
BlacksmithPlugin.warn("Could not get trait from NPC!");
return;
}

View File

@ -14,9 +14,6 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager;
import org.jetbrains.annotations.NotNull;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A class which deals with everything economy
*/
@ -32,15 +29,14 @@ public class EconomyManager {
* Sets up Vault economy support
*
* @param servicesManager <p>The services manager to use for finding a Vault provider</p>
* @param logger <p>The logger to use for logging</p>
* @return <p>True if Vault was successfully set up</p>
*/
public static boolean setUp(@NotNull ServicesManager servicesManager, @NotNull Logger logger) {
public static boolean setUp(@NotNull ServicesManager servicesManager) {
//If already set up, there is nothing to do
if (economy != null) {
return true;
}
return setupVault(servicesManager, logger);
return setupVault(servicesManager);
}
/**
@ -191,10 +187,9 @@ public class EconomyManager {
* Sets up Vault for economy
*
* @param servicesManager <p>The services manager to use for finding a Vault provider</p>
* @param logger <p>The logger to use for logging</p>
* @return <p>True if Vault was successfully set up</p>
*/
private static boolean setupVault(@NotNull ServicesManager servicesManager, @NotNull Logger logger) {
private static boolean setupVault(@NotNull ServicesManager servicesManager) {
// Setup Vault
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
if (economyProvider != null) {
@ -202,7 +197,7 @@ public class EconomyManager {
return true;
} else {
// Disable if no economy plugin was found
logger.log(Level.SEVERE, "Failed to load an economy plugin. Disabling...");
BlacksmithPlugin.error("Failed to load an economy plugin. Disabling...");
return false;
}
}

View File

@ -23,7 +23,6 @@ import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
@ -152,8 +151,7 @@ public class ReforgeSession extends Session implements Runnable {
private void succeedReforge() {
// Remove any damage done to the item
if (ItemHelper.updateDamage(this.itemToReforge, 0)) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to update damage for " +
this.itemToReforge);
BlacksmithPlugin.warn("Unable to update damage for " + this.itemToReforge);
}
//Replace damaged anvils with a normal anvil

View File

@ -20,7 +20,6 @@ import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
@ -166,7 +165,7 @@ public class SalvageSession extends Session implements Runnable {
// TODO: Find a better calculation than 1 enchantment level = 1 exp level
// Gives the player back some of the EXP used on an item
this.player.giveExpLevels(this.enchantmentLevels);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Giving salvage " + this.salvage);
BlacksmithPlugin.debug("Giving salvage " + this.salvage);
for (ItemStack item : this.salvage) {
giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), this.npc, item);
}

View File

@ -27,7 +27,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
@ -93,7 +92,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
if (!canBeSalvaged(itemInHand, salvageAbleItems, extended)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(),
"{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Cannot salvage provided item: " + itemInHand);
BlacksmithPlugin.debug("Cannot salvage provided item: " + itemInHand);
return;
}
@ -161,7 +160,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
if (!SalvageHelper.isSalvageable(player.getServer(), itemInHand)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(),
"{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Provided with non-salvage-able item " + itemInHand);
BlacksmithPlugin.debug("Provided with non-salvage-able item " + itemInHand);
return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
}

View File

@ -16,7 +16,6 @@ import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
import java.util.Random;
import java.util.logging.Level;
/**
* A runnable session for performing a reforging/salvage task
@ -147,7 +146,7 @@ public abstract class Session implements Runnable {
newDurability = (short) (maxDurability - random.nextInt(maxDurability - 25));
}
if (ItemHelper.updateDamage(item, maxDurability - newDurability)) {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to update damage for " + item);
BlacksmithPlugin.warn("Unable to update damage for " + item);
}
}

View File

@ -17,7 +17,6 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
/**
* A helper class for getting an object value as the correct type
@ -120,7 +119,7 @@ public final class ConfigHelper {
try {
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
} catch (IOException exception) {
instance.getLogger().log(Level.WARNING, "Unable to save old backup and do migration");
BlacksmithPlugin.warn("Unable to save old backup and do migration");
return;
}
@ -129,8 +128,7 @@ public final class ConfigHelper {
FileConfiguration oldConfiguration = instance.getConfig();
InputStream configStream = FileHelper.getInputStreamForInternalFile("/config.yml");
if (configStream == null) {
instance.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal " +
"configuration could not be read!");
BlacksmithPlugin.error("Could not migrate the configuration, as the internal configuration could not be read!");
return;
}
YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
@ -141,13 +139,13 @@ public final class ConfigHelper {
try {
InputStream migrationStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt");
if (migrationStream == null) {
instance.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal migration paths could not be read!");
BlacksmithPlugin.error("Could not migrate the configuration, as the internal migration paths could not be read!");
return;
}
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(migrationStream),
"=", ColorConversion.NORMAL);
} catch (IOException exception) {
instance.getLogger().log(Level.WARNING, "Unable to load config migration file");
BlacksmithPlugin.warn("Unable to load config migration file");
return;
}
@ -169,7 +167,7 @@ public final class ConfigHelper {
try {
newConfiguration.save(new File(dataFolderPath, "config.yml"));
} catch (IOException exception) {
instance.getLogger().log(Level.WARNING, "Unable to save migrated config");
BlacksmithPlugin.warn("Unable to save migrated config");
}
instance.reloadConfig();

View File

@ -14,7 +14,6 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
/**
* A helper class for getting information about items
@ -230,8 +229,7 @@ public final class ItemHelper {
blacklisted.add(material);
}
} else {
BlacksmithPlugin.getInstance().getLogger().log(Level.WARNING, "Unable to verify " + item +
" as a valid repairable item");
BlacksmithPlugin.warn("Unable to verify " + item + " as a valid repairable item");
}
}

View File

@ -21,7 +21,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
/**
* A helper class for deciding the salvage returned if salvaging an item
@ -158,25 +157,25 @@ public final class SalvageHelper {
}
for (Recipe recipe : server.getRecipesFor(new ItemStack(salvagedItem.getType(), salvagedItem.getAmount()))) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Considering recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
BlacksmithPlugin.debug("Considering recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
// Only consider crafting table recipes
if (!(recipe instanceof ShapedRecipe) && !(recipe instanceof ShapelessRecipe)) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Recipe had invalid type");
BlacksmithPlugin.debug("Recipe had invalid type");
continue;
}
// Make sure the player has enough items
if (salvagedItem.getAmount() < recipe.getResult().getAmount()) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Too few items for recipe");
BlacksmithPlugin.debug("Too few items for recipe");
continue;
}
// Get actual salvage, as long as any can be produced
List<ItemStack> salvage = getRecipeSalvage(recipe, salvagedItem, trashSalvage);
if (salvage != null && !salvage.isEmpty()) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Valid recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Actual salvage: " + salvage);
BlacksmithPlugin.debug("Valid recipe: " + recipe.getResult() + " -> " + getRawRecipeSalvage(recipe));
BlacksmithPlugin.debug("Actual salvage: " + salvage);
return new RecipeResult(recipe, salvage);
}
}
@ -199,11 +198,11 @@ public final class SalvageHelper {
return null;
}
List<ItemStack> copy = copyItems(ingredients);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Copied salvage: " + copy);
BlacksmithPlugin.debug("Copied salvage: " + copy);
List<ItemStack> salvage = getSalvage(copy, salvagedItem, trashSalvage);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Combining salvage: " + salvage);
BlacksmithPlugin.debug("Combining salvage: " + salvage);
List<ItemStack> combined = combineStacks(salvage);
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Combined : " + combined);
BlacksmithPlugin.debug("Combined : " + combined);
return combined;
}
@ -244,10 +243,10 @@ public final class SalvageHelper {
durability = 1;
}
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Durability: " + durability + "/" + maxDurability);
BlacksmithPlugin.debug("Durability: " + durability + "/" + maxDurability);
double percentageRemaining = (double) durability / maxDurability;
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "Remaining: " + percentageRemaining);
BlacksmithPlugin.debug("Remaining: " + percentageRemaining);
return pickRandomSalvage(recipeItems, trashSalvage, percentageRemaining);
}
@ -270,7 +269,7 @@ public final class SalvageHelper {
// If not damaged, just give everything
if (percentageRemaining == 1) {
BlacksmithPlugin.getInstance().getLogger().log(Level.INFO, "100% Remaining. Copying " + itemsToChooseFrom);
BlacksmithPlugin.debug("100% Remaining. Copying " + itemsToChooseFrom);
return copyItems(itemsToChooseFrom);
}