Updates KnarLib
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
2025-09-06 14:58:55 +02:00
parent 90d3c49c12
commit ca3d29d730
23 changed files with 186 additions and 655 deletions

View File

@@ -83,7 +83,7 @@
<dependency> <dependency>
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId> <artifactId>knarlib</artifactId>
<version>1.2.7</version> <version>1.2.14</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -13,49 +13,40 @@ import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter; import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand; import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter; import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter;
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings; import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings; import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.blacksmith.listener.NPCClickListener; import net.knarcraft.blacksmith.listener.NPCClickListener;
import net.knarcraft.blacksmith.listener.PlayerListener; import net.knarcraft.blacksmith.listener.PlayerListener;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.trait.ScrapperTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait;
import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.plugin.ConfigCommentPlugin;
import net.knarcraft.knarlib.util.ConfigHelper;
import net.knarcraft.knarlib.util.UpdateChecker; import net.knarcraft.knarlib.util.UpdateChecker;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.logging.Level; import java.util.logging.Level;
/** /**
* Blacksmith's main class * Blacksmith's main class
*/ */
public class BlacksmithPlugin extends JavaPlugin { public class BlacksmithPlugin extends ConfigCommentPlugin {
private static final String CONFIG_FILE_NAME = "config.yml";
private static BlacksmithPlugin instance; private static BlacksmithPlugin instance;
private GlobalBlacksmithSettings blacksmithConfig; private GlobalBlacksmithSettings blacksmithConfig;
private GlobalScrapperSettings scrapperConfig; private GlobalScrapperSettings scrapperConfig;
private static Translator translator; private static Translator translator;
private static StringFormatter stringFormatter;
private FileConfiguration configuration;
/** /**
* Constructor required for MockBukkit * Constructor required for MockBukkit
@@ -108,29 +99,10 @@ public class BlacksmithPlugin extends JavaPlugin {
this.reloadConfig(); this.reloadConfig();
blacksmithConfig.load(); blacksmithConfig.load();
scrapperConfig.load(); scrapperConfig.load();
translator.loadLanguages(this.getDataFolder(), "en", this.getConfiguration().getString( translator.loadLanguages(this.getDataFolder(), "en", this.getConfig().getString(
"language", "en")); "language", "en"));
} }
/**
* Gets the raw configuration
*
* @return <p>The raw configuration</p>
*/
@NotNull
public FileConfiguration getConfiguration() {
return this.configuration;
}
/**
* Gets the string formatter to use for formatting
*
* @return <p>The string formatter to use</p>
*/
public static @NotNull StringFormatter getStringFormatter() {
return BlacksmithPlugin.stringFormatter;
}
/** /**
* Gets the translator to use for translation * Gets the translator to use for translation
* *
@@ -148,20 +120,13 @@ public class BlacksmithPlugin extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; instance = this;
ConfigHelper.saveDefaults(this);
//Copy default config to disk, and add missing configuration values
this.saveDefaultConfig();
this.getConfig().options().copyDefaults(true);
this.reloadConfig();
this.saveConfig();
//Migrate from an earlier configuration file syntax if necessary //Migrate from an earlier configuration file syntax if necessary
if (getConfiguration().getString("scrapper.defaults.dropItem") == null) { if (getConfig().getString("scrapper.defaults.dropItem") == null) {
ConfigHelper.migrateConfig(this.getDataFolder().getPath().replaceAll("\\\\", "/"), net.knarcraft.knarlib.util.ConfigHelper.migrateConfig(this);
getConfiguration());
this.reloadConfig();
} }
initializeConfigurations(getConfiguration()); initializeConfigurations(getConfig());
//Set up Vault integration //Set up Vault integration
if (!setUpVault()) { if (!setUpVault()) {
@@ -185,28 +150,6 @@ public class BlacksmithPlugin extends JavaPlugin {
() -> this.getDescription().getVersion(), null); () -> this.getDescription().getVersion(), null);
} }
@Override
public void reloadConfig() {
super.reloadConfig();
this.configuration = new StargateYamlConfiguration();
this.configuration.options().copyDefaults(true);
try {
this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException | InvalidConfigurationException exception) {
error("Unable to load the configuration! Message: " + exception.getMessage());
}
}
@Override
public void saveConfig() {
super.saveConfig();
try {
this.configuration.save(new File(getDataFolder(), CONFIG_FILE_NAME));
} catch (IOException exception) {
error("Unable to save the configuration! Message: " + exception.getMessage());
}
}
/** /**
* Calls an event * Calls an event
* *
@@ -267,7 +210,7 @@ public class BlacksmithPlugin extends JavaPlugin {
//Prepare the translator //Prepare the translator
translator = new Translator(); translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); translator.registerMessageCategory(Translatable.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(this.getDataFolder(), "en", translator.loadLanguages(this.getDataFolder(), "en",
fileConfiguration.getString("language", "en")); fileConfiguration.getString("language", "en"));
PluginDescriptionFile description = this.getDescription(); PluginDescriptionFile description = this.getDescription();
@@ -277,7 +220,7 @@ public class BlacksmithPlugin extends JavaPlugin {
} else { } else {
prefix = description.getPrefix(); prefix = description.getPrefix();
} }
BlacksmithPlugin.stringFormatter = new StringFormatter(prefix, translator); FormatBuilder.setStringFormatter(new StringFormatter(prefix, translator));
// This reload is necessary to get values just added to the configuration for some reason // This reload is necessary to get values just added to the configuration for some reason
this.reload(); this.reload();
@@ -319,22 +262,4 @@ public class BlacksmithPlugin extends JavaPlugin {
registerCommand("preset", new PresetCommand(), new PresetTabCompleter()); registerCommand("preset", new PresetCommand(), new PresetTabCompleter());
} }
/**
* Registers a command
*
* @param commandName <p>The name of the command</p>
* @param executor <p>The executor to bind to the command</p>
* @param tabCompleter <p>The tab completer to bind to the command, or null</p>
*/
private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor executor,
@Nullable TabCompleter tabCompleter) {
PluginCommand command = this.getCommand(commandName);
if (command != null) {
command.setExecutor(executor);
if (tabCompleter != null) {
command.setTabCompleter(tabCompleter);
}
}
}
} }

View File

