Uses KnarLib where possible

This commit is contained in:
Kristian Knarvik 2022-11-07 11:29:33 +01:00
parent 6a7d106d31
commit bb63fb59d1
25 changed files with 193 additions and 492 deletions

22
pom.xml
View File

@ -13,7 +13,7 @@
<description>Add costs for creating plugin signs</description>
<properties>
<java.version>17</java.version>
<java.version>16</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://git.knarcraft.net/EpicKnarvik97/PaidSigns</url>
@ -41,6 +41,20 @@
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>net.knarcraft:knarlib</artifact>
<includes>
<include>net/knarcraft/knarlib/**</include>
</includes>
</filter>
<filter>
<excludes>
<exclude>*.MF</exclude>
<exclude>*.yml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
@ -88,5 +102,11 @@
<version>23.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.knarcraft</groupId>
<artifactId>knarlib</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -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();

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}

View File

@ -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<String> 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());
}
}

View File

@ -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<String> signNames = PaidSigns.getInstance().getSignManager().getAllPaidSigns().keySet();
List<String> 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<Short, PaidSignCondition> 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 <p>The boolean value to translate</p>
* @return <p>The translation of the boolean value</p>
*/
public static String translateBoolean(boolean booleanValue) {
if (booleanValue) {
return Translator.getTranslatedMessage(PaidSignsTranslatableMessage.BOOLEAN_TRUE);
} else {
return Translator.getTranslatedMessage(PaidSignsTranslatableMessage.BOOLEAN_FALSE);
}
}
}

View File

@ -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<>();

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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<>();
}

View File

@ -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;
}
}

View File

@ -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

View File

@ -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();
}
}

View File

@ -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 <p>The boolean value to translate</p>
* @return <p>The translation of the boolean value</p>
*/
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 <p>The input string to replace in</p>
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement value</p>
* @return <p>The input string with the placeholder replaced</p>
*/
public static String replacePlaceholder(String input, String placeholder, String replacement) {
return input.replace(placeholder, replacement);
}
/**
* Replaces placeholders in a string
*
* @param input <p>The input string to replace in</p>
* @param placeholders <p>The placeholders to replace</p>
* @param replacements <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
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 <p>The translatable message to translate and format</p>
* @return <p>The translated and formatted message</p>
*/
public static String getTranslatedInfoMessage(TranslatableMessage translatableMessage) {
return formatInfoMessage(Translator.getTranslatedMessage(translatableMessage));
}
/**
* Gets a translated and formatted error message
*
* @param translatableMessage <p>The translatable message to translate and format</p>
* @return <p>The translated and formatted message</p>
*/
public static String getTranslatedErrorMessage(TranslatableMessage translatableMessage) {
return formatErrorMessage(Translator.getTranslatedMessage(translatableMessage));
}
/**
* Formats an information message by adding the prefix and text color
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
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 <p>The message to format</p>
* @return <p>The formatted message</p>
*/
public static String formatErrorMessage(String message) {
return ChatColor.DARK_RED + formatMessage(message);
}
/**
* Translates all found color codes to formatting in a string
*
* @param message <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
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 <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private static String formatMessage(String message) {
return Translator.getTranslatedMessage(TranslatableMessage.PREFIX) + " " +
ChatColor.RESET + message;
}
}

View File

@ -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<TranslatableMessage, String> translatedMessages;
private static Map<TranslatableMessage, String> 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 <p>The message to translate</p>
* @return <p>The translated message</p>
*/
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 <p>The language chosen by the user</p>
* @return <p>A mapping of all strings for the given language</p>
*/
public static Map<TranslatableMessage, String> 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 <p>The selected language</p>
* @return <p>The loaded translated strings, or null if no custom language file exists</p>
*/
public static Map<TranslatableMessage, String> 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 <p>The selected language</p>
* @param reader <p>The buffered reader to read from</p>
* @return <p>The loaded translated strings</p>
*/
private static Map<TranslatableMessage, String> loadTranslatableMessages(String language, BufferedReader reader) {
Map<TranslatableMessage, String> 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;
}
}

View File

@ -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 <p>The original message to replace the cost placeholder for</p>
* @return <p>The message with the cost instead of the cost placeholder</p>
*/
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);

View File

@ -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)));
}
}

View File

@ -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 <p>The message to strip color codes from</p>
* @return <p>The message without color codes</p>
*/
public static String stripColorCodes(String message) {
return ChatColor.stripColor(translateAllColorCodes(message));
}
/**
* Translates all found color codes to formatting in a string
*
* @param message <p>The string to search for color codes</p>
* @return <p>The message with color codes translated</p>
*/
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;
}
}

View File

@ -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 <p>A buffered read for reading the file</p>
* @throws FileNotFoundException <p>If unable to get an input stream for the given file</p>
*/
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));
}
}

View File

@ -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<String> plugins;
private static Map<String, List<String>> permissions;
private TabCompleteHelper() {
private PaidSignsTabCompleteHelper() {
}
/**
* Finds tab complete values that match the start of the typed text
*
* @param values <p>The values to filter</p>
* @param typedText <p>The text the player has started typing</p>
* @return <p>The given string values that start with the player's typed text</p>
*/
public static List<String> filterMatchingStartsWith(@NotNull List<String> 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<String> 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;

View File

@ -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"