Updates some KnarLib code
Some checks failed
EpicKnarvik97/Blacksmith/pipeline/head There was a failure building this commit

This commit is contained in:
Kristian Knarvik 2024-01-25 16:57:10 +01:00
parent c532160fb5
commit 455db78988
10 changed files with 56 additions and 53 deletions

View File

@ -87,7 +87,7 @@
<dependency> <dependency>
<groupId>net.knarcraft</groupId> <groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId> <artifactId>knarlib</artifactId>
<version>1.2.4</version> <version>1.2.5</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -19,7 +19,6 @@ 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.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
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.property.ColorConversion; import net.knarcraft.knarlib.property.ColorConversion;
@ -79,16 +78,6 @@ public class BlacksmithPlugin extends JavaPlugin {
return instance; return instance;
} }
/**
* Gets the translation of the given translatable message
*
* @param translatableMessage <p>The message to translate</p>
* @return <p>The message, in the language specified in the configuration</p>
*/
public static @NotNull String translate(TranslatableMessage translatableMessage) {
return BlacksmithPlugin.getTranslator().getTranslatedMessage(translatableMessage);
}
/** /**
* Gets settings for the blacksmith plugin's blacksmiths * Gets settings for the blacksmith plugin's blacksmiths
* *
@ -118,15 +107,6 @@ public class BlacksmithPlugin extends JavaPlugin {
this.getConfig().getString("language", "en")); this.getConfig().getString("language", "en"));
} }
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static @NotNull Translator getTranslator() {
return BlacksmithPlugin.translator;
}
/** /**
* Gets the string formatter to use for formatting * Gets the string formatter to use for formatting
* *
@ -136,6 +116,15 @@ public class BlacksmithPlugin extends JavaPlugin {
return BlacksmithPlugin.stringFormatter; return BlacksmithPlugin.stringFormatter;
} }
/**
* Gets the translator to use for translation
*
* @return <p>The translator to use</p>
*/
public static @NotNull Translator getTranslator() {
return BlacksmithPlugin.translator;
}
@Override @Override
public void onDisable() { public void onDisable() {
getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled."); getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled.");

View File

@ -170,7 +170,8 @@ public abstract class EditCommand<K extends CustomTrait<L>, L extends Setting> i
getCurrentValueMessage(setting.getCommandName(), rawValue)); getCurrentValueMessage(setting.getCommandName(), rawValue));
} else { } else {
//Add a marker if the value has been customized //Add a marker if the value has been customized
String marker = BlacksmithPlugin.translate(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); String marker = BlacksmithPlugin.getStringFormatter().getUnformattedMessage(
BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER);
formatter.displayNeutralMessage(sender, formatter.displayNeutralMessage(sender,
getCurrentValueMessage(setting.getCommandName(), rawValue) + marker); getCurrentValueMessage(setting.getCommandName(), rawValue) + marker);
} }

View File

@ -4,7 +4,6 @@ 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.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
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;
@ -56,8 +55,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, StringFormatter.replacePlaceholder( BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithPlugin.translate(BlacksmithTranslatableMessage.PRESET_MATERIALS), BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.PRESET_MATERIALS,
"{materials}", String.join(", ", materialNames))); "{materials}", String.join(", ", materialNames)));
return true; return true;
} }

View File

@ -5,6 +5,8 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
/** /**
* An enum representing all blacksmith-related settings * An enum representing all blacksmith-related settings
*/ */
@ -88,8 +90,8 @@ public enum BlacksmithSetting implements Setting {
/** /**
* The setting for the enchantments a blacksmith cannot apply to items * The setting for the enchantments a blacksmith cannot apply to items
*/ */
ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse", ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, List.of("binding_curse",
"mending", "vanishing_curse"}, "The enchantments a " + "mending", "vanishing_curse"), "The enchantments a " +
"blacksmith is denied from applying to an item. Disable anything you find too op or annoying.", "blacksmith is denied from applying to an item. Disable anything you find too op or annoying.",
true, false), true, false),