@@ -6,12 +6,12 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.blacksmith.trait.CustomTrait; import net.knarcraft.blacksmith.trait.CustomTrait;
import net.knarcraft.blacksmith.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.TypeValidationHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@@ -21,9 +21,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getDefaultValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getValueChangedMessage;
/** /**
* A generic implementation of the edit command * A generic implementation of the edit command
@@ -64,8 +64,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
@NotNull String[] args) { @NotNull String[] args) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(traitClass)) { if (npc == null || !npc.hasTrait(traitClass)) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.NO_NPC_SELECTED).error(sender);
BlacksmithTranslatableMessage.NO_NPC_SELECTED);
return true; return true;
} }
@@ -130,8 +129,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
return false; return false;
} }
settings.changeValue(setting, newValue); settings.changeValue(setting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue)).success(sender);
getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue)));
//Save the changes immediately to prevent data loss on server crash //Save the changes immediately to prevent data loss on server crash
CitizensAPI.getNPCRegistry().saveToStore(); CitizensAPI.getNPCRegistry().saveToStore();
} }
@@ -156,21 +154,19 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
return; return;
} }
StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
//Find the value of the specified setting //Find the value of the specified setting
String commandName = setting.getCommandName(); String commandName = setting.getCommandName();
String currentValue = String.valueOf(settings.getRawValue(setting)); String currentValue = String.valueOf(settings.getRawValue(setting));
String defaultValue = String.valueOf(setting.getDefaultValue()); String defaultValue = String.valueOf(setting.getDefaultValue());
String marker = formatter.getUnFormattedMessage(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); String marker = new FormatBuilder(Translatable.SETTING_OVERRIDDEN_MARKER).toString();
boolean isMessage = setting.isMessage(); boolean isMessage = setting.isMessage();
boolean isSet = !InputParsingHelper.isEmpty(currentValue); boolean isSet = !InputParsingHelper.isEmpty(currentValue);
// Display the description for how this setting is used // Display the description for how this setting is used
formatter.displaySuccessMessage(sender, setting.getDescription()); new FormatBuilder(setting.getDescription()).success(sender);
// Display the default value // Display the default value
formatter.displayNeutralMessage(sender, getDefaultValueMessage(commandName, defaultValue)); getDefaultValueMessage(commandName, defaultValue).neutral(sender);
if (isMessage) { if (isMessage) {
ConfigCommandHelper.displayRaw(sender, defaultValue); ConfigCommandHelper.displayRaw(sender, defaultValue);
} }
@@ -181,7 +177,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
} }
// Display the value with a marker if it's customized // Display the value with a marker if it's customized
formatter.displayNeutralMessage(sender, getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")); new FormatBuilder(getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")).neutral(sender);
if (isMessage) { if (isMessage) {
ConfigCommandHelper.displayRaw(sender, currentValue); ConfigCommandHelper.displayRaw(sender, currentValue);

View File

@@ -1,9 +1,9 @@
package net.knarcraft.blacksmith.command; package net.knarcraft.blacksmith.command;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.SmithPreset;
import net.knarcraft.blacksmith.config.SmithPresetFilter; import net.knarcraft.blacksmith.config.SmithPresetFilter;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
@@ -37,8 +37,7 @@ public class PresetCommand implements CommandExecutor {
SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]); SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]);
if (!smithPreset.supportsFilter(filter)) { if (!smithPreset.supportsFilter(filter)) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INVALID_FILTER_FOR_PRESET).error(sender);
BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET);
return false; return false;
} }
includedMaterials = smithPreset.getFilteredMaterials(filter); includedMaterials = smithPreset.getFilteredMaterials(filter);
@@ -46,8 +45,7 @@ public class PresetCommand implements CommandExecutor {
includedMaterials = SmithPreset.valueOf(presetName).getMaterials(); includedMaterials = SmithPreset.valueOf(presetName).getMaterials();
} }
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INVALID_PRESET_OR_FILTER).error(sender);
BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER);
return false; return false;
} }
@@ -56,9 +54,8 @@ public class PresetCommand implements CommandExecutor {
for (Material material : includedMaterials) { for (Material material : includedMaterials) {
materialNames.add(material.name()); materialNames.add(material.name());
} }
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, new FormatBuilder(Translatable.PRESET_MATERIALS).replace("{materials}",
BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.PRESET_MATERIALS, String.join(", ", materialNames)).success(sender);
"{materials}", String.join(", ", materialNames)));
return true; return true;
} }

View File

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

View File

@@ -5,8 +5,8 @@ import net.knarcraft.blacksmith.command.ReloadCommand;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings; import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.formatting.ItemType; import net.knarcraft.blacksmith.formatting.ItemType;
import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.blacksmith.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
@@ -92,19 +92,17 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} else { } else {
currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material)); currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material));
} }
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, Translatable.getItemCurrentValueMessage(setting.getCommandName(),
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), ItemType.MATERIAL, material.name(), currentValue).success(sender);
ItemType.MATERIAL, material.name(), currentValue));
return true; return true;
} else if (setting == BlacksmithSetting.ENCHANTMENT_COST) { } else if (setting == BlacksmithSetting.ENCHANTMENT_COST) {
Enchantment enchantment = InputParsingHelper.matchEnchantment(selector); Enchantment enchantment = InputParsingHelper.matchEnchantment(selector);
if (enchantment == null) { if (enchantment == null) {
return false; return false;
} }
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, Translatable.getItemCurrentValueMessage(setting.getCommandName(),
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.ENCHANTMENT, enchantment.getKey().getKey(), ItemType.ENCHANTMENT, enchantment.getKey().getKey(),
String.valueOf(settings.getEnchantmentCost(enchantment)))); String.valueOf(settings.getEnchantmentCost(enchantment))).success(sender);
return true; return true;
} else { } else {
return false; return false;
@@ -156,9 +154,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
ItemType itemType = ItemType.ENCHANTMENT; ItemType itemType = ItemType.ENCHANTMENT;
String itemChanged = enchantment.getKey().getKey(); String itemChanged = enchantment.getKey().getKey();
settings.setEnchantmentCost(enchantment, newPrice); settings.setEnchantmentCost(enchantment, newPrice);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, newValue).success(sender);
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, newValue));
return true; return true;
} else { } else {
return false; return false;
@@ -198,9 +194,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} }
} }
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, String.valueOf(newPrice)).success(sender);
itemType, itemChanged, String.valueOf(newPrice)));
return true; return true;
} }

View File

@@ -1,246 +0,0 @@
package net.knarcraft.blacksmith.config;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* A YAML configuration which retains all comments
*
* <p>This configuration converts all comments to YAML values when loaded, which all start with comment_. When saved,
* those YAML values are converted to normal text comments. This ensures that the comments aren't removed by the
* YamlConfiguration during its parsing.</p>
*
* <p>Note: When retrieving a configuration section, that will include comments that start with "comment_". You should
* filter those before parsing input.</p>
*
* @author Kristian Knarvik
* @author Thorin
*/
public class StargateYamlConfiguration extends YamlConfiguration {
private static final String START_OF_COMMENT_LINE = "[HASHTAG]";
private static final String END_OF_COMMENT = "_endOfComment_";
private static final String START_OF_COMMENT = "comment_";
@Override
@NotNull
@SuppressWarnings("deprecation")
protected String buildHeader() {
return "";
}
@Override
@NotNull
public String saveToString() {
// Convert YAML comments to normal comments
return this.convertYAMLMappingsToComments(super.saveToString());
}
@Override
public void loadFromString(@NotNull String contents) throws InvalidConfigurationException {
// Convert normal comments to YAML comments to prevent them from disappearing
super.loadFromString(this.convertCommentsToYAMLMappings(contents));
}
/**
* Gets a configuration section's keys, without any comment entries
*
* @param configurationSection <p>The configuration section to get keys for</p>
* @param deep <p>Whether to get keys for child elements as well</p>
* @return <p>The configuration section's keys, with comment entries removed</p>
*/
public static Set<String> getKeysWithoutComments(@NotNull ConfigurationSection configurationSection, boolean deep) {
Set<String> keys = new HashSet<>(configurationSection.getKeys(deep));
keys.removeIf(key -> key.matches(START_OF_COMMENT + "[0-9]+"));
return keys;
}
/**
* Reads a file with comments, and recreates them into yaml mappings
*
* <p>A mapping follows this format: comment_{CommentNumber}: "The comment"
* This needs to be done as comments otherwise get removed using
* the {@link FileConfiguration#save(File)} method. The config
* needs to be saved if a config value has changed.</p>
*/
@NotNull
private String convertCommentsToYAMLMappings(@NotNull String configString) {
StringBuilder yamlBuilder = new StringBuilder();
List<String> currentComment = new ArrayList<>();
int commentId = 0;
int previousIndentation = 0;
for (String line : configString.split("\n")) {
String trimmed = line.trim();
if (trimmed.startsWith("#")) {
// Store the indentation of the block
if (currentComment.isEmpty()) {
previousIndentation = getIndentation(line);
}
//Temporarily store the comment line
addComment(currentComment, trimmed);
} else {
addYamlString(yamlBuilder, currentComment, line, previousIndentation, commentId);
commentId++;
previousIndentation = 0;
}
}
return yamlBuilder.toString();
}
/**
* Adds a YAML string to the given string builder
*
* @param yamlBuilder <p>The string builder used for building YAML</p>
* @param currentComment <p>The comment to add as a YAML string</p>
* @param line <p>The current line</p>
* @param previousIndentation <p>The indentation of the current block comment</p>
* @param commentId <p>The id of the comment</p>
*/
private void addYamlString(@NotNull StringBuilder yamlBuilder, @NotNull List<String> currentComment,
@NotNull String line, int previousIndentation, int commentId) {
String trimmed = line.trim();
//Write the full formatted comment to the StringBuilder
if (!currentComment.isEmpty()) {
int indentation = trimmed.isEmpty() ? previousIndentation : getIndentation(line);
generateCommentYAML(yamlBuilder, currentComment, commentId, indentation);
currentComment.clear();
}
//Add the non-comment line assuming it isn't empty
if (!trimmed.isEmpty()) {
yamlBuilder.append(line).append("\n");
}
}
/**
* Adds the given comment to the given list
*
* @param commentParts <p>The list to add to</p>
* @param comment <p>The comment to add</p>
*/
private void addComment(@NotNull List<String> commentParts, @NotNull String comment) {
if (comment.startsWith("# ")) {
commentParts.add(comment.replaceFirst("# ", START_OF_COMMENT_LINE));
} else {
commentParts.add(comment.replaceFirst("#", START_OF_COMMENT_LINE));
}
}
/**
* Generates a YAML-compatible string for one comment block
*
* @param yamlBuilder <p>The string builder to add the generated YAML to</p>
* @param commentLines <p>The lines of the comment to convert into YAML</p>
* @param commentId <p>The unique id of the comment</p>
* @param indentation <p>The indentation to add to every line</p>
*/
private void generateCommentYAML(@NotNull StringBuilder yamlBuilder, @NotNull List<String> commentLines,
int commentId, int indentation) {
String subIndentation = this.addIndentation(indentation + 2);
//Add the comment start marker
yamlBuilder.append(this.addIndentation(indentation)).append(START_OF_COMMENT).append(commentId).append(": |\n");
for (String commentLine : commentLines) {
//Add each comment line with the proper indentation
yamlBuilder.append(subIndentation).append(commentLine).append("\n");
}
//Add the comment end marker
yamlBuilder.append(subIndentation).append(subIndentation).append(END_OF_COMMENT).append("\n");
}
/**
* Converts the internal YAML mapping format to a readable config file
*
* <p>The internal YAML structure is converted to a string with the same format as a standard configuration file.
* The internal structure has comments in the format: START_OF_COMMENT + id + multi-line YAML string +
* END_OF_COMMENT.</p>
*
* @param yamlString <p>A string using the YAML format</p>
* @return <p>The corresponding comment string</p>
*/
@NotNull
private String convertYAMLMappingsToComments(@NotNull String yamlString) {
StringBuilder finalText = new StringBuilder();
String[] lines = yamlString.split("\n");
for (int currentIndex = 0; currentIndex < lines.length; currentIndex++) {
String line = lines[currentIndex];
String possibleComment = line.trim();
if (possibleComment.startsWith(START_OF_COMMENT)) {
//Add an empty line before every comment block
finalText.append("\n");
currentIndex = readComment(finalText, lines, currentIndex + 1, getIndentation(line));
} else {
//Output the configuration key
finalText.append(line).append("\n");
}
}
return finalText.toString().trim();
}
/**
* Fully reads a comment
*
* @param builder <p>The string builder to write to</p>
* @param lines <p>The lines to read from</p>
* @param startIndex <p>The index to start reading from</p>
* @param commentIndentation <p>The indentation of the read comment</p>
* @return <p>The index containing the next non-comment line</p>
*/
private int readComment(@NotNull StringBuilder builder, @NotNull String[] lines, int startIndex,
int commentIndentation) {
for (int currentIndex = startIndex; currentIndex < lines.length; currentIndex++) {
String line = lines[currentIndex];
String possibleComment = line.trim();
if (!line.contains(END_OF_COMMENT)) {
possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, "");
builder.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n");
} else {
return currentIndex;
}
}
return startIndex;
}
/**
* Gets a string containing the given indentation
*
* @param indentationSpaces <p>The number spaces to use for indentation</p>
* @return <p>A string containing the number of spaces specified</p>
*/
@NotNull
private String addIndentation(int indentationSpaces) {
return " ".repeat(Math.max(0, indentationSpaces));
}
/**
* Gets the indentation (number of spaces) of the given line
*
* @param line <p>The line to get indentation of</p>
* @return <p>The number of spaces in the line's indentation</p>
*/
private int getIndentation(@NotNull String line) {
int spacesFound = 0;
for (char aCharacter : line.toCharArray()) {
if (aCharacter == ' ') {
spacesFound++;
} else {
break;
}
}
return spacesFound;
}
}

