From 2faaa2dcda579175ec2c71552e97f1e74114a1cc Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:24:33 -0700 Subject: [PATCH] Improve the channel permissions API (#523) * Improve the channel permissions API * Don't check join permission when leaving a channel * Adjust suggestions --- .../api/channels/ChannelPermissions.java | 108 ++++++++++++++++++ .../carbon/api/channels/ChannelRegistry.java | 14 +++ .../carbon/api/channels/ChatChannel.java | 43 +------ .../channels/CarbonChannelRegistry.java | 9 +- .../channels/ChannelPermissionsImpl.java | 58 ++++++++++ .../common/channels/ConfigChatChannel.java | 33 +----- .../common/channels/PartyChatChannel.java | 29 +---- .../common/command/commands/JoinCommand.java | 21 ++-- .../common/command/commands/LeaveCommand.java | 11 +- .../listeners/ChatListenerInternal.java | 2 +- .../common/users/WrappedCarbonPlayer.java | 2 +- .../carbon/common/util/ChannelUtils.java | 2 +- .../integration/fuuid/AllianceChannel.java | 26 +---- .../integration/fuuid/FactionChannel.java | 26 +---- .../paper/integration/fuuid/TruceChannel.java | 24 +--- .../integration/mcmmo/McmmoPartyChannel.java | 24 +--- .../towny/ResidentListChannel.java | 24 +--- 17 files changed, 239 insertions(+), 217 deletions(-) create mode 100644 api/src/main/java/net/draycia/carbon/api/channels/ChannelPermissions.java create mode 100644 common/src/main/java/net/draycia/carbon/common/channels/ChannelPermissionsImpl.java diff --git a/api/src/main/java/net/draycia/carbon/api/channels/ChannelPermissions.java b/api/src/main/java/net/draycia/carbon/api/channels/ChannelPermissions.java new file mode 100644 index 00000000..823af1bd --- /dev/null +++ b/api/src/main/java/net/draycia/carbon/api/channels/ChannelPermissions.java @@ -0,0 +1,108 @@ +/* + * CarbonChat + * + * Copyright (c) 2024 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.api.channels; + +import java.util.function.Function; +import net.draycia.carbon.api.users.CarbonPlayer; + +/** + * Permissions handling for a channel. + * + * @since 3.0.0 + */ +public interface ChannelPermissions { + + /** + * Checks if the player may join this channel. + * + * @param carbonPlayer the player attempting to join + * @return if the player may join + * @since 3.0.0 + */ + ChannelPermissionResult joinPermitted(CarbonPlayer carbonPlayer); + + /** + * Checks if the player may send messages in this channel. + * + * @param carbonPlayer the player attempting to speak + * @return if the player may speak + * @since 3.0.0 + */ + ChannelPermissionResult speechPermitted(CarbonPlayer carbonPlayer); + + /** + * Checks if the player may receive messages from this channel. + * + * @param player the player that's receiving messages + * @return if the player may receive messages + * @since 3.0.0 + */ + ChannelPermissionResult hearingPermitted(CarbonPlayer player); + + /** + * Returns whether the result of {@link #joinPermitted(CarbonPlayer)} is dynamic. + * + *

An example of a dynamic permissions is the built-in party channel that only allows players in a party to join.

+ * + *

An example of static permissions is the built-in config channels that simply check permission strings. The fact that a player's + * permissions may change during gameplay does not make the permission dynamic, as the server will resend commands on permission changes.

+ * + *

If the result is static, then we can avoid sending commands to the player that they will just get denied use + * of on execute. If it's dynamic, we must send the command regardless in case they gain permission later.

+ * + * @return whether the permissions are dynamic + * @since 3.0.0 + */ + boolean dynamic(); + + /** + * Creates a new {@link ChannelPermissions} that performs the same check for + * {@link #joinPermitted(CarbonPlayer)}, {@link #hearingPermitted(CarbonPlayer)}, + * and {@link #speechPermitted(CarbonPlayer)}. + * + * @param check permission check + * @return new permissions object + * @since 3.0.0 + */ + static ChannelPermissions uniformDynamic(final Function check) { + return new ChannelPermissions() { + @Override + public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { + return check.apply(player); + } + + @Override + public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { + return check.apply(player); + } + + @Override + public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { + return check.apply(player); + } + + @Override + public boolean dynamic() { + return true; + } + }; + } + +} diff --git a/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java b/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java index c22e3b87..970e973f 100644 --- a/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java +++ b/api/src/main/java/net/draycia/carbon/api/channels/ChannelRegistry.java @@ -109,4 +109,18 @@ public interface ChannelRegistry { */ void allKeys(Consumer action); + /** + * Create a {@link ChannelPermissions channel permissions handler} for the provided base permission string. + * + *

The handler will check the base permission for joins, {@literal .see} for receiving/seeing messages, + * and {@literal .speak} for speaking/sending messages.

+ * + *

The built-in deny messages are used, same as user-configured config channels.

+ * + * @param permission permission string + * @return permission handler + * @since 3.0.0 + */ + ChannelPermissions permission(String permission); + } diff --git a/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java b/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java index c258d71f..ceaedb62 100644 --- a/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java +++ b/api/src/main/java/net/draycia/carbon/api/channels/ChatChannel.java @@ -38,31 +38,12 @@ import org.checkerframework.framework.qual.DefaultQualifier; public interface ChatChannel extends Keyed, ChatComponentRenderer { /** - * Checks if the player may join this channel. + * Returns the permissions handler for the channel. * - * @param carbonPlayer the player attempting to join - * @return if the player may join + * @return the permissions handler * @since 3.0.0 */ - ChannelPermissionResult joinPermitted(CarbonPlayer carbonPlayer); - - /** - * Checks if the player may send messages in this channel. - * - * @param carbonPlayer the player attempting to speak - * @return if the player may speak - * @since 3.0.0 - */ - ChannelPermissionResult speechPermitted(CarbonPlayer carbonPlayer); - - /** - * Checks if the player may receive messages from this channel. - * - * @param player the player that's receiving messages - * @return if the player may receive messages - * @since 3.0.0 - */ - ChannelPermissionResult hearingPermitted(CarbonPlayer player); + ChannelPermissions permissions(); /** * Returns a list of all recipients that will receive messages from the sender. @@ -89,24 +70,6 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer { */ boolean shouldRegisterCommands(); - /** - * Returns whether the result of {@link #joinPermitted(CarbonPlayer)} is dynamic. - * - *

An example of a dynamic permissions is the built-in party channel that only allows players in a party to join.

- * - *

An example of static permissions is the built-in config channels that simply check permission strings. The fact that a player's - * permissions may change during gameplay does not make the permission dynamic, as the server will resend commands on permission changes.

- * - *

If the result is static, then we can avoid sending commands to the player that they will just get denied use - * of on execute. If it's dynamic, we must send the command regardless in case they gain permission later.

- * - * @return whether the permissions are dynamic - * @since 3.0.0 - */ - default boolean dynamicPermission() { - return true; - } - /** * The text that can be used to refer to this channel in commands. * diff --git a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java index e2ce0c79..03b307e1 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/CarbonChannelRegistry.java @@ -41,6 +41,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.function.Supplier; import java.util.stream.Collectors; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.channels.ChannelRegistry; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.event.CarbonEventHandler; @@ -364,12 +365,12 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann channel.commandAliases(), commandManager.createDefaultCommandMeta()) .optional("message", signedGreedyStringParser()); - if (!channel.dynamicPermission()) { + if (!channel.permissions().dynamic()) { builder = builder.permission(PredicatePermission.of(sender -> { if (!(sender instanceof CarbonPlayer player)) { return true; } - return channel.joinPermitted(player).permitted(); + return channel.permissions().joinPermitted(player).permitted(); })); } @@ -516,4 +517,8 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann ); } + @Override + public ChannelPermissions permission(final String permission) { + return new ChannelPermissionsImpl(permission, this.carbonMessages); + } } diff --git a/common/src/main/java/net/draycia/carbon/common/channels/ChannelPermissionsImpl.java b/common/src/main/java/net/draycia/carbon/common/channels/ChannelPermissionsImpl.java new file mode 100644 index 00000000..e0058975 --- /dev/null +++ b/common/src/main/java/net/draycia/carbon/common/channels/ChannelPermissionsImpl.java @@ -0,0 +1,58 @@ +/* + * CarbonChat + * + * Copyright (c) 2024 Josua Parks (Vicarious) + * Contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package net.draycia.carbon.common.channels; + +import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; +import net.draycia.carbon.api.users.CarbonPlayer; +import net.draycia.carbon.common.messages.CarbonMessages; + +import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult; + +public record ChannelPermissionsImpl(String permission, CarbonMessages messages) implements ChannelPermissions { + @Override + public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { + return channelPermissionResult( + player.hasPermission(this.permission()), + () -> this.messages.channelNoPermission(player) + ); + } + + @Override + public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { + return channelPermissionResult( + player.hasPermission(this.permission() + ".speak"), + () -> this.messages.channelNoPermission(player) + ); + } + + @Override + public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { + return channelPermissionResult( + player.hasPermission(this.permission() + ".see"), + () -> this.messages.channelNoPermission(player) + ); + } + + @Override + public boolean dynamic() { + return false; + } +} diff --git a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java index 26b1e9c4..5d1834c1 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/ConfigChatChannel.java @@ -29,7 +29,7 @@ import java.util.Map; import java.util.Objects; import java.util.UUID; import net.draycia.carbon.api.CarbonServer; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; @@ -64,7 +64,6 @@ import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.objectmapping.meta.Setting; import static java.util.Objects.requireNonNull; -import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult; @ConfigSerializable @DefaultQualifier(NonNull.class) @@ -161,32 +160,8 @@ public class ConfigChatChannel implements ChatChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( - player.hasPermission(this.permission()), - () -> this.messages.channelNoPermission(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - player.hasPermission(this.permission() + ".speak"), - () -> this.messages.channelNoPermission(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this.key), - () -> this.messages.channelNoPermission(player) - ); - } - - @Override - public boolean dynamicPermission() { - return false; + public ChannelPermissions permissions() { + return new ChannelPermissionsImpl(this.permission(), this.messages); } @Override @@ -194,7 +169,7 @@ public class ConfigChatChannel implements ChatChannel { final List recipients = new ArrayList<>(); for (final CarbonPlayer player : this.server.players()) { - if (this.hearingPermitted(player).permitted()) { + if (this.permissions().hearingPermitted(player).permitted() && !player.leftChannels().contains(this.key)) { recipients.add(player); } } diff --git a/common/src/main/java/net/draycia/carbon/common/channels/PartyChatChannel.java b/common/src/main/java/net/draycia/carbon/common/channels/PartyChatChannel.java index 3adffbf9..0b3d548f 100644 --- a/common/src/main/java/net/draycia/carbon/common/channels/PartyChatChannel.java +++ b/common/src/main/java/net/draycia/carbon/common/channels/PartyChatChannel.java @@ -19,23 +19,20 @@ */ package net.draycia.carbon.common.channels; -import com.google.inject.Inject; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.UUID; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.Party; import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; -import net.draycia.carbon.common.messages.CarbonMessages; import net.draycia.carbon.common.messages.SourcedAudience; import net.draycia.carbon.common.users.WrappedCarbonPlayer; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.Component; -import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.framework.qual.DefaultQualifier; @@ -50,8 +47,6 @@ public class PartyChatChannel extends ConfigChatChannel { public static final String FILE_NAME = "partychat.conf"; - private transient @MonotonicNonNull @Inject CarbonMessages messages; - public PartyChatChannel() { this.key = Key.key("carbon", "partychat"); this.commandAliases = List.of("pc"); @@ -63,27 +58,11 @@ public class PartyChatChannel extends ConfigChatChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( player.party().join() != null, () -> this.messages.cannotUsePartyChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - player.party().join() != null, - () -> this.messages.cannotUsePartyChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - player.party().join() != null, - () -> this.messages.cannotUsePartyChannel(player) - ); + )); } @Override diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java index 5c30c31c..7d0edf49 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/JoinCommand.java @@ -20,6 +20,7 @@ package net.draycia.carbon.common.command.commands; import com.google.inject.Inject; +import java.util.Objects; import net.draycia.carbon.api.channels.ChannelPermissionResult; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; @@ -71,12 +72,18 @@ public final class JoinCommand extends CarbonCommand { @Override public void init() { final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases()) - .required("channel", greedyStringParser(), SuggestionProvider.blocking( - (context, s) -> { - final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer(); - return sender.leftChannels().stream().map(Key::value).map(Suggestion::suggestion).toList(); - } - )) + .required("channel", greedyStringParser(), SuggestionProvider.blocking((context, s) -> { + final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer(); + return sender.leftChannels().stream() + .map(this.channelRegistry::channel) + .filter(Objects::nonNull) + .filter(channel -> channel.permissions().joinPermitted(sender).permitted() + || channel.permissions().hearingPermitted(sender).permitted() + || channel.permissions().speechPermitted(sender).permitted()) + .map(channel -> channel.key().value()) + .map(Suggestion::suggestion) + .toList(); + })) .permission("carbon.join") .senderType(PlayerCommander.class) .commandDescription(richDescription(this.carbonMessages.commandJoinDescription())) @@ -87,7 +94,7 @@ public final class JoinCommand extends CarbonCommand { this.carbonMessages.channelNotFound(sender); return; } - final ChannelPermissionResult permitted = channel.joinPermitted(sender); + final ChannelPermissionResult permitted = channel.permissions().joinPermitted(sender); if (!permitted.permitted()) { sender.sendMessage(permitted.reason()); return; diff --git a/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java b/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java index 9eb6f8bf..a24d74c2 100644 --- a/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java +++ b/common/src/main/java/net/draycia/carbon/common/command/commands/LeaveCommand.java @@ -20,7 +20,7 @@ package net.draycia.carbon.common.command.commands; import com.google.inject.Inject; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import java.util.Objects; import net.draycia.carbon.api.channels.ChatChannel; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.common.channels.CarbonChannelRegistry; @@ -75,7 +75,9 @@ public final class LeaveCommand extends CarbonCommand { final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer(); return this.channelRegistry.keys().stream() .map(this.channelRegistry::channel) - .filter(x -> !sender.leftChannels().contains(x.key()) && x.speechPermitted(sender).permitted()) + .filter(Objects::nonNull) + .filter(x -> !sender.leftChannels().contains(x.key()) + && (x.permissions().joinPermitted(sender).permitted() || x.permissions().hearingPermitted(sender).permitted() || x.permissions().speechPermitted(sender).permitted())) .map(x -> x.key().value()) .map(Suggestion::suggestion) .toList(); @@ -90,11 +92,6 @@ public final class LeaveCommand extends CarbonCommand { this.carbonMessages.channelNotFound(sender); return; } - final ChannelPermissionResult permitted = channel.joinPermitted(sender); - if (!permitted.permitted()) { - sender.sendMessage(permitted.reason()); - return; - } if (sender.leftChannels().contains(channel.key())) { this.carbonMessages.channelAlreadyLeft(sender); return; diff --git a/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java b/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java index 727a35d4..b1514743 100644 --- a/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java +++ b/common/src/main/java/net/draycia/carbon/common/listeners/ChatListenerInternal.java @@ -70,7 +70,7 @@ public abstract class ChatListenerInternal { } protected @Nullable CarbonChatEventImpl prepareAndEmitChatEvent(final CarbonPlayer sender, final String messageContent, final @Nullable SignedMessage signedMessage, final ChatChannel channel) { - final ChannelPermissionResult permitted = channel.speechPermitted(sender); + final ChannelPermissionResult permitted = channel.permissions().speechPermitted(sender); if (!permitted.permitted()) { sender.sendMessage(permitted.reason()); return null; diff --git a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java index fef7e40b..6c28d62e 100644 --- a/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java +++ b/common/src/main/java/net/draycia/carbon/common/users/WrappedCarbonPlayer.java @@ -248,7 +248,7 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer { continue; } - if (text.startsWith(prefix) && chatChannel.speechPermitted(this).permitted()) { + if (text.startsWith(prefix) && chatChannel.permissions().speechPermitted(this).permitted()) { channel = chatChannel; formattedMessage = formattedMessage.replaceText(TextReplacementConfig.builder() .once() diff --git a/common/src/main/java/net/draycia/carbon/common/util/ChannelUtils.java b/common/src/main/java/net/draycia/carbon/common/util/ChannelUtils.java index 5172f9e7..9a9962f7 100644 --- a/common/src/main/java/net/draycia/carbon/common/util/ChannelUtils.java +++ b/common/src/main/java/net/draycia/carbon/common/util/ChannelUtils.java @@ -35,7 +35,7 @@ public final class ChannelUtils { // TODO: Emit events for (final CarbonPlayer recipient : CarbonChatProvider.carbonChat().server().players()) { - if (channel.hearingPermitted(recipient).permitted()) { + if (channel.permissions().hearingPermitted(recipient).permitted() && !recipient.leftChannels().contains(channel.key())) { recipient.sendMessage(msg); } } diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AllianceChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AllianceChannel.java index 34fe50ff..e2342ce1 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AllianceChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/AllianceChannel.java @@ -28,11 +28,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; -import net.draycia.carbon.common.messages.CarbonMessages; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import org.bukkit.entity.Player; @@ -50,7 +49,6 @@ public class AllianceChannel extends AbstractFactionsChannel { public static final String FILE_NAME = "factionsuuid-alliancechat.conf"; - private transient @MonotonicNonNull @Inject CarbonMessages messages; private transient @MonotonicNonNull @Inject UserManager users; public AllianceChannel() { @@ -65,27 +63,11 @@ public class AllianceChannel extends AbstractFactionsChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( this.hasRelations(player, Relation.ALLY), () -> this.messages.cannotUseFactionAllianceChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.hasRelations(player, Relation.ALLY), - () -> this.messages.cannotUseFactionAllianceChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.hasRelations(player, Relation.ALLY), - () -> this.messages.cannotUseFactionAllianceChannel(player) - ); + )); } @Override diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionChannel.java index 7e3fef3f..a110c075 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/FactionChannel.java @@ -25,11 +25,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; -import net.draycia.carbon.common.messages.CarbonMessages; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import org.bukkit.entity.Player; @@ -47,7 +46,6 @@ public class FactionChannel extends AbstractFactionsChannel { public static final String FILE_NAME = "factionsuuid-factionchat.conf"; - private transient @MonotonicNonNull @Inject CarbonMessages messages; private transient @MonotonicNonNull @Inject UserManager users; public FactionChannel() { @@ -62,27 +60,11 @@ public class FactionChannel extends AbstractFactionsChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( this.faction(player) != null, () -> this.messages.cannotUseFactionChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.faction(player) != null, - () -> this.messages.cannotUseFactionChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.faction(player) != null, - () -> this.messages.cannotUseFactionChannel(player) - ); + )); } @Override diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/TruceChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/TruceChannel.java index 53213d56..91b554b4 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/TruceChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/fuuid/TruceChannel.java @@ -28,7 +28,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource; @@ -65,27 +65,11 @@ public class TruceChannel extends AbstractFactionsChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( this.hasRelations(player, Relation.TRUCE), () -> this.messages.cannotUseTruceChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.hasRelations(player, Relation.TRUCE), - () -> this.messages.cannotUseTruceChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.hasRelations(player, Relation.TRUCE), - () -> this.messages.cannotUseTruceChannel(player) - ); + )); } @Override diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/mcmmo/McmmoPartyChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/mcmmo/McmmoPartyChannel.java index eb676ef0..60b04e3f 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/mcmmo/McmmoPartyChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/mcmmo/McmmoPartyChannel.java @@ -26,7 +26,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.common.channels.ConfigChatChannel; @@ -65,27 +65,11 @@ public class McmmoPartyChannel extends ConfigChatChannel { } @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( this.party(player) != null, () -> this.messages.cannotUseMcmmoPartyChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.party(player) != null, - () -> this.messages.cannotUseMcmmoPartyChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.party(player) != null, - () -> this.messages.cannotUseMcmmoPartyChannel(player) - ); + )); } @Override diff --git a/paper/src/main/java/net/draycia/carbon/paper/integration/towny/ResidentListChannel.java b/paper/src/main/java/net/draycia/carbon/paper/integration/towny/ResidentListChannel.java index d128ee4c..523e8f6e 100644 --- a/paper/src/main/java/net/draycia/carbon/paper/integration/towny/ResidentListChannel.java +++ b/paper/src/main/java/net/draycia/carbon/paper/integration/towny/ResidentListChannel.java @@ -25,7 +25,7 @@ import com.palmergames.bukkit.towny.object.ResidentList; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import net.draycia.carbon.api.channels.ChannelPermissionResult; +import net.draycia.carbon.api.channels.ChannelPermissions; import net.draycia.carbon.api.users.CarbonPlayer; import net.draycia.carbon.api.users.UserManager; import net.draycia.carbon.common.channels.ConfigChatChannel; @@ -53,27 +53,11 @@ abstract class ResidentListChannel extends ConfigChatCha protected abstract @Nullable T residentList(CarbonPlayer player); @Override - public ChannelPermissionResult joinPermitted(final CarbonPlayer player) { - return channelPermissionResult( + public ChannelPermissions permissions() { + return ChannelPermissions.uniformDynamic(player -> channelPermissionResult( this.residentList(player) != null, () -> this.cannotUseChannel(player) - ); - } - - @Override - public ChannelPermissionResult speechPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.residentList(player) != null, - () -> this.cannotUseChannel(player) - ); - } - - @Override - public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) { - return channelPermissionResult( - this.residentList(player) != null, - () -> this.cannotUseChannel(player) - ); + )); } @Override