Updates code to account for KnarLib changes

This commit is contained in:
Kristian Knarvik 2022-11-07 22:22:23 +01:00
parent a501a3cbb4
commit a036c39dc3
7 changed files with 47 additions and 35 deletions

View File

@ -13,7 +13,7 @@ import net.knarcraft.blacksmith.listener.NPCClickListener;
import net.knarcraft.blacksmith.listener.PlayerListener;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.knarlib.KnarLib;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.util.UpdateChecker;
@ -32,6 +32,7 @@ public class BlacksmithPlugin extends JavaPlugin {
private static BlacksmithPlugin instance;
private GlobalSettings config;
private static Translator translator;
private static StringFormatter stringFormatter;
/**
* Gets an instance of the Blacksmith plugin
@ -57,7 +58,7 @@ public class BlacksmithPlugin extends JavaPlugin {
public void reload() {
config.load();
this.reloadConfig();
translator.loadLanguages(this.getConfig().getString("language", "en"));
translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en"));
}
/**
@ -69,6 +70,15 @@ public class BlacksmithPlugin extends JavaPlugin {
return BlacksmithPlugin.translator;
}
/**
* Gets the string formatter to use for formatting
*
* @return <p>The string formatter to use</p>
*/
public static StringFormatter getStringFormatter() {
return BlacksmithPlugin.stringFormatter;
}
@Override
public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");
@ -77,7 +87,6 @@ public class BlacksmithPlugin extends JavaPlugin {
@Override
public void onEnable() {
instance = this;
KnarLib.setPlugin(this);
//Copy default config to disk
FileConfiguration fileConfiguration = this.getConfig();
@ -94,7 +103,8 @@ public class BlacksmithPlugin extends JavaPlugin {
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(fileConfiguration.getString("language", "en"));
translator.loadLanguages(this.getDataFolder(), fileConfiguration.getString("language", "en"));
BlacksmithPlugin.stringFormatter = new StringFormatter(this.getDescription().getPrefix(), translator);
//Set up Vault integration
if (!setUpVault()) {

View File

@ -9,7 +9,6 @@ import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.formatting.ItemType;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
@ -42,7 +41,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
//Changing reforge-able items' default isn't recommended
if (commandName.equalsIgnoreCase(NPCSetting.REFORGE_ABLE_ITEMS.getCommandName())) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.DEFAULT_REFORGE_ABLE_ITEMS_UNCHANGEABLE);
return false;
}
@ -105,7 +104,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
String newValue = args[1];
if (detectedGlobalSetting != null) {
settings.changeValue(detectedGlobalSetting, newValue);
StringFormatter.displaySuccessMessage(sender, getValueChangedMessage(detectedGlobalSetting.getCommandName(), newValue));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(detectedGlobalSetting.getCommandName(), newValue));
return true;
} else if (detectedNPCSetting != null) {
//This makes sure all arguments are treated as a sentence
@ -113,7 +113,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
}
settings.changeValue(detectedNPCSetting, newValue);
StringFormatter.displaySuccessMessage(sender, getValueChangedMessage(detectedNPCSetting.getCommandName(), newValue));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(detectedNPCSetting.getCommandName(), newValue));
return true;
} else {
return false;
@ -151,7 +152,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
return false;
}
//Display the current value of the setting
StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue));
//Print the value with any colors displayed as &a-f0-9
if (printRawValue) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage(
@ -182,7 +184,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} else {
currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material));
}
StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.MATERIAL, material.name(), currentValue));
return true;
} else if (setting == GlobalSetting.ENCHANTMENT_COST) {
@ -190,7 +193,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
if (enchantment == null) {
return false;
}
StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.ENCHANTMENT, enchantment.getKey().getKey(),
String.valueOf(settings.getEnchantmentCost(enchantment))));
return true;
@ -257,8 +261,9 @@ public class BlackSmithConfigCommand implements CommandExecutor {
return false;
}
StringFormatter.displaySuccessMessage(sender, BlacksmithTranslatableMessage.getItemValueChangedMessage(
detectedGlobalSetting.getCommandName(), itemType, itemChanged, newValue));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemValueChangedMessage(detectedGlobalSetting.getCommandName(),
itemType, itemChanged, newValue));
return true;
}