View File

@@ -3,10 +3,10 @@ package net.knarcraft.blacksmith.config.blacksmith;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.knarlib.config.StargateYamlConfiguration;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@@ -310,7 +310,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*/ */
private void loadSettings() { private void loadSettings() {
instance.reloadConfig(); instance.reloadConfig();
FileConfiguration configuration = instance.getConfiguration(); FileConfiguration configuration = instance.getConfig();
for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) { for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) {
if (!configuration.contains(blacksmithSetting.getPath())) { if (!configuration.contains(blacksmithSetting.getPath())) {
@@ -501,7 +501,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* Saves all current settings to the config file * Saves all current settings to the config file
*/ */
private void save() { private void save() {
FileConfiguration fileConfiguration = instance.getConfiguration(); FileConfiguration fileConfiguration = instance.getConfig();
//Save all default settings //Save all default settings
for (BlacksmithSetting setting : BlacksmithSetting.values()) { for (BlacksmithSetting setting : BlacksmithSetting.values()) {

View File

@@ -145,7 +145,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
*/ */
private void loadSettings() { private void loadSettings() {
instance.reloadConfig(); instance.reloadConfig();
FileConfiguration configuration = instance.getConfiguration(); FileConfiguration configuration = instance.getConfig();
for (ScrapperSetting setting : ScrapperSetting.values()) { for (ScrapperSetting setting : ScrapperSetting.values()) {
if (!configuration.contains(setting.getPath())) { if (!configuration.contains(setting.getPath())) {
@@ -164,7 +164,7 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* Saves all current settings to the config file * Saves all current settings to the config file
*/ */
private void save() { private void save() {
FileConfiguration fileConfiguration = instance.getConfiguration(); FileConfiguration fileConfiguration = instance.getConfig();
//Save all default settings //Save all default settings
for (ScrapperSetting setting : ScrapperSetting.values()) { for (ScrapperSetting setting : ScrapperSetting.values()) {

View File

@@ -1,33 +0,0 @@
package net.knarcraft.blacksmith.formatting;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper;
import org.bukkit.entity.Player;
import java.util.List;
/**
* A formatter for formatting displayed messages
*/
public final class BlacksmithStringFormatter {
private BlacksmithStringFormatter() {
}
/**
* Sends a message from a blacksmith NPC to a player
*
* @param npc <p>The NPC sending the message</p>
* @param player <p>The player to send the message to</p>
* @param message <p>The message to send</p>
*/
public static void sendNPCMessage(NPC npc, Player player, String message) {
player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders(
BlacksmithTranslatableMessage.NPC_TALK_FORMAT, List.of("{npc}", "{message}"),
List.of(npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB))));
}
}

View File

@@ -1,7 +1,6 @@
package net.knarcraft.blacksmith.formatting; package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.formatting.StringFormatter;
/** /**
* An enum representing all item types used in messages * An enum representing all item types used in messages
@@ -17,11 +16,9 @@ public enum ItemType {
* @return <p>The name of this item type</p> * @return <p>The name of this item type</p>
*/ */
public String getItemTypeName() { public String getItemTypeName() {
StringFormatter stringFormatter = BlacksmithPlugin.getStringFormatter();
return switch (this) { return switch (this) {
case MATERIAL -> stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); case MATERIAL -> new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).toString();
case ENCHANTMENT -> case ENCHANTMENT -> new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).toString();
stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
}; };
} }

View File

@@ -0,0 +1,40 @@
package net.knarcraft.blacksmith.formatting;
import net.citizensnpcs.api.npc.NPC;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
/**
* A formatter for formatting displayed messages
*/
public final class NPCFormatter {
private NPCFormatter() {
}
/**
* Sends a message from a blacksmith NPC to a player
*
* @param npc <p>The NPC sending the message</p>
* @param player <p>The player to send the message to</p>
* @param message <p>The message to send</p>
*/
public static void sendNPCMessage(@NotNull NPC npc, @NotNull Player player, @NotNull String message) {
player.sendMessage(new FormatBuilder(Translatable.NPC_TALK_FORMAT).replace("{npc}",
npc.getName()).replace("{message}", message).color().toString());
}
/**
* Sends a message from a blacksmith NPC to a player
*
* @param npc <p>The NPC sending the message</p>
* @param player <p>The player to send the message to</p>
* @param formatBuilder <p>The format builder to send</p>
*/
public static void sendNPCMessage(@NotNull NPC npc, @NotNull Player player, @NotNull FormatBuilder formatBuilder) {
sendNPCMessage(npc, player, formatBuilder.toString());
}
}

View File

@@ -1,6 +1,7 @@
package net.knarcraft.blacksmith.formatting; package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -53,8 +54,7 @@ public final class TimeFormatter {
* @return <p>Text describing the time interval</p> * @return <p>Text describing the time interval</p>
*/ */
private static String getMessageFromInterval(TimeInterval interval) { private static String getMessageFromInterval(TimeInterval interval) {
String text = BlacksmithPlugin.getStringFormatter().getUnFormattedMessage( String text = new FormatBuilder(Translatable.valueOf(interval.name())).toString();
BlacksmithTranslatableMessage.valueOf(interval.name()));
//Choose a random entry if a comma-separated list is provided //Choose a random entry if a comma-separated list is provided
if (text.contains(",")) { if (text.contains(",")) {

View File

@@ -1,19 +1,16 @@
package net.knarcraft.blacksmith.formatting; package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.formatting.StringReplacer;
import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableMessage;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.List;
/** /**
* An enum containing all translatable global messages * An enum containing all translatable global messages
* *
* <p>This does not include NPC messages as they are configurable per-npc, and can be translated by changing the * <p>This does not include NPC messages as they are configurable per-npc, and can be translated by changing the
* default message values in the main config file.</p> * default message values in the main config file.</p>
*/ */
public enum BlacksmithTranslatableMessage implements TranslatableMessage { public enum Translatable implements TranslatableMessage {
/** /**
* The message displayed when a configuration value has been successfully changed * The message displayed when a configuration value has been successfully changed
@@ -149,12 +146,11 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* Gets the message to display when displaying the raw value of messages * Gets the message to display when displaying the raw value of messages
* *
* @param rawValue <p>The raw value to display</p> * @param rawValue <p>The raw value to display</p>
* @return <p>The message to display</p> * @return <p>The format builder to display</p>
*/ */
@NotNull @NotNull
public static String getRawValueMessage(@NotNull String rawValue) { public static FormatBuilder getRawValueMessage(@NotNull String rawValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.RAW_VALUE, return new FormatBuilder(Translatable.RAW_VALUE).replace("{rawValue}", rawValue);
"{rawValue}", rawValue);
} }
/** /**
@@ -162,12 +158,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* *
* @param setting <p>The setting whose value has been changed</p> * @param setting <p>The setting whose value has been changed</p>
* @param newValue <p>The new value of the setting</p> * @param newValue <p>The new value of the setting</p>
* @return <p>The string to display to a user</p> * @return <p>The format builder to display to a user</p>
*/ */
@NotNull @NotNull
public static String getValueChangedMessage(@NotNull String setting, @NotNull String newValue) { public static FormatBuilder getValueChangedMessage(@NotNull String setting, @NotNull String newValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.VALUE_CHANGED, return new FormatBuilder(Translatable.VALUE_CHANGED).replace("{setting}", setting).
List.of("{setting}", "{newValue}"), List.of(setting, newValue)); replace("{newValue}", newValue);
} }
/** /**
@@ -177,18 +173,14 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @param itemType <p>The type of item changed ("material" or "enchantment")</p> * @param itemType <p>The type of item changed ("material" or "enchantment")</p>
* @param item <p>The item the setting was changed for (a material or an enchantment name)</p> * @param item <p>The item the setting was changed for (a material or an enchantment name)</p>
* @param newValue <p>The new value of the setting</p> * @param newValue <p>The new value of the setting</p>
* @return <p>The string to display to a user</p> * @return <p>The format builder to display to a user</p>
*/ */
@NotNull @NotNull
public static String getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType, public static FormatBuilder getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String newValue) { @NotNull String item, @NotNull String newValue) {
StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnFormattedMessage( return new FormatBuilder(Translatable.VALUE_FOR_ITEM_CHANGED).replace("{setting}", setting).
BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED)); replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item).
stringReplacer.add("{setting}", setting); replace("{newValue}", newValue);
stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{newValue}", newValue);
return stringReplacer.replace();
} }
/** /**
@@ -196,13 +188,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* *
* @param setting <p>The setting whose value is shown</p> * @param setting <p>The setting whose value is shown</p>
* @param defaultValue <p>The default value of the setting</p> * @param defaultValue <p>The default value of the setting</p>
* @return <p>The string to display to a user</p> * @return <p>The format builder to display to a user</p>
*/ */
@NotNull @NotNull
public static String getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) { public static FormatBuilder getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.DEFAULT_VALUE, return new FormatBuilder(Translatable.DEFAULT_VALUE).replace("{setting}", setting).
List.of("{setting}", "{defaultValue}"), replace("{defaultValue}", defaultValue);
List.of(setting, defaultValue));
} }
/** /**
@@ -210,13 +201,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* *
* @param setting <p>The setting whose value is shown</p> * @param setting <p>The setting whose value is shown</p>
* @param currentValue <p>The current value of the setting</p> * @param currentValue <p>The current value of the setting</p>
* @return <p>The string to display to a user</p> * @return <p>The format builder to display to a user</p>
*/ */
@NotNull @NotNull
public static String getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) { public static FormatBuilder getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.CURRENT_VALUE, return new FormatBuilder(Translatable.CURRENT_VALUE).replace("{setting}", setting).
List.of("{setting}", "{currentValue}"), replace("{currentValue}", currentValue);
List.of(setting, currentValue));
} }
/** /**
@@ -226,24 +216,20 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @param itemType <p>The type of item shown ("material" or "enchantment")</p> * @param itemType <p>The type of item shown ("material" or "enchantment")</p>
* @param item <p>The item the setting is shown for (a material or an enchantment name)</p> * @param item <p>The item the setting is shown for (a material or an enchantment name)</p>
* @param currentValue <p>The current value of the setting</p> * @param currentValue <p>The current value of the setting</p>
* @return <p>The string to display to a user</p> * @return <p>The format builder to display to a user</p>
*/ */
@NotNull @NotNull
public static String getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType, public static FormatBuilder getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String currentValue) { @NotNull String item, @NotNull String currentValue) {
StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnFormattedMessage( return new FormatBuilder(Translatable.CURRENT_VALUE_FOR_ITEM).replace("{setting}", setting).
BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM)); replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item).
stringReplacer.add("{setting}", setting); replace("{currentValue}", currentValue);
stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{currentValue}", currentValue);
return stringReplacer.replace();
} }
@Override @Override
@NotNull @NotNull
public TranslatableMessage[] getAllMessages() { public TranslatableMessage[] getAllMessages() {
return BlacksmithTranslatableMessage.values(); return Translatable.values();
} }
} }

View File

@@ -2,10 +2,11 @@ package net.knarcraft.blacksmith.listener;
import net.citizensnpcs.api.event.NPCRightClickEvent; import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.blacksmith.trait.CustomTrait; import net.knarcraft.blacksmith.trait.CustomTrait;
import net.knarcraft.blacksmith.trait.ScrapperTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@@ -44,8 +45,7 @@ public class NPCClickListener implements Listener {
//Permission check //Permission check
if (!player.hasPermission("blacksmith.use")) { if (!player.hasPermission("blacksmith.use")) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(player, new FormatBuilder(Translatable.PERMISSION_DENIED).error(player);
BlacksmithTranslatableMessage.PERMISSION_DENIED);
return; return;
} }

View File

@@ -4,9 +4,10 @@ import net.citizensnpcs.api.util.DataKey;
import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -15,8 +16,6 @@ import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.List;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/** /**
* The class representing the Blacksmith NPC trait * The class representing the Blacksmith NPC trait
*/ */
@@ -69,7 +68,7 @@ public class BlacksmithTrait extends CustomTrait<BlacksmithSetting> {
public void startSession(@NotNull Player player) { public void startSession(@NotNull Player player) {
ItemStack hand = player.getInventory().getItemInMainHand(); ItemStack hand = player.getInventory().getItemInMainHand();
if (hand.getType().isAir()) { if (hand.getType().isAir()) {
sendNPCMessage(this.npc, player, config.getNoItemMessage()); NPCFormatter.sendNPCMessage(this.npc, player, config.getNoItemMessage());
return; return;
} }
@@ -81,14 +80,14 @@ public class BlacksmithTrait extends CustomTrait<BlacksmithSetting> {
(!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType())); (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType()));
if (notHoldingAnvil && notHoldingRepairable) { if (notHoldingAnvil && notHoldingRepairable) {
String invalidMessage = StringFormatter.replacePlaceholder(config.getInvalidItemMessage(), String invalidMessage = new FormatBuilder(config.getInvalidItemMessage()).replace("{title}",
"{title}", config.getBlacksmithTitle()); config.getBlacksmithTitle()).toString();
sendNPCMessage(this.npc, player, invalidMessage); NPCFormatter.sendNPCMessage(this.npc, player, invalidMessage);
return; return;
} }
if (ItemHelper.getDamage(hand) == 0 && !ItemHelper.isAnvil(hand.getType(), true)) { if (ItemHelper.getDamage(hand) == 0 && !ItemHelper.isAnvil(hand.getType(), true)) {
sendNPCMessage(this.npc, player, config.getNotDamagedMessage()); NPCFormatter.sendNPCMessage(this.npc, player, config.getNotDamagedMessage());
return; return;
} }
@@ -104,8 +103,8 @@ public class BlacksmithTrait extends CustomTrait<BlacksmithSetting> {
//Tell the player the cost of repairing the item //Tell the player the cost of repairing the item
String cost = EconomyManager.formatBlacksmithCost(player); String cost = EconomyManager.formatBlacksmithCost(player);
String itemName = hand.getType().name().toLowerCase().replace('_', ' '); String itemName = hand.getType().name().toLowerCase().replace('_', ' ');
sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost).replace("{item}", NPCFormatter.sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost).
itemName)); replace("{item}", itemName));
} }
@Override @Override

