Merge branch 'refs/heads/dev' into book-splitting

# Conflicts:
#	src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java
#	src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java
#	src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java
#	src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java
#	src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java
This commit is contained in:
2025-09-06 15:09:34 +02:00
23 changed files with 187 additions and 653 deletions

View File

@@ -13,20 +13,21 @@ import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigCommand;
import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter;
import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand;
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.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.PlayerListener;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.manager.PlayerUsageManager;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
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.TranslatableTimeUnit;
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 org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
@@ -37,27 +38,21 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
/**
* 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 GlobalBlacksmithSettings blacksmithConfig;
private GlobalScrapperSettings scrapperConfig;
private static Translator translator;
private static StringFormatter stringFormatter;
private FileConfiguration configuration;
/**
* Constructor required for MockBukkit
@@ -110,29 +105,10 @@ public class BlacksmithPlugin extends JavaPlugin {
this.reloadConfig();
blacksmithConfig.load();
scrapperConfig.load();
translator.loadLanguages(this.getDataFolder(), "en", this.getConfiguration().getString(
translator.loadLanguages(this.getDataFolder(), "en", this.getConfig().getString(
"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
*
@@ -150,20 +126,13 @@ public class BlacksmithPlugin extends JavaPlugin {
@Override
public void onEnable() {
instance = this;
//Copy default config to disk, and add missing configuration values
this.saveDefaultConfig();
this.getConfig().options().copyDefaults(true);
this.reloadConfig();
this.saveConfig();
ConfigHelper.saveDefaults(this);
//Migrate from an earlier configuration file syntax if necessary
if (getConfiguration().getString("scrapper.defaults.dropItem") == null) {
ConfigHelper.migrateConfig(this.getDataFolder().getPath().replaceAll("\\\\", "/"),
getConfiguration());
this.reloadConfig();
if (getConfig().getString("scrapper.defaults.dropItem") == null) {
net.knarcraft.knarlib.util.ConfigHelper.migrateConfig(this);
}
initializeConfigurations(getConfiguration());
initializeConfigurations(getConfig());
//Set up Vault integration
if (!setUpVault()) {
@@ -191,28 +160,6 @@ public class BlacksmithPlugin extends JavaPlugin {
System.currentTimeMillis() - 3600000), 36000, 36000);
}
@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
*
@@ -273,7 +220,7 @@ public class BlacksmithPlugin extends JavaPlugin {
//Prepare the translator
translator = new Translator();
translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND);
translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
translator.registerMessageCategory(Translatable.ITEM_TYPE_ENCHANTMENT);
translator.loadLanguages(this.getDataFolder(), "en",
fileConfiguration.getString("language", "en"));
PluginDescriptionFile description = this.getDescription();
@@ -283,7 +230,7 @@ public class BlacksmithPlugin extends JavaPlugin {
} else {
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();
@@ -325,22 +272,4 @@ public class BlacksmithPlugin extends JavaPlugin {
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.SettingValueType;
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.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
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 org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@@ -21,9 +21,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage;
import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage;
import static net.knarcraft.blacksmith.formatting.Translatable.getCurrentValueMessage;
import static net.knarcraft.blacksmith.formatting.Translatable.getDefaultValueMessage;
import static net.knarcraft.blacksmith.formatting.Translatable.getValueChangedMessage;
/**
* 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) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
if (npc == null || !npc.hasTrait(traitClass)) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.NO_NPC_SELECTED);
new FormatBuilder(Translatable.NO_NPC_SELECTED).error(sender);
return true;
}
@@ -130,8 +129,7 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
return false;
}
settings.changeValue(setting, newValue);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue)));
getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue)).success(sender);
//Save the changes immediately to prevent data loss on server crash
CitizensAPI.getNPCRegistry().saveToStore();
}
@@ -156,21 +154,19 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
return;
}
StringFormatter formatter = BlacksmithPlugin.getStringFormatter();
//Find the value of the specified setting
String commandName = setting.getCommandName();
String currentValue = String.valueOf(settings.getRawValue(setting));
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 isSet = !InputParsingHelper.isEmpty(currentValue);
// Display the description for how this setting is used
formatter.displaySuccessMessage(sender, setting.getDescription());
new FormatBuilder(setting.getDescription()).success(sender);
// Display the default value
formatter.displayNeutralMessage(sender, getDefaultValueMessage(commandName, defaultValue));
getDefaultValueMessage(commandName, defaultValue).neutral(sender);
if (isMessage) {
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
formatter.displayNeutralMessage(sender, getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : ""));
new FormatBuilder(getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")).neutral(sender);
if (isMessage) {
ConfigCommandHelper.displayRaw(sender, currentValue);

View File

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

View File

@@ -1,7 +1,8 @@
package net.knarcraft.blacksmith.command;
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.CommandSender;
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,
@NotNull String[] args) {
BlacksmithPlugin.getInstance().reload();
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, BlacksmithTranslatableMessage.PLUGIN_RELOADED);
new FormatBuilder(Translatable.PLUGIN_RELOADED).success(sender);
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.blacksmith.BlacksmithSetting;
import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.blacksmith.formatting.ItemType;
import net.knarcraft.blacksmith.formatting.Translatable;
import net.knarcraft.blacksmith.util.ConfigCommandHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
@@ -93,9 +93,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
} else {
currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material));
}
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.MATERIAL, material.name(), currentValue));
Translatable.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.MATERIAL, material.name(), currentValue).success(sender);
return true;
} else if (setting == BlacksmithSetting.ENCHANTMENT_COST) {
Enchantment enchantment = InputParsingHelper.matchEnchantment(selector);
@@ -106,10 +105,9 @@ public class BlackSmithConfigCommand implements CommandExecutor {
if (enchantmentKey == null) {
return false;
}
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.ENCHANTMENT, enchantmentKey.getKey(),
String.valueOf(settings.getEnchantmentCost(enchantment))));
Translatable.getItemCurrentValueMessage(setting.getCommandName(),
ItemType.ENCHANTMENT, enchantmentKey.getKey(),
String.valueOf(settings.getEnchantmentCost(enchantment))).success(sender);
return true;
} else {
return false;
@@ -161,9 +159,7 @@ public class BlackSmithConfigCommand implements CommandExecutor {
ItemType itemType = ItemType.ENCHANTMENT;
String itemChanged = enchantment.getKey().getKey();
settings.setEnchantmentCost(enchantment, newPrice);
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, newValue));
Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, newValue).success(sender);
return true;
} else {
return false;
@@ -203,9 +199,8 @@ public class BlackSmithConfigCommand implements CommandExecutor {
}
}
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, String.valueOf(newPrice)));
Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(),
itemType, itemChanged, String.valueOf(newPrice)).success(sender);
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.config.SettingValueType;
import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
import net.knarcraft.blacksmith.util.ConfigHelper;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
import net.knarcraft.knarlib.config.StargateYamlConfiguration;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
@@ -310,7 +310,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
*/
private void loadSettings() {
instance.reloadConfig();
FileConfiguration configuration = instance.getConfiguration();
FileConfiguration configuration = instance.getConfig();
for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) {
if (!configuration.contains(blacksmithSetting.getPath())) {
@@ -501,7 +501,7 @@ public class GlobalBlacksmithSettings implements Settings<BlacksmithSetting> {
* Saves all current settings to the config file
*/
private void save() {
FileConfiguration fileConfiguration = instance.getConfiguration();
FileConfiguration fileConfiguration = instance.getConfig();
//Save all default settings
for (BlacksmithSetting setting : BlacksmithSetting.values()) {

View File

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

View File

@@ -1,34 +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(ColorHelper.translateColorCodes(npc.getRawName(), ColorConversion.RGB),
ColorHelper.translateColorCodes(message, ColorConversion.RGB))));
}
}

View File

@@ -1,7 +1,6 @@
package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.FormatBuilder;
/**
* 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>
*/
public String getItemTypeName() {
StringFormatter stringFormatter = BlacksmithPlugin.getStringFormatter();
return switch (this) {
case MATERIAL -> stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
case ENCHANTMENT ->
stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
case MATERIAL -> new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).toString();
case ENCHANTMENT -> new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).toString();
};
}

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

