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>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.2.4</version>
<version>1.2.5</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -19,7 +19,6 @@ import net.knarcraft.blacksmith.listener.PlayerListener;
import net.knarcraft.blacksmith.manager.EconomyManager;
import net.knarcraft.blacksmith.trait.BlacksmithTrait;
import net.knarcraft.knarlib.formatting.StringFormatter;
import net.knarcraft.knarlib.formatting.TranslatableMessage;
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
import net.knarcraft.knarlib.formatting.Translator;
import net.knarcraft.knarlib.property.ColorConversion;
@ -79,16 +78,6 @@ public class BlacksmithPlugin extends JavaPlugin {
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
*
@ -118,15 +107,6 @@ public class BlacksmithPlugin extends JavaPlugin {
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
*
@ -136,6 +116,15 @@ public class BlacksmithPlugin extends JavaPlugin {
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
public void onDisable() {
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));
} else {
//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,
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.SmithPresetFilter;
import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage;
import net.knarcraft.knarlib.formatting.StringFormatter;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -56,9 +55,9 @@ public class PresetCommand implements CommandExecutor {
for (Material material : includedMaterials) {
materialNames.add(material.name());
}
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, StringFormatter.replacePlaceholder(
BlacksmithPlugin.translate(BlacksmithTranslatableMessage.PRESET_MATERIALS),
"{materials}", String.join(", ", materialNames)));
BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender,
BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.PRESET_MATERIALS,
"{materials}", String.join(", ", materialNames)));
return true;
}

View File

@ -5,6 +5,8 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
/**
* 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
*/
ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse",
"mending", "vanishing_curse"}, "The enchantments a " +
ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, List.of("binding_curse",
"mending", "vanishing_curse"), "The enchantments a " +
"blacksmith is denied from applying to an item. Disable anything you find too op or annoying.",
true, false),

View File

@ -6,6 +6,8 @@ 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
*/
@ -24,9 +26,8 @@ public final class BlacksmithStringFormatter {
*/
public static void sendNPCMessage(NPC npc, Player player, String message) {
player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders(
BlacksmithTranslatableMessage.NPC_TALK_FORMAT,
new String[]{"{npc}", "{message}"},
new String[]{npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB)}));
BlacksmithTranslatableMessage.NPC_TALK_FORMAT, List.of("{npc}", "{message}"),
List.of(npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB))));
}
}

View File

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

View File

@ -1,6 +1,7 @@
package net.knarcraft.blacksmith.formatting;
import net.knarcraft.blacksmith.BlacksmithPlugin;
import net.knarcraft.knarlib.formatting.StringFormatter;
/**
* 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>
*/
public String getItemTypeName() {
StringFormatter stringFormatter = BlacksmithPlugin.getStringFormatter();
return switch (this) {
case MATERIAL -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
case ENCHANTMENT -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT);
case MATERIAL -> stringFormatter.getUnformattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL);
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>
*/
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
if (text.contains(",")) {

View File

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