View File

@@ -6,9 +6,10 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.config.TraitSettings; import net.knarcraft.blacksmith.config.TraitSettings;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.formatting.TimeFormatter; import net.knarcraft.blacksmith.formatting.TimeFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -23,8 +24,6 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/** /**
* A custom trait that utilizes sessions * A custom trait that utilizes sessions
*/ */
@@ -77,7 +76,7 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
public void continueSession(@NotNull Player player) { public void continueSession(@NotNull Player player) {
//Another player is using the blacksmith //Another player is using the blacksmith
if (session.isNotInSession(player)) { if (session.isNotInSession(player)) {
sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage()); NPCFormatter.sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage());
return; return;
} }
@@ -85,8 +84,8 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
if (session.isRunning()) { if (session.isRunning()) {
int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000); int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000);
boolean showExactTime = showExactTime(); boolean showExactTime = showExactTime();
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getBusyWorkingMessage(), NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getBusyWorkingMessage()).
"{time}", TimeFormatter.formatTime(showExactTime, timeRemaining))); replace("{time}", TimeFormatter.formatTime(showExactTime, timeRemaining)));
return; return;
} }
if (session.isSessionInvalid()) { if (session.isSessionInvalid()) {
@@ -147,8 +146,8 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
if (!calendar.after(coolDowns.get(playerId))) { if (!calendar.after(coolDowns.get(playerId))) {
int secondDifference = (int) (coolDowns.get(playerId).getTimeInMillis() - calendar.getTimeInMillis()) / 1000; int secondDifference = (int) (coolDowns.get(playerId).getTimeInMillis() - calendar.getTimeInMillis()) / 1000;
boolean exactTime = showExactTime(); boolean exactTime = showExactTime();
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getCoolDownUnexpiredMessage(), NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getCoolDownUnexpiredMessage()).
"{time}", TimeFormatter.formatTime(exactTime, secondDifference))); replace("{time}", TimeFormatter.formatTime(exactTime, secondDifference)));
return false; return false;
} }
coolDowns.remove(playerId); coolDowns.remove(playerId);
@@ -181,7 +180,7 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
* @param player <p>The player that initiated the reforge</p> * @param player <p>The player that initiated the reforge</p>
*/ */
private void startMainAction(@NotNull NPC npc, @NotNull Player player) { private void startMainAction(@NotNull NPC npc, @NotNull Player player) {
sendNPCMessage(this.npc, player, config.getStartWorkingMessage()); NPCFormatter.sendNPCMessage(this.npc, player, config.getStartWorkingMessage());
boolean isBlacksmith = this instanceof BlacksmithTrait; boolean isBlacksmith = this instanceof BlacksmithTrait;

View File

@@ -5,6 +5,7 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings;
import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent; import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent;
import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent; import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
@@ -24,8 +25,6 @@ import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/** /**
* A representation of the session between a player and a blacksmith * A representation of the session between a player and a blacksmith
*/ */
@@ -70,12 +69,12 @@ public class ReforgeSession extends Session implements Runnable {
// Prevent player from switching items during session // Prevent player from switching items during session
ItemStack itemInHand = this.player.getInventory().getItemInMainHand(); ItemStack itemInHand = this.player.getInventory().getItemInMainHand();
if (!itemToReforge.equals(itemInHand)) { if (!itemToReforge.equals(itemInHand)) {
sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
return true; return true;
} }
// The player is unable to pay // The player is unable to pay
if (EconomyManager.cannotPayForHeldItemReforge(this.player)) { if (EconomyManager.cannotPayForHeldItemReforge(this.player)) {
sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage());
return true; return true;
} }
return false; return false;
@@ -103,7 +102,7 @@ public class ReforgeSession extends Session implements Runnable {
@Override @Override
public void run() { public void run() {
boolean success = reforgeItem(); boolean success = reforgeItem();
sendNPCMessage(this.npc, this.player, success ? this.config.getSuccessMessage() : this.config.getFailMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, success ? this.config.getSuccessMessage() : this.config.getFailMessage());
playSound(success ? Sound.BLOCK_ANVIL_USE : Sound.ENTITY_VILLAGER_NO); playSound(success ? Sound.BLOCK_ANVIL_USE : Sound.ENTITY_VILLAGER_NO);
//Stop the reforged item from displaying in the blacksmith's hand //Stop the reforged item from displaying in the blacksmith's hand

View File

@@ -5,6 +5,7 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings;
import net.knarcraft.blacksmith.event.ScrapperSalvageFailEvent; import net.knarcraft.blacksmith.event.ScrapperSalvageFailEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent; import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.property.SalvageMethod;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
@@ -22,8 +23,6 @@ import java.util.Calendar;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/** /**
* A representation of the session between a player and a scrapper * A representation of the session between a player and a scrapper
*/ */
@@ -66,12 +65,12 @@ public class SalvageSession extends Session implements Runnable {
// Prevent player from switching items during session // Prevent player from switching items during session
ItemStack itemInHand = this.player.getInventory().getItemInMainHand(); ItemStack itemInHand = this.player.getInventory().getItemInMainHand();
if (!itemToSalvage.equals(itemInHand)) { if (!itemToSalvage.equals(itemInHand)) {
sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
return true; return true;
} }
if (EconomyManager.cannotPayForSalvage(this.player, this.salvageMethod)) { if (EconomyManager.cannotPayForSalvage(this.player, this.salvageMethod)) {
sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage());
return true; return true;
} }
return false; return false;
@@ -146,7 +145,7 @@ public class SalvageSession extends Session implements Runnable {
playSound(Sound.BLOCK_ANVIL_USE); playSound(Sound.BLOCK_ANVIL_USE);
giveSalvage(); giveSalvage();
BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageSucceedEvent(this.npc, this.entity, this.player)); BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageSucceedEvent(this.npc, this.entity, this.player));
sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage());
} }
} }
@@ -161,14 +160,14 @@ public class SalvageSession extends Session implements Runnable {
//Damage the item if possible //Damage the item if possible
damageItemRandomly(this.itemToSalvage); damageItemRandomly(this.itemToSalvage);
giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), this.itemToSalvage); giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), this.itemToSalvage);
sendNPCMessage(this.npc, this.player, this.config.getFailSalvageMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getFailSalvageMessage());
} else { } else {
// Give half the salvage // Give half the salvage
List<ItemStack> halfSalvage = SalvageHelper.pickRandomSalvage(this.salvage, new ArrayList<>(), 0.5); List<ItemStack> halfSalvage = SalvageHelper.pickRandomSalvage(this.salvage, new ArrayList<>(), 0.5);
for (ItemStack item : halfSalvage) { for (ItemStack item : halfSalvage) {
giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), item); giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), item);
} }
sendNPCMessage(this.npc, this.player, this.config.getFailExtendedSalvageMessage()); NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getFailExtendedSalvageMessage());
} }
} }