View File

@@ -1,19 +1,16 @@
package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.formatting.StringReplacer;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* 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
* 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
@@ -154,12 +151,11 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* Gets the message to display when displaying the raw value of messages
*
* @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
public static String getRawValueMessage(@NotNull String rawValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.RAW_VALUE,
"{rawValue}", rawValue);
public static FormatBuilder getRawValueMessage(@NotNull String rawValue) {
return new FormatBuilder(Translatable.RAW_VALUE).replace("{rawValue}", rawValue);
}
/**
@@ -167,12 +163,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
*
* @param setting <p>The setting whose value has been changed</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
public static String getValueChangedMessage(@NotNull String setting, @NotNull String newValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.VALUE_CHANGED,
List.of("{setting}", "{newValue}"), List.of(setting, newValue));
public static FormatBuilder getValueChangedMessage(@NotNull String setting, @NotNull String newValue) {
return new FormatBuilder(Translatable.VALUE_CHANGED).replace("{setting}", setting).
replace("{newValue}", newValue);
}
/**
@@ -182,18 +178,14 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @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 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
public static String getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String newValue) {
StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnFormattedMessage(
BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED));
stringReplacer.add("{setting}", setting);
stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{newValue}", newValue);
return stringReplacer.replace();
public static FormatBuilder getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String newValue) {
return new FormatBuilder(Translatable.VALUE_FOR_ITEM_CHANGED).replace("{setting}", setting).
replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item).
replace("{newValue}", newValue);
}
/**
@@ -201,13 +193,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
*
* @param setting <p>The setting whose value is shown</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
public static String getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.DEFAULT_VALUE,
List.of("{setting}", "{defaultValue}"),
List.of(setting, defaultValue));
public static FormatBuilder getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) {
return new FormatBuilder(Translatable.DEFAULT_VALUE).replace("{setting}", setting).
replace("{defaultValue}", defaultValue);
}
/**
@@ -215,13 +206,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
*
* @param setting <p>The setting whose value is shown</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
public static String getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) {
return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.CURRENT_VALUE,
List.of("{setting}", "{currentValue}"),
List.of(setting, currentValue));
public static FormatBuilder getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) {
return new FormatBuilder(Translatable.CURRENT_VALUE).replace("{setting}", setting).
replace("{currentValue}", currentValue);
}
/**
@@ -231,24 +221,20 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @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 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
public static String getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String currentValue) {
StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnFormattedMessage(
BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM));
stringReplacer.add("{setting}", setting);
stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{currentValue}", currentValue);
return stringReplacer.replace();
public static FormatBuilder getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType,
@NotNull String item, @NotNull String currentValue) {
return new FormatBuilder(Translatable.CURRENT_VALUE_FOR_ITEM).replace("{setting}", setting).
replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item).
replace("{currentValue}", currentValue);
}
@Override
@NotNull
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.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.CustomTrait;
import net.knarcraft.blacksmith.trait.ScrapperTrait;
import net.knarcraft.knarlib.formatting.FormatBuilder;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@@ -44,8 +45,7 @@ public class NPCClickListener implements Listener {
//Permission check
if (!player.hasPermission("blacksmith.use")) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(player,
BlacksmithTranslatableMessage.PERMISSION_DENIED);
new FormatBuilder(Translatable.PERMISSION_DENIED).error(player);
return;
}

View File

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

View File

@@ -6,9 +6,10 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.Setting;
import net.knarcraft.blacksmith.config.Settings;
import net.knarcraft.blacksmith.config.TraitSettings;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.formatting.TimeFormatter;
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.LivingEntity;
import org.bukkit.entity.Player;
@@ -23,8 +24,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/**
* 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) {
//Another player is using the blacksmith
if (session.isNotInSession(player)) {
sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage());
NPCFormatter.sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage());
return;
}
@@ -85,8 +84,8 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
if (session.isRunning()) {
int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000);
boolean showExactTime = showExactTime();
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getBusyWorkingMessage(),
"{time}", TimeFormatter.formatTime(showExactTime, timeRemaining)));
NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getBusyWorkingMessage()).
replace("{time}", TimeFormatter.formatTime(showExactTime, timeRemaining)));
return;
}
if (session.isSessionInvalid()) {
@@ -147,8 +146,8 @@ public abstract class CustomTrait<K extends Setting> extends Trait {
if (!calendar.after(coolDowns.get(playerId))) {
int secondDifference = (int) (coolDowns.get(playerId).getTimeInMillis() - calendar.getTimeInMillis()) / 1000;
boolean exactTime = showExactTime();
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getCoolDownUnexpiredMessage(),
"{time}", TimeFormatter.formatTime(exactTime, secondDifference)));
NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getCoolDownUnexpiredMessage()).
replace("{time}", TimeFormatter.formatTime(exactTime, secondDifference)));
return false;
}
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>
*/
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;

