From f4a581115641cba49fbc2c4e101bb45721ae3f99 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 11 Jan 2024 14:24:01 +0100 Subject: [PATCH] Greatly improves messaging, adds Jenkinsfile --- Jenkinsfile | 33 ++++++++++ pom.xml | 34 ++++++++++ .../playerpayouts/PlayerPayouts.java | 32 +++++++++- .../playerpayouts/command/ReloadCommand.java | 3 +- .../command/SetGroupPaymentCommand.java | 17 +++-- .../command/SetPlayerPaymentCommand.java | 16 +++-- .../playerpayouts/config/Translatable.java | 63 +++++++++++++++++++ .../config/payout/PayoutAction.java | 2 +- .../config/payout/PayoutActionParser.java | 15 +++-- .../config/payout/PayoutDelimiter.java | 3 + src/main/resources/config.yml | 2 + src/main/resources/strings.yml | 10 +++ 12 files changed, 211 insertions(+), 19 deletions(-) create mode 100644 Jenkinsfile create mode 100644 src/main/java/net/knarcraft/playerpayouts/config/Translatable.java create mode 100644 src/main/resources/strings.yml diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..2e16cba --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,33 @@ +pipeline { + agent any + tools { + jdk 'JDK17' + } + stages { + stage('Build') { + steps { + echo 'Building...' + sh 'mvn clean & mvn validate & mvn compile' + } + } + stage('Test') { + steps { + echo 'Testing...' + sh 'mvn test' + } + } + stage('Verify') { + steps { + echo 'Verifying...' + sh 'mvn verify -Dmaven.test.skip=true' + } + } + stage('Deploy') { + steps { + echo 'Deploying...' + sh 'mvn deploy -Dmaven.install.skip=true -Dmaven.test.skip=true' + archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true + } + } + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9ed881d..b49bd25 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,20 @@ false + + + net.knarcraft:knarlib + + net/knarcraft/knarlib/** + + + + + *.MF + *.yml + + + @@ -73,7 +87,21 @@ paper-repo https://papermc.io/repo/repository/maven-public/ + + knarcraft-repo + https://git.knarcraft.net/api/packages/EpicKnarvik97/maven + + + + knarcraft-repo + https://git.knarcraft.net/api/packages/EpicKnarvik97/maven + + + knarcraft-repo + https://git.knarcraft.net/api/packages/EpicKnarvik97/maven + + @@ -100,5 +128,11 @@ 2.20.1 provided + + net.knarcraft + knarlib + 1.2.4 + compile + diff --git a/src/main/java/net/knarcraft/playerpayouts/PlayerPayouts.java b/src/main/java/net/knarcraft/playerpayouts/PlayerPayouts.java index 3c45d75..1ba2c95 100644 --- a/src/main/java/net/knarcraft/playerpayouts/PlayerPayouts.java +++ b/src/main/java/net/knarcraft/playerpayouts/PlayerPayouts.java @@ -1,12 +1,15 @@ package net.knarcraft.playerpayouts; import com.earth2me.essentials.IEssentials; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.playerpayouts.command.ReloadCommand; import net.knarcraft.playerpayouts.command.SetGroupPaymentCommand; import net.knarcraft.playerpayouts.command.SetGroupPaymentTabCompleter; import net.knarcraft.playerpayouts.command.SetPlayerPaymentCommand; import net.knarcraft.playerpayouts.command.SetPlayerPaymentTabCompleter; import net.knarcraft.playerpayouts.config.Configuration; +import net.knarcraft.playerpayouts.config.Translatable; import net.knarcraft.playerpayouts.listener.PlayerJoinListener; import net.knarcraft.playerpayouts.manager.EconomyManager; import net.knarcraft.playerpayouts.manager.PermissionManager; @@ -20,6 +23,7 @@ import org.bukkit.command.PluginCommand; import org.bukkit.command.TabCompleter; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; +import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.java.JavaPlugin; @@ -36,6 +40,7 @@ public final class PlayerPayouts extends JavaPlugin { private Configuration configuration; private IEssentials essentials = null; + private StringFormatter stringFormatter; private static PlayerPayouts playerPayouts; @Override @@ -49,6 +54,20 @@ public final class PlayerPayouts extends JavaPlugin { this.saveConfig(); this.configuration = new Configuration(fileConfiguration); + Translator translator = new Translator(); + translator.registerMessageCategory(Translatable.GROUP_PAYOUTS_UNAVAILABLE); + translator.loadLanguages(this.getDataFolder(), "en", + fileConfiguration.getString("language", "en")); + + PluginDescriptionFile description = this.getDescription(); + String prefix; + if (description.getPrefix() == null) { + prefix = "PlayerPayouts"; + } else { + prefix = description.getPrefix(); + } + stringFormatter = new StringFormatter(prefix, translator); + // Plugin startup logic if (Bukkit.getPluginManager().getPlugin("Vault") != null) { if (!setupVault()) { @@ -114,6 +133,15 @@ public final class PlayerPayouts extends JavaPlugin { playerPayouts.configuration.load(playerPayouts.getConfig()); } + /** + * Gets a string formatter + * + * @return

A string formatter

+ */ + public static StringFormatter getStringFormatter() { + return getInstance().stringFormatter; + } + /** * Pay all players that have been on the server long enough */ @@ -144,8 +172,8 @@ public final class PlayerPayouts extends JavaPlugin { } EconomyManager.deposit(player, payment); if (configuration.displayPaymentMessage()) { - player.sendMessage(this.getDescription().getPrefix() + "You got a paycheck of " + - EconomyManager.format(payment)); + stringFormatter.displaySuccessMessage(player, stringFormatter.replacePlaceholder( + Translatable.PAYOUT_RECEIVED, "{value}", EconomyManager.format(payment))); } PlayerTracker.trackPlayer(player); } diff --git a/src/main/java/net/knarcraft/playerpayouts/command/ReloadCommand.java b/src/main/java/net/knarcraft/playerpayouts/command/ReloadCommand.java index 46d6963..325fb35 100644 --- a/src/main/java/net/knarcraft/playerpayouts/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/playerpayouts/command/ReloadCommand.java @@ -1,6 +1,7 @@ package net.knarcraft.playerpayouts.command; import net.knarcraft.playerpayouts.PlayerPayouts; +import net.knarcraft.playerpayouts.config.Translatable; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -19,7 +20,7 @@ public class ReloadCommand implements TabExecutor { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { PlayerPayouts.reload(); - sender.sendMessage("Plugin reloaded!"); + PlayerPayouts.getStringFormatter().displaySuccessMessage(sender, Translatable.PLUGIN_RELOADED); return true; } diff --git a/src/main/java/net/knarcraft/playerpayouts/command/SetGroupPaymentCommand.java b/src/main/java/net/knarcraft/playerpayouts/command/SetGroupPaymentCommand.java index e716aeb..5ccfbaf 100644 --- a/src/main/java/net/knarcraft/playerpayouts/command/SetGroupPaymentCommand.java +++ b/src/main/java/net/knarcraft/playerpayouts/command/SetGroupPaymentCommand.java @@ -1,6 +1,9 @@ package net.knarcraft.playerpayouts.command; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.playerpayouts.PlayerPayouts; import net.knarcraft.playerpayouts.config.Configuration; +import net.knarcraft.playerpayouts.config.Translatable; import net.knarcraft.playerpayouts.manager.PermissionManager; import net.knarcraft.playerpayouts.util.StringHelper; import org.bukkit.command.Command; @@ -31,9 +34,10 @@ public class SetGroupPaymentCommand implements CommandExecutor { return false; } + StringFormatter stringFormatter = PlayerPayouts.getStringFormatter(); + if (!PermissionManager.isInitialized()) { - commandSender.sendMessage("Vault permissions aren't available, and thus per-group payouts are " + - "unavailable."); + stringFormatter.displayErrorMessage(commandSender, Translatable.GROUP_PAYOUTS_UNAVAILABLE); return false; } @@ -41,15 +45,18 @@ public class SetGroupPaymentCommand implements CommandExecutor { String group = arguments[0]; if (StringHelper.isNonValue(arguments[1])) { configuration.setGroupPayout(group, null); - commandSender.sendMessage(String.format("Group payout for group %s has been cleared", group)); + stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholder( + Translatable.GROUP_PAYOUT_CLEARED, "{group}", group)); } else { Double payout = Double.parseDouble(arguments[1]); configuration.setGroupPayout(group, payout); - commandSender.sendMessage(String.format("Group payout for group %s has been set to %s", group, payout)); + stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholders( + Translatable.GROUP_PAYOUT_SET, new String[]{"{group}", "{value}"}, + new String[]{group, String.valueOf(payout)})); } return true; } catch (NumberFormatException exception) { - commandSender.sendMessage("Payout must be a number"); + stringFormatter.displayErrorMessage(commandSender, Translatable.PAYOUT_NUMBER_REQUIRED); return false; } } diff --git a/src/main/java/net/knarcraft/playerpayouts/command/SetPlayerPaymentCommand.java b/src/main/java/net/knarcraft/playerpayouts/command/SetPlayerPaymentCommand.java index 957c58e..47df25f 100644 --- a/src/main/java/net/knarcraft/playerpayouts/command/SetPlayerPaymentCommand.java +++ b/src/main/java/net/knarcraft/playerpayouts/command/SetPlayerPaymentCommand.java @@ -1,6 +1,9 @@ package net.knarcraft.playerpayouts.command; +import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.playerpayouts.PlayerPayouts; import net.knarcraft.playerpayouts.config.Configuration; +import net.knarcraft.playerpayouts.config.Translatable; import net.knarcraft.playerpayouts.util.StringHelper; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; @@ -35,6 +38,7 @@ public class SetPlayerPaymentCommand implements CommandExecutor { return false; } String playerName = arguments[0]; + StringFormatter stringFormatter = PlayerPayouts.getStringFormatter(); // Parse the player id UUID playerId; @@ -56,7 +60,7 @@ public class SetPlayerPaymentCommand implements CommandExecutor { } if (playerId == null) { - commandSender.sendMessage("You must supply a valid name of an online player, or a UUID"); + stringFormatter.displayErrorMessage(commandSender, Translatable.PLAYER_ID_REQUIRED); return false; } } @@ -66,16 +70,18 @@ public class SetPlayerPaymentCommand implements CommandExecutor { try { if (StringHelper.isNonValue(arguments[1])) { configuration.setPlayerPayout(playerId, null); - commandSender.sendMessage(String.format("Player payout for player %s has been cleared", playerName)); + stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholder( + Translatable.PLAYER_PAYOUT_CLEARED, "{player}", playerName)); } else { Double payout = Double.parseDouble(arguments[1]); configuration.setPlayerPayout(playerId, payout); - commandSender.sendMessage(String.format("Player payout for player %s has been set to %s", playerName, - payout)); + stringFormatter.displaySuccessMessage(commandSender, stringFormatter.replacePlaceholders( + Translatable.PLAYER_PAYOUT_SET, new String[]{"{player}", "{value}"}, + new String[]{playerName, String.valueOf(payout)})); } return true; } catch (NumberFormatException exception) { - commandSender.sendMessage("Payout must be a number"); + stringFormatter.displayErrorMessage(commandSender, Translatable.PAYOUT_NUMBER_REQUIRED); return false; } } diff --git a/src/main/java/net/knarcraft/playerpayouts/config/Translatable.java b/src/main/java/net/knarcraft/playerpayouts/config/Translatable.java new file mode 100644 index 0000000..9c89fdb --- /dev/null +++ b/src/main/java/net/knarcraft/playerpayouts/config/Translatable.java @@ -0,0 +1,63 @@ +package net.knarcraft.playerpayouts.config; + +import net.knarcraft.knarlib.formatting.TranslatableMessage; +import org.jetbrains.annotations.NotNull; + +/** + * Player Payout's translatable strings + */ +public enum Translatable implements TranslatableMessage { + + /** + * The message displayed when group payouts are unavailable because of a missing permission plugin + */ + GROUP_PAYOUTS_UNAVAILABLE, + + /** + * The message displayed when the plugin is reloaded + */ + PLUGIN_RELOADED, + + /** + * The message displayed when a group payout override has been cleared + */ + GROUP_PAYOUT_CLEARED, + + /** + * The message displayed when a group payout override has been set + */ + GROUP_PAYOUT_SET, + + /** + * The message displayed when a non-number has been given as the payout + */ + PAYOUT_NUMBER_REQUIRED, + + /** + * The message displayed when an invalid player name or UUID has been given + */ + PLAYER_ID_REQUIRED, + + /** + * The message displayed when a player payout override has been cleared + */ + PLAYER_PAYOUT_CLEARED, + + /** + * The message displayed when a player payout override has been set + */ + PLAYER_PAYOUT_SET, + + /** + * The message displayed when a payout has been received + */ + PAYOUT_RECEIVED, + + ; + + @Override + public @NotNull TranslatableMessage[] getAllMessages() { + return Translatable.values(); + } + +} diff --git a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutAction.java b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutAction.java index cd4b87e..ae24107 100644 --- a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutAction.java +++ b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutAction.java @@ -3,7 +3,7 @@ package net.knarcraft.playerpayouts.config.payout; import org.jetbrains.annotations.NotNull; /** - * Instantiates a new payout action + * A payout action is the combination of two components, joined by a delimiter * * @param component1