View File

@@ -8,12 +8,13 @@ import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings;
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
import net.knarcraft.blacksmith.container.RecipeResult; import net.knarcraft.blacksmith.container.RecipeResult;
import net.knarcraft.blacksmith.container.SalvageResult; import net.knarcraft.blacksmith.container.SalvageResult;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.property.SalvageMethod;
import net.knarcraft.blacksmith.property.SalvageState; import net.knarcraft.blacksmith.property.SalvageState;
import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.blacksmith.util.SalvageHelper; import net.knarcraft.blacksmith.util.SalvageHelper;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.formatting.StringReplacer; import net.knarcraft.knarlib.formatting.StringReplacer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
@@ -28,8 +29,6 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/** /**
* The class representing a scrapper NPC trait * The class representing a scrapper NPC trait
*/ */
@@ -86,7 +85,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
public void startSession(@NotNull Player player) { public void startSession(@NotNull Player player) {
ItemStack itemInHand = player.getInventory().getItemInMainHand().clone(); ItemStack itemInHand = player.getInventory().getItemInMainHand().clone();
if (itemInHand.getType().isAir()) { if (itemInHand.getType().isAir()) {
sendNPCMessage(this.npc, player, config.getNoItemMessage()); NPCFormatter.sendNPCMessage(this.npc, player, config.getNoItemMessage());
return; return;
} }
@@ -95,8 +94,8 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
// Check if the item can be salvaged // Check if the item can be salvaged
if (!canBeSalvaged(itemInHand, salvageAbleItems, extended)) { if (!canBeSalvaged(itemInHand, salvageAbleItems, extended)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(), NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()).
"{title}", getSettings().getScrapperTitle())); replace("{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.debug("Cannot salvage provided item: " + itemInHand); BlacksmithPlugin.debug("Cannot salvage provided item: " + itemInHand);
return; return;
} }
@@ -168,15 +167,15 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
// As there is no recipe for netherite items, the check needs to be after the netherite salvage check // As there is no recipe for netherite items, the check needs to be after the netherite salvage check
if (!SalvageHelper.isSalvageable(player.getServer(), itemInHand)) { if (!SalvageHelper.isSalvageable(player.getServer(), itemInHand)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(), NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()).
"{title}", getSettings().getScrapperTitle())); replace("{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.debug("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); return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} }
// Check if the item is enchanted, and whether this blacksmith can salvage it // Check if the item is enchanted, and whether this blacksmith can salvage it
if (!itemInHand.getEnchantments().isEmpty() && !getSettings().salvageEnchanted()) { if (!itemInHand.getEnchantments().isEmpty() && !getSettings().salvageEnchanted()) {
sendNPCMessage(this.npc, player, getSettings().getCannotSalvageEnchantedMessage()); NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageEnchantedMessage());
return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} }
@@ -185,7 +184,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE; SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE;
boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty(); boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty();
if (noUsefulSalvage) { if (noUsefulSalvage) {
sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage());
return new SalvageResult(method, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); return new SalvageResult(method, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} else { } else {
return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE, return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE,
@@ -207,7 +206,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
} }
if (!getSettings().salvageNetherite()) { if (!getSettings().salvageNetherite()) {
sendNPCMessage(this.npc, player, getSettings().getCannotSalvageNetheriteMessage()); NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageNetheriteMessage());
return new SalvageResult(SalvageMethod.NETHERITE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); return new SalvageResult(SalvageMethod.NETHERITE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} }
@@ -233,13 +232,13 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
} }
if (!getSettings().salvageArmorTrims()) { if (!getSettings().salvageArmorTrims()) {
sendNPCMessage(this.npc, player, getSettings().getCannotSalvageArmorTrimMessage()); NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageArmorTrimMessage());
return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} }
List<ItemStack> salvage = SalvageHelper.getArmorTrimSalvage(itemInHand, armorMeta); List<ItemStack> salvage = SalvageHelper.getArmorTrimSalvage(itemInHand, armorMeta);
if (salvage == null || salvage.isEmpty()) { if (salvage == null || salvage.isEmpty()) {
sendNPCMessage(this.npc, player, getSettings().getArmorTrimSalvageNotFoundMessage()); NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getArmorTrimSalvageNotFoundMessage());
return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
} }
@@ -260,7 +259,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
replacer.add("{cost}", cost); replacer.add("{cost}", cost);
replacer.add("{item}", itemInHand.getType().name().toLowerCase().replace('_', ' ')); replacer.add("{item}", itemInHand.getType().name().toLowerCase().replace('_', ' '));
if (salvageMethod == SalvageMethod.ARMOR_TRIM) { if (salvageMethod == SalvageMethod.ARMOR_TRIM) {
sendNPCMessage(this.npc, player, replacer.replace(getSettings().getArmorTrimCostMessage())); NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getArmorTrimCostMessage()));
} else if (salvageMethod == SalvageMethod.SALVAGE || salvageMethod == SalvageMethod.EXTENDED_SALVAGE) { } else if (salvageMethod == SalvageMethod.SALVAGE || salvageMethod == SalvageMethod.EXTENDED_SALVAGE) {
String expectedYield; String expectedYield;
if (ItemHelper.getDamage(itemInHand) <= 0) { if (ItemHelper.getDamage(itemInHand) <= 0) {
@@ -269,9 +268,9 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
expectedYield = getSettings().getPartialSalvageMessage(); expectedYield = getSettings().getPartialSalvageMessage();
} }
replacer.add("{yield}", expectedYield); replacer.add("{yield}", expectedYield);
sendNPCMessage(this.npc, player, replacer.replace(getSettings().getCostMessage())); NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getCostMessage()));
} else if (salvageMethod == SalvageMethod.NETHERITE) { } else if (salvageMethod == SalvageMethod.NETHERITE) {
sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage())); NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage()));
} else { } else {
BlacksmithPlugin.error("Unrecognized salvage method " + salvageMethod); BlacksmithPlugin.error("Unrecognized salvage method " + salvageMethod);
} }

