From e071a3e6fc233341f3c599f315f5012ce6ed0603 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 15:56:31 +1000 Subject: [PATCH 01/14] Start cleaning up code --- .github/workflows/maven.yml | 7 +- README.md | 9 +- .../commands/impl/UltraStaffChatCommand.java | 107 +++++++----------- .../managers/StaffChatManager.java | 6 +- .../ultrastaffchat/utils/StaffChat.java | 18 ++- 5 files changed, 64 insertions(+), 83 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 3ac8dac..5372353 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -1,13 +1,8 @@ -# This workflow will build a Java project with Maven -# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven - -name: Maven Build +name: build on: push: branches: [ main ] - pull_request: - branches: [ main ] jobs: build: diff --git a/README.md b/README.md index 84140de..2499c95 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@
-![GitHub Workflow Status](https://img.shields.io/github/workflow/status/HyperaOfficial/UltraStaffChat/maven.yml?color=%232155CC&&style=for-the-badge) -![GitHub Release Date](https://img.shields.io/github/release-date/HyperaOfficial/UltraStaffChat?color=%232155CC&&label=Last%20Release&&style=for-the-badge) -![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/HyperaOfficial/UltraStaffChat?color=%232155CC&&style=for-the-badge) -![GitHub](https://img.shields.io/github/license/HyperaOfficial/UltraStaffChat?color=%232155CC&&style=for-the-badge) +![Build Status](https://img.shields.io/github/workflow/status/HyperaOfficial/UltraStaffChat/build?color=%232155CC&&style=for-the-badge) +![CodeFactor Grade](https://img.shields.io/codefactor/grade/github/HyperaOfficial/UltraStaffChat/main?style=for-the-badge) +![Version](https://img.shields.io/github/v/tag/HyperaOfficial/UltraStaffChat?color=2155CC&label=Version&style=for-the-badge) +![Code Size](https://img.shields.io/github/languages/code-size/HyperaOfficial/UltraStaffChat?color=%232155CC&&style=for-the-badge) +![License](https://img.shields.io/github/license/HyperaOfficial/UltraStaffChat?color=%232155CC&&style=for-the-badge) ## Download Click [here](https://www.spigotmc.org/resources/68956/) to download UltraStaffChat Bungeecord. diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java index 2cdf4fc..4af5571 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java @@ -52,111 +52,80 @@ public void execute(CommandSender sender, String[] args) { Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); if(args.length > 0) { - if (args[0].matches("(?i:(ab(t|out)|credit(s)?|contibutor(s)?))")) { - audience.sendMessage(Component.text() - .append( - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), - Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), - Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), - Component.text().content(" was created by:\n").color(NamedTextColor.GRAY).build(), - LegacyComponentSerializer.legacyAmpersand().deserialize("&7 - &c" + String.join("\n &7- &c", Common.getContributors())) - ) - ); + if(args[0].matches("(?i:(ab(t|out)|credit(s)?|contibutor(s)?))")) { + audience.sendMessage(Component.text().append(Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), Component.text().content(" was created by:\n").color(NamedTextColor.GRAY).build(), LegacyComponentSerializer.legacyAmpersand().deserialize("&7 - &c" + String.join("\n &7- &c", Common.getContributors())))); return; } - if (args[0].matches("(?i:(r(eload)?))")) { - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) {audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); return;} - audience.sendMessage(Component.text() - .append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN) - ) - ); + if(args[0].matches("(?i:(r(eload)?))")) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN))); + UltraStaffChat.getConfig().reload(); Discord.reload(); - audience.sendMessage(Component.text() - .append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN) - ) - ); + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN))); return; } - if (args[0].matches("(?i:(debug))")) { - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) {audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); return;} + if(args[0].matches("(?i:(debug))")) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + String log; - String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt";; + String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt"; + boolean advanced = false; - if (args.length > 1) { - for (String arg : args) { - if (arg.matches("(?i:(--a(dv(anced)?)?))")) { + if(args.length > 1) { + for(String arg : args) { + if(arg.matches("(?i:(--a(dv(anced)?)?))")) { advanced = true; break; } } } - if (advanced) { - audience.sendMessage(Component.text().append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE) - )); + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE))); log = generateAdvancedDebugLog(); } else { - audience.sendMessage(Component.text().append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Generating debug log...").color(NamedTextColor.WHITE) - )); + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating debug log...").color(NamedTextColor.WHITE))); log = generateDebugLog(); } try { File logFolder = new File(UltraStaffChat.getInstance().getDataFolder() + "/logs"); - if (!logFolder.exists()) logFolder.mkdirs(); + if(!logFolder.exists()) + logFolder.mkdirs(); File logFile = new File(logFolder + "/" + filename); - if (logFile.createNewFile()) { + if(logFile.createNewFile()) { FileWriter logWriter = new FileWriter(logFile); logWriter.write(log); logWriter.close(); - } else throw new Exception(); + } else { + throw new Exception(); + } String pasteURL = PasteUtils.createPaste(log); - if (pasteURL == null) throw new Exception(); - if (advanced) { - audience.sendMessage(Component.text().append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), - Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))) - )); + if(pasteURL == null) + throw new Exception(); + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))))); } else { - audience.sendMessage(Component.text().append( - LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)) - )); + audience.sendMessage(Component.text().append(LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)))); } } catch (Exception e) { Common.logPrefix("Failed to generate debug log!"); - audience.sendMessage(Component.text().append( - Component.text().content("[").color(NamedTextColor.GRAY), - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), - Component.text().content("] ").color(NamedTextColor.GRAY), - Component.text().content("Failed to generate debug log").color(NamedTextColor.RED) - )); + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Failed to generate debug log").color(NamedTextColor.RED))); return; } - return; } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java b/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java index 0509568..f09ecee 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java +++ b/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java @@ -50,7 +50,11 @@ public static void broadcastAFK(ProxiedPlayer player, boolean afk) { if (staffToggleAFKEvent.isCancelled()) return; Audience audience = UltraStaffChat.getInstance().getAdventure().permission(messageRaw("permission-afk")); audience.sendMessage(adventurise((afk ? messageRaw("afk-broadcast") : messageRaw("no-afk-broadcast")).replace("{player}", player.getName()))); - if (afk) {Discord.broadcastDiscordAFKEnable(player);} else Discord.broadcastDiscordAFKDisable(player); + if (afk) { + Discord.broadcastDiscordAFKEnable(player); + } else { + Discord.broadcastDiscordAFKDisable(player); + } } public static void broadcastJoin(ProxiedPlayer player) { diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java index ca6a0aa..3bf45d4 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java @@ -30,7 +30,11 @@ public class StaffChat { private static boolean staffChatMuted = false; public static void disableMessages(ProxiedPlayer p) { - if (!mutedPlayers.containsKey(p.getUniqueId())) { mutedPlayers.put(p.getUniqueId(), true); return; }; if (mutedPlayers.get(p.getUniqueId()).equals(false)) mutedPlayers.replace(p.getUniqueId(), false, true); + if (!mutedPlayers.containsKey(p.getUniqueId())) { + mutedPlayers.put(p.getUniqueId(), true); + return; + } + if (mutedPlayers.get(p.getUniqueId()).equals(false)) mutedPlayers.replace(p.getUniqueId(), false, true); } public static void enableMessages(ProxiedPlayer p) { @@ -38,7 +42,11 @@ public static void enableMessages(ProxiedPlayer p) { } public static boolean toggleMessages(ProxiedPlayer p) { - if (hasMessagesDisabled(p)) { enableMessages(p); } else disableMessages(p); + if (hasMessagesDisabled(p)) { + enableMessages(p); + } else { + disableMessages(p); + } return hasMessagesDisabled(p); } @@ -84,7 +92,11 @@ public static void enableChatStaffChat(ProxiedPlayer p) { } public static void disableChatStaffChat(ProxiedPlayer p) { - if (!toggledPlayers.containsKey(p.getUniqueId())) { toggledPlayers.put(p.getUniqueId(), false); return; }; if (toggledPlayers.get(p.getUniqueId()).equals(true)) toggledPlayers.replace(p.getUniqueId(), true, false); + if (!toggledPlayers.containsKey(p.getUniqueId())) { + toggledPlayers.put(p.getUniqueId(), false); + return; + } + if (toggledPlayers.get(p.getUniqueId()).equals(true)) toggledPlayers.replace(p.getUniqueId(), true, false); } public static boolean hasChatStaffChatEnabled(ProxiedPlayer p) { From dc6965aa354603d330db3f87953b4d21751dcaed Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 16:06:01 +1000 Subject: [PATCH 02/14] Reformat code --- .../hypera/ultrastaffchat/UltraStaffChat.java | 13 +- .../ultrastaffchat/commands/Command.java | 10 +- .../commands/CommandManager.java | 34 +- .../commands/impl/MuteStaffChatCommand.java | 80 +- .../commands/impl/StaffChatCommand.java | 63 +- .../commands/impl/StaffChatMuteCommand.java | 88 +- .../commands/impl/StaffChatToggleCommand.java | 88 +- .../commands/impl/StaffListCommand.java | 2 +- .../commands/impl/UltraStaffChatCommand.java | 196 +++-- .../hypera/ultrastaffchat/config/Config.java | 17 +- .../events/staff/StaffChatEvent.java | 58 +- .../events/staff/StaffJoinEvent.java | 27 +- .../events/staff/StaffLeaveEvent.java | 27 +- .../events/staff/StaffSwitchServerEvent.java | 64 +- .../events/staff/StaffToggleAFKEvent.java | 52 +- .../ultrastaffchat/listeners/Listener.java | 4 +- .../listeners/ListenerManager.java | 31 +- .../listeners/impl/ChatListener.java | 66 +- .../listeners/impl/JoinLeaveListener.java | 42 +- .../listeners/impl/SwitchListener.java | 21 +- .../managers/StaffChatManager.java | 38 +- .../hypera/ultrastaffchat/utils/Common.java | 35 +- .../hypera/ultrastaffchat/utils/Discord.java | 534 ++++++------ .../ultrastaffchat/utils/DiscordWebhook.java | 757 +++++++++--------- .../ultrastaffchat/utils/PasteUtils.java | 67 +- .../ultrastaffchat/utils/StaffChat.java | 162 ++-- .../ultrastaffchat/utils/TimeUtils.java | 22 +- 27 files changed, 1330 insertions(+), 1268 deletions(-) diff --git a/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java b/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java index c741f0f..51322c9 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java +++ b/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java @@ -20,12 +20,11 @@ import dev.hypera.ultrastaffchat.commands.CommandManager; import dev.hypera.ultrastaffchat.config.Config; +import dev.hypera.ultrastaffchat.listeners.ListenerManager; import dev.hypera.ultrastaffchat.objects.ErrorCode; import dev.hypera.ultrastaffchat.utils.Common; import dev.hypera.ultrastaffchat.utils.Debug; -import dev.hypera.ultrastaffchat.listeners.ListenerManager; import dev.hypera.ultrastaffchat.utils.Discord; - import dev.hypera.updatelib.UpdateLibBuilder; import dev.hypera.updatelib.internal.UpdateLib; import net.kyori.adventure.platform.bungeecord.BungeeAudiences; @@ -77,14 +76,7 @@ public void onEnable() { Debug.finishStart(); - Common.log( - "&r &r &r", - "&c __ ___________", - "&c / / / / __/ ___/ &r &c&lUltraStaffChat&r &8BungeeCord", - "&c/ /_/ /\\ \\/ /__&c &r &c &r &8Running Version &c" + getDescription().getVersion(), - "&c\\____/___/\\___/", - "&r &r &r" - ); + Common.log("&r &r &r", "&c __ ___________", "&c / / / / __/ ___/ &r &c&lUltraStaffChat&r &8BungeeCord", "&c/ /_/ /\\ \\/ /__&c &r &c &r &8Running Version &c" + getDescription().getVersion(), "&c\\____/___/\\___/", "&r &r &r"); Common.logPrefix("&aSuccessfully started. &8Took " + Debug.getStartLength() + "ms"); } catch (Exception ex) { Common.error(ErrorCode.UNKNOWN, "Error while starting.", ex); @@ -123,4 +115,5 @@ public UpdateLib getUpdateLib() { throw new IllegalStateException("Cannot retrieve UpdateLib while the plugin is not enabled"); return updateLib; } + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/Command.java b/src/main/java/dev/hypera/ultrastaffchat/commands/Command.java index daae032..982e7e1 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/Command.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/Command.java @@ -19,9 +19,11 @@ package dev.hypera.ultrastaffchat.commands; public abstract class Command extends net.md_5.bungee.api.plugin.Command { - public Command(String name, String permission, String... aliases) { - super(name, permission, aliases); - } - public abstract boolean isDisabled(); + public Command(String name, String permission, String... aliases) { + super(name, permission, aliases); + } + + public abstract boolean isDisabled(); + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/CommandManager.java b/src/main/java/dev/hypera/ultrastaffchat/commands/CommandManager.java index 70ac85f..c433cca 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/CommandManager.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/CommandManager.java @@ -29,22 +29,22 @@ public class CommandManager { - public static void setup() { - Reflections reflections = new Reflections("dev.hypera.ultrastaffchat.commands.impl"); - - Set> allCommands = - reflections.getSubTypesOf(Command.class); - - for (Class commandClass : allCommands) { - try { - Constructor constructor = commandClass.getConstructor(); - constructor.setAccessible(true); - Command c = constructor.newInstance(); - if (!c.isDisabled()) ProxyServer.getInstance().getPluginManager().registerCommand(UltraStaffChat.getInstance(), c); - } catch (Exception ex) { - Common.error(ErrorCode.REGISTER_FAILED_COMMAND, "Failed to register command.", ex); - } - } - } + public static void setup() { + Reflections reflections = new Reflections("dev.hypera.ultrastaffchat.commands.impl"); + + Set> allCommands = reflections.getSubTypesOf(Command.class); + + for(Class commandClass : allCommands) { + try { + Constructor constructor = commandClass.getConstructor(); + constructor.setAccessible(true); + Command c = constructor.newInstance(); + if(!c.isDisabled()) + ProxyServer.getInstance().getPluginManager().registerCommand(UltraStaffChat.getInstance(), c); + } catch (Exception ex) { + Common.error(ErrorCode.REGISTER_FAILED_COMMAND, "Failed to register command.", ex); + } + } + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/MuteStaffChatCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/MuteStaffChatCommand.java index ee0d4ef..c54961e 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/MuteStaffChatCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/MuteStaffChatCommand.java @@ -26,53 +26,55 @@ import net.md_5.bungee.api.CommandSender; public class MuteStaffChatCommand extends Command { - public MuteStaffChatCommand() { - super("mutestaffchat", null, "mutesc"); - } + public MuteStaffChatCommand() { + super("mutestaffchat", null, "mutesc"); - @Override - public void execute(CommandSender sender, String[] args) { + } - Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + @Override + public void execute(CommandSender sender, String[] args) { - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-globalmute"))) { - Common.logPrefix(UltraStaffChat.getConfig().getString("permission-globalmute")); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - if (args.length > 1) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-usage"))); - return; - } + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-globalmute"))) { + Common.logPrefix(UltraStaffChat.getConfig().getString("permission-globalmute")); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } - if (args.length == 0) { - boolean toggled = StaffChat.toggleStaffChatMute(); - if (toggled) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&cmuted"))); - } else - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&aunmuted"))); - return; - } + if(args.length > 1) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-usage"))); + return; + } - if (args[0].matches("(?i:(off|false|disable(d)?|unmute(d)?))")) { - StaffChat.unmuteStaffChat(); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&aunmuted"))); - return; - } + if(args.length == 0) { + boolean toggled = StaffChat.toggleStaffChatMute(); + if(toggled) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&cmuted"))); + } else + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&aunmuted"))); + return; + } - if (args[0].matches("(?i:(on|true|enable(d)?|mute(d)?))")) { - StaffChat.muteStaffChat(); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&cmuted"))); - return; - } + if(args[0].matches("(?i:(off|false|disable(d)?|unmute(d)?))")) { + StaffChat.unmuteStaffChat(); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&aunmuted"))); + return; + } - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-usage"))); - } + if(args[0].matches("(?i:(on|true|enable(d)?|mute(d)?))")) { + StaffChat.muteStaffChat(); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-message").replaceAll("\\{mute}", "&cmuted"))); + return; + } + + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-staffchat-usage"))); + } + + @Override + public boolean isDisabled() { + return !UltraStaffChat.getConfig().getBoolean("mute-staffchat-enabled"); + } - @Override - public boolean isDisabled() { - return !UltraStaffChat.getConfig().getBoolean("mute-staffchat-enabled"); - } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatCommand.java index 67273bd..8e9537e 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatCommand.java @@ -28,41 +28,42 @@ public class StaffChatCommand extends Command { - public StaffChatCommand() { - super("staffchat", null, "sc"); - } + public StaffChatCommand() { + super("staffchat", null, "sc"); + } - @Override - public void execute(CommandSender sender, String[] args) { - Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + @Override + public void execute(CommandSender sender, String[] args) { + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-talk"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-talk"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } - if (args.length == 0) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-usage"))); - return; - } + if(args.length == 0) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-usage"))); + return; + } - if (StaffChat.staffChatIsMuted()) { - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); - return; - } - } + if(StaffChat.staffChatIsMuted()) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); + return; + } + } - StringBuilder message = new StringBuilder(); - for (String word : args) { - message.append(word).append(" "); - } - message.deleteCharAt(message.length() - 1); - StaffChatManager.broadcastMessage(sender, message.toString()); - } + StringBuilder message = new StringBuilder(); + for(String word : args) { + message.append(word).append(" "); + } + message.deleteCharAt(message.length() - 1); + StaffChatManager.broadcastMessage(sender, message.toString()); + } + + @Override + public boolean isDisabled() { + return false; + } - @Override - public boolean isDisabled() { - return false; - } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatMuteCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatMuteCommand.java index ab77c70..f3837a1 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatMuteCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatMuteCommand.java @@ -27,57 +27,59 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; public class StaffChatMuteCommand extends Command { - public StaffChatMuteCommand() { - super("staffchatmute", null, "scmute"); - } - @Override - public void execute(CommandSender sender, String[] args) { - Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + public StaffChatMuteCommand() { + super("staffchatmute", null, "scmute"); + } - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-mute"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } + @Override + public void execute(CommandSender sender, String[] args) { + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - if (!(sender instanceof ProxiedPlayer)) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("ingame-only"))); - return; - } + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-mute"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } - if (args.length > 1) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-usage"))); - return; - } + if(!(sender instanceof ProxiedPlayer)) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("ingame-only"))); + return; + } + if(args.length > 1) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-usage"))); + return; + } - ProxiedPlayer p = (ProxiedPlayer) sender; - if (args.length == 0) { - boolean toggled = StaffChat.toggleMessages(p); - if (toggled) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&cmuted"))); - } else - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&aunmuted"))); - return; - } - if (args[0].matches("(?i:(off|false|mute(d)?|disable(d)?))")) { - StaffChat.enableMessages(p); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&cmuted"))); - return; - } + ProxiedPlayer p = (ProxiedPlayer) sender; + if(args.length == 0) { + boolean toggled = StaffChat.toggleMessages(p); + if(toggled) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&cmuted"))); + } else + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&aunmuted"))); + return; + } - if (args[0].matches("(?i:(on|true|unmute(d)?|enable(d)?))")) { - StaffChat.disableMessages(p); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&aunmuted"))); - return; - } + if(args[0].matches("(?i:(off|false|mute(d)?|disable(d)?))")) { + StaffChat.enableMessages(p); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&cmuted"))); + return; + } - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-usage"))); - } + if(args[0].matches("(?i:(on|true|unmute(d)?|enable(d)?))")) { + StaffChat.disableMessages(p); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-message").replaceAll("\\{mute}", "&aunmuted"))); + return; + } + + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("mute-usage"))); + } + + @Override + public boolean isDisabled() { + return !UltraStaffChat.getConfig().getBoolean("mute-enabled"); + } - @Override - public boolean isDisabled() { - return !UltraStaffChat.getConfig().getBoolean("mute-enabled"); - } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatToggleCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatToggleCommand.java index 170377c..9d3eb83 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatToggleCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffChatToggleCommand.java @@ -27,57 +27,59 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; public class StaffChatToggleCommand extends Command { - public StaffChatToggleCommand() { - super("staffchattoggle", null, "sctoggle"); - } - @Override - public void execute(CommandSender sender, String[] args) { - Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + public StaffChatToggleCommand() { + super("staffchattoggle", null, "sctoggle"); + } - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-toggle"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } + @Override + public void execute(CommandSender sender, String[] args) { + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - if (!(sender instanceof ProxiedPlayer)) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("ingame-only"))); - return; - } + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-toggle"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } - if (args.length > 1) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-usage"))); - return; - } + if(!(sender instanceof ProxiedPlayer)) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("ingame-only"))); + return; + } + if(args.length > 1) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-usage"))); + return; + } - ProxiedPlayer p = (ProxiedPlayer) sender; - if (args.length == 0) { - boolean toggled = StaffChat.toggleChatStaffChat(p); - if (toggled) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&aon"))); - } else - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&coff"))); - return; - } - if (args[0].matches("(?i:(off|false|disable(d)?))")) { - StaffChat.disableChatStaffChat(p); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&coff"))); - return; - } + ProxiedPlayer p = (ProxiedPlayer) sender; + if(args.length == 0) { + boolean toggled = StaffChat.toggleChatStaffChat(p); + if(toggled) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&aon"))); + } else + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&coff"))); + return; + } - if (args[0].matches("(?i:(on|true|enable(d)?))")) { - StaffChat.enableChatStaffChat(p); - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&aon"))); - return; - } + if(args[0].matches("(?i:(off|false|disable(d)?))")) { + StaffChat.disableChatStaffChat(p); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&coff"))); + return; + } - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-usage"))); - } + if(args[0].matches("(?i:(on|true|enable(d)?))")) { + StaffChat.enableChatStaffChat(p); + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-message").replaceAll("\\{toggle}", "&aon"))); + return; + } + + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("toggle-usage"))); + } + + @Override + public boolean isDisabled() { + return !UltraStaffChat.getConfig().getBoolean("toggle-enabled"); + } - @Override - public boolean isDisabled() { - return !UltraStaffChat.getConfig().getBoolean("toggle-enabled"); - } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffListCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffListCommand.java index 09c171e..e827c35 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffListCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffListCommand.java @@ -37,7 +37,7 @@ public StaffListCommand() { public void execute(CommandSender sender, String[] args) { Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - if (!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-list"))) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-list"))) { audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); return; } diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java index 4af5571..477c5e8 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java @@ -43,106 +43,98 @@ public class UltraStaffChatCommand extends Command { - public UltraStaffChatCommand() { - super("ultrastaffchat", null, "usc"); - } - - @Override - public void execute(CommandSender sender, String[] args) { - Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); - - if(args.length > 0) { - if(args[0].matches("(?i:(ab(t|out)|credit(s)?|contibutor(s)?))")) { - audience.sendMessage(Component.text().append(Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), Component.text().content(" was created by:\n").color(NamedTextColor.GRAY).build(), LegacyComponentSerializer.legacyAmpersand().deserialize("&7 - &c" + String.join("\n &7- &c", Common.getContributors())))); - return; - } - - if(args[0].matches("(?i:(r(eload)?))")) { - if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } - - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN))); - - UltraStaffChat.getConfig().reload(); - Discord.reload(); - - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN))); - return; - } - - if(args[0].matches("(?i:(debug))")) { - if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } - - String log; - String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt"; - - boolean advanced = false; - if(args.length > 1) { - for(String arg : args) { - if(arg.matches("(?i:(--a(dv(anced)?)?))")) { - advanced = true; - break; - } - } - } - - if(advanced) { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE))); - log = generateAdvancedDebugLog(); - } else { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating debug log...").color(NamedTextColor.WHITE))); - log = generateDebugLog(); - } - - try { - File logFolder = new File(UltraStaffChat.getInstance().getDataFolder() + "/logs"); - if(!logFolder.exists()) - logFolder.mkdirs(); - - File logFile = new File(logFolder + "/" + filename); - if(logFile.createNewFile()) { - FileWriter logWriter = new FileWriter(logFile); - logWriter.write(log); - logWriter.close(); - } else { - throw new Exception(); - } - - String pasteURL = PasteUtils.createPaste(log); - if(pasteURL == null) - throw new Exception(); - if(advanced) { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))))); - } else { - audience.sendMessage(Component.text().append(LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)))); - } - } catch (Exception e) { - Common.logPrefix("Failed to generate debug log!"); - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Failed to generate debug log").color(NamedTextColor.RED))); - return; - } - } - } - - audience.sendMessage(Component.text() - .append( - Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), - Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), - Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), - Component.text().content("\nSupport").color(NamedTextColor.RED).build(), - Component.text().content(": ").color(NamedTextColor.GRAY).build(), - Component.text().content("https://discord.hypera.dev").color(NamedTextColor.AQUA).clickEvent(ClickEvent.openUrl("https://discord.hypera.dev")).build() - ) - ); - } - - @Override - public boolean isDisabled() { - return false; - } + public UltraStaffChatCommand() { + super("ultrastaffchat", null, "usc"); + } + + @Override + public void execute(CommandSender sender, String[] args) { + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + + if(args.length > 0) { + if(args[0].matches("(?i:(ab(t|out)|credit(s)?|contibutor(s)?))")) { + audience.sendMessage(Component.text().append(Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), Component.text().content(" was created by:\n").color(NamedTextColor.GRAY).build(), LegacyComponentSerializer.legacyAmpersand().deserialize("&7 - &c" + String.join("\n &7- &c", Common.getContributors())))); + return; + } + + if(args[0].matches("(?i:(r(eload)?))")) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN))); + + UltraStaffChat.getConfig().reload(); + Discord.reload(); + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN))); + return; + } + + if(args[0].matches("(?i:(debug))")) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + String log; + String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt"; + + boolean advanced = false; + if(args.length > 1) { + for(String arg : args) { + if(arg.matches("(?i:(--a(dv(anced)?)?))")) { + advanced = true; + break; + } + } + } + + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE))); + log = generateAdvancedDebugLog(); + } else { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating debug log...").color(NamedTextColor.WHITE))); + log = generateDebugLog(); + } + + try { + File logFolder = new File(UltraStaffChat.getInstance().getDataFolder() + "/logs"); + if(!logFolder.exists()) + logFolder.mkdirs(); + + File logFile = new File(logFolder + "/" + filename); + if(logFile.createNewFile()) { + FileWriter logWriter = new FileWriter(logFile); + logWriter.write(log); + logWriter.close(); + } else { + throw new Exception(); + } + + String pasteURL = PasteUtils.createPaste(log); + if(pasteURL == null) + throw new Exception(); + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))))); + } else { + audience.sendMessage(Component.text().append(LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)))); + } + } catch (Exception e) { + Common.logPrefix("Failed to generate debug log!"); + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Failed to generate debug log").color(NamedTextColor.RED))); + return; + } + } + } + + audience.sendMessage(Component.text().append(Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD).clickEvent(ClickEvent.openUrl("https://www.spigotmc.org/resources/" + Common.getResourceId() + "/")), Component.text().content(" Version ").color(NamedTextColor.GRAY).build(), Component.text().content(UltraStaffChat.getInstance().getDescription().getVersion()).color(NamedTextColor.RED).build(), Component.text().content("\nSupport").color(NamedTextColor.RED).build(), Component.text().content(": ").color(NamedTextColor.GRAY).build(), Component.text().content("https://discord.hypera.dev").color(NamedTextColor.AQUA).clickEvent(ClickEvent.openUrl("https://discord.hypera.dev")).build())); + } + + @Override + public boolean isDisabled() { + return false; + } + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/config/Config.java b/src/main/java/dev/hypera/ultrastaffchat/config/Config.java index 9f1e813..11a8c4a 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/config/Config.java +++ b/src/main/java/dev/hypera/ultrastaffchat/config/Config.java @@ -72,27 +72,32 @@ public boolean reload() { } public String getString(String path) { - if(null == configuration) return null; + if(null == configuration) + return null; return configuration.getString(path); } public Boolean getBoolean(String path) { - if(null == configuration) return null; + if(null == configuration) + return null; return configuration.getBoolean(path); } public Integer getInteger(String path) { - if(null == configuration) return null; + if(null == configuration) + return null; return configuration.getInt(path); } public List getStringList(String path) { - if(null == configuration) return null; + if(null == configuration) + return null; return configuration.getStringList(path); } public int getVersion() { - if(null == configuration) return -1; + if(null == configuration) + return -1; return configuration.getInt("version"); } @@ -107,5 +112,5 @@ public Configuration getConfiguration() { public File getConfigurationFile() { return configurationFile; } - + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffChatEvent.java b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffChatEvent.java index 1cd1810..1a9f232 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffChatEvent.java +++ b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffChatEvent.java @@ -24,41 +24,41 @@ public class StaffChatEvent extends Event implements Cancellable { - private final CommandSender sender; - private final CommandSender receiver; - private String message; - private boolean cancelled = false; + private final CommandSender sender; + private final CommandSender receiver; + private String message; + private boolean cancelled = false; - public StaffChatEvent(CommandSender sender, CommandSender receiver, String message) { - this.sender = sender; - this.receiver = receiver; - this.message = message; - } + public StaffChatEvent(CommandSender sender, CommandSender receiver, String message) { + this.sender = sender; + this.receiver = receiver; + this.message = message; + } - public CommandSender getSender() { - return sender; - } + public CommandSender getSender() { + return sender; + } - public CommandSender getReceiver() { - return receiver; - } + public CommandSender getReceiver() { + return receiver; + } - public String getMessage() { - return message; - } + public String getMessage() { + return message; + } - public void setMessage(String message) { - this.message = message; - } + public void setMessage(String message) { + this.message = message; + } - @Override - public boolean isCancelled() { - return cancelled; - } + @Override + public boolean isCancelled() { + return cancelled; + } - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffJoinEvent.java b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffJoinEvent.java index 71b0fee..ff5a7e5 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffJoinEvent.java +++ b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffJoinEvent.java @@ -23,24 +23,25 @@ import net.md_5.bungee.api.plugin.Cancellable; /** - * Called when a player with the permission `staffchat.join` joins the proxy. When cancelled, the staff join message is not shown. + * Called when a player with the permission `staffchat.join` joins the proxy. When cancelled, the staff join message is + * not shown. */ public class StaffJoinEvent extends PostLoginEvent implements Cancellable { - private boolean cancelled = false; + private boolean cancelled = false; - public StaffJoinEvent(ProxiedPlayer player) { - super(player); - } + public StaffJoinEvent(ProxiedPlayer player) { + super(player); + } - @Override - public boolean isCancelled() { - return cancelled; - } + @Override + public boolean isCancelled() { + return cancelled; + } - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffLeaveEvent.java b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffLeaveEvent.java index 24ec3a0..61040f1 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffLeaveEvent.java +++ b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffLeaveEvent.java @@ -23,24 +23,25 @@ import net.md_5.bungee.api.plugin.Cancellable; /** - * Called when a player with the permission `staffchat.leave` leaves the proxy. When cancelled, the staff leave message is not shown. + * Called when a player with the permission `staffchat.leave` leaves the proxy. When cancelled, the staff leave message + * is not shown. */ public class StaffLeaveEvent extends PlayerDisconnectEvent implements Cancellable { - private boolean cancelled = false; + private boolean cancelled = false; - public StaffLeaveEvent(ProxiedPlayer player) { - super(player); - } + public StaffLeaveEvent(ProxiedPlayer player) { + super(player); + } - @Override - public boolean isCancelled() { - return cancelled; - } + @Override + public boolean isCancelled() { + return cancelled; + } - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffSwitchServerEvent.java b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffSwitchServerEvent.java index b648a59..441c8fa 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffSwitchServerEvent.java +++ b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffSwitchServerEvent.java @@ -24,37 +24,37 @@ public class StaffSwitchServerEvent extends Event implements Cancellable { - private final ProxiedPlayer player; - private final String from; - private final String to; - private boolean cancelled = false; - - public StaffSwitchServerEvent(ProxiedPlayer player, String from, String to) { - this.player = player; - this.from = from; - this.to = to; - } - - public ProxiedPlayer getPlayer() { - return player; - } - - public String getFrom() { - return from; - } - - public String getTo() { - return to; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } + private final ProxiedPlayer player; + private final String from; + private final String to; + private boolean cancelled = false; + + public StaffSwitchServerEvent(ProxiedPlayer player, String from, String to) { + this.player = player; + this.from = from; + this.to = to; + } + + public ProxiedPlayer getPlayer() { + return player; + } + + public String getFrom() { + return from; + } + + public String getTo() { + return to; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffToggleAFKEvent.java b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffToggleAFKEvent.java index 6df8c40..a1196fd 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffToggleAFKEvent.java +++ b/src/main/java/dev/hypera/ultrastaffchat/events/staff/StaffToggleAFKEvent.java @@ -24,31 +24,31 @@ public class StaffToggleAFKEvent extends Event implements Cancellable { - private final ProxiedPlayer player; - private final boolean afk; - private boolean cancelled = false; - - public StaffToggleAFKEvent(ProxiedPlayer player, boolean afk) { - this.player = player; - this.afk = afk; - } - - public ProxiedPlayer getPlayer() { - return player; - } - - public boolean isAfk() { - return afk; - } - - @Override - public boolean isCancelled() { - return cancelled; - } - - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } + private final ProxiedPlayer player; + private final boolean afk; + private boolean cancelled = false; + + public StaffToggleAFKEvent(ProxiedPlayer player, boolean afk) { + this.player = player; + this.afk = afk; + } + + public ProxiedPlayer getPlayer() { + return player; + } + + public boolean isAfk() { + return afk; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/Listener.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/Listener.java index c3a70eb..45617a3 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/Listener.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/Listener.java @@ -20,8 +20,8 @@ public class Listener implements net.md_5.bungee.api.plugin.Listener { - public Listener() { + public Listener() { - } + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/ListenerManager.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/ListenerManager.java index 64d71ee..8b636f3 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/ListenerManager.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/ListenerManager.java @@ -30,21 +30,20 @@ public class ListenerManager { - public static void setup() { - Reflections reflections = new Reflections("dev.hypera.ultrastaffchat.listeners.impl"); - - Set> allListeners = - reflections.getSubTypesOf(Listener.class); - - for (Class listenerClass : allListeners) { - try { - Constructor constructor = listenerClass.getConstructor(); - constructor.setAccessible(true); - ProxyServer.getInstance().getPluginManager().registerListener(UltraStaffChat.getInstance(), constructor.newInstance()); - } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) { - Common.error(ErrorCode.REGISTER_FAILED_LISTENER, "Failed to register listener.", ex); - } - } - } + public static void setup() { + Reflections reflections = new Reflections("dev.hypera.ultrastaffchat.listeners.impl"); + + Set> allListeners = reflections.getSubTypesOf(Listener.class); + + for(Class listenerClass : allListeners) { + try { + Constructor constructor = listenerClass.getConstructor(); + constructor.setAccessible(true); + ProxyServer.getInstance().getPluginManager().registerListener(UltraStaffChat.getInstance(), constructor.newInstance()); + } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) { + Common.error(ErrorCode.REGISTER_FAILED_LISTENER, "Failed to register listener.", ex); + } + } + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java index 7f2b993..295ab05 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java @@ -29,40 +29,42 @@ public class ChatListener extends Listener { - @EventHandler - public void onChat(ChatEvent e) { - if (e.isCommand() || e.isProxyCommand()) return; - if ((e.getSender() instanceof ProxiedPlayer) && StaffChat.hasChatStaffChatEnabled((ProxiedPlayer) e.getSender())) { - e.setCancelled(true); - if (StaffChat.staffChatIsMuted()) { - if (!((ProxiedPlayer) e.getSender()).hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { - UltraStaffChat.getInstance().getAdventure().sender((ProxiedPlayer) e.getSender()).sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); - return; - } - } - StaffChatManager.broadcastMessage((ProxiedPlayer) e.getSender(), e.getMessage()); - return; - } + @EventHandler + public void onChat(ChatEvent e) { + if(e.isCommand() || e.isProxyCommand()) + return; + if((e.getSender() instanceof ProxiedPlayer) && StaffChat.hasChatStaffChatEnabled((ProxiedPlayer) e.getSender())) { + e.setCancelled(true); + if(StaffChat.staffChatIsMuted()) { + if(!((ProxiedPlayer) e.getSender()).hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { + UltraStaffChat.getInstance().getAdventure().sender((ProxiedPlayer) e.getSender()).sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); + return; + } + } + StaffChatManager.broadcastMessage((ProxiedPlayer) e.getSender(), e.getMessage()); + return; + } - if (!(e.getSender() instanceof ProxiedPlayer)) return; + if(!(e.getSender() instanceof ProxiedPlayer)) + return; - ProxiedPlayer p = (ProxiedPlayer) e.getSender(); + ProxiedPlayer p = (ProxiedPlayer) e.getSender(); - if (p.hasPermission(UltraStaffChat.getConfig().getString("permission-talk"))) { - String prefix = UltraStaffChat.getConfig().getString("staffchat-prefix"); - if (prefix != "" && e.getMessage().startsWith(prefix)) { - e.setCancelled(true); - if (StaffChat.staffChatIsMuted()) { - if (!((ProxiedPlayer) e.getSender()).hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { - UltraStaffChat.getInstance().getAdventure().sender((ProxiedPlayer) e.getSender()).sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); - return; - } - } - String message = e.getMessage().substring(prefix.length()); - StaffChatManager.broadcastMessage((ProxiedPlayer) e.getSender(), message); - e.setCancelled(true); - } - } - } + if(p.hasPermission(UltraStaffChat.getConfig().getString("permission-talk"))) { + String prefix = UltraStaffChat.getConfig().getString("staffchat-prefix"); + if(prefix != "" && e.getMessage().startsWith(prefix)) { + e.setCancelled(true); + if(StaffChat.staffChatIsMuted()) { + if(!((ProxiedPlayer) e.getSender()).hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { + UltraStaffChat.getInstance().getAdventure().sender((ProxiedPlayer) e.getSender()).sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("staffchat-muted"))); + return; + } + } + String message = e.getMessage().substring(prefix.length()); + StaffChatManager.broadcastMessage((ProxiedPlayer) e.getSender(), message); + e.setCancelled(true); + } + } + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/JoinLeaveListener.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/JoinLeaveListener.java index 0eb723f..f0f8502 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/JoinLeaveListener.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/JoinLeaveListener.java @@ -31,26 +31,26 @@ public class JoinLeaveListener extends Listener { - @EventHandler - public void onPostJoin(PostLoginEvent event) { - if (Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-join"))) { - // We have to delay the broadcast so we can display the server they joined. - ProxyServer.getInstance().getScheduler().schedule(UltraStaffChat.getInstance(), () -> StaffChatManager.broadcastJoin(event.getPlayer()), 3, TimeUnit.SECONDS); - } - - if(UltraStaffChat.getConfig().getBoolean("update-enabled") && Common.hasPermission(event.getPlayer(), Common.messageRaw("permission-update"))) { - if(null != UltraStaffChat.getInstance().getUpdateLib().getLastResponse() && UltraStaffChat.getInstance().getUpdateLib().getLastResponse().isUpdateAvailable()) { - UltraStaffChat.getInstance().getAdventure().player(event.getPlayer()).sendMessage(Common.adventurise(Common.messageRaw("update-message").replace("{version}", UltraStaffChat.getInstance().getUpdateLib().getLastResponse().getSpigotVersion()).replace("{current}", UltraStaffChat.getInstance().getDescription().getVersion()))); - } - } - - } - - @EventHandler - public void onDisconnect(PlayerDisconnectEvent event) { - if (Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-leave"))) { - StaffChatManager.broadcastLeave(event.getPlayer()); - } - } + @EventHandler + public void onPostJoin(PostLoginEvent event) { + if(Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-join"))) { + // We have to delay the broadcast so we can display the server they joined. + ProxyServer.getInstance().getScheduler().schedule(UltraStaffChat.getInstance(), () -> StaffChatManager.broadcastJoin(event.getPlayer()), 3, TimeUnit.SECONDS); + } + + if(UltraStaffChat.getConfig().getBoolean("update-enabled") && Common.hasPermission(event.getPlayer(), Common.messageRaw("permission-update"))) { + if(null != UltraStaffChat.getInstance().getUpdateLib().getLastResponse() && UltraStaffChat.getInstance().getUpdateLib().getLastResponse().isUpdateAvailable()) { + UltraStaffChat.getInstance().getAdventure().player(event.getPlayer()).sendMessage(Common.adventurise(Common.messageRaw("update-message").replace("{version}", UltraStaffChat.getInstance().getUpdateLib().getLastResponse().getSpigotVersion()).replace("{current}", UltraStaffChat.getInstance().getDescription().getVersion()))); + } + } + + } + + @EventHandler + public void onDisconnect(PlayerDisconnectEvent event) { + if(Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-leave"))) { + StaffChatManager.broadcastLeave(event.getPlayer()); + } + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/SwitchListener.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/SwitchListener.java index f0ed274..ee95f67 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/SwitchListener.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/SwitchListener.java @@ -27,14 +27,15 @@ public class SwitchListener extends Listener { - @EventHandler - public void onServerSwitch(ServerSwitchEvent event) { - if (Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-switch"))) { - try { - StaffChatManager.broadcastSwitch(event.getPlayer(), event.getFrom().getName(), event.getPlayer().getServer().getInfo().getName()); - } catch (Exception e) { - // Ignore exception, its just a null pointer because the user joined not switched - } - } - } + @EventHandler + public void onServerSwitch(ServerSwitchEvent event) { + if(Common.hasPermission(event.getPlayer(), UltraStaffChat.getConfig().getString("permission-switch"))) { + try { + StaffChatManager.broadcastSwitch(event.getPlayer(), event.getFrom().getName(), event.getPlayer().getServer().getInfo().getName()); + } catch (Exception e) { + // Ignore exception, its just a null pointer because the user joined not switched + } + } + } + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java b/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java index f09ecee..d201ede 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java +++ b/src/main/java/dev/hypera/ultrastaffchat/managers/StaffChatManager.java @@ -19,7 +19,11 @@ package dev.hypera.ultrastaffchat.managers; import dev.hypera.ultrastaffchat.UltraStaffChat; -import dev.hypera.ultrastaffchat.events.staff.*; +import dev.hypera.ultrastaffchat.events.staff.StaffChatEvent; +import dev.hypera.ultrastaffchat.events.staff.StaffJoinEvent; +import dev.hypera.ultrastaffchat.events.staff.StaffLeaveEvent; +import dev.hypera.ultrastaffchat.events.staff.StaffSwitchServerEvent; +import dev.hypera.ultrastaffchat.events.staff.StaffToggleAFKEvent; import dev.hypera.ultrastaffchat.utils.Discord; import dev.hypera.ultrastaffchat.utils.StaffChat; import net.kyori.adventure.audience.Audience; @@ -34,8 +38,10 @@ public class StaffChatManager { public static void broadcastMessage(CommandSender sender, String message) { UltraStaffChat.getInstance().getAdventure().filter(person -> { - if (!person.hasPermission(messageRaw("permission-read"))) return false; - if (person instanceof ProxiedPlayer && StaffChat.hasMessagesDisabled((ProxiedPlayer) person)) return false; + if(!person.hasPermission(messageRaw("permission-read"))) + return false; + if(person instanceof ProxiedPlayer && StaffChat.hasMessagesDisabled((ProxiedPlayer) person)) + return false; StaffChatEvent staffChatEvent = new StaffChatEvent(sender, person, message); ProxyServer.getInstance().getPluginManager().callEvent(staffChatEvent); return !staffChatEvent.isCancelled(); @@ -44,13 +50,15 @@ public static void broadcastMessage(CommandSender sender, String message) { } public static void broadcastAFK(ProxiedPlayer player, boolean afk) { - if(!UltraStaffChat.getConfig().getBoolean("afk-enabled")) return; + if(!UltraStaffChat.getConfig().getBoolean("afk-enabled")) + return; StaffToggleAFKEvent staffToggleAFKEvent = new StaffToggleAFKEvent(player, afk); ProxyServer.getInstance().getPluginManager().callEvent(staffToggleAFKEvent); - if (staffToggleAFKEvent.isCancelled()) return; + if(staffToggleAFKEvent.isCancelled()) + return; Audience audience = UltraStaffChat.getInstance().getAdventure().permission(messageRaw("permission-afk")); audience.sendMessage(adventurise((afk ? messageRaw("afk-broadcast") : messageRaw("no-afk-broadcast")).replace("{player}", player.getName()))); - if (afk) { + if(afk) { Discord.broadcastDiscordAFKEnable(player); } else { Discord.broadcastDiscordAFKDisable(player); @@ -58,30 +66,36 @@ public static void broadcastAFK(ProxiedPlayer player, boolean afk) { } public static void broadcastJoin(ProxiedPlayer player) { - if(!UltraStaffChat.getConfig().getBoolean("join-enabled")) return; + if(!UltraStaffChat.getConfig().getBoolean("join-enabled")) + return; StaffJoinEvent staffJoinEvent = new StaffJoinEvent(player); ProxyServer.getInstance().getPluginManager().callEvent(staffJoinEvent); - if (staffJoinEvent.isCancelled()) return; + if(staffJoinEvent.isCancelled()) + return; Audience audience = UltraStaffChat.getInstance().getAdventure().permission(messageRaw("permission-join")); audience.sendMessage(adventurise(messageRaw("join-message").replace("{player}", player.getName()).replace("{server}", getServerSafe(player)))); Discord.broadcastDiscordJoin(player); } public static void broadcastLeave(ProxiedPlayer player) { - if(!UltraStaffChat.getConfig().getBoolean("leave-enabled")) return; + if(!UltraStaffChat.getConfig().getBoolean("leave-enabled")) + return; StaffLeaveEvent staffLeaveEvent = new StaffLeaveEvent(player); ProxyServer.getInstance().getPluginManager().callEvent(staffLeaveEvent); - if (staffLeaveEvent.isCancelled()) return; + if(staffLeaveEvent.isCancelled()) + return; Audience audience = UltraStaffChat.getInstance().getAdventure().permission(messageRaw("permission-leave")); audience.sendMessage(adventurise(messageRaw("leave-message").replace("{player}", player.getName()))); Discord.broadcastDiscordLeave(player); } public static void broadcastSwitch(ProxiedPlayer player, String from, String to) { - if(!UltraStaffChat.getConfig().getBoolean("switch-enabled")) return; + if(!UltraStaffChat.getConfig().getBoolean("switch-enabled")) + return; StaffSwitchServerEvent staffSwitchServerEvent = new StaffSwitchServerEvent(player, from, to); ProxyServer.getInstance().getPluginManager().callEvent(staffSwitchServerEvent); - if (staffSwitchServerEvent.isCancelled()) return; + if(staffSwitchServerEvent.isCancelled()) + return; Audience audience = UltraStaffChat.getInstance().getAdventure().permission(messageRaw("permission-switch")); Discord.broadcastDiscordSwitch(player, from, to); audience.sendMessage(adventurise(messageRaw("switch-message").replace("{player}", player.getName()).replace("{from}", from).replace("{to}", to))); diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/Common.java b/src/main/java/dev/hypera/ultrastaffchat/utils/Common.java index bae116b..aa3232c 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/Common.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/Common.java @@ -41,15 +41,11 @@ public class Common { private static final int bStatsId = 5040; private static final long resourceId = 68956; private static final int configVersion = 36; - private static final String[] contributors = new String[] { - "Joshua Sing", - "Christian F", - "SLLCoding", - "Hunter Y" - }; + private static final String[] contributors = new String[] { "Joshua Sing", "Christian F", "SLLCoding", "Hunter Y" }; /** * Logs messages to console. + * * @param messages The messages to be logged. */ public static void log(String... messages) { @@ -59,6 +55,7 @@ public static void log(String... messages) { /** * Logs messages to console with a prefix. + * * @param messages The messages to be logged. */ public static void logPrefix(String... messages) { @@ -68,8 +65,9 @@ public static void logPrefix(String... messages) { /** * Logs an error log to console. + * * @param errorCode Error code. - * @param message Any information about the error. + * @param message Any information about the error. * @param throwable Throwable. */ public static void error(ErrorCode errorCode, String message, Throwable throwable) { @@ -89,7 +87,9 @@ public static void error(ErrorCode errorCode, String message, Throwable throwabl /** * Colourises a message and converts it to a BaseComponent. + * * @param message The message to be colourised. + * * @return BaseComponent Colourised message. */ public static BaseComponent[] colour(String message) { @@ -98,7 +98,9 @@ public static BaseComponent[] colour(String message) { /** * Colourises a message. + * * @param message The message to be colourised. + * * @return Colourised message. */ public static String basicColour(String message) { @@ -107,8 +109,10 @@ public static String basicColour(String message) { /** * Checks if a CommandSender has a permission. - * @param sender The CommandSender to be checked. + * + * @param sender The CommandSender to be checked. * @param permission The permission. + * * @return If the CommandSender has the permission. */ public static boolean hasPermission(CommandSender sender, String permission) { @@ -117,6 +121,7 @@ public static boolean hasPermission(CommandSender sender, String permission) { /** * Get SpigotMC Resource ID. + * * @return Resource ID. */ public static long getResourceId() { @@ -125,6 +130,7 @@ public static long getResourceId() { /** * Get bStats ID. + * * @return bStats ID. */ public static int getbStatsId() { @@ -133,6 +139,7 @@ public static int getbStatsId() { /** * Get current configuration version. + * * @return Current config version. */ public static int getConfigVersion() { @@ -141,6 +148,7 @@ public static int getConfigVersion() { /** * Returns a list of people that have contributed to UltraStaffChat. + * * @return contributors. */ public static String[] getContributors() { @@ -149,6 +157,7 @@ public static String[] getContributors() { /** * Get a Component message from the config. + * * @return (Adventure Component) message. */ public static Component message(String key) { @@ -157,6 +166,7 @@ public static Component message(String key) { /** * Turns a message into an Adventure Component. + * * @return (Adventure Component) message. */ public static Component adventurise(String message) { @@ -165,17 +175,19 @@ public static Component adventurise(String message) { /** * Get a raw message from the config. + * * @return (String) message. */ public static String messageRaw(String key) { Config config = UltraStaffChat.getConfig(); String message = config.getString(key); - if (message == null) return "&7undefined"; + if(message == null) + return "&7undefined"; return message; } public static String getDisplayNameSafe(CommandSender s) { - if ((s instanceof ProxiedPlayer)) { + if((s instanceof ProxiedPlayer)) { return ((ProxiedPlayer) s).getDisplayName(); } else { return "Console"; @@ -183,10 +195,11 @@ public static String getDisplayNameSafe(CommandSender s) { } public static String getServerSafe(CommandSender s) { - if ((s instanceof ProxiedPlayer)) { + if((s instanceof ProxiedPlayer)) { return ((ProxiedPlayer) s).getServer().getInfo().getName(); } else { return "Undefined"; } } + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java b/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java index 123dd02..5162d4c 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java @@ -25,271 +25,279 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.config.Configuration; -import java.awt.*; +import java.awt.Color; import java.io.IOException; public class Discord { - private static final String footer = "UltraStaffChat - Bungeecord v" + UltraStaffChat.getInstance().getDescription().getVersion(); - private static final String footerUrl = "https://i.hypera.dev/assets/hypera-icon-white.png"; - private static boolean enabled = false; - private static String hookURL = ""; - - public static void setup() { - if (!UltraStaffChat.getConfig().getBoolean("discord-enabled").equals(true)) return; - if (UltraStaffChat.getConfig().getString("discord-webhook") == null || UltraStaffChat.getConfig().getString("discord-webhook").contains("XXXXXXXXXXXXXXXXXX")) { - Common.logPrefix("&cError: Discord messages are enabled but the webhook URL has not been configured!"); - return; - } - - hookURL = UltraStaffChat.getConfig().getString("discord-webhook"); - enabled = true; - } - - public static void reload() { - enabled = false; - setup(); - } - - public static boolean isEnabled() { - return enabled; - } - - public static void broadcastDiscordJoin(ProxiedPlayer p) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format"); - hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); - hook.execute(); - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(join_leavePlaceholders(embedFormat.getString("description"), p)); - embed.setTitle(join_leavePlaceholders(embedFormat.getString("title"), p)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format.embed.fields." + key); - embed.addField(join_leavePlaceholders(sec.getString("name"), p), join_leavePlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - public static void broadcastDiscordLeave(ProxiedPlayer p) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format"); - hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); - hook.execute(); - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(join_leavePlaceholders(embedFormat.getString("description"), p)); - embed.setTitle(join_leavePlaceholders(embedFormat.getString("title"), p)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format.embed.fields." + key); - embed.addField(join_leavePlaceholders(sec.getString("name"), p), join_leavePlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - public static void broadcastDiscordSwitch(ProxiedPlayer p, String from, String to) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format"); - hook.setContent(switchPlaceholders(textFormat.getString("text"), p, from, to)); - try { - hook.execute(); - } catch (IOException e) { - Common.log(e.toString()); - } - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(switchPlaceholders(embedFormat.getString("description"), p, from, to)); - embed.setTitle(switchPlaceholders(embedFormat.getString("title"), p, from, to)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format.embed.fields." + key); - embed.addField(switchPlaceholders(sec.getString("name"), p, from, to), switchPlaceholders(sec.getString("value"), p, from, to), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - public static void broadcastDiscordStaffChatMessage(CommandSender s, String message) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format"); - hook.setContent(messagePlaceholders(textFormat.getString("text"), s, message)); - try { - hook.execute(); - } catch (IOException e) { - Common.log(e.toString()); - } - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(messagePlaceholders(embedFormat.getString("description"), s, message)); - embed.setTitle(messagePlaceholders(embedFormat.getString("title"), s, message)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format.embed.fields." + key); - embed.addField(messagePlaceholders(sec.getString("name"), s, message), messagePlaceholders(sec.getString("value"), s, message), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - public static void broadcastDiscordAFKEnable(ProxiedPlayer p) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format"); - hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); - hook.execute(); - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(afkPlaceholders(embedFormat.getString("description"), p)); - embed.setTitle(afkPlaceholders(embedFormat.getString("title"), p)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format.embed.fields." + key); - embed.addField(afkPlaceholders(sec.getString("name"), p), afkPlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - public static void broadcastDiscordAFKDisable(ProxiedPlayer p) { - if (!isEnabled()) return; - ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { - try { - DiscordWebhook hook = getHook(); - - if (UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { - Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format"); - hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); - hook.execute(); - return; - } - - EmbedObject embed = createEmbed(); - Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format.embed"); - embed.setColor(Color.decode(embedFormat.getString("color"))); - embed.setDescription(afkPlaceholders(embedFormat.getString("description"), p)); - embed.setTitle(afkPlaceholders(embedFormat.getString("title"), p)); - embed.setThumbnail(embedFormat.getString("thumbnail")); - embed.setImage(embedFormat.getString("image")); - embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); - embed.setUrl(embedFormat.getString("url")); - for (String key : embedFormat.getSection("fields").getKeys()) { - Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format.embed.fields." + key); - embed.addField(afkPlaceholders(sec.getString("name"), p), afkPlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); - } - hook.addEmbed(embed); - hook.execute(); - } catch (Exception e) { - Common.log(e.toString()); - } - }); - } - - private static String messagePlaceholders(String string, CommandSender s, String m) { - return string.replaceAll("\\{message}", m).replaceAll("\\{player}", Common.getDisplayNameSafe(s)).replaceAll("\\{server}", Common.getServerSafe(s)); - } - - private static String join_leavePlaceholders(String string, ProxiedPlayer p) { - return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)).replaceAll("\\{server}", Common.getServerSafe(p)); - } - - private static String switchPlaceholders(String string, ProxiedPlayer p, String from, String to) { - return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)).replaceAll("\\{to}", to).replaceAll("\\{from}", from); - } - - private static String afkPlaceholders(String string, ProxiedPlayer p) { - return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)); - } - - private static DiscordWebhook getHook() { - DiscordWebhook hook = new DiscordWebhook(hookURL); - hook.setAvatarUrl(UltraStaffChat.getConfig().getString("discord-image")); - hook.setUsername(UltraStaffChat.getConfig().getString("discord-username")); - return hook; - } - - private static DiscordWebhook.EmbedObject createEmbed() { - return new DiscordWebhook.EmbedObject().setFooter(footer, footerUrl); - } + private static final String footer = "UltraStaffChat - Bungeecord v" + UltraStaffChat.getInstance().getDescription().getVersion(); + private static final String footerUrl = "https://i.hypera.dev/assets/hypera-icon-white.png"; + private static boolean enabled = false; + private static String hookURL = ""; + + public static void setup() { + if(!UltraStaffChat.getConfig().getBoolean("discord-enabled").equals(true)) + return; + if(UltraStaffChat.getConfig().getString("discord-webhook") == null || UltraStaffChat.getConfig().getString("discord-webhook").contains("XXXXXXXXXXXXXXXXXX")) { + Common.logPrefix("&cError: Discord messages are enabled but the webhook URL has not been configured!"); + return; + } + + hookURL = UltraStaffChat.getConfig().getString("discord-webhook"); + enabled = true; + } + + public static void reload() { + enabled = false; + setup(); + } + + public static boolean isEnabled() { + return enabled; + } + + public static void broadcastDiscordJoin(ProxiedPlayer p) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format"); + hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); + hook.execute(); + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(join_leavePlaceholders(embedFormat.getString("description"), p)); + embed.setTitle(join_leavePlaceholders(embedFormat.getString("title"), p)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-join-format.embed.fields." + key); + embed.addField(join_leavePlaceholders(sec.getString("name"), p), join_leavePlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + public static void broadcastDiscordLeave(ProxiedPlayer p) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format"); + hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); + hook.execute(); + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(join_leavePlaceholders(embedFormat.getString("description"), p)); + embed.setTitle(join_leavePlaceholders(embedFormat.getString("title"), p)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-leave-format.embed.fields." + key); + embed.addField(join_leavePlaceholders(sec.getString("name"), p), join_leavePlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + public static void broadcastDiscordSwitch(ProxiedPlayer p, String from, String to) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format"); + hook.setContent(switchPlaceholders(textFormat.getString("text"), p, from, to)); + try { + hook.execute(); + } catch (IOException e) { + Common.log(e.toString()); + } + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(switchPlaceholders(embedFormat.getString("description"), p, from, to)); + embed.setTitle(switchPlaceholders(embedFormat.getString("title"), p, from, to)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-switch-format.embed.fields." + key); + embed.addField(switchPlaceholders(sec.getString("name"), p, from, to), switchPlaceholders(sec.getString("value"), p, from, to), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + public static void broadcastDiscordStaffChatMessage(CommandSender s, String message) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format"); + hook.setContent(messagePlaceholders(textFormat.getString("text"), s, message)); + try { + hook.execute(); + } catch (IOException e) { + Common.log(e.toString()); + } + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(messagePlaceholders(embedFormat.getString("description"), s, message)); + embed.setTitle(messagePlaceholders(embedFormat.getString("title"), s, message)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-format.embed.fields." + key); + embed.addField(messagePlaceholders(sec.getString("name"), s, message), messagePlaceholders(sec.getString("value"), s, message), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + public static void broadcastDiscordAFKEnable(ProxiedPlayer p) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format"); + hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); + hook.execute(); + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(afkPlaceholders(embedFormat.getString("description"), p)); + embed.setTitle(afkPlaceholders(embedFormat.getString("title"), p)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-enable-format.embed.fields." + key); + embed.addField(afkPlaceholders(sec.getString("name"), p), afkPlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + public static void broadcastDiscordAFKDisable(ProxiedPlayer p) { + if(!isEnabled()) + return; + ProxyServer.getInstance().getScheduler().runAsync(UltraStaffChat.getInstance(), () -> { + try { + DiscordWebhook hook = getHook(); + + if(UltraStaffChat.getConfig().getBoolean("discord-embed").equals(false)) { + Configuration textFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format"); + hook.setContent(join_leavePlaceholders(textFormat.getString("text"), p)); + hook.execute(); + return; + } + + EmbedObject embed = createEmbed(); + Configuration embedFormat = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format.embed"); + embed.setColor(Color.decode(embedFormat.getString("color"))); + embed.setDescription(afkPlaceholders(embedFormat.getString("description"), p)); + embed.setTitle(afkPlaceholders(embedFormat.getString("title"), p)); + embed.setThumbnail(embedFormat.getString("thumbnail")); + embed.setImage(embedFormat.getString("image")); + embed.setAuthor(embedFormat.getString("author.name"), embedFormat.getString("author.url"), embedFormat.getString("image")); + embed.setUrl(embedFormat.getString("url")); + for(String key : embedFormat.getSection("fields").getKeys()) { + Configuration sec = UltraStaffChat.getConfig().getConfiguration().getSection("discord-afk-disable-format.embed.fields." + key); + embed.addField(afkPlaceholders(sec.getString("name"), p), afkPlaceholders(sec.getString("value"), p), sec.getBoolean("inline")); + } + hook.addEmbed(embed); + hook.execute(); + } catch (Exception e) { + Common.log(e.toString()); + } + }); + } + + private static String messagePlaceholders(String string, CommandSender s, String m) { + return string.replaceAll("\\{message}", m).replaceAll("\\{player}", Common.getDisplayNameSafe(s)).replaceAll("\\{server}", Common.getServerSafe(s)); + } + + private static String join_leavePlaceholders(String string, ProxiedPlayer p) { + return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)).replaceAll("\\{server}", Common.getServerSafe(p)); + } + + private static String switchPlaceholders(String string, ProxiedPlayer p, String from, String to) { + return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)).replaceAll("\\{to}", to).replaceAll("\\{from}", from); + } + + private static String afkPlaceholders(String string, ProxiedPlayer p) { + return string.replaceAll("\\{player}", Common.getDisplayNameSafe(p)); + } + + private static DiscordWebhook getHook() { + DiscordWebhook hook = new DiscordWebhook(hookURL); + hook.setAvatarUrl(UltraStaffChat.getConfig().getString("discord-image")); + hook.setUsername(UltraStaffChat.getConfig().getString("discord-username")); + return hook; + } + + private static DiscordWebhook.EmbedObject createEmbed() { + return new DiscordWebhook.EmbedObject().setFooter(footer, footerUrl); + } + } diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/DiscordWebhook.java b/src/main/java/dev/hypera/ultrastaffchat/utils/DiscordWebhook.java index 8c8d093..03b0f7e 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/DiscordWebhook.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/DiscordWebhook.java @@ -1,6 +1,5 @@ /** - * Created by k3kdude (https://github.com/k3kdude) - * Original: https://gist.github.com/k3kdude/fba6f6b37594eae3d6f9475330733bdb + * Created by k3kdude (https://github.com/k3kdude) Original: https://gist.github.com/k3kdude/fba6f6b37594eae3d6f9475330733bdb */ package dev.hypera.ultrastaffchat.utils; @@ -19,377 +18,391 @@ /** * Class used to execute Discord Webhooks with low effort */ +@SuppressWarnings("all") public class DiscordWebhook { - private final String url; - private String content; - private String username; - private String avatarUrl; - private boolean tts; - private List embeds = new ArrayList<>(); - - /** - * Constructs a new DiscordWebhook instance - * - * @param url The webhook URL obtained in Discord - */ - public DiscordWebhook(String url) { - this.url = url; - } - - public void setContent(String content) { - this.content = content; - } - - public void setUsername(String username) { - this.username = username; - } - - public void setAvatarUrl(String avatarUrl) { - this.avatarUrl = avatarUrl; - } - - public void setTts(boolean tts) { - this.tts = tts; - } - - public void addEmbed(EmbedObject embed) { - this.embeds.add(embed); - } - - public void execute() throws IOException { - if (this.content == null && this.embeds.isEmpty()) { - throw new IllegalArgumentException("Set content or add at least one EmbedObject"); - } - - JSONObject json = new JSONObject(); - - json.put("content", this.content); - json.put("username", this.username); - json.put("avatar_url", this.avatarUrl); - json.put("tts", this.tts); - - if (!this.embeds.isEmpty()) { - List embedObjects = new ArrayList<>(); - - for (EmbedObject embed : this.embeds) { - JSONObject jsonEmbed = new JSONObject(); - - jsonEmbed.put("title", embed.getTitle()); - jsonEmbed.put("description", embed.getDescription()); - jsonEmbed.put("url", embed.getUrl()); - - if (embed.getColor() != null) { - Color color = embed.getColor(); - int rgb = color.getRed(); - rgb = (rgb << 8) + color.getGreen(); - rgb = (rgb << 8) + color.getBlue(); - - jsonEmbed.put("color", rgb); - } - - EmbedObject.Footer footer = embed.getFooter(); - EmbedObject.Image image = embed.getImage(); - EmbedObject.Thumbnail thumbnail = embed.getThumbnail(); - EmbedObject.Author author = embed.getAuthor(); - List fields = embed.getFields(); - - if (footer != null) { - JSONObject jsonFooter = new JSONObject(); - - jsonFooter.put("text", footer.getText()); - jsonFooter.put("icon_url", footer.getIconUrl()); - jsonEmbed.put("footer", jsonFooter); - } - - if (image != null) { - JSONObject jsonImage = new JSONObject(); - - jsonImage.put("url", image.getUrl()); - jsonEmbed.put("image", jsonImage); - } - - if (thumbnail != null) { - JSONObject jsonThumbnail = new JSONObject(); - - jsonThumbnail.put("url", thumbnail.getUrl()); - jsonEmbed.put("thumbnail", jsonThumbnail); - } - - if (author != null) { - JSONObject jsonAuthor = new JSONObject(); - - jsonAuthor.put("name", author.getName()); - jsonAuthor.put("url", author.getUrl()); - jsonAuthor.put("icon_url", author.getIconUrl()); - jsonEmbed.put("author", jsonAuthor); - } - - List jsonFields = new ArrayList<>(); - for (EmbedObject.Field field : fields) { - JSONObject jsonField = new JSONObject(); - - jsonField.put("name", field.getName()); - jsonField.put("value", field.getValue()); - jsonField.put("inline", field.isInline()); - - jsonFields.add(jsonField); - } - - jsonEmbed.put("fields", jsonFields.toArray()); - embedObjects.add(jsonEmbed); - } - - json.put("embeds", embedObjects.toArray()); - } - - URL url = new URL(this.url); - HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.addRequestProperty("Content-Type", "application/json"); - connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_"); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - - OutputStream stream = connection.getOutputStream(); - stream.write(json.toString().getBytes()); - stream.flush(); - stream.close(); - - connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream - connection.disconnect(); - } - - public static class EmbedObject { - private String title; - private String description; - private String url; - private Color color; - - private Footer footer; - private Thumbnail thumbnail; - private Image image; - private Author author; - private List fields = new ArrayList<>(); - - public String getTitle() { - return title; - } - - public String getDescription() { - return description; - } - - public String getUrl() { - return url; - } - - public Color getColor() { - return color; - } - - public Footer getFooter() { - return footer; - } - - public Thumbnail getThumbnail() { - return thumbnail; - } - - public Image getImage() { - return image; - } - - public Author getAuthor() { - return author; - } - - public List getFields() { - return fields; - } - - public EmbedObject setTitle(String title) { - this.title = title; - return this; - } - - public EmbedObject setDescription(String description) { - this.description = description; - return this; - } - - public EmbedObject setUrl(String url) { - this.url = url; - return this; - } - - public EmbedObject setColor(Color color) { - this.color = color; - return this; - } - - public EmbedObject setFooter(String text, String icon) { - this.footer = new Footer(text, icon); - return this; - } - - public EmbedObject setThumbnail(String url) { - this.thumbnail = new Thumbnail(url); - return this; - } - - public EmbedObject setImage(String url) { - this.image = new Image(url); - return this; - } - - public EmbedObject setAuthor(String name, String url, String icon) { - this.author = new Author(name, url, icon); - return this; - } - - public EmbedObject addField(String name, String value, boolean inline) { - this.fields.add(new Field(name, value, inline)); - return this; - } - - private class Footer { - private String text; - private String iconUrl; - - private Footer(String text, String iconUrl) { - this.text = text; - this.iconUrl = iconUrl; - } - - private String getText() { - return text; - } - - private String getIconUrl() { - return iconUrl; - } - } - - private class Thumbnail { - private String url; - - private Thumbnail(String url) { - this.url = url; - } - - private String getUrl() { - return url; - } - } - - private class Image { - private String url; - - private Image(String url) { - this.url = url; - } - - private String getUrl() { - return url; - } - } - - private class Author { - private String name; - private String url; - private String iconUrl; - - private Author(String name, String url, String iconUrl) { - this.name = name; - this.url = url; - this.iconUrl = iconUrl; - } - - private String getName() { - return name; - } - - private String getUrl() { - return url; - } - - private String getIconUrl() { - return iconUrl; - } - } - - private class Field { - private String name; - private String value; - private boolean inline; - - private Field(String name, String value, boolean inline) { - this.name = name; - this.value = value; - this.inline = inline; - } - - private String getName() { - return name; - } - - private String getValue() { - return value; - } - - private boolean isInline() { - return inline; - } - } - } - - private class JSONObject { - - private final HashMap map = new HashMap<>(); - - void put(String key, Object value) { - if (value != null) { - map.put(key, value); - } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - Set> entrySet = map.entrySet(); - builder.append("{"); - - int i = 0; - for (Map.Entry entry : entrySet) { - Object val = entry.getValue(); - builder.append(quote(entry.getKey())).append(":"); - - if (val instanceof String) { - builder.append(quote(String.valueOf(val))); - } else if (val instanceof Integer) { - builder.append(Integer.valueOf(String.valueOf(val))); - } else if (val instanceof Boolean) { - builder.append(val); - } else if (val instanceof JSONObject) { - builder.append(val.toString()); - } else if (val.getClass().isArray()) { - builder.append("["); - int len = Array.getLength(val); - for (int j = 0; j < len; j++) { - builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : ""); - } - builder.append("]"); - } - - builder.append(++i == entrySet.size() ? "}" : ","); - } - - return builder.toString(); - } - - private String quote(String string) { - return "\"" + string + "\""; - } - } + private final String url; + private String content; + private String username; + private String avatarUrl; + private boolean tts; + private List embeds = new ArrayList<>(); + + /** + * Constructs a new DiscordWebhook instance + * + * @param url The webhook URL obtained in Discord + */ + public DiscordWebhook(String url) { + this.url = url; + } + + public void setContent(String content) { + this.content = content; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setAvatarUrl(String avatarUrl) { + this.avatarUrl = avatarUrl; + } + + public void setTts(boolean tts) { + this.tts = tts; + } + + public void addEmbed(EmbedObject embed) { + this.embeds.add(embed); + } + + public void execute() throws IOException { + if(this.content == null && this.embeds.isEmpty()) { + throw new IllegalArgumentException("Set content or add at least one EmbedObject"); + } + + JSONObject json = new JSONObject(); + + json.put("content", this.content); + json.put("username", this.username); + json.put("avatar_url", this.avatarUrl); + json.put("tts", this.tts); + + if(!this.embeds.isEmpty()) { + List embedObjects = new ArrayList<>(); + + for(EmbedObject embed : this.embeds) { + JSONObject jsonEmbed = new JSONObject(); + + jsonEmbed.put("title", embed.getTitle()); + jsonEmbed.put("description", embed.getDescription()); + jsonEmbed.put("url", embed.getUrl()); + + if(embed.getColor() != null) { + Color color = embed.getColor(); + int rgb = color.getRed(); + rgb = (rgb << 8) + color.getGreen(); + rgb = (rgb << 8) + color.getBlue(); + + jsonEmbed.put("color", rgb); + } + + EmbedObject.Footer footer = embed.getFooter(); + EmbedObject.Image image = embed.getImage(); + EmbedObject.Thumbnail thumbnail = embed.getThumbnail(); + EmbedObject.Author author = embed.getAuthor(); + List fields = embed.getFields(); + + if(footer != null) { + JSONObject jsonFooter = new JSONObject(); + + jsonFooter.put("text", footer.getText()); + jsonFooter.put("icon_url", footer.getIconUrl()); + jsonEmbed.put("footer", jsonFooter); + } + + if(image != null) { + JSONObject jsonImage = new JSONObject(); + + jsonImage.put("url", image.getUrl()); + jsonEmbed.put("image", jsonImage); + } + + if(thumbnail != null) { + JSONObject jsonThumbnail = new JSONObject(); + + jsonThumbnail.put("url", thumbnail.getUrl()); + jsonEmbed.put("thumbnail", jsonThumbnail); + } + + if(author != null) { + JSONObject jsonAuthor = new JSONObject(); + + jsonAuthor.put("name", author.getName()); + jsonAuthor.put("url", author.getUrl()); + jsonAuthor.put("icon_url", author.getIconUrl()); + jsonEmbed.put("author", jsonAuthor); + } + + List jsonFields = new ArrayList<>(); + for(EmbedObject.Field field : fields) { + JSONObject jsonField = new JSONObject(); + + jsonField.put("name", field.getName()); + jsonField.put("value", field.getValue()); + jsonField.put("inline", field.isInline()); + + jsonFields.add(jsonField); + } + + jsonEmbed.put("fields", jsonFields.toArray()); + embedObjects.add(jsonEmbed); + } + + json.put("embeds", embedObjects.toArray()); + } + + URL url = new URL(this.url); + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); + connection.addRequestProperty("Content-Type", "application/json"); + connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_"); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + + OutputStream stream = connection.getOutputStream(); + stream.write(json.toString().getBytes()); + stream.flush(); + stream.close(); + + connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream + connection.disconnect(); + } + + public static class EmbedObject { + + private String title; + private String description; + private String url; + private Color color; + + private Footer footer; + private Thumbnail thumbnail; + private Image image; + private Author author; + private List fields = new ArrayList<>(); + + public String getTitle() { + return title; + } + + public String getDescription() { + return description; + } + + public String getUrl() { + return url; + } + + public Color getColor() { + return color; + } + + public Footer getFooter() { + return footer; + } + + public Thumbnail getThumbnail() { + return thumbnail; + } + + public Image getImage() { + return image; + } + + public Author getAuthor() { + return author; + } + + public List getFields() { + return fields; + } + + public EmbedObject setTitle(String title) { + this.title = title; + return this; + } + + public EmbedObject setDescription(String description) { + this.description = description; + return this; + } + + public EmbedObject setUrl(String url) { + this.url = url; + return this; + } + + public EmbedObject setColor(Color color) { + this.color = color; + return this; + } + + public EmbedObject setFooter(String text, String icon) { + this.footer = new Footer(text, icon); + return this; + } + + public EmbedObject setThumbnail(String url) { + this.thumbnail = new Thumbnail(url); + return this; + } + + public EmbedObject setImage(String url) { + this.image = new Image(url); + return this; + } + + public EmbedObject setAuthor(String name, String url, String icon) { + this.author = new Author(name, url, icon); + return this; + } + + public EmbedObject addField(String name, String value, boolean inline) { + this.fields.add(new Field(name, value, inline)); + return this; + } + + private class Footer { + + private String text; + private String iconUrl; + + private Footer(String text, String iconUrl) { + this.text = text; + this.iconUrl = iconUrl; + } + + private String getText() { + return text; + } + + private String getIconUrl() { + return iconUrl; + } + + } + + private class Thumbnail { + + private String url; + + private Thumbnail(String url) { + this.url = url; + } + + private String getUrl() { + return url; + } + + } + + private class Image { + + private String url; + + private Image(String url) { + this.url = url; + } + + private String getUrl() { + return url; + } + + } + + private class Author { + + private String name; + private String url; + private String iconUrl; + + private Author(String name, String url, String iconUrl) { + this.name = name; + this.url = url; + this.iconUrl = iconUrl; + } + + private String getName() { + return name; + } + + private String getUrl() { + return url; + } + + private String getIconUrl() { + return iconUrl; + } + + } + + private class Field { + + private String name; + private String value; + private boolean inline; + + private Field(String name, String value, boolean inline) { + this.name = name; + this.value = value; + this.inline = inline; + } + + private String getName() { + return name; + } + + private String getValue() { + return value; + } + + private boolean isInline() { + return inline; + } + + } + + } + + private class JSONObject { + + private final HashMap map = new HashMap<>(); + + void put(String key, Object value) { + if(value != null) { + map.put(key, value); + } + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + Set> entrySet = map.entrySet(); + builder.append("{"); + + int i = 0; + for(Map.Entry entry : entrySet) { + Object val = entry.getValue(); + builder.append(quote(entry.getKey())).append(":"); + + if(val instanceof String) { + builder.append(quote(String.valueOf(val))); + } else if(val instanceof Integer) { + builder.append(Integer.valueOf(String.valueOf(val))); + } else if(val instanceof Boolean) { + builder.append(val); + } else if(val instanceof JSONObject) { + builder.append(val.toString()); + } else if(val.getClass().isArray()) { + builder.append("["); + int len = Array.getLength(val); + for(int j = 0; j < len; j++) { + builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : ""); + } + builder.append("]"); + } + + builder.append(++i == entrySet.size() ? "}" : ","); + } + + return builder.toString(); + } + + private String quote(String string) { + return "\"" + string + "\""; + } + + } } \ No newline at end of file diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/PasteUtils.java b/src/main/java/dev/hypera/ultrastaffchat/utils/PasteUtils.java index f579915..cc4b5fb 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/PasteUtils.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/PasteUtils.java @@ -32,47 +32,48 @@ public class PasteUtils { - private static final String PASTE_BASE = "https://paste.hypera.dev/"; + private static final String PASTE_BASE = "https://paste.hypera.dev/"; - /** - * Create a new paste. - * It is recommended to run this async to avoid slowing down the main thread. - * - * @param content Paste content. - * @return Paste Url. - * @throws Exception Anything that goes wrong while creating the paste. - */ - public static String createPaste(String... content) throws Exception { - URL url = new URL(PASTE_BASE + "/documents"); - HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setRequestMethod("POST"); - httpsURLConnection.setRequestProperty("User-Agent", "UltraStaffChat v" + UltraStaffChat.getInstance().getDescription().getVersion() + " Paste Generator"); - httpsURLConnection.setRequestProperty("Content-Type", "text/plain"); - httpsURLConnection.setDoOutput(true); + /** + * Create a new paste. It is recommended to run this async to avoid slowing down the main thread. + * + * @param content Paste content. + * + * @return Paste Url. + * @throws Exception Anything that goes wrong while creating the paste. + */ + public static String createPaste(String... content) throws Exception { + URL url = new URL(PASTE_BASE + "/documents"); + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setRequestMethod("POST"); + httpsURLConnection.setRequestProperty("User-Agent", "UltraStaffChat v" + UltraStaffChat.getInstance().getDescription().getVersion() + " Paste Generator"); + httpsURLConnection.setRequestProperty("Content-Type", "text/plain"); + httpsURLConnection.setDoOutput(true); - OutputStream outputStream = httpsURLConnection.getOutputStream(); - outputStream.write(String.join("\n", content).getBytes()); - outputStream.flush(); - outputStream.close(); + OutputStream outputStream = httpsURLConnection.getOutputStream(); + outputStream.write(String.join("\n", content).getBytes()); + outputStream.flush(); + outputStream.close(); - if(httpsURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { - InputStreamReader inputStreamReader = new InputStreamReader(httpsURLConnection.getInputStream()); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + if(httpsURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { + InputStreamReader inputStreamReader = new InputStreamReader(httpsURLConnection.getInputStream()); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); - JsonElement jsonElement = new JsonParser().parse(bufferedReader); - JsonObject object = jsonElement.getAsJsonObject(); - if(!object.has("key")) throw new IllegalStateException("Server did not response with a paste key"); + JsonElement jsonElement = new JsonParser().parse(bufferedReader); + JsonObject object = jsonElement.getAsJsonObject(); + if(!object.has("key")) + throw new IllegalStateException("Server did not response with a paste key"); - String pasteUrl = PASTE_BASE + object.get("key").getAsString() + ".txt"; + String pasteUrl = PASTE_BASE + object.get("key").getAsString() + ".txt"; - inputStreamReader.close(); - bufferedReader.close(); + inputStreamReader.close(); + bufferedReader.close(); - return pasteUrl; - } + return pasteUrl; + } - return null; - } + return null; + } } diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java index 3bf45d4..28faf07 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java @@ -25,81 +25,89 @@ public class StaffChat { - private static HashMap mutedPlayers = new HashMap<>(); - private static HashMap toggledPlayers = new HashMap<>(); - private static boolean staffChatMuted = false; - - public static void disableMessages(ProxiedPlayer p) { - if (!mutedPlayers.containsKey(p.getUniqueId())) { - mutedPlayers.put(p.getUniqueId(), true); - return; - } - if (mutedPlayers.get(p.getUniqueId()).equals(false)) mutedPlayers.replace(p.getUniqueId(), false, true); - } - - public static void enableMessages(ProxiedPlayer p) { - if (mutedPlayers.containsKey(p.getUniqueId()) && mutedPlayers.get(p.getUniqueId()).equals(true)) mutedPlayers.replace(p.getUniqueId(), true, false); - } - - public static boolean toggleMessages(ProxiedPlayer p) { - if (hasMessagesDisabled(p)) { - enableMessages(p); - } else { - disableMessages(p); - } - return hasMessagesDisabled(p); - } - - public static boolean hasMessagesDisabled(ProxiedPlayer p) { - return mutedPlayers.containsKey(p.getUniqueId()) && mutedPlayers.get(p.getUniqueId()).equals(true); - } - - public static boolean staffChatIsMuted() { - return staffChatMuted; - } - - public static void muteStaffChat() { - staffChatMuted = true; - } - - public static void unmuteStaffChat() { - staffChatMuted = false; - } - - public static boolean toggleStaffChatMute() { - if (staffChatIsMuted()) { - unmuteStaffChat(); - return false; - } else - muteStaffChat(); - return true; - } - - public static boolean toggleChatStaffChat(ProxiedPlayer p) { - if (!toggledPlayers.containsKey(p.getUniqueId()) || toggledPlayers.get(p.getUniqueId()).equals(false)) { - if (!toggledPlayers.containsKey(p.getUniqueId())) toggledPlayers.put(p.getUniqueId(), true); - if (toggledPlayers.get(p.getUniqueId()).equals(false)) toggledPlayers.replace(p.getUniqueId(), false, true); - return true; - } - - toggledPlayers.replace(p.getUniqueId(), true, false); - return false; - } - - public static void enableChatStaffChat(ProxiedPlayer p) { - if (toggledPlayers.containsKey(p.getUniqueId()) && toggledPlayers.get(p.getUniqueId()).equals(false)) toggledPlayers.replace(p.getUniqueId(), false, true); - if (!toggledPlayers.containsKey(p.getUniqueId())) toggledPlayers.put(p.getUniqueId(), true); - } - - public static void disableChatStaffChat(ProxiedPlayer p) { - if (!toggledPlayers.containsKey(p.getUniqueId())) { - toggledPlayers.put(p.getUniqueId(), false); - return; - } - if (toggledPlayers.get(p.getUniqueId()).equals(true)) toggledPlayers.replace(p.getUniqueId(), true, false); - } - - public static boolean hasChatStaffChatEnabled(ProxiedPlayer p) { - return toggledPlayers.containsKey(p.getUniqueId()) && toggledPlayers.get(p.getUniqueId()).equals(true); - } + private static HashMap mutedPlayers = new HashMap<>(); + private static HashMap toggledPlayers = new HashMap<>(); + private static boolean staffChatMuted = false; + + public static void disableMessages(ProxiedPlayer p) { + if(!mutedPlayers.containsKey(p.getUniqueId())) { + mutedPlayers.put(p.getUniqueId(), true); + return; + } + if(mutedPlayers.get(p.getUniqueId()).equals(false)) + mutedPlayers.replace(p.getUniqueId(), false, true); + } + + public static void enableMessages(ProxiedPlayer p) { + if(mutedPlayers.containsKey(p.getUniqueId()) && mutedPlayers.get(p.getUniqueId()).equals(true)) + mutedPlayers.replace(p.getUniqueId(), true, false); + } + + public static boolean toggleMessages(ProxiedPlayer p) { + if(hasMessagesDisabled(p)) { + enableMessages(p); + } else { + disableMessages(p); + } + return hasMessagesDisabled(p); + } + + public static boolean hasMessagesDisabled(ProxiedPlayer p) { + return mutedPlayers.containsKey(p.getUniqueId()) && mutedPlayers.get(p.getUniqueId()).equals(true); + } + + public static boolean staffChatIsMuted() { + return staffChatMuted; + } + + public static void muteStaffChat() { + staffChatMuted = true; + } + + public static void unmuteStaffChat() { + staffChatMuted = false; + } + + public static boolean toggleStaffChatMute() { + if(staffChatIsMuted()) { + unmuteStaffChat(); + return false; + } else + muteStaffChat(); + return true; + } + + public static boolean toggleChatStaffChat(ProxiedPlayer p) { + if(!toggledPlayers.containsKey(p.getUniqueId()) || toggledPlayers.get(p.getUniqueId()).equals(false)) { + if(!toggledPlayers.containsKey(p.getUniqueId())) + toggledPlayers.put(p.getUniqueId(), true); + if(toggledPlayers.get(p.getUniqueId()).equals(false)) + toggledPlayers.replace(p.getUniqueId(), false, true); + return true; + } + + toggledPlayers.replace(p.getUniqueId(), true, false); + return false; + } + + public static void enableChatStaffChat(ProxiedPlayer p) { + if(toggledPlayers.containsKey(p.getUniqueId()) && toggledPlayers.get(p.getUniqueId()).equals(false)) + toggledPlayers.replace(p.getUniqueId(), false, true); + if(!toggledPlayers.containsKey(p.getUniqueId())) + toggledPlayers.put(p.getUniqueId(), true); + } + + public static void disableChatStaffChat(ProxiedPlayer p) { + if(!toggledPlayers.containsKey(p.getUniqueId())) { + toggledPlayers.put(p.getUniqueId(), false); + return; + } + if(toggledPlayers.get(p.getUniqueId()).equals(true)) + toggledPlayers.replace(p.getUniqueId(), true, false); + } + + public static boolean hasChatStaffChatEnabled(ProxiedPlayer p) { + return toggledPlayers.containsKey(p.getUniqueId()) && toggledPlayers.get(p.getUniqueId()).equals(true); + } + } \ No newline at end of file diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/TimeUtils.java b/src/main/java/dev/hypera/ultrastaffchat/utils/TimeUtils.java index 0e69283..0b4b99e 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/TimeUtils.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/TimeUtils.java @@ -24,17 +24,19 @@ public class TimeUtils { - private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - private static final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + private static final SimpleDateFormat fileDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); - public static String formatDateTime(Date date) { - if(!dateFormat.getTimeZone().getID().equalsIgnoreCase("UTC")) dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return dateFormat.format(date); - } + public static String formatDateTime(Date date) { + if(!dateFormat.getTimeZone().getID().equalsIgnoreCase("UTC")) + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return dateFormat.format(date); + } - public static String formatFileDateTime(Date date) { - if(!fileDateFormat.getTimeZone().getID().equalsIgnoreCase("UTC")) fileDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); - return fileDateFormat.format(date); - } + public static String formatFileDateTime(Date date) { + if(!fileDateFormat.getTimeZone().getID().equalsIgnoreCase("UTC")) + fileDateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + return fileDateFormat.format(date); + } } From 7c1a0eb1ba85551cf5e3519c92be0e17950e20c8 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 16:24:52 +1000 Subject: [PATCH 03/14] Update build.yml --- .github/workflows/build.yml | 27 +++++++++++++++++++ .github/workflows/maven.yml | 19 ------------- .../hypera/ultrastaffchat/UltraStaffChat.java | 9 ++++++- 3 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/build.yml delete mode 100644 .github/workflows/maven.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..7ec40d4 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,27 @@ +name: build + +on: + [ push ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 1.8 + uses: actions/setup-java@v1 + with: + java-version: 1.8 + - name: Cache Maven Dependencies + uses: actions/cache@v2 + with: + path: ~/.m2 + key: ubuntu-latest-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ubuntu-latest-m2 + - name: Build with Maven + run: mvn -B package --file pom.xml + - name: Upload artifacts + uses: actions/upload-artifact@v2 + with: + path: target/*.jar + retention-days: 7 \ No newline at end of file diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml deleted file mode 100644 index 5372353..0000000 --- a/.github/workflows/maven.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: build - -on: - push: - branches: [ main ] - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: Set up JDK 1.8 - uses: actions/setup-java@v1 - with: - java-version: 1.8 - - name: Build with Maven - run: mvn -B package --file pom.xml diff --git a/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java b/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java index 51322c9..8b1b095 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java +++ b/src/main/java/dev/hypera/ultrastaffchat/UltraStaffChat.java @@ -76,7 +76,14 @@ public void onEnable() { Debug.finishStart(); - Common.log("&r &r &r", "&c __ ___________", "&c / / / / __/ ___/ &r &c&lUltraStaffChat&r &8BungeeCord", "&c/ /_/ /\\ \\/ /__&c &r &c &r &8Running Version &c" + getDescription().getVersion(), "&c\\____/___/\\___/", "&r &r &r"); + Common.log( + "&r &r &r", + "&c __ ___________", + "&c / / / / __/ ___/ &r &c&lUltraStaffChat&r &8BungeeCord", + "&c/ /_/ /\\ \\/ /__&c &r &c &r &8Running Version &c" + getDescription().getVersion(), + "&c\\____/___/\\___/", + "&r &r &r" + ); Common.logPrefix("&aSuccessfully started. &8Took " + Debug.getStartLength() + "ms"); } catch (Exception ex) { Common.error(ErrorCode.UNKNOWN, "Error while starting.", ex); From 0bdaaa1c3e9c25d9dacc4d8e8311e7bd0ff2bf83 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 16:26:50 +1000 Subject: [PATCH 04/14] Update build.yml --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7ec40d4..4d5831a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,5 +23,5 @@ jobs: - name: Upload artifacts uses: actions/upload-artifact@v2 with: - path: target/*.jar + path: target/UltraStaffChat-*.jar retention-days: 7 \ No newline at end of file From 84a6172faa058b8183104d180d46c689587ecc17 Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 07:52:53 +0100 Subject: [PATCH 05/14] Added embed field cap of 25 fields. --- .../hypera/ultrastaffchat/utils/Discord.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java b/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java index 5162d4c..a615b2c 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/Discord.java @@ -25,7 +25,7 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.config.Configuration; -import java.awt.Color; +import java.awt.*; import java.io.IOException; public class Discord { @@ -43,6 +43,27 @@ public static void setup() { return; } + Configuration config = UltraStaffChat.getConfig().getConfiguration(); + if (config.getSection("discord-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } else if (config.getSection("discord-join-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } else if (config.getSection("discord-leave-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } else if (config.getSection("discord-switch-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } else if (config.getSection("discord-afk-enable-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } else if (config.getSection("discord-afk-disable-format.embed.fields").getKeys().size() > 25) { + Common.logPrefix("&cError: You can have a max of 25 fields in an embed."); + return; + } + hookURL = UltraStaffChat.getConfig().getString("discord-webhook"); enabled = true; } From c9297ef7094a4f766cc605cee377809786d5b32c Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 18:54:35 +1000 Subject: [PATCH 06/14] Enable DependaBot --- .github/dependabot.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..76668eb --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: "maven" + directory: "/" + schedule: + interval: "weekly" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "daily" \ No newline at end of file From 66925ae24f741bc49e4d176b7db9b1e1adba15ee Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 18:56:05 +1000 Subject: [PATCH 07/14] Change maven dependabot interval to daily --- .github/dependabot.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 76668eb..7957476 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -3,7 +3,7 @@ updates: - package-ecosystem: "maven" directory: "/" schedule: - interval: "weekly" + interval: "daily" - package-ecosystem: "github-actions" directory: "/" schedule: From 02b82f2bda2f2ece90637386bc2085814b94c705 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 20:18:25 +1000 Subject: [PATCH 08/14] Add linter --- .github/workflows/build.yml | 4 ++-- .github/workflows/linter.yml | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/linter.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4d5831a..6b3fab1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,6 @@ name: build -on: - [ push ] +on: [ push ] jobs: build: @@ -20,6 +19,7 @@ jobs: restore-keys: ubuntu-latest-m2 - name: Build with Maven run: mvn -B package --file pom.xml + - name: Upload artifacts uses: actions/upload-artifact@v2 with: diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml new file mode 100644 index 0000000..982404c --- /dev/null +++ b/.github/workflows/linter.yml @@ -0,0 +1,18 @@ +name: lint + +on: [ push, pull_request ] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout Code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: Lint + uses: github/super-linter@v3 + env: + DEFAULT_BRANCH: main + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From 0857302074b8f9bf1402edf43a38a337aaa5f9a6 Mon Sep 17 00:00:00 2001 From: Joshua Sing Date: Tue, 4 May 2021 21:09:51 +1000 Subject: [PATCH 09/14] Fix --- .github/workflows/linter.yml | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 .github/workflows/linter.yml diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml deleted file mode 100644 index 982404c..0000000 --- a/.github/workflows/linter.yml +++ /dev/null @@ -1,18 +0,0 @@ -name: lint - -on: [ push, pull_request ] - -jobs: - lint: - name: Lint - runs-on: ubuntu-latest - steps: - - name: Checkout Code - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - name: Lint - uses: github/super-linter@v3 - env: - DEFAULT_BRANCH: main - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From ee01daae91a826dc54dbaf07b2f06d8a48924db9 Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 16:16:19 +0100 Subject: [PATCH 10/14] Fixed toggle so staffchat can be toggled off. --- .../dev/hypera/ultrastaffchat/utils/StaffChat.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java index 28faf07..66a5bc9 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java +++ b/src/main/java/dev/hypera/ultrastaffchat/utils/StaffChat.java @@ -78,16 +78,14 @@ public static boolean toggleStaffChatMute() { } public static boolean toggleChatStaffChat(ProxiedPlayer p) { - if(!toggledPlayers.containsKey(p.getUniqueId()) || toggledPlayers.get(p.getUniqueId()).equals(false)) { - if(!toggledPlayers.containsKey(p.getUniqueId())) - toggledPlayers.put(p.getUniqueId(), true); - if(toggledPlayers.get(p.getUniqueId()).equals(false)) - toggledPlayers.replace(p.getUniqueId(), false, true); + if (!toggledPlayers.containsKey(p.getUniqueId())) { + toggledPlayers.put(p.getUniqueId(), true); return true; + } else { + boolean oldToggled = toggledPlayers.get(p.getUniqueId()); + toggledPlayers.replace(p.getUniqueId(), !oldToggled); + return !oldToggled; } - - toggledPlayers.replace(p.getUniqueId(), true, false); - return false; } public static void enableChatStaffChat(ProxiedPlayer p) { From e353973a4c825855b4d4350c419d94fe3994c6a4 Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 17:06:22 +0100 Subject: [PATCH 11/14] Fixed incorrect string comparator when checking for prefix. Fixes #1. --- .../dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java index 295ab05..55421c8 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java +++ b/src/main/java/dev/hypera/ultrastaffchat/listeners/impl/ChatListener.java @@ -52,7 +52,7 @@ public void onChat(ChatEvent e) { if(p.hasPermission(UltraStaffChat.getConfig().getString("permission-talk"))) { String prefix = UltraStaffChat.getConfig().getString("staffchat-prefix"); - if(prefix != "" && e.getMessage().startsWith(prefix)) { + if(!prefix.equals("") && e.getMessage().startsWith(prefix)) { e.setCancelled(true); if(StaffChat.staffChatIsMuted()) { if(!((ProxiedPlayer) e.getSender()).hasPermission(UltraStaffChat.getConfig().getString("permission-bypass"))) { From 1d0237d64559b7a7b931fa6a69a1144ad7007340 Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 18:19:28 +0100 Subject: [PATCH 12/14] Added StaffAfkCommand and edited config to add placeholders to AFK messages. --- .../commands/impl/StaffAfkCommand.java | 79 +++++++++++++++++++ src/main/resources/config.yml | 2 + 2 files changed, 81 insertions(+) create mode 100644 src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffAfkCommand.java diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffAfkCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffAfkCommand.java new file mode 100644 index 0000000..b103689 --- /dev/null +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/StaffAfkCommand.java @@ -0,0 +1,79 @@ +/* + * UltraStaffChat BungeeCord - A 100% Customizable StaffChat Plugin for BungeeCord! + * Copyright (C) 2021 SLLCoding + * + * 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 dev.hypera.ultrastaffchat.commands.impl; + +import dev.hypera.ultrastaffchat.UltraStaffChat; +import dev.hypera.ultrastaffchat.commands.Command; +import dev.hypera.ultrastaffchat.utils.Common; +import net.kyori.adventure.audience.Audience; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class StaffAfkCommand extends Command { + + public StaffAfkCommand() { + super("staffchatafk", null, "scafk"); + } + + @Override + public void execute(CommandSender sender, String[] args) { + Audience audience = UltraStaffChat.getInstance().getAdventure().sender(sender); + + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-afk"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + if(!(sender instanceof ProxiedPlayer)) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("ingame-only"))); + return; + } + + ProxiedPlayer p = (ProxiedPlayer) sender; + boolean toggled = toggleAfk(p); + Audience all = UltraStaffChat.getInstance().getAdventure().permission(UltraStaffChat.getConfig().getString("permission-read")); + if(toggled) { + all.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("afk-broadcast").replaceAll("\\{player}", p.getName()))); + } else + all.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-afk-broadcast").replaceAll("\\{player}", p.getName()))); + } + + @Override + public boolean isDisabled() { + return !UltraStaffChat.getConfig().getBoolean("afk-enabled"); + } + + private final Map afkStatus = new HashMap<>(); + + private boolean toggleAfk(ProxiedPlayer player) { + if (!afkStatus.containsKey(player.getUniqueId())) { + afkStatus.put(player.getUniqueId(), true); + return true; + } else { + boolean oldAfk = afkStatus.get(player.getUniqueId()); + afkStatus.replace(player.getUniqueId(), !oldAfk); + return !oldAfk; + } + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 8c0f1e6..c710afd 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -282,9 +282,11 @@ mute-staffchat-usage: "&cUsage: /mutesc " afk-enabled: true # AFK | Message +# - Placeholders: Player - {player} afk-broadcast: "&7{player} is now &c&lAFK&7." # AFK | No longer AFK Message +# - Placeholders: Player - {player} no-afk-broadcast: "&7{player} is no longer &c&lAFK&7." From 05fb0b65f015b5ba695b9ae7a74f444b7aef999f Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 18:20:02 +0100 Subject: [PATCH 13/14] Bump version to 5.0.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a28a646..ed0f5f1 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ dev.hypera UltraStaffChat - 5.0.0 + 5.0.1 jar UltraStaffChat From 9b9d8c5f03d72726529070efdaaddc4a5fad8804 Mon Sep 17 00:00:00 2001 From: Luis Date: Tue, 4 May 2021 19:49:50 +0100 Subject: [PATCH 14/14] Split UltraStaffChatCommand into multiple parts for Codefactor. --- .../commands/impl/UltraStaffChatCommand.java | 139 ++++++++++-------- 1 file changed, 75 insertions(+), 64 deletions(-) diff --git a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java index 477c5e8..68dcc52 100644 --- a/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java +++ b/src/main/java/dev/hypera/ultrastaffchat/commands/impl/UltraStaffChatCommand.java @@ -58,74 +58,12 @@ public void execute(CommandSender sender, String[] args) { } if(args[0].matches("(?i:(r(eload)?))")) { - if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } - - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN))); - - UltraStaffChat.getConfig().reload(); - Discord.reload(); - - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN))); + reload(sender, audience); return; } if(args[0].matches("(?i:(debug))")) { - if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) { - audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); - return; - } - - String log; - String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt"; - - boolean advanced = false; - if(args.length > 1) { - for(String arg : args) { - if(arg.matches("(?i:(--a(dv(anced)?)?))")) { - advanced = true; - break; - } - } - } - - if(advanced) { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE))); - log = generateAdvancedDebugLog(); - } else { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating debug log...").color(NamedTextColor.WHITE))); - log = generateDebugLog(); - } - - try { - File logFolder = new File(UltraStaffChat.getInstance().getDataFolder() + "/logs"); - if(!logFolder.exists()) - logFolder.mkdirs(); - - File logFile = new File(logFolder + "/" + filename); - if(logFile.createNewFile()) { - FileWriter logWriter = new FileWriter(logFile); - logWriter.write(log); - logWriter.close(); - } else { - throw new Exception(); - } - - String pasteURL = PasteUtils.createPaste(log); - if(pasteURL == null) - throw new Exception(); - if(advanced) { - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))))); - } else { - audience.sendMessage(Component.text().append(LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)))); - } - } catch (Exception e) { - Common.logPrefix("Failed to generate debug log!"); - audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Failed to generate debug log").color(NamedTextColor.RED))); - return; - } + debug(sender, args, audience); } } @@ -137,4 +75,77 @@ public boolean isDisabled() { return false; } + private void reload(CommandSender sender, Audience audience) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-reload"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Reloading configuration files...").color(NamedTextColor.GREEN))); + + UltraStaffChat.getConfig().reload(); + Discord.reload(); + + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully reloaded configuration files").color(NamedTextColor.GREEN))); + } + + private void debug(CommandSender sender, String[] args, Audience audience) { + if(!sender.hasPermission(UltraStaffChat.getConfig().getString("permission-debug"))) { + audience.sendMessage(Common.adventurise(UltraStaffChat.getConfig().getString("no-permission"))); + return; + } + + String log; + + boolean advanced = false; + if(args.length > 1) { + for(String arg : args) { + if(arg.matches("(?i:(--a(dv(anced)?)?))")) { + advanced = true; + break; + } + } + } + + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating advanced debug log...").color(NamedTextColor.WHITE))); + log = generateAdvancedDebugLog(); + } else { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Generating debug log...").color(NamedTextColor.WHITE))); + log = generateDebugLog(); + } + + logDebug(audience, log, advanced); + } + + private void logDebug(Audience audience, String log, boolean advanced) { + try { + String filename = "USC-" + TimeUtils.formatFileDateTime(Date.from(Instant.now())) + "-Debug.txt"; + File logFolder = new File(UltraStaffChat.getInstance().getDataFolder() + "/logs"); + if(!logFolder.exists()) + logFolder.mkdirs(); + + File logFile = new File(logFolder + "/" + filename); + if(logFile.createNewFile()) { + FileWriter logWriter = new FileWriter(logFile); + logWriter.write(log); + logWriter.close(); + } else { + throw new Exception(); + } + + String pasteURL = PasteUtils.createPaste(log); + if(pasteURL == null) + throw new Exception(); + if(advanced) { + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Successfully generated debug log ").color(NamedTextColor.GREEN), Component.text().content("[View]").color(NamedTextColor.GRAY).clickEvent(ClickEvent.openUrl(pasteURL)).hoverEvent(HoverEvent.showText(Component.text().content(pasteURL))))); + } else { + audience.sendMessage(Component.text().append(LegacyComponentSerializer.legacy('&').deserialize(log).clickEvent(ClickEvent.openUrl(pasteURL)))); + } + } catch (Exception e) { + Common.logPrefix("Failed to generate debug log!"); + audience.sendMessage(Component.text().append(Component.text().content("[").color(NamedTextColor.GRAY), Component.text().content("UltraStaffChat").color(NamedTextColor.RED).decorate(TextDecoration.BOLD), Component.text().content("] ").color(NamedTextColor.GRAY), Component.text().content("Failed to generate debug log").color(NamedTextColor.RED))); + } + } + }