From 2a3caad9ae3c2d9d0ae0e4a6503ae1381a59d35f Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 13 May 2020 23:27:39 +0200 Subject: [PATCH] refactor: MessageUtils --- .vscode/settings.json | 2 + .../main/java/de/cliffbreak/varo/Varo.java | 18 ++- .../cliffbreak/varo/commands/VaroCommand.java | 23 ++-- .../varo/listeners/ChatListener.java | 10 +- .../listeners/PlayerJoinQuitListener.java | 10 +- .../cliffbreak/varo/uitls/MessageUtils.java | 106 ++++++------------ 6 files changed, 76 insertions(+), 93 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a2f107b..d1e7de1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,9 +7,11 @@ "Aragur", "Gamemode", "Gamerules", + "Gson", "Minecraft", "Mojang", "cliffbreak", + "koords", "mkdir", "npcs", "teamcolors", diff --git a/varo/src/main/java/de/cliffbreak/varo/Varo.java b/varo/src/main/java/de/cliffbreak/varo/Varo.java index b91153c..bda2ce6 100644 --- a/varo/src/main/java/de/cliffbreak/varo/Varo.java +++ b/varo/src/main/java/de/cliffbreak/varo/Varo.java @@ -22,7 +22,9 @@ import de.cliffbreak.varo.listeners.PlayerJoinQuitListener; import de.cliffbreak.varo.listeners.PlayerLoginListener; import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener; import de.cliffbreak.varo.managers.NPCManager; +import de.cliffbreak.varo.managers.TeamManager; import de.cliffbreak.varo.uitls.BanUtils; +import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.uitls.PlayerCache; import de.cliffbreak.varo.uitls.VaroUtils; @@ -34,6 +36,8 @@ public class Varo extends JavaPlugin { private PlayerCache playerCache; private BanUtils banUtils; private VaroUtils varoUtils; + private TeamManager teamManager; + private MessageUtils messageUtils; private boolean canCreatePortal = false; @@ -49,6 +53,8 @@ public class Varo extends JavaPlugin { this.playerCache = new PlayerCache(this); this.banUtils = new BanUtils(this); this.varoUtils = new VaroUtils(this); + this.teamManager = new TeamManager(this); + this.messageUtils = new MessageUtils(this); this.config.addDefault("Varo.Start", 1590427800000f); this.config.addDefault("Varo.Debug", false); @@ -61,7 +67,7 @@ public class Varo extends JavaPlugin { this.getServer().getPluginManager().registerEvents(new PlayerJoinQuitListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerClientOptionsChangeListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerResourcePackStatusListener(), this); - this.getServer().getPluginManager().registerEvents(new ChatListener(), this); + this.getServer().getPluginManager().registerEvents(new ChatListener(this), this); this.getServer().getPluginManager().registerEvents(new EntityRegainHealthListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); this.getServer().getPluginManager().registerEvents(new BannedItemListener(), this); @@ -97,11 +103,11 @@ public class Varo extends JavaPlugin { } public NPCManager getNPCManager() { - return npcManager; + return this.npcManager; } public PlayerCache getPlayerCache() { - return playerCache; + return this.playerCache; } public BanUtils getBanUtils() { @@ -109,7 +115,11 @@ public class Varo extends JavaPlugin { } public TeamManager getTeamManager() { - return teamManager; + return this.teamManager; + } + + public MessageUtils getMessageUtils() { + return this.messageUtils; } public boolean canCreatePortal() { diff --git a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java index 1262959..0d8a0bf 100644 --- a/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java +++ b/varo/src/main/java/de/cliffbreak/varo/commands/VaroCommand.java @@ -17,7 +17,6 @@ import org.json.simple.JSONValue; import org.json.simple.parser.ParseException; import de.cliffbreak.varo.Varo; -import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.uitls.WebUtils; public class VaroCommand implements CommandExecutor { @@ -85,16 +84,16 @@ public class VaroCommand implements CommandExecutor { p.sendTitle(new Title("TODO: Title, etc")); } } else if (args.length == 1 && args[0].equals("teamcolors")) { - sender.sendMessage(MessageUtils.getRichTextComponent("§c" + "Team 01", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§6" + "Team 02", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§e" + "Team 03", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§2" + "Team 04", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§a" + "Team 05", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§b" + "Team 06", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§3" + "Team 07", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§9" + "Team 08", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§d" + "Team 09", "")); - sender.sendMessage(MessageUtils.getRichTextComponent("§5" + "Team 10", "")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§c" + "Team 01")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§6" + "Team 02")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§e" + "Team 03")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§2" + "Team 04")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§a" + "Team 05")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§b" + "Team 06")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§3" + "Team 07")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§9" + "Team 08")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§d" + "Team 09")); + sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§5" + "Team 10")); } else if (args.length == 2 && args[0].equals("portal")) { if (args[1].equals("enable")) { @@ -108,7 +107,7 @@ public class VaroCommand implements CommandExecutor { } } else if (args.length == 1 && args[0].equals("test")) { if (sender instanceof Player) { - this.plugin.getNPCManager().createClone((Player) sender, false); + this.plugin.getNPCManager().createClone((Player) sender); } else { sender.sendMessage("§c§lFehler:§r§c Dieses Kommando kann nur durch einen Spieler ausgeführt werden."); } diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java index 2057ea4..3e31cca 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/ChatListener.java @@ -5,13 +5,19 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; -import de.cliffbreak.varo.uitls.MessageUtils; +import de.cliffbreak.varo.Varo; public class ChatListener implements Listener { + private final Varo plugin; + + public ChatListener(final Varo plugin) { + this.plugin = plugin; + } + @EventHandler public void onChat(final AsyncPlayerChatEvent e) { e.setCancelled(true); - Bukkit.broadcast(MessageUtils.getRichTextComponent(e.getPlayer().getName(), e.getMessage())); + Bukkit.broadcast(this.plugin.getMessageUtils().getChatMessage(e.getPlayer().getName(), e.getMessage())); } } diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java index 544367e..f360d0f 100644 --- a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerJoinQuitListener.java @@ -4,7 +4,6 @@ import java.time.Duration; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; -import java.time.format.DateTimeFormatter; import com.destroystokyo.paper.event.server.PaperServerListPingEvent; @@ -17,7 +16,6 @@ import org.bukkit.event.server.ServerListPingEvent; import org.bukkit.scheduler.BukkitRunnable; import de.cliffbreak.varo.Varo; -import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.uitls.PacketReader; public class PlayerJoinQuitListener implements Listener { @@ -32,9 +30,9 @@ public class PlayerJoinQuitListener implements Listener { public void onPlayerQuit(final PlayerQuitEvent e) { e.setQuitMessage(null); if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { - this.plugin.getNPCManager().createClone(e.getPlayer(), true); - Bukkit.broadcast( - MessageUtils.getRichTextComponent(e.getPlayer().getName(), "§f hat den Server verlassen.", true)); + this.plugin.getNPCManager().createClone(e.getPlayer()); + Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(), + "hat den Server verlassen.")); } } @@ -49,7 +47,7 @@ public class PlayerJoinQuitListener implements Listener { packetReader.inject(); Bukkit.broadcast( - MessageUtils.getRichTextComponent(e.getPlayer().getName(), "§f hat den Server betreten.", true)); + this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(), "hat den Server betreten.")); if (this.plugin.getConfig().getBoolean("Varo.Debug")) { e.getPlayer().sendMessage("\n§7§l######### §9Cliffbreak.de - §lVaro §7§l#########\n \n" diff --git a/varo/src/main/java/de/cliffbreak/varo/uitls/MessageUtils.java b/varo/src/main/java/de/cliffbreak/varo/uitls/MessageUtils.java index ad5aaf4..44a614f 100644 --- a/varo/src/main/java/de/cliffbreak/varo/uitls/MessageUtils.java +++ b/varo/src/main/java/de/cliffbreak/varo/uitls/MessageUtils.java @@ -2,6 +2,9 @@ package de.cliffbreak.varo.uitls; import java.util.Calendar; +import org.bukkit.scoreboard.Team; + +import de.cliffbreak.varo.Varo; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; @@ -9,85 +12,50 @@ import net.md_5.bungee.api.chat.TextComponent; public class MessageUtils { - public static String getTimestampedMessage(final String message) { + private final Varo plugin; - return " §8" + getTimestamp() + " §7| " + message; + public MessageUtils(final Varo plugin) { + this.plugin = plugin; } - /** - * Create a TextComponent with a more detailed player name on hover - * - * @param player - * the playername the message should be sent from - * @param message - * the message to send - * @return TextComponent - */ - public static TextComponent getRichTextComponent(final String player, final String message) { - return getRichTextComponent(player, message, false, true); + public TextComponent getServiceMessage(final String player, final String message) { + final TextComponent component = new TextComponent(" "); + component.addExtra(getTimeStamp()); + component.addExtra(getPlayerComponent(player)); + component.addExtra(new TextComponent(" §r§f" + message)); + return component; } - /** - * Create a TextComponent with a more detailed player name on hover - * - * @param player - * the playername the message should be sent from - * @param message - * the message to send - * @param isServiceMessage - * is this a service message (f.e. PlayerJoinEvent) - * @return TextComponent - */ - public static TextComponent getRichTextComponent(final String player, final String message, - final boolean isServiceMessage) { - return getRichTextComponent(player, message, isServiceMessage, true); + public TextComponent getChatMessage(final String player, final String message) { + final TextComponent component = new TextComponent(" "); + component.addExtra(getTimeStamp()); + component.addExtra(getPlayerComponent(player)); + component.addExtra(new TextComponent(" §7>> §r§f" + message)); + return component; } - /** - * Create a TextComponent with a more detailed player name on hover and an optional timestamp - * - * @param player - * the playername the message should be sent from - * @param message - * the message to send - * @param isServiceMessage - * is this a service message (f.e. PlayerJoinEvent) - * @param useTimestamp - * should a timestamp be returned? - * @return TextComponent - */ - public static TextComponent getRichTextComponent(final String player, final String message, - final boolean isServiceMessage, final boolean useTimestamp) { - final TextComponent mainComponent = new TextComponent(" "); - final TextComponent timeComponent = new TextComponent("§8" + getTimestamp() + " §7| "); - final TextComponent playerComponent = new TextComponent("§b" + player); - final TextComponent messageComponent = new TextComponent(" §7>> §r§f" + message); - timeComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, - new ComponentBuilder("§7" + getTimestampSeconds()).create())); - playerComponent.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, - new ComponentBuilder("§e" + "Name:" + "§r " + player + "\n§b" + "Team:" + "§r " + "kein Team :(") + private TextComponent getPlayerComponent(String player) { + if (player.isEmpty()) { + return new TextComponent(); + } + final Team team = this.plugin.getTeamManager().getTeamByPlayer(player); + final TextComponent component = new TextComponent(team.getPrefix().substring(2, 4) + player); + component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new ComponentBuilder("§e" + "Name:" + "§r " + player + "\n§b" + "Team:" + "§r " + team.getDisplayName()) .create())); - playerComponent.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player + " ")); - if (useTimestamp) - mainComponent.addExtra(timeComponent); - mainComponent.addExtra(playerComponent); - if (!isServiceMessage) - mainComponent.addExtra(messageComponent); - else - mainComponent.addExtra(message); - return mainComponent; + component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player + " ")); + return component; } - private static String getTimestamp() { + private TextComponent getTimeStamp() { final Calendar calendar = Calendar.getInstance(); - return String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" - + String.format("%02d", calendar.get(Calendar.MINUTE)); - } - - private static String getTimestampSeconds() { - final Calendar calendar = Calendar.getInstance(); - return String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" - + String.format("%02d", calendar.get(Calendar.MINUTE)) + ":" - + String.format("%02d", calendar.get(Calendar.SECOND)); + final TextComponent component = new TextComponent( + "§8" + String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" + + String.format("%02d", calendar.get(Calendar.MINUTE)) + " §7| "); + component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, + new ComponentBuilder("§7" + String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" + + String.format("%02d", calendar.get(Calendar.MINUTE)) + ":" + + String.format("%02d", calendar.get(Calendar.SECOND))).create())); + return component; } } \ No newline at end of file