View File

@@ -1,20 +1,19 @@
package net.knarcraft.blacksmith.util; package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getDefaultValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; import static net.knarcraft.blacksmith.formatting.Translatable.getValueChangedMessage;
/** /**
* A helper class for the configuration command * A helper class for the configuration command
@@ -43,8 +42,7 @@ public final class ConfigCommandHelper {
newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); newValue = String.join(" ", Arrays.asList(args).subList(1, args.length));
} }
settings.changeValue(detectedSetting, newValue); settings.changeValue(detectedSetting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, getValueChangedMessage(detectedSetting.getCommandName(), newValue).success(sender);
getValueChangedMessage(detectedSetting.getCommandName(), newValue));
} }
/** /**
@@ -57,7 +55,6 @@ public final class ConfigCommandHelper {
public static <K extends Setting> void displayCurrentValue(@NotNull K setting, public static <K extends Setting> void displayCurrentValue(@NotNull K setting,
@NotNull Settings<K> settings, @NotNull Settings<K> settings,
@NotNull CommandSender sender) { @NotNull CommandSender sender) {
StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
boolean printRawValue = false; boolean printRawValue = false;
//Find the value of the specified setting //Find the value of the specified setting
@@ -70,16 +67,16 @@ public final class ConfigCommandHelper {
} }
// Display the description of the setting // Display the description of the setting
formatter.displaySuccessMessage(sender, setting.getDescription()); new FormatBuilder(setting.getDescription()).success(sender);
// Display the setting's default value // Display the setting's default value
formatter.displayNeutralMessage(sender, getDefaultValueMessage(setting.getCommandName(), defaultValue)); getDefaultValueMessage(setting.getCommandName(), defaultValue).neutral(sender);
if (printRawValue) { if (printRawValue) {
displayRaw(sender, defaultValue); displayRaw(sender, defaultValue);
} }
// Display the current value of the setting // Display the current value of the setting
formatter.displayNeutralMessage(sender, getCurrentValueMessage(setting.getCommandName(), currentValue)); getCurrentValueMessage(setting.getCommandName(), currentValue).neutral(sender);
if (printRawValue) { if (printRawValue) {
displayRaw(sender, currentValue); displayRaw(sender, currentValue);
} }
@@ -92,8 +89,7 @@ public final class ConfigCommandHelper {
* @param value <p>The value to display raw</p> * @param value <p>The value to display raw</p>
*/ */
public static void displayRaw(@NotNull CommandSender sender, @NotNull String value) { public static void displayRaw(@NotNull CommandSender sender, @NotNull String value) {
sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( sender.sendMessage(Translatable.getRawValueMessage(value.replace(ChatColor.COLOR_CHAR, '&')).toString());
value.replace(ChatColor.COLOR_CHAR, '&')));
} }
} }

