Update to use placeholders for messages - WIP

This commit is contained in:
GeorgH93 2023-06-22 00:12:34 +02:00
parent 6e32cbdcf5
commit 76bc6318d5
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 76 additions and 24 deletions

View File

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

View File

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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}