View File

@ -9,7 +9,6 @@ import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -31,7 +30,7 @@ public class BlackSmithEditCommand implements CommandExecutor {
@NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(BlacksmithTrait.class)) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.NO_NPC_SELECTED);
return true;
}
@ -85,7 +84,8 @@ public class BlackSmithEditCommand implements CommandExecutor {
//Change the setting
blacksmithTrait.getSettings().changeSetting(npcSetting, newValue);
StringFormatter.displaySuccessMessage(sender, getValueChangedMessage(npcSetting.getCommandName(), String.valueOf(newValue)));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(npcSetting.getCommandName(), String.valueOf(newValue)));
//Save the changes immediately to prevent data loss on server crash
CitizensAPI.getNPCRegistry().saveToStore();
}
@ -104,12 +104,13 @@ public class BlackSmithEditCommand implements CommandExecutor {
if (InputParsingHelper.isEmpty(rawValue)) {
//Display the default value, if no custom value has been specified
rawValue = String.valueOf(BlacksmithPlugin.getInstance().getSettings().getRawValue(npcSetting));
StringFormatter.displaySuccessMessage(sender, getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getCurrentValueMessage(npcSetting.getCommandName(), rawValue));
} else {
//Add a marker if the value has been customized
String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage(
BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER);
StringFormatter.displaySuccessMessage(sender,
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getCurrentValueMessage(npcSetting.getCommandName(), rawValue) + marker);
}
if (npcSetting.getPath().startsWith("defaults.messages")) {

View File

@ -37,7 +37,7 @@ public class PresetCommand implements CommandExecutor {
SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]);
if (!smithPreset.supportsFilter(filter)) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET);
return false;
}
@ -46,7 +46,7 @@ public class PresetCommand implements CommandExecutor {
includedMaterials = SmithPreset.valueOf(presetName).getMaterials();
}
} catch (IllegalArgumentException exception) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER);
return false;
}
@ -56,7 +56,7 @@ public class PresetCommand implements CommandExecutor {
for (Material material : includedMaterials) {
materialNames.add(material.name());
}
StringFormatter.displaySuccessMessage(sender, StringFormatter.replacePlaceholder(
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, StringFormatter.replacePlaceholder(
BlacksmithPlugin.getTranslator().getTranslatedMessage(BlacksmithTranslatableMessage.PRESET_MATERIALS),
"{materials}", String.join(", ", materialNames)));
return true;

View File

@ -2,7 +2,6 @@ package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
@ -21,8 +20,7 @@ public class ReloadCommand implements TabExecutor {
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
BlacksmithPlugin.getInstance().reload();
StringFormatter.displaySuccessMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithTranslatableMessage.PLUGIN_RELOADED);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, BlacksmithTranslatableMessage.PLUGIN_RELOADED);
return true;
}

View File

@ -3,7 +3,6 @@ package net.knarcraft.blacksmith.listener;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -26,7 +25,7 @@ public class NPCClickListener implements Listener {
//Permission check
if (!player.hasPermission("blacksmith.use")) {
StringFormatter.displayErrorMessage(player, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(player,
BlacksmithTranslatableMessage.PERMISSION_DENIED);
return;
}

View File

@ -3,7 +3,6 @@ package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.command.CommandSender;
import java.util.List;
@ -52,7 +51,7 @@ public final class TypeValidationHelper {
private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED);
}
return isStringList;
@ -71,7 +70,7 @@ public final class TypeValidationHelper {
return intValue >= 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
}
return false;
@ -88,7 +87,7 @@ public final class TypeValidationHelper {
private static boolean isNonEmptyString(Object value, CommandSender sender) {
boolean isString = value instanceof String string && !string.strip().isEmpty();
if (!isString && sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED);
}
return isString;
@ -106,7 +105,7 @@ public final class TypeValidationHelper {
return ConfigHelper.asDouble(value) >= 0.0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
}
return false;
@ -125,7 +124,7 @@ public final class TypeValidationHelper {
return ConfigHelper.asInt(value) >= 0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
StringFormatter.displayErrorMessage(sender, BlacksmithPlugin.getTranslator(),
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
}
return false;