View File

@@ -1,22 +1,10 @@
package net.knarcraft.blacksmith.util; package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.FileHelper;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* A helper class for getting an object value as the correct type * A helper class for getting an object value as the correct type
@@ -106,103 +94,4 @@ public final class ConfigHelper {
} }
} }
/**
* Changes all configuration values from the old name to the new name
*
* @param dataFolderPath <p>The path to this plugin's data</p>
* @param currentConfiguration <p>The current config to back up</p>
*/
public static void migrateConfig(@NotNull String dataFolderPath, @NotNull FileConfiguration currentConfiguration) {
BlacksmithPlugin instance = BlacksmithPlugin.getInstance();
//Save the old config just in case something goes wrong
try {
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
} catch (IOException exception) {
BlacksmithPlugin.warn("Unable to save old backup and do migration");
return;
}
//Load old and new configuration
instance.reloadConfig();
FileConfiguration oldConfiguration = instance.getConfig();
InputStream configStream = FileHelper.getInputStreamForInternalFile("/config.yml");
if (configStream == null) {
BlacksmithPlugin.error("Could not migrate the configuration, as the internal configuration could not be read!");
return;
}
YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
FileHelper.getBufferedReaderFromInputStream(configStream));
//Read all available config migrations
Map<String, String> migrationFields;
try {
InputStream migrationStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt");
if (migrationStream == null) {
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) {
BlacksmithPlugin.warn("Unable to load config migration file");
return;
}
//Replace old config names with the new ones
for (String key : migrationFields.keySet()) {
if (oldConfiguration.contains(key)) {
migrateProperty(migrationFields, key, oldConfiguration);
}
}
// Copy all keys to the new config
for (String key : StargateYamlConfiguration.getKeysWithoutComments(oldConfiguration, true)) {
if (oldConfiguration.get(key) instanceof MemorySection) {
continue;
}
newConfiguration.set(key, oldConfiguration.get(key));
}
try {
newConfiguration.save(new File(dataFolderPath, "config.yml"));
} catch (IOException exception) {
BlacksmithPlugin.warn("Unable to save migrated config");
}
instance.reloadConfig();
}
/**
* Migrates one configuration property
*
* @param migrationFields <p>The configuration fields to be migrated</p>
* @param key <p>The key/path of the property to migrate</p>
* @param oldConfiguration <p>The original pre-migration configuration</p>
*/
private static void migrateProperty(@NotNull Map<String, String> migrationFields, @NotNull String key,
@NotNull FileConfiguration oldConfiguration) {
String newPath = migrationFields.get(key);
Object oldValue = oldConfiguration.get(key);
if (!newPath.trim().isEmpty()) {
if (oldConfiguration.isConfigurationSection(key)) {
// Copy each value of a configuration section
ConfigurationSection sourceSection = oldConfiguration.getConfigurationSection(key);
ConfigurationSection destinationSection = oldConfiguration.createSection(newPath);
if (sourceSection == null) {
return;
}
for (String path : StargateYamlConfiguration.getKeysWithoutComments(sourceSection, true)) {
destinationSection.set(path, sourceSection.get(path));
}
} else {
// Copy the value to the new path
oldConfiguration.set(newPath, oldValue);
}
}
// Remove the old path's value
oldConfiguration.set(key, null);
}
} }

