diff --git a/pom.xml b/pom.xml index a8f9733..337c79b 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ Add costs for creating plugin signs - 17 + 16 UTF-8 https://git.knarcraft.net/EpicKnarvik97/PaidSigns @@ -41,6 +41,20 @@ false + + + net.knarcraft:knarlib + + net/knarcraft/knarlib/** + + + + + *.MF + *.yml + + + @@ -88,5 +102,11 @@ 23.0.0 provided + + net.knarcraft + knarlib + 1.0-SNAPSHOT + compile + diff --git a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java index 283dcf6..b7a8b7b 100644 --- a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java +++ b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java @@ -1,5 +1,7 @@ package net.knarcraft.paidsigns; +import net.knarcraft.knarlib.KnarLib; +import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.paidsigns.command.AddCommand; import net.knarcraft.paidsigns.command.AddConditionCommand; import net.knarcraft.paidsigns.command.AddConditionTabCompleter; @@ -13,7 +15,7 @@ import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.RemoveConditionCommand; import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter; import net.knarcraft.paidsigns.command.RemoveTabCommand; -import net.knarcraft.paidsigns.formatting.Translator; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.listener.SignBreakListener; import net.knarcraft.paidsigns.listener.SignListener; import net.knarcraft.paidsigns.manager.EconomyManager; @@ -63,9 +65,15 @@ public final class PaidSigns extends JavaPlugin { @Override public void onEnable() { + KnarLib.setPlugin(this); + setupVault(); loadConfig(); + + //Initialize translator + Translator.registerMessageCategory(PaidSignsTranslatableMessage.BOOLEAN_TRUE); Translator.loadLanguages(language); + signManager = new PaidSignManager(PaidSignManager.loadSigns()); TrackedSignManager.loadTrackedSigns(); diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java index 5a89fbf..4fc3f1a 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddCommand.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.property.OptionState; import org.bukkit.command.Command; @@ -30,7 +30,7 @@ public class AddCommand extends TokenizedCommand { try { cost = Double.parseDouble(arguments.get(1)); } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } String permission = ""; @@ -71,18 +71,18 @@ public class AddCommand extends TokenizedCommand { try { PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor, matchAnyCondition); if (manager.getPaidSign(signName) != null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_NAME_DUPLICATE)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_NAME_DUPLICATE); return false; } manager.addPaidSign(sign); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_ADDED_PAID_SIGN)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_ADDED_PAID_SIGN); return true; } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); return false; } catch (IllegalArgumentException e) { - sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_INVALID_INPUT), "{input}", e.getMessage())); + StringFormatter.displayErrorMessage(sender, StringFormatter.replacePlaceholder( + PaidSignsTranslatableMessage.ERROR_INVALID_INPUT, "{input}", e.getMessage())); } return false; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java index 3a52319..56033e1 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionCommand.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.property.OptionState; import org.bukkit.command.Command; @@ -30,11 +30,11 @@ public class AddConditionCommand extends TokenizedCommand { try { lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1); if (lineNumber < 0 || lineNumber > 3) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } String stringToMatch = arguments.get(2); @@ -73,18 +73,17 @@ public class AddConditionCommand extends TokenizedCommand { PaidSignManager signManager = PaidSigns.getInstance().getSignManager(); PaidSign sign = signManager.getPaidSign(name); if (sign == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND); return false; } sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor); try { signManager.saveSigns(); } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); return false; } - sender.sendMessage(StringFormatter.getTranslatedInfoMessage( - TranslatableMessage.SUCCESS_ADDED_PAID_SIGN_CONDITION)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_ADDED_PAID_SIGN_CONDITION); return true; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java index be44805..12caed0 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddConditionTabCompleter.java @@ -1,6 +1,7 @@ package net.knarcraft.paidsigns.command; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -28,17 +29,17 @@ public class AddConditionTabCompleter extends TokenizedTabCompleter { initializeValues(); } if (argumentSize == 1) { - return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + return TabCompletionHelper.filterMatchingStartsWith(PaidSignsTabCompleteHelper.getPaidSignNames(), arguments.get(0)); } else if (argumentSize == 2) { - return TabCompleteHelper.filterMatchingStartsWith(this.lineIndices, arguments.get(1)); + return TabCompletionHelper.filterMatchingStartsWith(this.lineIndices, arguments.get(1)); } else if (argumentSize == 3) { - return TabCompleteHelper.filterMatchingStartsWith(stringsToMatch, arguments.get(2)); + return TabCompletionHelper.filterMatchingStartsWith(stringsToMatch, arguments.get(2)); } else if (argumentSize == 4) { - return TabCompleteHelper.filterMatchingStartsWith(booleans, arguments.get(3)); + return TabCompletionHelper.filterMatchingStartsWith(booleans, arguments.get(3)); } else if (argumentSize == 5) { - return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(4)); + return TabCompletionHelper.filterMatchingStartsWith(optionStates, arguments.get(4)); } else if (argumentSize == 6) { - return TabCompleteHelper.filterMatchingStartsWith(optionStates, arguments.get(5)); + return TabCompletionHelper.filterMatchingStartsWith(optionStates, arguments.get(5)); } return new ArrayList<>(); } @@ -47,10 +48,10 @@ public class AddConditionTabCompleter extends TokenizedTabCompleter { * Initializes the values available for tab completion */ private void initializeValues() { - lineIndices = TabCompleteHelper.getSignLines(); - stringsToMatch = TabCompleteHelper.getExampleConditionStrings(); - booleans = TabCompleteHelper.getBooleans(); - optionStates = TabCompleteHelper.getOptionStates(); + lineIndices = PaidSignsTabCompleteHelper.getSignLines(); + stringsToMatch = PaidSignsTabCompleteHelper.getExampleConditionStrings(); + booleans = PaidSignsTabCompleteHelper.getBooleans(); + optionStates = PaidSignsTabCompleteHelper.getOptionStates(); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java index aa82ee3..b0a61b8 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/AddTabCompleter.java @@ -1,6 +1,7 @@ package net.knarcraft.paidsigns.command; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.knarlib.util.TabCompletionHelper; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -29,17 +30,17 @@ public class AddTabCompleter extends TokenizedTabCompleter { } if (argumentSize == 1) { - return TabCompleteHelper.filterMatchingStartsWith(names, arguments.get(0)); + return TabCompletionHelper.filterMatchingStartsWith(names, arguments.get(0)); } else if (argumentSize == 2) { - return TabCompleteHelper.filterMatchingStartsWith(costs, arguments.get(1)); + return TabCompletionHelper.filterMatchingStartsWith(costs, arguments.get(1)); } else if (argumentSize == 3) { - return TabCompleteHelper.tabCompletePermission(arguments.get(arguments.size() - 1)); + return PaidSignsTabCompleteHelper.tabCompletePermission(arguments.get(arguments.size() - 1)); } else if (argumentSize == 4) { - return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(3)); + return TabCompletionHelper.filterMatchingStartsWith(options, arguments.get(3)); } else if (argumentSize == 5) { - return TabCompleteHelper.filterMatchingStartsWith(options, arguments.get(4)); + return TabCompletionHelper.filterMatchingStartsWith(options, arguments.get(4)); } else if (argumentSize == 6) { - return TabCompleteHelper.filterMatchingStartsWith(booleans, arguments.get(5)); + return TabCompletionHelper.filterMatchingStartsWith(booleans, arguments.get(5)); } return new ArrayList<>(); } @@ -48,10 +49,10 @@ public class AddTabCompleter extends TokenizedTabCompleter { * Initializes the values available for tab completion */ private static void initializeValues() { - names = TabCompleteHelper.getExamplePaidSignNames(); - costs = TabCompleteHelper.getCosts(); - options = TabCompleteHelper.getOptionStates(); - booleans = TabCompleteHelper.getBooleans(); + names = PaidSignsTabCompleteHelper.getExamplePaidSignNames(); + costs = PaidSignsTabCompleteHelper.getCosts(); + options = PaidSignsTabCompleteHelper.getOptionStates(); + booleans = PaidSignsTabCompleteHelper.getBooleans(); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java b/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java index ca7005e..390a83d 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/EditCommand.java @@ -1,10 +1,10 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSignCondition; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.property.OptionState; import net.knarcraft.paidsigns.property.PaidSignConditionProperty; @@ -31,7 +31,7 @@ public class EditCommand extends TokenizedCommand { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND); return false; } @@ -44,7 +44,7 @@ public class EditCommand extends TokenizedCommand { return parseGivenProperty(sign, sender); } } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); return false; } } @@ -61,7 +61,7 @@ public class EditCommand extends TokenizedCommand { @NotNull CommandSender sender) throws NumberFormatException { short signLine = (short) (Short.parseShort(arguments.get(1)) - 1); if (signLine < 0 || signLine > 3 || sign.getConditions().get(signLine) == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_NO_SUCH_CONDITION)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_NO_SUCH_CONDITION); return false; } if (argumentSize < 4) { @@ -70,8 +70,7 @@ public class EditCommand extends TokenizedCommand { PaidSignConditionProperty conditionProperty = PaidSignConditionProperty.getFromString(arguments.get(2)); if (conditionProperty == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_PROPERTY_NOT_RECOGNIZED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PROPERTY_NOT_RECOGNIZED); return false; } String value = arguments.get(3); @@ -90,8 +89,7 @@ public class EditCommand extends TokenizedCommand { @NotNull CommandSender sender) throws IOException { PaidSignProperty property = PaidSignProperty.getFromString(arguments.get(1)); if (property == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_PROPERTY_NOT_RECOGNIZED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PROPERTY_NOT_RECOGNIZED); return false; } String value = arguments.get(2); @@ -99,7 +97,7 @@ public class EditCommand extends TokenizedCommand { updateProperty(sender, sign, property, value); return true; } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } } @@ -137,7 +135,7 @@ public class EditCommand extends TokenizedCommand { manager.removePaidSign(sign.getName()); manager.addPaidSign(updatedSign); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_UPDATED_PAID_SIGN)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_UPDATED_PAID_SIGN); } /** @@ -173,12 +171,11 @@ public class EditCommand extends TokenizedCommand { try { PaidSigns.getInstance().getSignManager().saveSigns(); } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); return false; } - sender.sendMessage(StringFormatter.getTranslatedInfoMessage( - TranslatableMessage.SUCCESS_UPDATED_PAID_SIGN_CONDITION)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_UPDATED_PAID_SIGN_CONDITION); return true; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/EditTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/EditTabCompleter.java index 2c67f6e..5da677d 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/EditTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/EditTabCompleter.java @@ -1,10 +1,11 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.property.PaidSignConditionProperty; import net.knarcraft.paidsigns.property.PaidSignProperty; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -34,7 +35,7 @@ public class EditTabCompleter extends TokenizedTabCompleter { } if (argumentSize == 1) { - return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + return TabCompletionHelper.filterMatchingStartsWith(PaidSignsTabCompleteHelper.getPaidSignNames(), arguments.get(0)); } else if (argumentSize >= 2) { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign != null) { @@ -54,7 +55,7 @@ public class EditTabCompleter extends TokenizedTabCompleter { if (argumentSize == 2) { List conditions = getAvailableSignConditions(sign); conditions.addAll(getPaidSignProperties()); - return TabCompleteHelper.filterMatchingStartsWith(conditions, arguments.get(1)); + return TabCompletionHelper.filterMatchingStartsWith(conditions, arguments.get(1)); } else if (argumentSize >= 3) { try { return tabCompleteSignLine(sign); @@ -79,11 +80,11 @@ public class EditTabCompleter extends TokenizedTabCompleter { if (signLine < 0 || signLine > 3 || sign.getConditions().get(signLine) == null) { return new ArrayList<>(); } else if (argumentSize == 3) { - return TabCompleteHelper.filterMatchingStartsWith(getPaidSignConditionProperties(), arguments.get(2)); + return TabCompletionHelper.filterMatchingStartsWith(getPaidSignConditionProperties(), arguments.get(2)); } else if (argumentSize == 4) { PaidSignConditionProperty property = PaidSignConditionProperty.getFromString(arguments.get(2)); if (property != null) { - return TabCompleteHelper.filterMatchingStartsWith(conditionPropertyExampleValues.get(property), + return TabCompletionHelper.filterMatchingStartsWith(conditionPropertyExampleValues.get(property), arguments.get(3)); } } @@ -99,9 +100,9 @@ public class EditTabCompleter extends TokenizedTabCompleter { PaidSignProperty paidSignProperty = PaidSignProperty.getFromString(arguments.get(1)); if (paidSignProperty != null) { if (paidSignProperty == PaidSignProperty.PERMISSION) { - return TabCompleteHelper.tabCompletePermission(arguments.get(2)); + return PaidSignsTabCompleteHelper.tabCompletePermission(arguments.get(2)); } else { - return TabCompleteHelper.filterMatchingStartsWith(propertyExampleValues.get(paidSignProperty), + return TabCompletionHelper.filterMatchingStartsWith(propertyExampleValues.get(paidSignProperty), arguments.get(2)); } } else { @@ -154,11 +155,11 @@ public class EditTabCompleter extends TokenizedTabCompleter { */ private void initializePropertyExampleValues() { propertyExampleValues = new HashMap<>(); - propertyExampleValues.put(PaidSignProperty.COST, TabCompleteHelper.getCosts()); - propertyExampleValues.put(PaidSignProperty.NAME, TabCompleteHelper.getPaidSignNames()); - propertyExampleValues.put(PaidSignProperty.IGNORE_CASE, TabCompleteHelper.getOptionStates()); - propertyExampleValues.put(PaidSignProperty.IGNORE_COLOR, TabCompleteHelper.getOptionStates()); - propertyExampleValues.put(PaidSignProperty.MATCH_ANY_CONDITION, TabCompleteHelper.getBooleans()); + propertyExampleValues.put(PaidSignProperty.COST, PaidSignsTabCompleteHelper.getCosts()); + propertyExampleValues.put(PaidSignProperty.NAME, PaidSignsTabCompleteHelper.getPaidSignNames()); + propertyExampleValues.put(PaidSignProperty.IGNORE_CASE, PaidSignsTabCompleteHelper.getOptionStates()); + propertyExampleValues.put(PaidSignProperty.IGNORE_COLOR, PaidSignsTabCompleteHelper.getOptionStates()); + propertyExampleValues.put(PaidSignProperty.MATCH_ANY_CONDITION, PaidSignsTabCompleteHelper.getBooleans()); } /** @@ -167,10 +168,10 @@ public class EditTabCompleter extends TokenizedTabCompleter { private void initializeConditionPropertyExampleValues() { conditionPropertyExampleValues = new HashMap<>(); conditionPropertyExampleValues.put(PaidSignConditionProperty.STRING_TO_MATCH, - TabCompleteHelper.getExampleConditionStrings()); - conditionPropertyExampleValues.put(PaidSignConditionProperty.IGNORE_COLOR, TabCompleteHelper.getOptionStates()); - conditionPropertyExampleValues.put(PaidSignConditionProperty.IGNORE_CASE, TabCompleteHelper.getOptionStates()); - conditionPropertyExampleValues.put(PaidSignConditionProperty.EXECUTE_REG_EX, TabCompleteHelper.getBooleans()); + PaidSignsTabCompleteHelper.getExampleConditionStrings()); + conditionPropertyExampleValues.put(PaidSignConditionProperty.IGNORE_COLOR, PaidSignsTabCompleteHelper.getOptionStates()); + conditionPropertyExampleValues.put(PaidSignConditionProperty.IGNORE_CASE, PaidSignsTabCompleteHelper.getOptionStates()); + conditionPropertyExampleValues.put(PaidSignConditionProperty.EXECUTE_REG_EX, PaidSignsTabCompleteHelper.getBooleans()); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java index 302ebb1..50ef28f 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListCommand.java @@ -1,11 +1,11 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSignCondition; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; -import net.knarcraft.paidsigns.formatting.Translator; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -16,10 +16,9 @@ import java.util.List; import java.util.Map; import java.util.Set; -import static net.knarcraft.paidsigns.formatting.StringFormatter.getTranslatedErrorMessage; -import static net.knarcraft.paidsigns.formatting.StringFormatter.replacePlaceholder; -import static net.knarcraft.paidsigns.formatting.StringFormatter.replacePlaceholders; -import static net.knarcraft.paidsigns.formatting.StringFormatter.translateBoolean; +import static net.knarcraft.knarlib.formatting.StringFormatter.displayErrorMessage; +import static net.knarcraft.knarlib.formatting.StringFormatter.replacePlaceholder; +import static net.knarcraft.knarlib.formatting.StringFormatter.replacePlaceholders; /** * A representation of the command for listing information about paid signs @@ -56,7 +55,7 @@ public class ListCommand extends TokenizedCommand { Set signNames = PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet(); List signNameList = new ArrayList<>(signNames); Collections.sort(signNameList); - String infoFormat = Translator.getTranslatedMessage(TranslatableMessage.PAID_SIGNS_INFO_FORMAT); + String infoFormat = Translator.getTranslatedMessage(PaidSignsTranslatableMessage.PAID_SIGNS_INFO_FORMAT); //Display up to 10 signs per page int signsPerPage = 7; @@ -66,19 +65,19 @@ public class ListCommand extends TokenizedCommand { break; } String signName = signNameList.get(signIndex); - signs.append(StringFormatter.replacePlaceholder(infoFormat, "{name}", signName)); + signs.append(replacePlaceholder(infoFormat, "{name}", signName)); } //Display that another page exists, if necessary boolean hasNextPage = alreadyDisplayed + signsPerPage < signNameList.size(); String nextPagePrompt = ""; if (hasNextPage) { - nextPagePrompt = StringFormatter.replacePlaceholder(Translator.getTranslatedMessage( - TranslatableMessage.PAID_SIGNS_NEXT_PROMPT), "{nextPage}", String.valueOf((pageNumber + 2))); + nextPagePrompt = replacePlaceholder(Translator.getTranslatedMessage( + PaidSignsTranslatableMessage.PAID_SIGNS_NEXT_PROMPT), "{nextPage}", String.valueOf((pageNumber + 2))); } sender.sendMessage(StringFormatter.replacePlaceholders(Translator.getTranslatedMessage( - TranslatableMessage.PAID_SIGNS_INFO), new String[]{"{signs}", "{nextPagePrompt}"}, + PaidSignsTranslatableMessage.PAID_SIGNS_INFO), new String[]{"{signs}", "{nextPagePrompt}"}, new String[]{signs.toString(), nextPagePrompt})); } @@ -91,7 +90,7 @@ public class ListCommand extends TokenizedCommand { private boolean parsePaidSignSelection(CommandSender sender) { PaidSign paidSign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (paidSign == null) { - sender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); + displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND); return false; } if (argumentSize < 2) { @@ -101,16 +100,16 @@ public class ListCommand extends TokenizedCommand { try { lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1); if (lineNumber < 0 || lineNumber > 3) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } } catch (NumberFormatException exception) { - sender.sendMessage(getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } if (!paidSign.getConditions().containsKey(lineNumber)) { - sender.sendMessage(replacePlaceholder(getTranslatedErrorMessage( - TranslatableMessage.ERROR_NO_SUCH_CONDITION), "{line}", String.valueOf(lineNumber))); + StringFormatter.displayErrorMessage(sender, replacePlaceholder(PaidSignsTranslatableMessage.ERROR_NO_SUCH_CONDITION, + "{line}", String.valueOf(lineNumber))); return false; } PaidSignCondition condition = paidSign.getConditions().get(lineNumber); @@ -129,11 +128,11 @@ public class ListCommand extends TokenizedCommand { */ private void displayPaidSignCondition(CommandSender sender, String signName, short signLine, PaidSignCondition condition) { - sender.sendMessage(StringFormatter.replacePlaceholders(Translator.getTranslatedMessage( - TranslatableMessage.PAID_SIGN_CONDITION_INFO), new String[]{"{name}", "{line}", "{match}", "{regex}", - "{case}", "{color}"}, new String[]{signName, String.valueOf(signLine + 1), condition.stringToMatch(), - translateBoolean(condition.executeRegex()), translateBoolean(condition.ignoreCase()), - translateBoolean(condition.ignoreColor())})); + sender.sendMessage(StringFormatter.replacePlaceholders(PaidSignsTranslatableMessage.PAID_SIGN_CONDITION_INFO, + new String[]{"{name}", "{line}", "{match}", "{regex}", "{case}", "{color}"}, + new String[]{signName, String.valueOf(signLine + 1), condition.stringToMatch(), + translateBoolean(condition.executeRegex()), translateBoolean(condition.ignoreCase()), + translateBoolean(condition.ignoreColor())})); } /** @@ -146,17 +145,31 @@ public class ListCommand extends TokenizedCommand { Map signConditions = paidSign.getConditions(); StringBuilder conditions = new StringBuilder(); for (short lineIndex : signConditions.keySet()) { - String format = Translator.getTranslatedMessage(TranslatableMessage.PAID_SIGN_INFO_CONDITION_FORMAT); + String format = Translator.getTranslatedMessage(PaidSignsTranslatableMessage.PAID_SIGN_INFO_CONDITION_FORMAT); conditions.append(StringFormatter.replacePlaceholders(format, new String[]{"{line}", "{condition}"}, new String[]{String.valueOf((lineIndex + 1)), signConditions.get(lineIndex).stringToMatch()})); } sender.sendMessage(replacePlaceholders(Translator.getTranslatedMessage( - TranslatableMessage.PAID_SIGN_INFO), new String[]{"{name}", "{cost}", "{permission}", "{case}", + PaidSignsTranslatableMessage.PAID_SIGN_INFO), new String[]{"{name}", "{cost}", "{permission}", "{case}", "{color}", "{any}", "{conditions}"}, new String[]{paidSign.getName(), String.valueOf(paidSign.getCost()), paidSign.getPermission(), translateBoolean(paidSign.getIgnoreCase()), translateBoolean(paidSign.getIgnoreColor()), translateBoolean(paidSign.matchAnyCondition()), conditions.toString()})); } + /** + * Translates the given boolean value + * + * @param booleanValue

