Check silent permissions on thread

This commit is contained in:
Vankka 2024-12-27 23:57:35 +02:00
parent 317a3c20f6
commit 8f639259e5
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
10 changed files with 60 additions and 39 deletions

View File

@ -75,7 +75,12 @@ public class BukkitDeathListener implements Listener {
} }
boolean wasCancelled = cancelled; boolean wasCancelled = cancelled;
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish( discordSRV.eventBus().publish(new DeathMessageReceiveEvent(
new DeathMessageReceiveEvent(event, player, component, null, wasCancelled))); event,
player,
component,
null,
wasCancelled
));
} }
} }

View File

@ -60,8 +60,13 @@ public class BukkitStatusMessageListener implements Listener {
MinecraftComponent component = JOIN_HANDLE.getComponent(event); MinecraftComponent component = JOIN_HANDLE.getComponent(event);
boolean firstJoin = !event.getPlayer().hasPlayedBefore(); boolean firstJoin = !event.getPlayer().hasPlayedBefore();
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish( discordSRV.eventBus().publish(new JoinMessageReceiveEvent(
new JoinMessageReceiveEvent(event, player, component, null, firstJoin, false) event,
player,
component,
null,
firstJoin,
false
)); ));
} }
@ -70,8 +75,12 @@ public class BukkitStatusMessageListener implements Listener {
DiscordSRVPlayer player = discordSRV.playerProvider().player(event.getPlayer()); DiscordSRVPlayer player = discordSRV.playerProvider().player(event.getPlayer());
MinecraftComponent component = QUIT_HANDLE.getComponent(event); MinecraftComponent component = QUIT_HANDLE.getComponent(event);
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish( discordSRV.eventBus().publish(new LeaveMessageReceiveEvent(
new LeaveMessageReceiveEvent(event, player, component, null, false) event,
player,
component,
null,
false
)); ));
} }
} }

View File

@ -21,7 +21,6 @@ package com.discordsrv.bukkit.listener.award;
import com.discordsrv.api.component.MinecraftComponent; import com.discordsrv.api.component.MinecraftComponent;
import com.discordsrv.api.events.message.receive.game.AwardMessageReceiveEvent; import com.discordsrv.api.events.message.receive.game.AwardMessageReceiveEvent;
import com.discordsrv.bukkit.BukkitDiscordSRV; import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.common.abstraction.player.IPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.intellij.lang.annotations.Language; import org.intellij.lang.annotations.Language;
@ -67,16 +66,13 @@ public class BukkitAwardForwarder implements IBukkitAwardForwarder {
} }
public void publishEvent(Object triggeringEvent, Player player, MinecraftComponent advancementName, MinecraftComponent message, boolean cancelled) { public void publishEvent(Object triggeringEvent, Player player, MinecraftComponent advancementName, MinecraftComponent message, boolean cancelled) {
IPlayer srvPlayer = discordSRV.playerProvider().player(player); discordSRV.eventBus().publish(new AwardMessageReceiveEvent(
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish( triggeringEvent,
new AwardMessageReceiveEvent( discordSRV.playerProvider().player(player),
triggeringEvent, advancementName,
srvPlayer, message,
advancementName, null,
message, cancelled
null,
cancelled
)
)); ));
} }
} }

View File

@ -63,16 +63,13 @@ public class BukkitChatForwarder implements IBukkitChatForwarder {
} }
@Override @Override
public void forwardMessage(Event event, Player player, MinecraftComponent component, boolean cancelled) { public void forwardMessage(Event triggeringEvent, Player player, MinecraftComponent component, boolean cancelled) {
IPlayer srvPlayer = discordSRV.playerProvider().player(player); discordSRV.eventBus().publish(new GameChatMessageReceiveEvent(
discordSRV.scheduler().run(() -> discordSRV.eventBus().publish( triggeringEvent,
new GameChatMessageReceiveEvent( discordSRV.playerProvider().player(player),
event, component,
srvPlayer, new GlobalChannel(discordSRV),
component, cancelled
new GlobalChannel(discordSRV),
cancelled
)
)); ));
} }
} }

View File

