mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Trigger bukkit chat events when using message commands
This commit is contained in:
7 files changed
+45
-2
No files matched your search
@@ -227,7 +227,16 @@ public interface CarbonPlayer extends Audience, Identified {
|
||||
void sendMessageAsPlayer(final String message);
|
||||
|
||||
/**
|
||||
* Returns whether or not the player is online.
|
||||
* Checks if the player is allowed to send the message or not.
|
||||
*
|
||||
* @param message the message
|
||||
* @return if the message can be sent in chat
|
||||
* @since 2.0.4
|
||||
*/
|
||||
boolean speechPermitted(final String message);
|
||||
|
||||
/**
|
||||
* Returns whether the player is online.
|
||||
*
|
||||
* @return if the player is online.
|
||||
* @since 2.0.0
|
||||
|
||||
@@ -59,6 +59,10 @@ public final class BukkitChatListener implements Listener {
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onPlayerChat(final @NonNull AsyncChatEvent event) {
|
||||
if (event.viewers().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final var playerResult = this.carbonChat.server().player(event.getPlayer().getUniqueId()).join();
|
||||
final @Nullable CarbonPlayer sender = playerResult.player();
|
||||
|
||||
|
||||
@@ -19,9 +19,11 @@
|
||||
*/
|
||||
package net.draycia.carbon.bukkit.users;
|
||||
|
||||
import io.papermc.paper.event.player.AsyncChatEvent;
|
||||
import java.util.Collection;
|
||||
import java.util.Locale;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
@@ -30,6 +32,7 @@ import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.AsyncPlayerChatEvent;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@@ -128,6 +131,14 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
||||
this.player().ifPresent(player -> player.chat(message));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean speechPermitted(final String message) {
|
||||
// ...........
|
||||
return new AsyncPlayerChatEvent(!Bukkit.isPrimaryThread(), this.player().get(), message, Set.of()).callEvent()
|
||||
&& new AsyncChatEvent(!Bukkit.isPrimaryThread(), this.player().get(), Set.of(),
|
||||
(player, name, msg, receiver) -> msg, Component.text(message), Component.text(message)).callEvent();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean online() {
|
||||
return this.player().isPresent();
|
||||
|
||||
@@ -319,6 +319,11 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
ChatChannel channel,
|
||||
final String plainMessage
|
||||
) {
|
||||
// Should we silent exit here? Chances are whatever caused the
|
||||
if (!sender.speechPermitted(plainMessage)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (final var chatChannel : this) {
|
||||
if (chatChannel.quickPrefix() == null) {
|
||||
continue;
|
||||
|
||||
@@ -172,7 +172,11 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
|
||||
@Override
|
||||
public Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients) {
|
||||
recipients.removeIf(it -> !this.hearingPermitted(it).permitted());
|
||||
try {
|
||||
recipients.removeIf(it -> !this.hearingPermitted(it).permitted());
|
||||
} catch (final UnsupportedOperationException ignored) {
|
||||
|
||||
}
|
||||
|
||||
return recipients;
|
||||
}
|
||||
|
||||
@@ -169,6 +169,11 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean speechPermitted(final String message) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean online() {
|
||||
return false;
|
||||
|
||||
@@ -214,4 +214,9 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
return this.carbonPlayerCommon().identity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean speechPermitted(final String message) {
|
||||
return this.carbonPlayerCommon().speechPermitted(message);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user