Improve the channel permissions API (#523)

* Improve the channel permissions API

* Don't check join permission when leaving a channel

* Adjust suggestions
This commit is contained in:
Jason Penilla authored and GitHub committed 2024-08-19 17:24:33 -07:00
1 parent a1c37ad84a
commit 2faaa2dcda
17 files changed
+239 -217

No files matched your search

@@ -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 <https://www.gnu.org/licenses/>.
*/
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.
*
* <p>An example of a dynamic permissions is the built-in party channel that only allows players in a party to join.</p>
*
* <p>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.</p>
*
* <p>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.</p>
*
* @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<CarbonPlayer, ChannelPermissionResult> 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;
}
};
}
}
@@ -109,4 +109,18 @@ public interface ChannelRegistry {
*/
void allKeys(Consumer<Key> action);
/**
* Create a {@link ChannelPermissions channel permissions handler} for the provided base permission string.
*
* <p>The handler will check the base permission for joins, {@literal <base>.see} for receiving/seeing messages,
* and {@literal <base>.speak} for speaking/sending messages.</p>
*
* <p>The built-in deny messages are used, same as user-configured config channels.</p>
*
* @param permission permission string
* @return permission handler
* @since 3.0.0
*/
ChannelPermissions permission(String permission);
}
@@ -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.
*
* <p>An example of a dynamic permissions is the built-in party channel that only allows players in a party to join.</p>
*
* <p>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.</p>
*
* <p>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.</p>
*
* @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.
*