From e76c79c8d8820f2b47337237b6aeffd287d3d79d Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Fri, 15 Jan 2021 19:29:44 +0100 Subject: [PATCH] Add Some Refactoring and enabling Doublelayer Messages --- src/de/steamwar/bungeecore/Message.java | 57 +++++++++++++------ .../bungeecore/commands/BanCommand.java | 4 +- .../bungeecore/commands/MuteCommand.java | 4 +- .../bungeecore/commands/WhoisCommand.java | 4 +- .../bungeecore/listeners/BanListener.java | 2 +- .../steamwar/bungeecore/sql/Punishment.java | 6 +- .../steamwar/messages/BungeeCore.properties | 4 +- 7 files changed, 50 insertions(+), 31 deletions(-) diff --git a/src/de/steamwar/bungeecore/Message.java b/src/de/steamwar/bungeecore/Message.java index 2d2fbb2..f3f0b26 100644 --- a/src/de/steamwar/bungeecore/Message.java +++ b/src/de/steamwar/bungeecore/Message.java @@ -28,27 +28,36 @@ import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.connection.ProxiedPlayer; +import java.text.ChoiceFormat; import java.text.MessageFormat; import java.util.Locale; import java.util.ResourceBundle; -import java.util.function.Function; public class Message { private Message(){} - public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, Object... params){ - return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, sender, params))); + public static TextComponent parseToComponent(String message, boolean prefixed, CommandSender sender, int[] useFormatter, Object... params){ + return new TextComponent(TextComponent.fromLegacyText(parse(message, prefixed, sender, useFormatter, params))); + } + + public static String parsePrefixed(String message, CommandSender sender, int[] useFormatter, Object... params){ + return parse(message, true, sender, useFormatter, params); } public static String parsePrefixed(String message, CommandSender sender, Object... params){ - return parse(message, true, sender, params); + return parse(message, true, sender, new int[0], params); + } + + + public static String parse(String message, CommandSender sender, int[] useFormatter, Object... params){ + return parse(message, false, sender, useFormatter, params); } public static String parse(String message, CommandSender sender, Object... params){ - return parse(message, false, sender, params); + return parse(message, false, sender, new int[0], params); } - private static String parse(String message, boolean prefixed, CommandSender sender,Object... params){ + private static String parse(String message, boolean prefixed, CommandSender sender, int[] useFormatter, Object... params){ Locale locale; if(sender instanceof ProxiedPlayer) locale = ((ProxiedPlayer)sender).getLocale(); @@ -61,7 +70,11 @@ public class Message { pattern = resourceBundle.getObject("PREFIX") + " "; pattern += (String)resourceBundle.getObject(message); - return new MessageFormat(pattern, locale).format(params); + MessageFormat format = new MessageFormat(pattern, locale); + for (int i : useFormatter) { + params[i] = new MessageFormat((String) resourceBundle.getObject((String) params[i]), locale).format(params); + } + return format.format(params); } public static void send(String message, CommandSender sender, Object... params){ @@ -80,6 +93,10 @@ public class Message { send(message, false, sender, type, null, null, params); } + public static void sendPrefixless(String message, CommandSender sender, ChatMessageType type, int[] useFormatter, Object... params) { + send(message, false, sender, type, null, null, useFormatter, params); + } + public static void send(String message, CommandSender sender, String onHover, ClickEvent onClick, Object... params){ send(message, true, sender, ChatMessageType.SYSTEM, onHover, onClick, params); } @@ -88,10 +105,10 @@ public class Message { send(message, false, sender, ChatMessageType.SYSTEM, onHover, onClick, params); } - public static void send(String message, boolean prefixed, CommandSender sender, ChatMessageType type, String onHover, ClickEvent onClick, Object... params){ + public static void send(String message, boolean prefixed, CommandSender sender, ChatMessageType type, String onHover, ClickEvent onClick, int[] useFormatter, Object... params){ if(type == ChatMessageType.CHAT && sender instanceof ProxiedPlayer && ((ProxiedPlayer)sender).getChatMode() != ProxiedPlayer.ChatMode.SHOWN) return; - TextComponent msg = parseToComponent(message, prefixed, sender, params); + TextComponent msg = parseToComponent(message, prefixed, sender, useFormatter, params); if(onHover != null) msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(onHover))); if(onClick != null) @@ -102,9 +119,13 @@ public class Message { sender.sendMessage(msg); } + public static void send(String message, boolean prefixed, CommandSender sender, ChatMessageType type, String onHover, ClickEvent onClick, Object... params){ + send(message, prefixed, sender, type, onHover, onClick, new int[0], params); + } + public static void broadcast(String message, String onHover, ClickEvent onClick, Object... params){ for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()) - send(message, player, parse(onHover, false, player), onClick, params); + send(message, player, parse(onHover, false, player, new int[0]), onClick, params); } public static void broadcast(String message, Object... params){ @@ -120,18 +141,18 @@ public class Message { } } - public static void team(String message, Function... params){ - team(message, ChatMessageType.SYSTEM, params); + public static void team(String message, Object... params){ + team(message, ChatMessageType.SYSTEM, new int[0], params); } - public static void team(String message, ChatMessageType type, Function... params){ + public static void team(String message, int[] useChoice, Object... params){ + team(message, ChatMessageType.SYSTEM, useChoice, params); + } + + public static void team(String message, ChatMessageType type, int[] useFormatter, Object... params){ for(ProxiedPlayer player : ProxyServer.getInstance().getPlayers()){ if(player.getGroups().contains(ConnectionListener.TEAM_GROUP)) { - Object[] objects = new Object[params.length]; - for (int i = 0; i < params.length; i++) { - objects[i] = params[i].apply(player); - } - sendPrefixless(message, player, type, objects); + sendPrefixless(message, player, type, useFormatter, params); } } } diff --git a/src/de/steamwar/bungeecore/commands/BanCommand.java b/src/de/steamwar/bungeecore/commands/BanCommand.java index 52b2638..8e6830f 100644 --- a/src/de/steamwar/bungeecore/commands/BanCommand.java +++ b/src/de/steamwar/bungeecore/commands/BanCommand.java @@ -27,7 +27,6 @@ import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; -import java.time.format.DateTimeFormatter; import java.util.Date; public class BanCommand extends BasicCommand { @@ -57,8 +56,7 @@ public class BanCommand extends BasicCommand { } String msg = banReason.toString(); target.ban(banTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); - Message.team("BAN_TEAM_BANNED", target.getUserName(), sender.getName(), - (args[1].equalsIgnoreCase("perma")?Message.parse("BAN_PERMA", sender):Message.parse("BAN_UNTIL", sender) + banTime.toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", sender)))), msg); + Message.team("BAN_TEAM_BANNED", new int[]{2}, target.getUserName(), sender.getName(), (args[1].equalsIgnoreCase("perma")?"BAN_PERMA":"BAN_UNTIL"), msg, banTime); } public static Timestamp parseTime(CommandSender sender, String arg){ diff --git a/src/de/steamwar/bungeecore/commands/MuteCommand.java b/src/de/steamwar/bungeecore/commands/MuteCommand.java index 043428e..e8661b9 100644 --- a/src/de/steamwar/bungeecore/commands/MuteCommand.java +++ b/src/de/steamwar/bungeecore/commands/MuteCommand.java @@ -24,7 +24,6 @@ import de.steamwar.bungeecore.sql.SteamwarUser; import net.md_5.bungee.api.CommandSender; import java.sql.Timestamp; -import java.time.format.DateTimeFormatter; public class MuteCommand extends BasicCommand { @@ -53,7 +52,6 @@ public class MuteCommand extends BasicCommand { } String msg = muteReason.toString(); target.mute(muteTime, msg, SteamwarUser.get(sender.getName()).getId(), args[1].equalsIgnoreCase("perma")); - Message.team("MUTE_TEAM_MUTED", player -> target.getUserName(), player -> sender.getName(), - player -> (args[1].equalsIgnoreCase("perma")?Message.parse("BAN_PERMA", player):Message.parse("BAN_UNTIL", player) + muteTime.toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", player)))), player -> msg); + Message.team("MUTE_TEAM_MUTED", target.getUserName(), new int[]{2}, sender.getName(), (args[1].equalsIgnoreCase("perma")?"BAN_PERMA":"BAN_UNTIL"), msg, muteTime); } } diff --git a/src/de/steamwar/bungeecore/commands/WhoisCommand.java b/src/de/steamwar/bungeecore/commands/WhoisCommand.java index d2e2f4b..4b1d562 100644 --- a/src/de/steamwar/bungeecore/commands/WhoisCommand.java +++ b/src/de/steamwar/bungeecore/commands/WhoisCommand.java @@ -20,6 +20,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.BungeeCore; +import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.sql.Punishment; import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.Team; @@ -28,6 +29,7 @@ import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.connection.ProxiedPlayer; import java.text.DecimalFormat; +import java.time.format.DateTimeFormatter; public class WhoisCommand extends BasicCommand { public WhoisCommand(){ @@ -74,7 +76,7 @@ public class WhoisCommand extends BasicCommand { BungeeCore.send(player, "§7Strafen: "); for (Punishment punishment : Punishment.getAllPunishmentsOfPlayer(user.getId())) { BungeeCore.send(player, "§7" + SteamwarUser.get(punishment.getPunisher()).getUserName() + "§8» §f§l" + punishment.getType().name() + ": §e" - + punishment.getStartTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT) + " - " + (punishment.isPerma()?"Perma":punishment.getEndTime().toLocalDateTime().format(BungeeCore.DATE_FORMAT)) + " §c" + punishment.getReason()); + + punishment.getStartTime().toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", player))) + " - " + (punishment.isPerma()?"Perma":punishment.getEndTime().toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", player)))) + " §c" + punishment.getReason()); } } } diff --git a/src/de/steamwar/bungeecore/listeners/BanListener.java b/src/de/steamwar/bungeecore/listeners/BanListener.java index 2d27685..a4fbbec 100644 --- a/src/de/steamwar/bungeecore/listeners/BanListener.java +++ b/src/de/steamwar/bungeecore/listeners/BanListener.java @@ -70,7 +70,7 @@ public class BanListener extends BasicListener { for(BannedUserIPs banned : ips) { SteamwarUser bannedUser = SteamwarUser.get(banned.getUserID()); potentialBan.append(Message.parse("BAN_AVOIDING_LIST", target, bannedUser.getUserName(), - banned.getTimestamp().toLocalDateTime().format(BungeeCore.DATE_FORMAT))); + banned.getTimestamp().toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", target))))); } TextComponent msg = new TextComponent(potentialBan.toString()); diff --git a/src/de/steamwar/bungeecore/sql/Punishment.java b/src/de/steamwar/bungeecore/sql/Punishment.java index 295a330..ada287d 100644 --- a/src/de/steamwar/bungeecore/sql/Punishment.java +++ b/src/de/steamwar/bungeecore/sql/Punishment.java @@ -134,11 +134,11 @@ public class Punishment { this.endTime = newUpdate; } - public String getBantime(Timestamp endTime, boolean perma, ProxiedPlayer player) { + public String getBantime(Timestamp endTime, boolean perma) { if(perma) - return Message.parse("BAN_PERMA", player); + return "Permanent"; else - return endTime.toLocalDateTime().format(DateTimeFormatter.ofPattern(Message.parse("TIMEFORMAT", player))); + return endTime.toLocalDateTime().format(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm")); } public enum PunishmentType { diff --git a/src/de/steamwar/messages/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index 883a77c..35f5a15 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -96,7 +96,7 @@ USAGE_IGNORE=§8/§7ignore §8[§eSpieler§8] #Various commands ALERT=§f{0} -BAN_TEAM_BANNED=§c {0} wurde von {1} {2} gebannt. §f§lGrund: §f{3} +BAN_TEAM_BANNED=§c{0} wurde von {1} {2} gebannt. §f§lGrund: §f{3} BANNED_MESSAGE_PERMA=§cDu bist permanent gebannt. §r§lGrund§r: §c{0} BANNED_MESSAGE_UNTIL=Du bist bis zum {0} gebannt. §r§lGrund§r: §c{1} MUTE_TEAM_MUTED=§c {0} wurde von {1} {2} gemuted. §f§lGrund: §f{3} @@ -105,7 +105,7 @@ MUTED_MESSAGE_UNTIL=Du bist bis zum {0} gemuted. §r§lGrund§r: §c{1} BAN_CHANGED={0} verändert von {1} von {2} auf {3} wegen {4} BAN_INVALID_TIME=§cUngültige Zeitangabe. BAN_PERMA=Permanent -BAN_UNTIL=bis zum +BAN_UNTIL=bis zum {4} BAN_AVOIDING_ALERT=§cMögliche Bannumgehung durch §r{0}§c: §c BAN_AVOIDING_LIST={0} §e{1} §c BAN_AVOIDING_BAN_HOVER=§cBanne Spieler wegen Bannumgehung