From 76bc6318d558236ca605055d3324f879d39e526f Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Thu, 22 Jun 2023 00:12:34 +0200 Subject: [PATCH] Update to use placeholders for messages - WIP --- .../Minepacks/Bukkit/Command/OpenCommand.java | 22 ++----- .../Bukkit/Command/RestoreCommand.java | 18 +++--- .../Minepacks/Bukkit/Placeholders.java | 60 +++++++++++++++++++ 3 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Placeholders.java diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/OpenCommand.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/OpenCommand.java index 1d69c9a..2d9d957 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/OpenCommand.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/OpenCommand.java @@ -18,15 +18,16 @@ package at.pcgamingfreaks.Minepacks.Bukkit.Command; import at.pcgamingfreaks.Bukkit.Message.Message; +import at.pcgamingfreaks.Bukkit.Message.Placeholder.Processors.GameModePlaceholderProcessor; import at.pcgamingfreaks.Bukkit.Util.Utils; import at.pcgamingfreaks.Calendar.TimeSpan; import at.pcgamingfreaks.Command.HelpData; import at.pcgamingfreaks.Message.MessageClickEvent; +import at.pcgamingfreaks.Message.Placeholder.Processors.FloatPlaceholderProcessor; import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.Minepacks.Bukkit.Permissions; import org.bukkit.Bukkit; -import org.bukkit.GameMode; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -34,12 +35,11 @@ import java.util.ArrayList; import java.util.List; -import java.util.Locale; public class OpenCommand extends MinepacksCommand { private final Message messageCooldown, messageWrongGameMode; - private final String allowedGameModes, descriptionOpenOthers, helpParam; + private final String descriptionOpenOthers, helpParam; private final Minepacks plugin; public OpenCommand(Minepacks plugin) @@ -47,21 +47,12 @@ public OpenCommand(Minepacks plugin) super(plugin, "open", plugin.getLanguage().getTranslated("Commands.Description.Backpack"), Permissions.USE, false, plugin.getLanguage().getCommandAliases("Open")); this.plugin = plugin; - messageCooldown = plugin.getLanguage().getMessage("Ingame.Open.Cooldown").replaceAll("\\{TimeLeft}", "%1\\$.1f").replaceAll("\\{TimeSpanLeft}", "%2\\$s"); - messageWrongGameMode = plugin.getLanguage().getMessage("Ingame.Open.WrongGameMode").replaceAll("\\{CurrentGameMode}", "%1\\$s").replaceAll("\\{AllowedGameModes}", "%1\\$s"); + messageCooldown = plugin.getLanguage().getMessage("Ingame.Open.Cooldown").placeholder("TimeLeft", new FloatPlaceholderProcessor(1)).placeholder("TimeSpanLeft"); + messageWrongGameMode = plugin.getLanguage().getMessage("Ingame.Open.WrongGameMode").replaceAll("\\{AllowedGameModes}", new GameModePlaceholderProcessor().process(plugin.getConfiguration().getAllowedGameModes())).placeholder("CurrentGameMode", new GameModePlaceholderProcessor()); descriptionOpenOthers = plugin.getLanguage().getTranslated("Commands.Description.OpenOthers"); helpParam = "<" + plugin.getLanguage().get("Commands.PlayerNameVariable") + ">"; StringBuilder allowedGameModesBuilder = new StringBuilder(); - for(GameMode gameMode : plugin.getConfiguration().getAllowedGameModes()) - { - if(allowedGameModesBuilder.length() > 1) - { - allowedGameModesBuilder.append(", "); - } - allowedGameModesBuilder.append(gameMode.name().toLowerCase(Locale.ROOT)); - } - allowedGameModes = allowedGameModesBuilder.toString(); //TODO translate } @Override @@ -92,8 +83,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String main, @NotNul } else { - //noinspection StringToUpperCaseOrToLowerCaseWithoutLocale - messageWrongGameMode.send(player, player.getGameMode().name().toLowerCase(), allowedGameModes); + messageWrongGameMode.send(player, player.getGameMode()); } } else diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/RestoreCommand.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/RestoreCommand.java index b11adef..618bb33 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/RestoreCommand.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/RestoreCommand.java @@ -25,7 +25,8 @@ import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand; import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; import at.pcgamingfreaks.Minepacks.Bukkit.Permissions; -import at.pcgamingfreaks.StringUtils; +import at.pcgamingfreaks.Minepacks.Bukkit.Placeholders; +import at.pcgamingfreaks.Util.StringUtils; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; @@ -48,11 +49,11 @@ public RestoreCommand(Minepacks plugin) { super(plugin, "restore", plugin.getLanguage().getTranslated("Commands.Description.Restore"), Permissions.RESTORE, plugin.getLanguage().getCommandAliases("Restore")); helpParam = "<" + plugin.getLanguage().get("Ingame.Restore.ParameterBackupName") + "> (" + plugin.getLanguage().get("Commands.PlayerNameVariable") + ")"; - messageBackupsHeader = plugin.getLanguage().getMessage("Ingame.Restore.Headline").replaceAll("\\{CurrentPage}", "%1\\$d").replaceAll("\\{MaxPage}", "%2\\$d").replaceAll("\\{MainCommand}", "%3\\$s").replaceAll("\\{SubCommand}", "%4\\$s"); - messageBackupsFooter = plugin.getLanguage().getMessage("Ingame.Restore.Footer").replaceAll("\\{CurrentPage}", "%1\\$d").replaceAll("\\{MaxPage}", "%2\\$d").replaceAll("\\{MainCommand}", "%3\\$s").replaceAll("\\{SubCommand}", "%4\\$s"); - messageBackupEntry = plugin.getLanguage().getMessage("Ingame.Restore.BackupEntry").replaceAll("\\{BackupIdentifier}", "%1\\$s").replaceAll("\\{BackupDate}", "%2\\$s") - .replaceAll("\\{BackupPlayerName}", "%3\\$s").replaceAll("\\{BackupPlayerUUID}", "%4\\$s").replaceAll("\\{MainCommand}", "%5\\$s").replaceAll("\\{SubCommand}", "%6\\$s"); - messageUnableToLoadBackup = plugin.getLanguage().getMessage("Ingame.Restore.NoValidBackup").replaceAll("\\{BackupIdentifier}", "%1\\$s"); + messageBackupsHeader = plugin.getLanguage().getMessage("Ingame.Restore.Headline").placeholders(Placeholders.PAGE_OPTIONS); + messageBackupsFooter = plugin.getLanguage().getMessage("Ingame.Restore.Footer").placeholders(Placeholders.PAGE_OPTIONS); + messageBackupEntry = plugin.getLanguage().getMessage("Ingame.Restore.BackupEntry").placeholder("BackupIdentifier").placeholder("BackupDate") + .placeholder("BackupPlayerName").placeholder("BackupPlayerUUID").placeholder("MainCommand").placeholder("SubCommand"); + messageUnableToLoadBackup = plugin.getLanguage().getMessage("Ingame.Restore.NoValidBackup").placeholder("BackupIdentifier"); messageNoUserFound = plugin.getLanguage().getMessage("Ingame.Restore.NoUserToRestoreToFound"); messageRestored = plugin.getLanguage().getMessage("Ingame.Restore.Restored"); listCommands = plugin.getLanguage().getCommandAliases("ListBackups", "list"); @@ -151,7 +152,8 @@ private void listBackups(final @NotNull CommandSender sender, final @NotNull Str int pages = backups.size() / elementsPerPage + 1; page = Math.min(page, pages - 1); int offset = page * elementsPerPage, end = Math.min(offset + elementsPerPage, backups.size()); - messageBackupsHeader.send(sender, page + 1, pages, mainCommandAlias, alias + ' ' + args[0]); + String subCom = alias + ' ' + args[0]; + messageBackupsHeader.send(sender, page + 1, pages, mainCommandAlias, subCom, page, page + 2); while(offset < end) { String backup = backups.get(offset++), uuid = "No UUID", date = "Unknown"; @@ -167,7 +169,7 @@ private void listBackups(final @NotNull CommandSender sender, final @NotNull Str } messageBackupEntry.send(sender, backup, date, components[0], uuid, mainCommandAlias, alias); } - messageBackupsFooter.send(sender, page + 1, pages, mainCommandAlias, alias + ' ' + args[0]); + messageBackupsFooter.send(sender, page + 1, pages, mainCommandAlias, subCom, page, page + 2); } @Override diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Placeholders.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Placeholders.java new file mode 100644 index 0000000..75f522d --- /dev/null +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Placeholders.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2022 GeorgH93 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package at.pcgamingfreaks.Minepacks.Bukkit; + +import at.pcgamingfreaks.Bukkit.Message.Placeholder.Processors.PlayerDisplayNamePlaceholderProcessor; +import at.pcgamingfreaks.Bukkit.Message.Placeholder.Processors.PlayerNamePlaceholderProcessor; +import at.pcgamingfreaks.Message.Placeholder.Placeholder; + +import org.jetbrains.annotations.NotNull; + +public class Placeholders +{ + private Placeholders(){} + + public static final @NotNull String[] PAGE_OPTIONS = { "CurrentPage", "MaxPage", "MainCommand", "SubCommand", "PrevPage", "NextPage" }; + public static final @NotNull Placeholder[] PLAYER_NAME = mkPlayerName(""); + + public static @NotNull Placeholder[] mkPlayerName(final @NotNull String prefix) + { + return mkPlayerName(prefix, "", false); + } + + public static @NotNull Placeholder[] mkPlayerName(final @NotNull String prefix, final @NotNull String suffix) + { + return mkPlayerName(prefix, suffix, false); + } + + public static @NotNull Placeholder[] mkPlayerName(final @NotNull String prefix, final @NotNull String suffix, final boolean regex) + { + return new Placeholder[]{ + new Placeholder(prefix + "Name" + suffix, PlayerNamePlaceholderProcessor.INSTANCE, regex), + new Placeholder(prefix + "DisplayName" + suffix, PlayerDisplayNamePlaceholderProcessor.INSTANCE, regex) + }; + } + + public static @NotNull Placeholder[] mkPlayerNameRegex(final @NotNull String prefix) + { + return mkPlayerName(prefix, "", true); + } + + public static @NotNull Placeholder[] mkPlayerNameRegex(final @NotNull String prefix, final @NotNull String suffix) + { + return mkPlayerName(prefix, suffix, true); + } +} \ No newline at end of file