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