The boolean value to translate

+ * @return

The translation of the boolean value

+ */ + public static String translateBoolean(boolean booleanValue) { + if (booleanValue) { + return Translator.getTranslatedMessage(PaidSignsTranslatableMessage.BOOLEAN_TRUE); + } else { + return Translator.getTranslatedMessage(PaidSignsTranslatableMessage.BOOLEAN_FALSE); + } + } + } diff --git a/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java index ddeeb3b..7ad6f34 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ListTabCompleter.java @@ -1,8 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -22,7 +23,7 @@ public class ListTabCompleter extends TokenizedTabCompleter { @NotNull String[] args) { super.onTabComplete(sender, command, alias, args); if (argumentSize == 1) { - return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + return TabCompletionHelper.filterMatchingStartsWith(PaidSignsTabCompleteHelper.getPaidSignNames(), arguments.get(0)); } else if (argumentSize == 2) { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign != null) { @@ -30,7 +31,7 @@ public class ListTabCompleter extends TokenizedTabCompleter { for (Short signLine : sign.getConditions().keySet()) { availableConditions.add(String.valueOf(signLine + 1)); } - return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); + return TabCompletionHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); } } return new ArrayList<>(); diff --git a/src/main/java/net/knarcraft/paidsigns/command/ReloadTabCommand.java b/src/main/java/net/knarcraft/paidsigns/command/ReloadTabCommand.java index 9ed786a..ac630d0 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/ReloadTabCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/ReloadTabCommand.java @@ -1,8 +1,8 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -20,7 +20,7 @@ public class ReloadTabCommand implements TabExecutor { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { PaidSigns.getInstance().reload(); - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_RELOADED)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_RELOADED); return true; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java index 133ebb5..3c21e7e 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionCommand.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.manager.PaidSignManager; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -28,23 +28,24 @@ public class RemoveConditionCommand extends TokenizedCommand { try { lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1); if (lineNumber < 0 || lineNumber > 3) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } } catch (NumberFormatException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_NUMBER); return false; } PaidSignManager signManager = PaidSigns.getInstance().getSignManager(); PaidSign sign = signManager.getPaidSign(name); if (sign == null) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND); return false; } if (!sign.getConditions().containsKey(lineNumber)) { - sender.sendMessage(StringFormatter.replacePlaceholder(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_NO_SUCH_CONDITION), "{line}", String.valueOf((lineNumber + 1)))); + StringFormatter.displayErrorMessage(sender, StringFormatter.replacePlaceholder( + PaidSignsTranslatableMessage.ERROR_NO_SUCH_CONDITION, "{line}", + String.valueOf((lineNumber + 1)))); return false; } @@ -52,10 +53,10 @@ public class RemoveConditionCommand extends TokenizedCommand { try { signManager.saveSigns(); } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); return false; } - sender.sendMessage(StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_REMOVED_CONDITION)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_REMOVED_CONDITION); return true; } diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java index 55161cf..5f2c4fe 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveConditionTabCompleter.java @@ -1,8 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -28,7 +29,7 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter { } if (argumentSize == 1) { - return TabCompleteHelper.filterMatchingStartsWith(TabCompleteHelper.getPaidSignNames(), arguments.get(0)); + return TabCompletionHelper.filterMatchingStartsWith(PaidSignsTabCompleteHelper.getPaidSignNames(), arguments.get(0)); } else if (argumentSize == 2) { PaidSign sign = PaidSigns.getInstance().getSignManager().getPaidSign(arguments.get(0)); if (sign != null) { @@ -36,7 +37,7 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter { for (Short signLine : sign.getConditions().keySet()) { availableConditions.add(String.valueOf(signLine + 1)); } - return TabCompleteHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); + return TabCompletionHelper.filterMatchingStartsWith(availableConditions, arguments.get(1)); } } return new ArrayList<>(); @@ -46,7 +47,7 @@ public class RemoveConditionTabCompleter extends TokenizedTabCompleter { * Initializes the values available for tab completion */ private void initializeValues() { - lineIndices = TabCompleteHelper.getSignLines(); + lineIndices = PaidSignsTabCompleteHelper.getSignLines(); } } diff --git a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java index 0d08fe4..22801e6 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/RemoveTabCommand.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.command; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; -import net.knarcraft.paidsigns.utility.TabCompleteHelper; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; +import net.knarcraft.paidsigns.utility.PaidSignsTabCompleteHelper; import net.knarcraft.paidsigns.utility.Tokenizer; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -31,15 +31,13 @@ public class RemoveTabCommand implements TabExecutor { try { if (PaidSigns.getInstance().getSignManager().removePaidSign(name)) { - sender.sendMessage(StringFormatter.getTranslatedInfoMessage( - TranslatableMessage.SUCCESS_REMOVED_PAID_SIGN)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.SUCCESS_REMOVED_PAID_SIGN); } else { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND)); + StringFormatter.displaySuccessMessage(sender, PaidSignsTranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND); } return true; } catch (IOException e) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_EXCEPTION_OCCURRED); } return false; } @@ -52,7 +50,7 @@ public class RemoveTabCommand implements TabExecutor { int argumentSize = args[args.length - 1].isEmpty() ? arguments.size() : arguments.size() - 1; if (argumentSize < 1) { - return TabCompleteHelper.getPaidSignNames(); + return PaidSignsTabCompleteHelper.getPaidSignNames(); } else { return new ArrayList<>(); } diff --git a/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java b/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java index a76ac78..dfb440d 100644 --- a/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java +++ b/src/main/java/net/knarcraft/paidsigns/command/TokenizedCommand.java @@ -1,7 +1,7 @@ package net.knarcraft.paidsigns.command; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.utility.Tokenizer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -39,8 +39,7 @@ public class TokenizedCommand implements CommandExecutor { Pattern.compile(regularExpression); return false; } catch (PatternSyntaxException exception) { - sender.sendMessage(StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_INVALID_REGULAR_EXPRESSION)); + StringFormatter.displayErrorMessage(sender, PaidSignsTranslatableMessage.ERROR_INVALID_REGULAR_EXPRESSION); return true; } } diff --git a/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java b/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java index 9ac4b7b..65cc78a 100644 --- a/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java +++ b/src/main/java/net/knarcraft/paidsigns/container/PaidSignCondition.java @@ -1,6 +1,7 @@ package net.knarcraft.paidsigns.container; -import net.knarcraft.paidsigns.utility.ColorHelper; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.ColorHelper; /** * A condition for deciding if a paid sign matches a sign line @@ -23,8 +24,8 @@ public record PaidSignCondition(String stringToMatch, boolean executeRegex, bool String stringToMatch = this.stringToMatch; //Strip color codes if they shouldn't matter if (this.ignoreColor) { - stringToMatch = ColorHelper.stripColorCodes(stringToMatch); - line = ColorHelper.stripColorCodes(line); + stringToMatch = ColorHelper.stripColorCodes(stringToMatch, ColorConversion.NORMAL); + line = ColorHelper.stripColorCodes(line, ColorConversion.NORMAL); } if (this.executeRegex) { //Match using RegEx diff --git a/src/main/java/net/knarcraft/paidsigns/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/paidsigns/formatting/PaidSignsTranslatableMessage.java similarity index 92% rename from src/main/java/net/knarcraft/paidsigns/formatting/TranslatableMessage.java rename to src/main/java/net/knarcraft/paidsigns/formatting/PaidSignsTranslatableMessage.java index 21224ad..3ccae16 100644 --- a/src/main/java/net/knarcraft/paidsigns/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/paidsigns/formatting/PaidSignsTranslatableMessage.java @@ -1,14 +1,11 @@ package net.knarcraft.paidsigns.formatting; +import net.knarcraft.knarlib.formatting.TranslatableMessage; + /** * An enum representing all translatable messages */ -public enum TranslatableMessage { - - /** - * The prefix to display in messages - */ - PREFIX, +public enum PaidSignsTranslatableMessage implements TranslatableMessage { /** * The message to display when a paid sign is successfully added @@ -139,5 +136,10 @@ public enum TranslatableMessage { * The error to display if an invalid property is given to the edit command */ ERROR_PROPERTY_NOT_RECOGNIZED, + ; + @Override + public TranslatableMessage[] getAllMessages() { + return PaidSignsTranslatableMessage.values(); + } } diff --git a/src/main/java/net/knarcraft/paidsigns/formatting/StringFormatter.java b/src/main/java/net/knarcraft/paidsigns/formatting/StringFormatter.java deleted file mode 100644 index 89bcecc..0000000 --- a/src/main/java/net/knarcraft/paidsigns/formatting/StringFormatter.java +++ /dev/null @@ -1,125 +0,0 @@ -package net.knarcraft.paidsigns.formatting; - -import net.md_5.bungee.api.ChatColor; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A formatter for formatting displayed messages - */ -public final class StringFormatter { - - private StringFormatter() { - - } - - /** - * Translates the given boolean value - * - * @param booleanValue

The boolean value to translate

- * @return

The translation of the boolean value

- */ - public static String translateBoolean(boolean booleanValue) { - if (booleanValue) { - return Translator.getTranslatedMessage(TranslatableMessage.BOOLEAN_TRUE); - } else { - return Translator.getTranslatedMessage(TranslatableMessage.BOOLEAN_FALSE); - } - } - - /** - * Replaces a placeholder in a string - * - * @param input

The input string to replace in

- * @param placeholder

The placeholder to replace

- * @param replacement

The replacement value

- * @return

The input string with the placeholder replaced

- */ - public static String replacePlaceholder(String input, String placeholder, String replacement) { - return input.replace(placeholder, replacement); - } - - /** - * Replaces placeholders in a string - * - * @param input

The input string to replace in

- * @param placeholders

The placeholders to replace

- * @param replacements

The replacement values

- * @return

The input string with placeholders replaced

- */ - public static String replacePlaceholders(String input, String[] placeholders, String[] replacements) { - for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) { - input = replacePlaceholder(input, placeholders[i], replacements[i]); - } - return input; - } - - /** - * Gets a translated and formatted info message - * - * @param translatableMessage

The translatable message to translate and format

- * @return

The translated and formatted message

- */ - public static String getTranslatedInfoMessage(TranslatableMessage translatableMessage) { - return formatInfoMessage(Translator.getTranslatedMessage(translatableMessage)); - } - - /** - * Gets a translated and formatted error message - * - * @param translatableMessage

The translatable message to translate and format

- * @return

The translated and formatted message

- */ - public static String getTranslatedErrorMessage(TranslatableMessage translatableMessage) { - return formatErrorMessage(Translator.getTranslatedMessage(translatableMessage)); - } - - /** - * Formats an information message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - public static String formatInfoMessage(String message) { - return ChatColor.DARK_GREEN + formatMessage(message); - } - - /** - * Formats an error message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - public static String formatErrorMessage(String message) { - return ChatColor.DARK_RED + formatMessage(message); - } - - /** - * Translates all found color codes to formatting in a string - * - * @param message

The string to search for color codes

- * @return

The message with color codes translated

- */ - public static String translateAllColorCodes(String message) { - message = ChatColor.translateAlternateColorCodes('&', message); - Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); - Matcher matcher = pattern.matcher(message); - while (matcher.find()) { - message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); - } - return message; - } - - /** - * Formats a message by adding the prefix and text color - * - * @param message

The message to format

- * @return

The formatted message

- */ - private static String formatMessage(String message) { - return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " + - ChatColor.RESET + message; - } - -} diff --git a/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java b/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java deleted file mode 100644 index 5070282..0000000 --- a/src/main/java/net/knarcraft/paidsigns/formatting/Translator.java +++ /dev/null @@ -1,118 +0,0 @@ -package net.knarcraft.paidsigns.formatting; - -import net.knarcraft.paidsigns.PaidSigns; -import net.knarcraft.paidsigns.utility.FileHelper; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStreamReader; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - -/** - * A tool to get strings translated to the correct language - */ -public final class Translator { - - private static Map translatedMessages; - private static Map backupTranslatedMessages; - - private Translator() { - - } - - /** - * Loads the languages used by this translator - */ - public static void loadLanguages(String selectedLanguage) { - backupTranslatedMessages = loadTranslatedMessages("en"); - translatedMessages = loadCustomTranslatedMessages(selectedLanguage); - if (translatedMessages == null) { - translatedMessages = loadTranslatedMessages(selectedLanguage); - } - } - - /** - * Gets a translated version of the given translatable message - * - * @param translatableMessage

The message to translate

- * @return

The translated message

- */ - public static String getTranslatedMessage(TranslatableMessage translatableMessage) { - if (translatedMessages == null) { - return "Translated strings not loaded"; - } - String translatedMessage; - if (translatedMessages.containsKey(translatableMessage)) { - translatedMessage = translatedMessages.get(translatableMessage); - } else if (backupTranslatedMessages.containsKey(translatableMessage)) { - translatedMessage = backupTranslatedMessages.get(translatableMessage); - } else { - translatedMessage = translatableMessage.toString(); - } - return StringFormatter.translateAllColorCodes(translatedMessage); - } - - /** - * Loads all translated messages for the given language - * - * @param language

The language chosen by the user

- * @return

A mapping of all strings for the given language

- */ - public static Map loadTranslatedMessages(String language) { - try { - BufferedReader reader = FileHelper.getBufferedReaderForInternalFile("/strings.yml"); - return loadTranslatableMessages(language, reader); - } catch (FileNotFoundException e) { - PaidSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to load translated messages"); - return null; - } - } - - /** - * Tries to load translated messages from a custom strings.yml file - * - * @param language

The selected language

- * @return

The loaded translated strings, or null if no custom language file exists

- */ - public static Map loadCustomTranslatedMessages(String language) { - File strings = new File(PaidSigns.getInstance().getDataFolder(), "strings.yml"); - if (!strings.exists()) { - PaidSigns.getInstance().getLogger().log(Level.FINEST, "Strings file not found"); - return null; - } - - try { - PaidSigns.getInstance().getLogger().log(Level.INFO, "Loading custom strings..."); - return loadTranslatableMessages(language, new BufferedReader(new InputStreamReader(new FileInputStream(strings)))); - } catch (FileNotFoundException e) { - PaidSigns.getInstance().getLogger().log(Level.WARNING, "Unable to load custom messages"); - return null; - } - } - - /** - * Loads translatable messages from the given reader - * - * @param language

The selected language

- * @param reader

The buffered reader to read from

- * @return

The loaded translated strings

- */ - private static Map loadTranslatableMessages(String language, BufferedReader reader) { - Map translatedMessages = new HashMap<>(); - YamlConfiguration configuration = YamlConfiguration.loadConfiguration(reader); - - for (TranslatableMessage message : TranslatableMessage.values()) { - String translated = configuration.getString(language + "." + message.toString()); - if (translated != null) { - translatedMessages.put(message, translated); - } - } - return translatedMessages; - } - -} \ No newline at end of file diff --git a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java index a3db905..89de36a 100644 --- a/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java +++ b/src/main/java/net/knarcraft/paidsigns/listener/SignListener.java @@ -1,10 +1,11 @@ package net.knarcraft.paidsigns.listener; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.container.PaidSignConditionMatch; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import net.knarcraft.paidsigns.manager.EconomyManager; import net.knarcraft.paidsigns.manager.TrackedSignManager; import org.bukkit.entity.Player; @@ -129,13 +130,11 @@ public class SignListener implements Listener { double cost = paidSign.getCost(); boolean canAfford = EconomyManager.canAfford(player, cost); if (!canAfford) { - player.sendMessage(replaceCost(cost, StringFormatter.getTranslatedErrorMessage( - TranslatableMessage.ERROR_CANNOT_AFFORD))); + StringFormatter.displayErrorMessage(player, replaceCost(cost, PaidSignsTranslatableMessage.ERROR_CANNOT_AFFORD)); event.setCancelled(true); } else { EconomyManager.withdraw(player, cost); - player.sendMessage(replaceCost(cost, StringFormatter.getTranslatedInfoMessage( - TranslatableMessage.SUCCESS_PAID_FOR_SIGN))); + StringFormatter.displaySuccessMessage(player, replaceCost(cost, PaidSignsTranslatableMessage.SUCCESS_PAID_FOR_SIGN)); try { TrackedSignManager.addTrackedSign(event.getBlock().getLocation(), player.getUniqueId(), cost); } catch (IOException ignored) { @@ -150,7 +149,7 @@ public class SignListener implements Listener { * @param message

The original message to replace the cost placeholder for

* @return

The message with the cost instead of the cost placeholder

*/ - private String replaceCost(double cost, String message) { + private String replaceCost(double cost, TranslatableMessage message) { String unit = EconomyManager.getCurrency(cost != 1); return String.format(StringFormatter.replacePlaceholders(message, new String[]{"{cost}", "{unit}"}, new String[]{"%.2f", "%s"}), cost, unit); diff --git a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java index da97300..ffea755 100644 --- a/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java +++ b/src/main/java/net/knarcraft/paidsigns/manager/TrackedSignManager.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.manager; +import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.TrackedSign; -import net.knarcraft.paidsigns.formatting.StringFormatter; -import net.knarcraft.paidsigns.formatting.TranslatableMessage; +import net.knarcraft.paidsigns.formatting.PaidSignsTranslatableMessage; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -155,10 +155,9 @@ public final class TrackedSignManager { double refundSum = trackedSign.cost() / 100 * PaidSigns.getInstance().getRefundPercentage(); EconomyManager.deposit(offlinePlayer, refundSum); if (offlinePlayer instanceof Player player) { - player.sendMessage(String.format(StringFormatter.replacePlaceholders( - StringFormatter.getTranslatedInfoMessage(TranslatableMessage.SUCCESS_REFUNDED), - new String[]{"{cost}", "{unit}"}, new String[]{"%.2f", "%s"}), refundSum, - EconomyManager.getCurrency(refundSum != 1))); + StringFormatter.displaySuccessMessage(player, String.format(StringFormatter.replacePlaceholders( + PaidSignsTranslatableMessage.SUCCESS_REFUNDED, new String[]{"{cost}", "{unit}"}, + new String[]{"%.2f", "%s"}), refundSum, EconomyManager.getCurrency(refundSum != 1))); } } diff --git a/src/main/java/net/knarcraft/paidsigns/utility/ColorHelper.java b/src/main/java/net/knarcraft/paidsigns/utility/ColorHelper.java deleted file mode 100644 index 24b2c76..0000000 --- a/src/main/java/net/knarcraft/paidsigns/utility/ColorHelper.java +++ /dev/null @@ -1,43 +0,0 @@ -package net.knarcraft.paidsigns.utility; - -import net.md_5.bungee.api.ChatColor; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * A helper class for dealing with colors - */ -public final class ColorHelper { - - private ColorHelper() { - - } - - /** - * Strips all color codes from the given message - * - * @param message

The message to strip color codes from

- * @return

The message without color codes

- */ - public static String stripColorCodes(String message) { - return ChatColor.stripColor(translateAllColorCodes(message)); - } - - /** - * Translates all found color codes to formatting in a string - * - * @param message

The string to search for color codes

- * @return

The message with color codes translated

- */ - public static String translateAllColorCodes(String message) { - message = ChatColor.translateAlternateColorCodes('&', message); - Pattern pattern = Pattern.compile("(#[a-fA-F0-9]{6})"); - Matcher matcher = pattern.matcher(message); - while (matcher.find()) { - message = message.replace(matcher.group(), "" + ChatColor.of(matcher.group())); - } - return message; - } - -} diff --git a/src/main/java/net/knarcraft/paidsigns/utility/FileHelper.java b/src/main/java/net/knarcraft/paidsigns/utility/FileHelper.java deleted file mode 100644 index ece2d2c..0000000 --- a/src/main/java/net/knarcraft/paidsigns/utility/FileHelper.java +++ /dev/null @@ -1,32 +0,0 @@ -package net.knarcraft.paidsigns.utility; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; - -/** - * A helper class for dealing with files - */ -public final class FileHelper { - - private FileHelper() { - - } - - /** - * Gets a buffered reader for - * - * @return

A buffered read for reading the file

- * @throws FileNotFoundException

If unable to get an input stream for the given file

- */ - public static BufferedReader getBufferedReaderForInternalFile(String file) throws FileNotFoundException { - InputStream inputStream = FileHelper.class.getResourceAsStream(file); - if (inputStream == null) { - throw new FileNotFoundException("Unable to read the given file"); - } - return new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); - } - -} diff --git a/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java b/src/main/java/net/knarcraft/paidsigns/utility/PaidSignsTabCompleteHelper.java similarity index 82% rename from src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java rename to src/main/java/net/knarcraft/paidsigns/utility/PaidSignsTabCompleteHelper.java index a294d80..d9cbb95 100644 --- a/src/main/java/net/knarcraft/paidsigns/utility/TabCompleteHelper.java +++ b/src/main/java/net/knarcraft/paidsigns/utility/PaidSignsTabCompleteHelper.java @@ -1,9 +1,9 @@ package net.knarcraft.paidsigns.utility; +import net.knarcraft.knarlib.util.TabCompletionHelper; import net.knarcraft.paidsigns.PaidSigns; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; -import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.HashMap; @@ -15,36 +15,15 @@ import java.util.StringJoiner; /** * A helper class for providing common tab complete options */ -public final class TabCompleteHelper { +public final class PaidSignsTabCompleteHelper { private static List plugins; private static Map> permissions; - private TabCompleteHelper() { + private PaidSignsTabCompleteHelper() { } - /** - * Finds tab complete values that match the start of the typed text - * - * @param values

The values to filter

- * @param typedText

The text the player has started typing

- * @return

The given string values that start with the player's typed text

- */ - public static List filterMatchingStartsWith(@NotNull List values, String typedText) { - //This little trick makes sure tab-completion works for paid sign names - if (!values.isEmpty() && values.get(0).startsWith("\"")) { - typedText = "\"" + typedText; - } - List configValues = new ArrayList<>(); - for (String value : values) { - if (value.toLowerCase().startsWith(typedText.toLowerCase())) { - configValues.add(value); - } - } - return configValues; - } - /** * Gets the available boolean values for tab completion * @@ -152,7 +131,7 @@ public final class TabCompleteHelper { output = new ArrayList<>(); } else { //Filter by the typed text - output = TabCompleteHelper.filterMatchingStartsWith(matchingPermissions, typedNode); + output = TabCompletionHelper.filterMatchingStartsWith(matchingPermissions, typedNode); } } else { output = plugins; diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml index 9f63661..be16fff 100644 --- a/src/main/resources/strings.yml +++ b/src/main/resources/strings.yml @@ -1,5 +1,4 @@ en: - PREFIX: "[PaidSigns]" SUCCESS_ADDED_PAID_SIGN: "&bSuccessfully added new paid sign" SUCCESS_ADDED_PAID_SIGN_CONDITION: "&bSuccessfully added new paid sign condition" SUCCESS_UPDATED_PAID_SIGN: "&bSuccessfully updated the paid sign property"