Add fake vanish join/leave + exclude vanished messages

This commit is contained in:
Josh Roy 2021-07-03 13:18:59 -04:00
parent 48532a0ae6
commit 37e3730e9d
3 changed files with 65 additions and 20 deletions

View File

@ -151,6 +151,14 @@ public class DiscordSettings implements IConf {
return config.getString("avatar-url", "https://crafthead.net/helm/{uuid}"); return config.getString("avatar-url", "https://crafthead.net/helm/{uuid}");
} }
public boolean isVanishFakeJoinLeave() {
return config.getBoolean("vanish-fake-join-leave", true);
}
public boolean isVanishHideMessages() {
return config.getBoolean("vanish-hide-messages", true);
}
// General command settings // General command settings
public boolean isCommandEnabled(String command) { public boolean isCommandEnabled(String command) {

View File

@ -3,8 +3,10 @@ package net.essentialsx.discord.listeners;
import com.earth2me.essentials.Console; import com.earth2me.essentials.Console;
import com.earth2me.essentials.utils.DateUtil; import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.FormatUtil; import com.earth2me.essentials.utils.FormatUtil;
import net.ess3.api.IUser;
import net.ess3.api.events.AfkStatusChangeEvent; import net.ess3.api.events.AfkStatusChangeEvent;
import net.ess3.api.events.MuteStatusChangeEvent; import net.ess3.api.events.MuteStatusChangeEvent;
import net.ess3.api.events.VanishStatusChangeEvent;
import net.essentialsx.api.v2.events.AsyncUserDataLoadEvent; import net.essentialsx.api.v2.events.AsyncUserDataLoadEvent;
import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent; import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent;
import net.essentialsx.api.v2.events.discord.DiscordMessageEvent; import net.essentialsx.api.v2.events.discord.DiscordMessageEvent;
@ -13,6 +15,7 @@ import net.essentialsx.discord.JDADiscordService;
import net.essentialsx.discord.util.DiscordUtil; import net.essentialsx.discord.util.DiscordUtil;
import net.essentialsx.discord.util.MessageUtil; import net.essentialsx.discord.util.MessageUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -103,36 +106,47 @@ public class BukkitListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR) @EventHandler(priority = EventPriority.MONITOR)
public void onJoin(AsyncUserDataLoadEvent event) { public void onJoin(AsyncUserDataLoadEvent event) {
// Delay join to let nickname load // Delay join to let nickname load
if (event.getJoinMessage() != null) { if (event.getJoinMessage() != null && !isVanishHide(event.getUser())) {
sendDiscordMessage(MessageType.DefaultTypes.JOIN, sendJoinQuitMessage(event.getUser().getBase(), event.getJoinMessage(), true);
MessageUtil.formatMessage(jda.getSettings().getJoinFormat(event.getUser().getBase()),
MessageUtil.sanitizeDiscordMarkdown(event.getUser().getName()),
MessageUtil.sanitizeDiscordMarkdown(event.getUser().getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(event.getJoinMessage())),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", event.getUser().getBase().getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? event.getUser().getName() : null,
event.getUser().getBase().getUniqueId());
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onQuit(PlayerQuitEvent event) { public void onQuit(PlayerQuitEvent event) {
if (event.getQuitMessage() != null) { if (event.getQuitMessage() != null && !isVanishHide(event.getPlayer())) {
sendDiscordMessage(MessageType.DefaultTypes.LEAVE, sendJoinQuitMessage(event.getPlayer(), event.getQuitMessage(), false);
MessageUtil.formatMessage(jda.getSettings().getQuitFormat(event.getPlayer()),
MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getName()),
MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(event.getQuitMessage())),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", event.getPlayer().getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? event.getPlayer().getName() : null,
event.getPlayer().getUniqueId());
} }
} }
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onVanishStatusChange(VanishStatusChangeEvent event) {
if (!jda.getSettings().isVanishFakeJoinLeave()) {
return;
}
if (event.getValue()) {
sendJoinQuitMessage(event.getAffected().getBase(), ChatColor.YELLOW + event.getAffected().getName() + " left the game", false);
return;
}
sendJoinQuitMessage(event.getAffected().getBase(), ChatColor.YELLOW + event.getAffected().getName() + " joined the game", true);
}
public void sendJoinQuitMessage(final Player player, final String message, boolean join) {
sendDiscordMessage(join ? MessageType.DefaultTypes.JOIN : MessageType.DefaultTypes.LEAVE,
MessageUtil.formatMessage(join ? jda.getSettings().getJoinFormat(player) : jda.getSettings().getQuitFormat(player),
MessageUtil.sanitizeDiscordMarkdown(player.getName()),
MessageUtil.sanitizeDiscordMarkdown(player.getDisplayName()),
MessageUtil.sanitizeDiscordMarkdown(message),
false,
jda.getSettings().isShowAvatar() ? jda.getSettings().getAvatarURL().replace("{uuid}", player.getUniqueId().toString()) : null,
jda.getSettings().isShowName() ? player.getName() : null,
player.getUniqueId()));
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onDeath(PlayerDeathEvent event) { public void onDeath(PlayerDeathEvent event) {
if (isVanishHide(event.getEntity())) {
return;
}
sendDiscordMessage(MessageType.DefaultTypes.DEATH, sendDiscordMessage(MessageType.DefaultTypes.DEATH,
MessageUtil.formatMessage(jda.getSettings().getDeathFormat(event.getEntity()), MessageUtil.formatMessage(jda.getSettings().getDeathFormat(event.getEntity()),
MessageUtil.sanitizeDiscordMarkdown(event.getEntity().getName()), MessageUtil.sanitizeDiscordMarkdown(event.getEntity().getName()),
@ -146,6 +160,10 @@ public class BukkitListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onAfk(AfkStatusChangeEvent event) { public void onAfk(AfkStatusChangeEvent event) {
if (isVanishHide(event.getAffected())) {
return;
}
final MessageFormat format; final MessageFormat format;
if (event.getValue()) { if (event.getValue()) {
format = jda.getSettings().getAfkFormat(event.getAffected().getBase()); format = jda.getSettings().getAfkFormat(event.getAffected().getBase());
@ -165,6 +183,9 @@ public class BukkitListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onKick(PlayerKickEvent event) { public void onKick(PlayerKickEvent event) {
if (isVanishHide(event.getPlayer())) {
return;
}
sendDiscordMessage(MessageType.DefaultTypes.KICK, sendDiscordMessage(MessageType.DefaultTypes.KICK,
MessageUtil.formatMessage(jda.getSettings().getKickFormat(), MessageUtil.formatMessage(jda.getSettings().getKickFormat(),
MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getName()), MessageUtil.sanitizeDiscordMarkdown(event.getPlayer().getName()),
@ -172,6 +193,14 @@ public class BukkitListener implements Listener {
MessageUtil.sanitizeDiscordMarkdown(event.getReason()))); MessageUtil.sanitizeDiscordMarkdown(event.getReason())));
} }
private boolean isVanishHide(final Player player) {
return isVanishHide(jda.getPlugin().getEss().getUser(player));
}
private boolean isVanishHide(final IUser user) {
return jda.getSettings().isVanishHideMessages() && user.isHidden();
}
private void sendDiscordMessage(final MessageType messageType, final String message) { private void sendDiscordMessage(final MessageType messageType, final String message) {
sendDiscordMessage(messageType, message, false, null, null, null); sendDiscordMessage(messageType, message, false, null, null, null);
} }

View File

@ -128,6 +128,14 @@ avatar-url: "https://crafthead.net/helm/{uuid}"
# Whether or not player messages should show their name as the bot name in Discord. # Whether or not player messages should show their name as the bot name in Discord.
show-name: false show-name: false
# Whether or not fake join and leave messages should be sent to Discord when a player toggles vanish in Minecraft.
# Fake join/leave messages will be sent the same as real join and leave messages (and to the same channel).
vanish-fake-join-leave: true
# Whether or not messages from vanished players should be sent to discord.
# This affects join, leave, death, and afk message types by default.
vanish-hide-messages: true
# Settings pertaining to the varies commands registered by EssentialsX on Discord. # Settings pertaining to the varies commands registered by EssentialsX on Discord.
commands: commands:
# The execute command allows for discord users to execute MC commands from Discord. # The execute command allows for discord users to execute MC commands from Discord.