Skip to content

Commit

Permalink
fix: Advance with the queue in case a deprecated plugin cancels the chat
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Nov 2, 2023
1 parent 9ca28ba commit 4bc8dd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
@@ -1,13 +1,16 @@
package io.github._4drian3d.signedvelocity.paper.listener;

import io.github._4drian3d.signedvelocity.common.queue.SignedQueue;
import io.github._4drian3d.signedvelocity.common.queue.SignedResult;
import io.github._4drian3d.signedvelocity.paper.SignedVelocity;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.bukkit.event.EventPriority;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.CompletableFuture;

public final class PlayerChatListener implements EventListener<AsyncChatEvent>, LocalExecutionDetector {
private final SignedQueue chatQueue;

Expand All @@ -22,7 +25,7 @@ public PlayerChatListener(final SignedVelocity plugin) {

@Override
public boolean ignoreCancelled() {
return true;
return false;
}

@Override
Expand All @@ -31,18 +34,25 @@ public void handle(final @NotNull AsyncChatEvent event) {
return;
}
final Player player = event.getPlayer();
this.chatQueue.dataFrom(player.getUniqueId())
.nextResult()
.thenAccept(result -> {
if (result.cancelled()) {
event.setCancelled(true);
} else {
final String modifiedChat = result.toModify();
if (modifiedChat != null) {
event.message(Component.text(modifiedChat));
}
}
}).join();
final CompletableFuture<SignedResult> nextResult
= this.chatQueue.dataFrom(player.getUniqueId()).nextResult();
// In case the chat has really been executed from the player,
// but some plugin has cancelled it by means of a deprecated event,
// simply let the queue advance, there is nothing to do
if (event.isCancelled()) {
return;
}

nextResult.thenAccept(result -> {
if (result.cancelled()) {
event.setCancelled(true);
} else {
final String modifiedChat = result.toModify();
if (modifiedChat != null) {
event.message(Component.text(modifiedChat));
}
}
}).join();
}

@Override
Expand Down
Expand Up @@ -21,7 +21,7 @@ public PlayerCommandListener(final SignedVelocity plugin) {

@Override
public boolean ignoreCancelled() {
return true;
return false;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
@@ -1,4 +1,4 @@
group = io.github._4drian3d
version = 1.2.0
version = 1.2.1
description = Allows you to cancel or modify messages or commands from Velocity without synchronization problems
org.gradle.jvmargs=-Xmx2G

0 comments on commit 4bc8dd4

Please sign in to comment.