View File

@@ -5,6 +5,7 @@ import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings;
import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent;
import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.util.InputParsingHelper;
import net.knarcraft.blacksmith.util.ItemHelper;
@@ -24,8 +25,6 @@ import java.util.Calendar;
import java.util.List;
import java.util.Random;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/**
* 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
ItemStack itemInHand = this.player.getInventory().getItemInMainHand();
if (!itemToReforge.equals(itemInHand)) {
sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
return true;
}
// The player is unable to pay
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 false;
@@ -103,7 +102,7 @@ public class ReforgeSession extends Session implements Runnable {
@Override
public void run() {
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);
//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.event.ScrapperSalvageFailEvent;
import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.manager.PlayerUsageManager;
import net.knarcraft.blacksmith.property.SalvageMethod;
@@ -26,8 +27,6 @@ import java.util.Calendar;
import java.util.List;
import java.util.Random;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/**
* A representation of the session between a player and a scrapper
*/
@@ -70,14 +69,14 @@ public class SalvageSession extends Session implements Runnable {
// Prevent player from switching items during session
ItemStack itemInHand = this.player.getInventory().getItemInMainHand();
if (!itemToSalvage.equals(itemInHand)) {
sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage());
return true;
}
// TODO: Check item cost. If
if (EconomyManager.cannotPayForSalvage(this.player, this.salvageMethod, this.itemToSalvage)) {
sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage());
NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage());
return true;
}
return false;
@@ -159,7 +158,7 @@ public class SalvageSession extends Session implements Runnable {
playSound(Sound.BLOCK_ANVIL_USE);
giveSalvage();
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());
}
}
@@ -174,14 +173,14 @@ public class SalvageSession extends Session implements Runnable {
//Damage the item if possible
damageItemRandomly(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 {
// Give half the salvage
List<ItemStack> halfSalvage = SalvageHelper.pickRandomSalvage(this.salvage, new ArrayList<>(), 0.5);
for (ItemStack item : halfSalvage) {
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

@@ -9,12 +9,13 @@ import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings;
import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting;
import net.knarcraft.blacksmith.container.RecipeResult;
import net.knarcraft.blacksmith.container.SalvageResult;
import net.knarcraft.blacksmith.formatting.NPCFormatter;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.property.SalvageMethod;
import net.knarcraft.blacksmith.property.SalvageState;
import net.knarcraft.blacksmith.util.ItemHelper;
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 org.bukkit.Bukkit;
import org.bukkit.Material;
@@ -30,8 +31,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage;
/**
* The class representing a scrapper NPC trait
*/
@@ -88,7 +87,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
public void startSession(@NotNull Player player) {
ItemStack itemInHand = player.getInventory().getItemInMainHand().clone();
if (itemInHand.getType().isAir()) {
sendNPCMessage(this.npc, player, config.getNoItemMessage());
NPCFormatter.sendNPCMessage(this.npc, player, config.getNoItemMessage());
return;
}
@@ -97,8 +96,8 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
// Check if the item can be salvaged
if (!canBeSalvaged(itemInHand, salvageAbleItems, extended)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(),
"{title}", getSettings().getScrapperTitle()));
NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()).
replace("{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.debug("Cannot salvage provided item: " + itemInHand);
return;
}
@@ -177,15 +176,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
if (!SalvageHelper.isSalvageable(player.getServer(), itemInHand)) {
sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(),
"{title}", getSettings().getScrapperTitle()));
NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()).
replace("{title}", getSettings().getScrapperTitle()));
BlacksmithPlugin.debug("Provided with non-salvage-able item " + itemInHand);
return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0);
}
// Check if the item is enchanted, and whether this scrapper can salvage it
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);
}
@@ -194,7 +193,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE;
boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty();
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);
} else {
return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE,
@@ -247,7 +246,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
}
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);
}
@@ -273,13 +272,13 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
}
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);
}
List<ItemStack> salvage = SalvageHelper.getArmorTrimSalvage(itemInHand, armorMeta);
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);
}
@@ -300,7 +299,7 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
replacer.add("{cost}", cost);
replacer.add("{item}", itemInHand.getType().name().toLowerCase().replace('_', ' '));
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) {
String expectedYield;
if (ItemHelper.getDamage(itemInHand) <= 0) {
@@ -309,13 +308,13 @@ public class ScrapperTrait extends CustomTrait<ScrapperSetting> {
expectedYield = getSettings().getPartialSalvageMessage();
}
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) {
sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage()));
NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage()));
} else if (salvageMethod == SalvageMethod.ENCHANTED_BOOK) {
StringReplacer replacer2 = new StringReplacer();
replacer2.add("{cost}", getEnchantedBookCost());
sendNPCMessage(this.npc, player, replacer2.replace(getSettings().getEnchantedBookCostMessage()));
NPCFormatter.sendNPCMessage(this.npc, player, replacer2.replace(getSettings().getEnchantedBookCostMessage()));
} else {
BlacksmithPlugin.error("Unrecognized salvage method " + salvageMethod);
}