View File

@ -6,6 +6,8 @@ import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper; import net.knarcraft.knarlib.util.ColorHelper;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.List;
/** /**
* A formatter for formatting displayed messages * A formatter for formatting displayed messages
*/ */
@ -24,9 +26,8 @@ public final class BlacksmithStringFormatter {
*/ */
public static void sendNPCMessage(NPC npc, Player player, String message) { public static void sendNPCMessage(NPC npc, Player player, String message) {
player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders( player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders(
BlacksmithTranslatableMessage.NPC_TALK_FORMAT, BlacksmithTranslatableMessage.NPC_TALK_FORMAT, List.of("{npc}", "{message}"),
new String[]{"{npc}", "{message}"}, List.of(npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB))));
new String[]{npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB)}));
} }
} }

View File

@ -1,9 +1,11 @@
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.StringFormatter; import net.knarcraft.knarlib.formatting.StringReplacer;
import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableMessage;
import java.util.List;
/** /**
* An enum containing all translatable global messages * An enum containing all translatable global messages
* *
@ -144,8 +146,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The message to display</p> * @return <p>The message to display</p>
*/ */
public static String getRawValueMessage(String rawValue) { public static String getRawValueMessage(String rawValue) {
return StringFormatter.replacePlaceholder(BlacksmithPlugin.translate( return BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.RAW_VALUE,
BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); "{rawValue}", rawValue);
} }
/** /**
@ -156,9 +158,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getValueChangedMessage(String setting, String newValue) { public static String getValueChangedMessage(String setting, String newValue) {
return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.VALUE_CHANGED,
BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"}, List.of("{setting}", "{newValue}"), List.of(setting, newValue));
new String[]{setting, newValue});
} }
/** /**
@ -171,10 +172,13 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) {
return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnformattedMessage(
BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED));
new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, stringReplacer.add("{setting}", setting);
new String[]{setting, itemType.getItemTypeName(), item, newValue}); stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{newValue}", newValue);
return stringReplacer.replace();
} }
/** /**
@ -185,9 +189,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getCurrentValueMessage(String setting, String currentValue) { public static String getCurrentValueMessage(String setting, String currentValue) {
return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.CURRENT_VALUE,
BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"}, List.of("{setting}", "{currentValue}"),
new String[]{setting, currentValue}); List.of(setting, currentValue));
} }
/** /**
@ -200,10 +204,13 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage {
* @return <p>The string to display to a user</p> * @return <p>The string to display to a user</p>
*/ */
public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) {
return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnformattedMessage(
BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM));
new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, stringReplacer.add("{setting}", setting);
new String[]{setting, itemType.getItemTypeName(), item, currentValue}); stringReplacer.add("{itemType}", itemType.getItemTypeName());
stringReplacer.add("{item}", item);
stringReplacer.add("{currentValue}", currentValue);
return stringReplacer.replace();
} }
@Override @Override

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.StringFormatter;
/** /**
* An enum representing all item types used in messages * An enum representing all item types used in messages
@ -16,9 +17,11 @@ 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 -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); case MATERIAL -> stringFormatter.getUnformattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
case ENCHANTMENT -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); case ENCHANTMENT ->
stringFormatter.getUnformattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
}; };
} }

View File

@ -53,7 +53,8 @@ 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.translate(BlacksmithTranslatableMessage.valueOf(interval.name())); String text = BlacksmithPlugin.getStringFormatter().getUnformattedMessage(
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

@ -25,8 +25,8 @@ public final class ConfigHelper {
if (value == null) { if (value == null) {
return new ArrayList<>(); return new ArrayList<>();
} }
if (value instanceof String) { if (value instanceof String string) {
return List.of(((String) value).split(",")); return List.of((string).split(","));
} else if (value instanceof List<?> list) { } else if (value instanceof List<?> list) {
List<String> strings = new ArrayList<>(); List<String> strings = new ArrayList<>();
for (Object object : list) { for (Object object : list) {