From f327f59d6caeec347302e65c5448267f29162014 Mon Sep 17 00:00:00 2001 From: ZeroHD Date: Tue, 3 Aug 2021 02:34:17 +0000 Subject: [PATCH] Added `discordignore` command (#17) Small tweaks + Fixed permission node + Removed unused imports Added `discordignore` command + Toggles Discord messages on/off for the user + Resolves #16 Co-authored-by: Joey Hines Reviewed-on: https://git.canopymc.net/Canopy/QoL/pulls/17 Reviewed-by: Etzelia Co-Authored-By: ZeroHD Co-Committed-By: ZeroHD --- pom.xml | 4 +- src/main/java/xyz/etztech/qol/QoL.java | 5 ++ .../qol/commands/DiscordIgnoreCommand.java | 55 +++++++++++++++++++ .../qol/listeners/DiscordSRVListener.java | 20 +++++++ src/main/resources/plugin.yml | 6 ++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 src/main/java/xyz/etztech/qol/commands/DiscordIgnoreCommand.java diff --git a/pom.xml b/pom.xml index d8ddb0f..dfdc86a 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xyz.etztech QoL - 1.14 + 1.15 jar @@ -53,7 +53,7 @@ com.discordsrv discordsrv - 1.19.1 + 1.23.0 provided diff --git a/src/main/java/xyz/etztech/qol/QoL.java b/src/main/java/xyz/etztech/qol/QoL.java index d4bbc59..1d53475 100644 --- a/src/main/java/xyz/etztech/qol/QoL.java +++ b/src/main/java/xyz/etztech/qol/QoL.java @@ -1,5 +1,6 @@ package xyz.etztech.qol; +import github.scarsz.discordsrv.DiscordSRV; import net.md_5.bungee.api.chat.BaseComponent; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -124,6 +125,10 @@ public class QoL extends JavaPlugin { new WikiCommand(this); new MoonCommand(this); + if (DiscordSRV.api.isAnyHooked()) { + new DiscordIgnoreCommand(this); + } + if (dynmap != null) { new MarkerCommand(this); } diff --git a/src/main/java/xyz/etztech/qol/commands/DiscordIgnoreCommand.java b/src/main/java/xyz/etztech/qol/commands/DiscordIgnoreCommand.java new file mode 100644 index 0000000..63ec03f --- /dev/null +++ b/src/main/java/xyz/etztech/qol/commands/DiscordIgnoreCommand.java @@ -0,0 +1,55 @@ +package xyz.etztech.qol.commands; + +import github.scarsz.discordsrv.DiscordSRV; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.metadata.FixedMetadataValue; +import xyz.etztech.qol.EtzTechUtil; +import xyz.etztech.qol.Lang; +import xyz.etztech.qol.QoL; + +public class DiscordIgnoreCommand implements CommandExecutor { + public static final String DISCORD_IGNORE_METADATA = "qol.discord_ignore"; + QoL plugin; + + public DiscordIgnoreCommand(QoL plugin) { + this.plugin = plugin; + plugin.getCommand("discordignore").setExecutor(this); + } + + @Override + public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { + if (!( commandSender instanceof Player)) { + EtzTechUtil.sms(commandSender, Lang.NO_CONSOLE.getDef()); + return true; + } + + if (!commandSender.hasPermission("qol.discordignore")) { + EtzTechUtil.sms(commandSender, Lang.NO_PERMISSION.getDef()); + return true; + } + + if (!DiscordSRV.api.isAnyHooked()) { + EtzTechUtil.sms(commandSender, "Command not enabled!"); + return true; + } + + Player player = (Player) commandSender; + + boolean ignoreState = player.hasMetadata(DISCORD_IGNORE_METADATA); + String msg; + if (ignoreState) { + player.removeMetadata(DISCORD_IGNORE_METADATA, plugin); + msg = "Discord messages will now appear."; + } + else { + player.setMetadata(DISCORD_IGNORE_METADATA, new FixedMetadataValue(plugin, DISCORD_IGNORE_METADATA)); + msg = "Ignoring Discord messages."; + } + + EtzTechUtil.sms(commandSender, org.bukkit.ChatColor.GREEN + msg); + return true; + } +} diff --git a/src/main/java/xyz/etztech/qol/listeners/DiscordSRVListener.java b/src/main/java/xyz/etztech/qol/listeners/DiscordSRVListener.java index b737bc5..72ca6fd 100644 --- a/src/main/java/xyz/etztech/qol/listeners/DiscordSRVListener.java +++ b/src/main/java/xyz/etztech/qol/listeners/DiscordSRVListener.java @@ -1,9 +1,15 @@ package xyz.etztech.qol.listeners; import github.scarsz.discordsrv.DiscordSRV; +import github.scarsz.discordsrv.api.events.DiscordGuildMessagePreBroadcastEvent; import github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import xyz.etztech.qol.EtzTechUtil; +import xyz.etztech.qol.Lang; import xyz.etztech.qol.QoL; import github.scarsz.discordsrv.api.Subscribe; +import xyz.etztech.qol.commands.DiscordIgnoreCommand; public class DiscordSRVListener { private final QoL plugin; @@ -20,4 +26,18 @@ public class DiscordSRVListener { } } + @Subscribe + public void discordGuildMessagePreBroadcastEvent(DiscordGuildMessagePreBroadcastEvent event) { + event.getRecipients().removeIf(recipient -> { + if (recipient instanceof Player) { + Player player = (Player) recipient; + + return player.hasMetadata(DiscordIgnoreCommand.DISCORD_IGNORE_METADATA); + } + else { + return false; + } + }); + } + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0f7467c..da07701 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -50,6 +50,9 @@ commands: description: Search the Minecraft wiki moon: description: Get information about the current moon phase + discordignore: + description: Mute chat messages coming from Discord + aliases: [dignore] permissions: qol.admin: description: Ability to reload the plugin @@ -126,3 +129,6 @@ permissions: qol.moon: description: Ability to use the moon command default: op + qol.discordignore: + description: Ability to use the use the discordignore command + default: op