View File

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

View File

@@ -1,22 +1,10 @@
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.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 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;
import net.knarcraft.blacksmith.BlacksmithPlugin;
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.command.CommandSender;
import org.bukkit.enchantments.Enchantment;
@@ -60,8 +60,7 @@ public final class TypeValidationHelper {
boolean isMaterial = value instanceof Material || (value instanceof String string &&
InputParsingHelper.matchMaterial(string) != null);
if (!isMaterial && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).error(sender);
}
return isMaterial;
}
@@ -105,8 +104,7 @@ public final class TypeValidationHelper {
boolean isEnchantment = value instanceof Enchantment || (value instanceof String string &&
InputParsingHelper.matchEnchantment(string) != null);
if (!isEnchantment && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).error(sender);
}
return isEnchantment;
}
@@ -121,8 +119,7 @@ public final class TypeValidationHelper {
private static boolean isStringList(@Nullable Object value, @Nullable CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED);
new FormatBuilder(Translatable.INPUT_STRING_LIST_REQUIRED).error(sender);
}
return isStringList;
}
@@ -138,8 +135,7 @@ public final class TypeValidationHelper {
boolean isPercentage = value != null && isPositiveInteger(value, null) &&
ConfigHelper.asInt(value) >= 0 && ConfigHelper.asInt(value) <= 100;
if (!isPercentage && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED);
new FormatBuilder(Translatable.INPUT_PERCENTAGE_REQUIRED).error(sender);
}
return isPercentage;
}
@@ -154,8 +150,7 @@ public final class TypeValidationHelper {
private static boolean isNonEmptyString(@Nullable Object value, @Nullable CommandSender sender) {
boolean isString = value instanceof String string && !string.isBlank();
if (!isString && sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED);
new FormatBuilder(Translatable.INPUT_STRING_REQUIRED).error(sender);
}
return isString;
}
@@ -172,8 +167,7 @@ public final class TypeValidationHelper {
return value != null && ConfigHelper.asDouble(value) >= 0.0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED);
new FormatBuilder(Translatable.INPUT_POSITIVE_DOUBLE_REQUIRED).error(sender);
}
return false;
}
@@ -191,8 +185,7 @@ public final class TypeValidationHelper {
return value != null && ConfigHelper.asInt(value) >= 0;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender,
BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED);
new FormatBuilder(Translatable.INPUT_POSITIVE_INTEGER_REQUIRED).error(sender);
}
return false;
}