@ -71,10 +71,11 @@ public class AwardMessageModule extends AbstractGameMessageModule<AwardMessageCo
return; return;
} }
if (checkIfShouldPermit(event.getPlayer())) { if (!checkIfShouldPermit(event.getPlayer())) {
process(event, event.getPlayer(), event.getGameChannel()); return;
} }
discordSRV.scheduler().run(() -> process(event, event.getPlayer(), event.getGameChannel()));
event.markAsProcessed(); event.markAsProcessed();
} }

View File

@ -45,7 +45,7 @@ public class DeathMessageModule extends AbstractGameMessageModule<DeathMessageCo
return; return;
} }
process(event, event.getPlayer(), event.getGameChannel()); discordSRV.scheduler().run(() -> process(event, event.getPlayer(), event.getGameChannel()));
event.markAsProcessed(); event.markAsProcessed();
} }

View File

@ -26,6 +26,7 @@ import com.discordsrv.api.eventbus.EventPriorities;
import com.discordsrv.api.eventbus.Subscribe; import com.discordsrv.api.eventbus.Subscribe;
import com.discordsrv.api.events.message.forward.game.JoinMessageForwardedEvent; import com.discordsrv.api.events.message.forward.game.JoinMessageForwardedEvent;
import com.discordsrv.api.events.message.receive.game.JoinMessageReceiveEvent; import com.discordsrv.api.events.message.receive.game.JoinMessageReceiveEvent;
import com.discordsrv.api.player.DiscordSRVPlayer;
import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.abstraction.player.IPlayer; import com.discordsrv.common.abstraction.player.IPlayer;
import com.discordsrv.common.config.main.channels.base.BaseChannelConfig; import com.discordsrv.common.config.main.channels.base.BaseChannelConfig;
@ -47,6 +48,7 @@ import java.util.concurrent.Future;
public class JoinMessageModule extends AbstractGameMessageModule<IMessageConfig, JoinMessageReceiveEvent> { public class JoinMessageModule extends AbstractGameMessageModule<IMessageConfig, JoinMessageReceiveEvent> {
private final Map<UUID, Future<?>> delayedTasks = new HashMap<>(); private final Map<UUID, Future<?>> delayedTasks = new HashMap<>();
private final ThreadLocal<Boolean> silentJoinPermission = new ThreadLocal<>();
public JoinMessageModule(DiscordSRV discordSRV) { public JoinMessageModule(DiscordSRV discordSRV) {
super(discordSRV, "JOIN_MESSAGES"); super(discordSRV, "JOIN_MESSAGES");
@ -58,7 +60,12 @@ public class JoinMessageModule extends AbstractGameMessageModule<IMessageConfig,
return; return;
} }
process(event, event.getPlayer(), event.getGameChannel()); DiscordSRVPlayer player = event.getPlayer();
boolean silentJoin = player instanceof IPlayer && ((IPlayer) player).hasPermission(Permission.SILENT_JOIN);
discordSRV.scheduler().run(() -> {
silentJoinPermission.set(silentJoin);
process(event, event.getPlayer(), event.getGameChannel());
});
event.markAsProcessed(); event.markAsProcessed();
} }
@ -69,7 +76,7 @@ public class JoinMessageModule extends AbstractGameMessageModule<IMessageConfig,
@NotNull BaseChannelConfig config, @NotNull BaseChannelConfig config,
@Nullable GameChannel channel @Nullable GameChannel channel
) { ) {
if (config.joinMessages().enableSilentPermission && player != null && player.hasPermission(Permission.SILENT_JOIN)) { if (player != null && config.joinMessages().enableSilentPermission && silentJoinPermission.get()) {
logger().info(player.username() + " is joining silently, join message will not be sent"); logger().info(player.username() + " is joining silently, join message will not be sent");
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }

View File

@ -26,6 +26,7 @@ import com.discordsrv.api.eventbus.EventPriorities;
import com.discordsrv.api.eventbus.Subscribe; import com.discordsrv.api.eventbus.Subscribe;
import com.discordsrv.api.events.message.forward.game.LeaveMessageForwardedEvent; import com.discordsrv.api.events.message.forward.game.LeaveMessageForwardedEvent;
import com.discordsrv.api.events.message.receive.game.LeaveMessageReceiveEvent; import com.discordsrv.api.events.message.receive.game.LeaveMessageReceiveEvent;
import com.discordsrv.api.player.DiscordSRVPlayer;
import com.discordsrv.common.DiscordSRV; import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.abstraction.player.IPlayer; import com.discordsrv.common.abstraction.player.IPlayer;
import com.discordsrv.common.config.main.channels.LeaveMessageConfig; import com.discordsrv.common.config.main.channels.LeaveMessageConfig;
@ -48,6 +49,7 @@ import java.util.concurrent.Future;
public class LeaveMessageModule extends AbstractGameMessageModule<LeaveMessageConfig, LeaveMessageReceiveEvent> { public class LeaveMessageModule extends AbstractGameMessageModule<LeaveMessageConfig, LeaveMessageReceiveEvent> {
private final Map<UUID, Pair<Long, Future<?>>> playersJoinedRecently = new ConcurrentHashMap<>(); private final Map<UUID, Pair<Long, Future<?>>> playersJoinedRecently = new ConcurrentHashMap<>();
private final ThreadLocal<Boolean> silentQuitPermission = new ThreadLocal<>();
public LeaveMessageModule(DiscordSRV discordSRV) { public LeaveMessageModule(DiscordSRV discordSRV) {
super(discordSRV, "LEAVE_MESSAGES"); super(discordSRV, "LEAVE_MESSAGES");
@ -81,7 +83,12 @@ public class LeaveMessageModule extends AbstractGameMessageModule<LeaveMessageCo
return; return;
} }
process(event, event.getPlayer(), event.getGameChannel()); DiscordSRVPlayer player = event.getPlayer();
boolean silentQuit = player instanceof IPlayer && ((IPlayer) player).hasPermission(Permission.SILENT_QUIT);
discordSRV.scheduler().run(() -> {
silentQuitPermission.set(silentQuit);
process(event, player, event.getGameChannel());
});
event.markAsProcessed(); event.markAsProcessed();
} }
@ -93,7 +100,7 @@ public class LeaveMessageModule extends AbstractGameMessageModule<LeaveMessageCo
@Nullable GameChannel channel @Nullable GameChannel channel
) { ) {
if (player != null) { if (player != null) {
Pair<Long, Future<?>> pair = playersJoinedRecently.remove(player.uniqueId()); Pair<Long, Future<?>> pair = playersJoinedRecently.get(player.uniqueId());
if (pair != null) { if (pair != null) {
long delta = System.currentTimeMillis() - pair.getKey(); long delta = System.currentTimeMillis() - pair.getKey();
if (delta < config.leaveMessages.ignoreIfJoinedWithinMS) { if (delta < config.leaveMessages.ignoreIfJoinedWithinMS) {
@ -103,7 +110,7 @@ public class LeaveMessageModule extends AbstractGameMessageModule<LeaveMessageCo
} }
} }
if (config.leaveMessages.enableSilentPermission && player != null && player.hasPermission(Permission.SILENT_QUIT)) { if (player != null && config.leaveMessages.enableSilentPermission && silentQuitPermission.get()) {
logger().info(player.username() + " is leaving silently, leave message will not be sent"); logger().info(player.username() + " is leaving silently, leave message will not be sent");
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }

View File

@ -56,12 +56,11 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
@Subscribe(priority = EventPriorities.LAST, ignoreCancelled = false, ignoreProcessed = false) @Subscribe(priority = EventPriorities.LAST, ignoreCancelled = false, ignoreProcessed = false)
public void onChatReceive(GameChatMessageReceiveEvent event) { public void onChatReceive(GameChatMessageReceiveEvent event) {
if (checkProcessor(event) || checkCancellation(event) || !discordSRV.isReady()) { if (checkProcessor(event) || checkCancellation(event)) {
return; return;
} }
GameChannel gameChannel = event.getGameChannel(); discordSRV.scheduler().run(() -> process(event, event.getPlayer(), event.getGameChannel()));
process(event, event.getPlayer(), gameChannel);
event.markAsProcessed(); event.markAsProcessed();
} }

View File

@ -46,7 +46,7 @@ public class ServerSwitchMessageModule extends AbstractGameMessageModule<ServerS
return; return;
} }
process(event, event.getPlayer(), null); discordSRV.scheduler().run(() -> process(event, event.getPlayer(), null));
event.markAsProcessed(); event.markAsProcessed();
} }