View File

@@ -1,8 +1,8 @@
package net.knarcraft.blacksmith.util; package net.knarcraft.blacksmith.util;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
@@ -59,8 +59,7 @@ public final class TypeValidationHelper {
boolean isMaterial = value instanceof Material || (value instanceof String string && boolean isMaterial = value instanceof Material || (value instanceof String string &&
InputParsingHelper.matchMaterial(string) != null); InputParsingHelper.matchMaterial(string) != null);
if (!isMaterial && sender != null) { if (!isMaterial && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).error(sender);
BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
} }
return isMaterial; return isMaterial;
} }
@@ -104,8 +103,7 @@ public final class TypeValidationHelper {
boolean isEnchantment = value instanceof Enchantment || (value instanceof String string && boolean isEnchantment = value instanceof Enchantment || (value instanceof String string &&
InputParsingHelper.matchEnchantment(string) != null); InputParsingHelper.matchEnchantment(string) != null);
if (!isEnchantment && sender != null) { if (!isEnchantment && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).error(sender);
BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
} }
return isEnchantment; return isEnchantment;
} }
@@ -120,8 +118,7 @@ public final class TypeValidationHelper {
private static boolean isStringList(@Nullable Object value, @Nullable CommandSender sender) { private static boolean isStringList(@Nullable Object value, @Nullable CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String; boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) { if (!isStringList && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INPUT_STRING_LIST_REQUIRED).error(sender);
BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED);
} }
return isStringList; return isStringList;
} }
@@ -137,8 +134,7 @@ public final class TypeValidationHelper {
boolean isPercentage = value != null && isPositiveInteger(value, null) && boolean isPercentage = value != null && isPositiveInteger(value, null) &&
ConfigHelper.asInt(value) >= 0 && ConfigHelper.asInt(value) <= 100; ConfigHelper.asInt(value) >= 0 && ConfigHelper.asInt(value) <= 100;
if (!isPercentage && sender != null) { if (!isPercentage && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INPUT_PERCENTAGE_REQUIRED).error(sender);
BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
} }
return isPercentage; return isPercentage;
} }
@@ -153,8 +149,7 @@ public final class TypeValidationHelper {
private static boolean isNonEmptyString(@Nullable Object value, @Nullable CommandSender sender) { private static boolean isNonEmptyString(@Nullable Object value, @Nullable CommandSender sender) {
boolean isString = value instanceof String string && !string.isBlank(); boolean isString = value instanceof String string && !string.isBlank();
if (!isString && sender != null) { if (!isString && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INPUT_STRING_REQUIRED).error(sender);
BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED);
} }
return isString; return isString;
} }
@@ -171,8 +166,7 @@ public final class TypeValidationHelper {
return value != null && ConfigHelper.asDouble(value) >= 0.0; return value != null && ConfigHelper.asDouble(value) >= 0.0;
} catch (NumberFormatException | NullPointerException exception) { } catch (NumberFormatException | NullPointerException exception) {
if (sender != null) { if (sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INPUT_POSITIVE_DOUBLE_REQUIRED).error(sender);
BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
} }
return false; return false;
} }
@@ -190,8 +184,7 @@ public final class TypeValidationHelper {
return value != null && ConfigHelper.asInt(value) >= 0; return value != null && ConfigHelper.asInt(value) >= 0;
} catch (NumberFormatException | NullPointerException exception) { } catch (NumberFormatException | NullPointerException exception) {
if (sender != null) { if (sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, new FormatBuilder(Translatable.INPUT_POSITIVE_INTEGER_REQUIRED).error(sender);
BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
} }
return false; return false;
} }