refactor: MessageUtils

This commit is contained in:
Simon Giesel 2020-05-13 23:27:39 +02:00
parent 7de3a45ed7
commit 2a3caad9ae
6 changed files with 76 additions and 93 deletions

View file

@ -7,9 +7,11 @@
"Aragur", "Aragur",
"Gamemode", "Gamemode",
"Gamerules", "Gamerules",
"Gson",
"Minecraft", "Minecraft",
"Mojang", "Mojang",
"cliffbreak", "cliffbreak",
"koords",
"mkdir", "mkdir",
"npcs", "npcs",
"teamcolors", "teamcolors",

View file

@ -22,7 +22,9 @@ import de.cliffbreak.varo.listeners.PlayerJoinQuitListener;
import de.cliffbreak.varo.listeners.PlayerLoginListener; import de.cliffbreak.varo.listeners.PlayerLoginListener;
import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener; import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener;
import de.cliffbreak.varo.managers.NPCManager; import de.cliffbreak.varo.managers.NPCManager;
import de.cliffbreak.varo.managers.TeamManager;
import de.cliffbreak.varo.uitls.BanUtils; import de.cliffbreak.varo.uitls.BanUtils;
import de.cliffbreak.varo.uitls.MessageUtils;
import de.cliffbreak.varo.uitls.PlayerCache; import de.cliffbreak.varo.uitls.PlayerCache;
import de.cliffbreak.varo.uitls.VaroUtils; import de.cliffbreak.varo.uitls.VaroUtils;
@ -34,6 +36,8 @@ public class Varo extends JavaPlugin {
private PlayerCache playerCache; private PlayerCache playerCache;
private BanUtils banUtils; private BanUtils banUtils;
private VaroUtils varoUtils; private VaroUtils varoUtils;
private TeamManager teamManager;
private MessageUtils messageUtils;
private boolean canCreatePortal = false; private boolean canCreatePortal = false;
@ -49,6 +53,8 @@ public class Varo extends JavaPlugin {
this.playerCache = new PlayerCache(this); this.playerCache = new PlayerCache(this);
this.banUtils = new BanUtils(this); this.banUtils = new BanUtils(this);
this.varoUtils = new VaroUtils(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.Start", 1590427800000f);
this.config.addDefault("Varo.Debug", false); 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 PlayerJoinQuitListener(this), this);
this.getServer().getPluginManager().registerEvents(new PlayerClientOptionsChangeListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerClientOptionsChangeListener(this), this);
this.getServer().getPluginManager().registerEvents(new PlayerResourcePackStatusListener(), 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 EntityRegainHealthListener(this), this);
this.getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this); this.getServer().getPluginManager().registerEvents(new PlayerDeathListener(this), this);
this.getServer().getPluginManager().registerEvents(new BannedItemListener(), this); this.getServer().getPluginManager().registerEvents(new BannedItemListener(), this);
@ -97,11 +103,11 @@ public class Varo extends JavaPlugin {
} }
public NPCManager getNPCManager() { public NPCManager getNPCManager() {
return npcManager; return this.npcManager;
} }
public PlayerCache getPlayerCache() { public PlayerCache getPlayerCache() {
return playerCache; return this.playerCache;
} }
public BanUtils getBanUtils() { public BanUtils getBanUtils() {
@ -109,7 +115,11 @@ public class Varo extends JavaPlugin {
} }
public TeamManager getTeamManager() { public TeamManager getTeamManager() {
return teamManager; return this.teamManager;
}
public MessageUtils getMessageUtils() {
return this.messageUtils;
} }
public boolean canCreatePortal() { public boolean canCreatePortal() {

View file

@ -17,7 +17,6 @@ import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.Varo;
import de.cliffbreak.varo.uitls.MessageUtils;
import de.cliffbreak.varo.uitls.WebUtils; import de.cliffbreak.varo.uitls.WebUtils;
public class VaroCommand implements CommandExecutor { public class VaroCommand implements CommandExecutor {
@ -85,16 +84,16 @@ public class VaroCommand implements CommandExecutor {
p.sendTitle(new Title("TODO: Title, etc")); p.sendTitle(new Title("TODO: Title, etc"));
} }
} else if (args.length == 1 && args[0].equals("teamcolors")) { } else if (args.length == 1 && args[0].equals("teamcolors")) {
sender.sendMessage(MessageUtils.getRichTextComponent("§c" + "Team 01", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§c" + "Team 01"));
sender.sendMessage(MessageUtils.getRichTextComponent("§6" + "Team 02", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§6" + "Team 02"));
sender.sendMessage(MessageUtils.getRichTextComponent("§e" + "Team 03", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§e" + "Team 03"));
sender.sendMessage(MessageUtils.getRichTextComponent("§2" + "Team 04", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§2" + "Team 04"));
sender.sendMessage(MessageUtils.getRichTextComponent("§a" + "Team 05", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§a" + "Team 05"));
sender.sendMessage(MessageUtils.getRichTextComponent("§b" + "Team 06", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§b" + "Team 06"));
sender.sendMessage(MessageUtils.getRichTextComponent("§3" + "Team 07", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§3" + "Team 07"));
sender.sendMessage(MessageUtils.getRichTextComponent("§9" + "Team 08", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§9" + "Team 08"));
sender.sendMessage(MessageUtils.getRichTextComponent("§d" + "Team 09", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§d" + "Team 09"));
sender.sendMessage(MessageUtils.getRichTextComponent("§5" + "Team 10", "")); sender.sendMessage(this.plugin.getMessageUtils().getServiceMessage("", "§5" + "Team 10"));
} else if (args.length == 2 && args[0].equals("portal")) { } else if (args.length == 2 && args[0].equals("portal")) {
if (args[1].equals("enable")) { if (args[1].equals("enable")) {
@ -108,7 +107,7 @@ public class VaroCommand implements CommandExecutor {
} }
} else if (args.length == 1 && args[0].equals("test")) { } else if (args.length == 1 && args[0].equals("test")) {
if (sender instanceof Player) { if (sender instanceof Player) {
this.plugin.getNPCManager().createClone((Player) sender, false); this.plugin.getNPCManager().createClone((Player) sender);
} else { } else {
sender.sendMessage("§c§lFehler:§r§c Dieses Kommando kann nur durch einen Spieler ausgeführt werden."); sender.sendMessage("§c§lFehler:§r§c Dieses Kommando kann nur durch einen Spieler ausgeführt werden.");
} }

View file

@ -5,13 +5,19 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import de.cliffbreak.varo.uitls.MessageUtils; import de.cliffbreak.varo.Varo;
public class ChatListener implements Listener { public class ChatListener implements Listener {
private final Varo plugin;
public ChatListener(final Varo plugin) {
this.plugin = plugin;
}
@EventHandler @EventHandler
public void onChat(final AsyncPlayerChatEvent e) { public void onChat(final AsyncPlayerChatEvent e) {
e.setCancelled(true); e.setCancelled(true);
Bukkit.broadcast(MessageUtils.getRichTextComponent(e.getPlayer().getName(), e.getMessage())); Bukkit.broadcast(this.plugin.getMessageUtils().getChatMessage(e.getPlayer().getName(), e.getMessage()));
} }
} }

View file

@ -4,7 +4,6 @@ import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import com.destroystokyo.paper.event.server.PaperServerListPingEvent; import com.destroystokyo.paper.event.server.PaperServerListPingEvent;
@ -17,7 +16,6 @@ import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import de.cliffbreak.varo.Varo; import de.cliffbreak.varo.Varo;
import de.cliffbreak.varo.uitls.MessageUtils;
import de.cliffbreak.varo.uitls.PacketReader; import de.cliffbreak.varo.uitls.PacketReader;
public class PlayerJoinQuitListener implements Listener { public class PlayerJoinQuitListener implements Listener {
@ -32,9 +30,9 @@ public class PlayerJoinQuitListener implements Listener {
public void onPlayerQuit(final PlayerQuitEvent e) { public void onPlayerQuit(final PlayerQuitEvent e) {
e.setQuitMessage(null); e.setQuitMessage(null);
if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) { if (!this.plugin.getBanUtils().isBanned(e.getPlayer().getUniqueId())) {
this.plugin.getNPCManager().createClone(e.getPlayer(), true); this.plugin.getNPCManager().createClone(e.getPlayer());
Bukkit.broadcast( Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(),
MessageUtils.getRichTextComponent(e.getPlayer().getName(), "§f hat den Server verlassen.", true)); "hat den Server verlassen."));
} }
} }
@ -49,7 +47,7 @@ public class PlayerJoinQuitListener implements Listener {
packetReader.inject(); packetReader.inject();
Bukkit.broadcast( 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")) { if (this.plugin.getConfig().getBoolean("Varo.Debug")) {
e.getPlayer().sendMessage("\n§7§l######### §9Cliffbreak.de - §lVaro §7§l#########\n \n" e.getPlayer().sendMessage("\n§7§l######### §9Cliffbreak.de - §lVaro §7§l#########\n \n"

View file

@ -2,6 +2,9 @@ package de.cliffbreak.varo.uitls;
import java.util.Calendar; 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.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent; import net.md_5.bungee.api.chat.HoverEvent;
@ -9,85 +12,50 @@ import net.md_5.bungee.api.chat.TextComponent;
public class MessageUtils { 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;
} }
/** public TextComponent getServiceMessage(final String player, final String message) {
* Create a TextComponent with a more detailed player name on hover final TextComponent component = new TextComponent(" ");
* component.addExtra(getTimeStamp());
* @param player component.addExtra(getPlayerComponent(player));
* the playername the message should be sent from component.addExtra(new TextComponent(" §r§f" + message));
* @param message return component;
* the message to send
* @return TextComponent
*/
public static TextComponent getRichTextComponent(final String player, final String message) {
return getRichTextComponent(player, message, false, true);
} }
/** public TextComponent getChatMessage(final String player, final String message) {
* Create a TextComponent with a more detailed player name on hover final TextComponent component = new TextComponent(" ");
* component.addExtra(getTimeStamp());
* @param player component.addExtra(getPlayerComponent(player));
* the playername the message should be sent from component.addExtra(new TextComponent(" §7>> §r§f" + message));
* @param message return component;
* 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);
} }
/** private TextComponent getPlayerComponent(String player) {
* Create a TextComponent with a more detailed player name on hover and an optional timestamp if (player.isEmpty()) {
* return new TextComponent();
* @param player }
* the playername the message should be sent from final Team team = this.plugin.getTeamManager().getTeamByPlayer(player);
* @param message final TextComponent component = new TextComponent(team.getPrefix().substring(2, 4) + player);
* the message to send component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
* @param isServiceMessage new ComponentBuilder("§e" + "Name:" + "§r " + player + "\n§b" + "Team:" + "§r " + team.getDisplayName())
* 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 :(")
.create())); .create()));
playerComponent.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player + " ")); component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + player + " "));
if (useTimestamp) return component;
mainComponent.addExtra(timeComponent);
mainComponent.addExtra(playerComponent);
if (!isServiceMessage)
mainComponent.addExtra(messageComponent);
else
mainComponent.addExtra(message);
return mainComponent;
} }
private static String getTimestamp() { private TextComponent getTimeStamp() {
final Calendar calendar = Calendar.getInstance(); final Calendar calendar = Calendar.getInstance();
return String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" final TextComponent component = new TextComponent(
+ String.format("%02d", calendar.get(Calendar.MINUTE)); "§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,
private static String getTimestampSeconds() { new ComponentBuilder("§7" + String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":"
final Calendar calendar = Calendar.getInstance(); + String.format("%02d", calendar.get(Calendar.MINUTE)) + ":"
return String.format("%02d", calendar.get(Calendar.HOUR_OF_DAY)) + ":" + String.format("%02d", calendar.get(Calendar.SECOND))).create()));
+ String.format("%02d", calendar.get(Calendar.MINUTE)) + ":" return component;
+ String.format("%02d", calendar.get(Calendar.SECOND));
} }
} }