The component "to the left of" the delimiter

* @param delimiter

The delimiter between the two components

diff --git a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutActionParser.java b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutActionParser.java index c842e87..e8dca48 100644 --- a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutActionParser.java +++ b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutActionParser.java @@ -41,6 +41,8 @@ public class PayoutActionParser { } for (int i = index; i < parts.size(); i++) { + /* If the payout component isn't null, we're in the next step of the for loop, and the entire value of + payoutComponent is the "left" component */ if (payoutComponent == null) { String part = parts.get(i); PayoutTarget target = PayoutTarget.match(part); @@ -58,6 +60,7 @@ public class PayoutActionParser { return payoutComponent; } + // Parse the delimiter PayoutDelimiter delimiter = PayoutDelimiter.match(parts.get(++i).charAt(0)); if (delimiter == null) { throw new ParseException("Unable to parse payout mode string delimiter " + parts.get(i) + "!", i); @@ -93,22 +96,24 @@ public class PayoutActionParser { for (int index = 0; index < input.length(); index++) { char character = input.charAt(index); if (PayoutDelimiter.match(character) != null) { + // Tokenize delimiters, and tokenize everything read before the delimiter if (!currentToken.isEmpty()) { tokens.add(currentToken.toString()); } tokens.add(String.valueOf(character)); currentToken = new StringBuilder(); } else if (Character.isLetter(character)) { - tokenizeNormalCharacter(currentToken, character, input.length(), index, tokens); + // Tokenize one letter + tokenizeLetter(currentToken, character, input.length(), index, tokens); } - // Unrecognized tokens are ignored + // Unrecognized tokens are ignored, to allow for spaces } return tokens; } /** - * Adds a normal character to the token. Adds the current token to tokens if at the end of the input + * Adds a letter to the token. Adds the current token to tokens if at the end of the input * * @param currentToken

The string builder containing the current token.

* @param character

The character found in the input.

@@ -116,8 +121,8 @@ public class PayoutActionParser { * @param index

The index of the read character.

* @param tokens

The list of processed tokens.

*/ - private static void tokenizeNormalCharacter(@NotNull StringBuilder currentToken, char character, int inputLength, - int index, @NotNull List tokens) { + private static void tokenizeLetter(@NotNull StringBuilder currentToken, char character, int inputLength, + int index, @NotNull List tokens) { currentToken.append(character); if (index == inputLength - 1) { tokens.add(currentToken.toString()); diff --git a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutDelimiter.java b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutDelimiter.java index 7031133..d301c00 100644 --- a/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutDelimiter.java +++ b/src/main/java/net/knarcraft/playerpayouts/config/payout/PayoutDelimiter.java @@ -3,6 +3,9 @@ package net.knarcraft.playerpayouts.config.payout; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +/** + * Delimiters usable in the payout setting string + */ public enum PayoutDelimiter { /** diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 42bf049..ec5fe64 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,3 +1,5 @@ +# The language to use. Currently, only "en" is available, unless you add a custom one yourself +language: en # The default payout if the player has no overrides defaultPayout: 10 # The amount of minutes to wait between each payout diff --git a/src/main/resources/strings.yml b/src/main/resources/strings.yml new file mode 100644 index 0000000..b68eef4 --- /dev/null +++ b/src/main/resources/strings.yml @@ -0,0 +1,10 @@ +en: + GROUP_PAYOUTS_UNAVAILABLE: "Vault permissions aren't available, and thus per-group payouts are unavailable." + PLUGIN_RELOADED: "Plugin reloaded!" + GROUP_PAYOUT_CLEARED: "Group payout for group {group} has been cleared" + GROUP_PAYOUT_SET: "Group payout for group {group} has been set to {value}" + PLAYER_PAYOUT_CLEARED: "Player payout for player {player} has been cleared" + PLAYER_PAYOUT_SET: "Player payout for player {player} has been set to {value}" + PAYOUT_NUMBER_REQUIRED: "Payout must be a number" + PLAYER_ID_REQUIRED: "You must supply a valid name of an online player, or a UUID" + PAYOUT_RECEIVED: "You got a paycheck of {value}" \ No newline at end of file