Add placeholder to clearchat broadcast, fix console not logging chat

This commit is contained in:
Draycia committed 2021-12-21 21:21:57 -08:00
1 parent 684548f192
commit 278ae422e7
6 files changed
+18 -23

No files matched your search

@@ -54,11 +54,11 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
/** /**
* Checks if the player may receive messages from this channel. * Checks if the player may receive messages from this channel.
* *
* @param audience the audience that's receiving messages * @param player the player that's receiving messages
* @return if the player may receive messages * @return if the player may receive messages
* @since 2.0.0 * @since 2.0.0
*/ */
ChannelPermissionResult hearingPermitted(final Audience audience); ChannelPermissionResult hearingPermitted(final CarbonPlayer player);
/** /**
* Returns a list of all recipients that will receive messages from the sender. * Returns a list of all recipients that will receive messages from the sender.
@@ -77,7 +77,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
* @return the recipients that may receive messages * @return the recipients that may receive messages
* @since 2.0.0 * @since 2.0.0
*/ */
Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients); Set<CarbonPlayer> filterRecipients(final CarbonPlayer sender, final Set<CarbonPlayer> recipients);
/** /**
* Messages will be sent in this channel if they start with this prefix. * Messages will be sent in this channel if they start with this prefix.
@@ -85,7 +85,7 @@ public final class BukkitChatListener implements Listener {
} }
} }
final var recipients = new ArrayList<>(channel.filterRecipients(sender, event.viewers())); final var recipients = new ArrayList<>(channel.recipients(sender));
final var renderers = new ArrayList<KeyedRenderer>(); final var renderers = new ArrayList<KeyedRenderer>();
renderers.add(keyedRenderer(key("carbon", "default"), channel)); renderers.add(keyedRenderer(key("carbon", "default"), channel));
@@ -98,7 +98,7 @@ public final class BasicChatChannel implements ChatChannel {
} }
@Override @Override
public ChannelPermissionResult hearingPermitted(final Audience audience) { public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return ChannelPermissionResult.allowed(); return ChannelPermissionResult.allowed();
} }
@@ -119,7 +119,7 @@ public final class BasicChatChannel implements ChatChannel {
} }
@Override @Override
public Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients) { public Set<CarbonPlayer> filterRecipients(final CarbonPlayer sender, final Set<CarbonPlayer> recipients) {
recipients.removeIf(it -> !this.hearingPermitted(it).permitted()); recipients.removeIf(it -> !this.hearingPermitted(it).permitted());
return recipients; return recipients;
@@ -41,7 +41,6 @@ import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver; import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import net.kyori.adventure.permission.PermissionChecker;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.moonshine.Moonshine; import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.exception.scan.UnscannableMethodException; import net.kyori.moonshine.exception.scan.UnscannableMethodException;
@@ -56,6 +55,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Setting; import org.spongepowered.configurate.objectmapping.meta.Setting;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static net.kyori.adventure.text.Component.empty;
import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.Component.text;
@ConfigSerializable @ConfigSerializable
@@ -140,18 +140,8 @@ public final class ConfigChatChannel implements ChatChannel {
} }
@Override @Override
public ChannelPermissionResult hearingPermitted(final Audience audience) { public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
final var permission = audience.get(PermissionChecker.POINTER); return ChannelPermissionResult.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see"));
if (permission.isEmpty()) {
return ChannelPermissionResult.allowed();
}
if (permission.get().test(this.permission() + ".see")) {
return ChannelPermissionResult.allowed();
}
return ChannelPermissionResult.denied(text("Insufficient permissions!"));
} }
@Override @Override
@@ -171,7 +161,7 @@ public final class ConfigChatChannel implements ChatChannel {
} }
@Override @Override
public Set<Audience> filterRecipients(final CarbonPlayer sender, final Set<Audience> recipients) { public Set<CarbonPlayer> filterRecipients(final CarbonPlayer sender, final Set<CarbonPlayer> recipients) {
try { try {
recipients.removeIf(it -> !this.hearingPermitted(it).permitted()); recipients.removeIf(it -> !this.hearingPermitted(it).permitted());
} catch (final UnsupportedOperationException ignored) { } catch (final UnsupportedOperationException ignored) {
@@ -55,14 +55,18 @@ public class ClearChatCommand {
} }
final Component senderName; final Component senderName;
final String username;
if (handler.getSender() instanceof PlayerCommander player) { if (handler.getSender() instanceof PlayerCommander player) {
senderName = CarbonPlayer.renderName(player.carbonPlayer()); senderName = CarbonPlayer.renderName(player.carbonPlayer());
username = player.carbonPlayer().username();
} else { } else {
senderName = Component.text("Console"); senderName = Component.text("Console");
username = "Console";
} }
carbonChat.server().sendMessage(configFactory.primaryConfig().clearChatSettings().broadcast(senderName)); carbonChat.server().sendMessage(configFactory.primaryConfig().clearChatSettings()
.broadcast(senderName, username));
}) })
.build(); .build();
@@ -57,9 +57,10 @@ public class ClearChatSettings {
return this.iterations; return this.iterations;
} }
public Component broadcast(final Component displayName) { public Component broadcast(final Component displayName, final String username) {
return MiniMessage.miniMessage().deserialize(this.broadcast, return MiniMessage.miniMessage().deserialize(this.broadcast,
TemplateResolver.templates(Template.template("display_name", displayName))); TemplateResolver.templates(Template.template("display_name", displayName),
Template.template("username", username)));
} }
} }