mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Actually use ChannelPermissionResult reason (#335)
This commit is contained in:
1 parent
9ed08dce18
commit
42cf881e5f
13 files changed
+209
-97
No files matched your search
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 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.Supplier;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
/**
|
||||
* Represents the result of a channel permission check.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface ChannelPermissionResult {
|
||||
|
||||
/**
|
||||
* Check whether the action checked was permitted.
|
||||
*
|
||||
* @return permitted
|
||||
* @since 3.0.0
|
||||
*/
|
||||
boolean permitted();
|
||||
|
||||
/**
|
||||
* Reason for permission being denied. When the action
|
||||
* was permitted, this should be equal to {@link Component#empty()}.
|
||||
*
|
||||
* @return deny reason
|
||||
* @since 3.0.0
|
||||
*/
|
||||
Component reason();
|
||||
|
||||
/**
|
||||
* Returns a result denoting that the player is permitted for the action.
|
||||
*
|
||||
* @return that the action is allowed
|
||||
* @since 3.0.0
|
||||
*/
|
||||
static ChannelPermissionResult allowed() {
|
||||
return ChannelPermissionResultImpl.ALLOWED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a result denoting that the action is denied for the player.
|
||||
*
|
||||
* @param reason the reason the action was denied
|
||||
* @return that the action is denied
|
||||
* @since 3.0.0
|
||||
*/
|
||||
static ChannelPermissionResult denied(final Component reason) {
|
||||
return new ChannelPermissionResultImpl(false, () -> reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a result denoting that the action is denied for the player.
|
||||
*
|
||||
* @param reason the reason the action was denied
|
||||
* @return that the action is denied
|
||||
* @since 3.0.0
|
||||
*/
|
||||
static ChannelPermissionResult denied(final Supplier<Component> reason) {
|
||||
return new ChannelPermissionResultImpl(false, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link ChannelPermissionResult} based on {@code allowed},
|
||||
* computing {@code denyReason} when needed.
|
||||
*
|
||||
* @param allowed whether the result is allowed
|
||||
* @param denyReason deny reason supplier
|
||||
* @return permission result
|
||||
* @since 3.0.0
|
||||
*/
|
||||
static ChannelPermissionResult channelPermissionResult(
|
||||
final boolean allowed,
|
||||
final Supplier<Component> denyReason
|
||||
) {
|
||||
if (allowed) {
|
||||
return allowed();
|
||||
}
|
||||
return denied(denyReason);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2023 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.Supplier;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
record ChannelPermissionResultImpl(
|
||||
boolean permitted,
|
||||
Supplier<Component> reasonSupplier
|
||||
) implements ChannelPermissionResult {
|
||||
|
||||
static final ChannelPermissionResult ALLOWED =
|
||||
new ChannelPermissionResultImpl(true, Component::empty);
|
||||
|
||||
@Override
|
||||
public Component reason() {
|
||||
return this.reasonSupplier.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,19 +20,15 @@
|
||||
package net.draycia.carbon.api.channels;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.ChatComponentRenderer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Keyed;
|
||||
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;
|
||||
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
|
||||
/**
|
||||
* ChatChannel interface, supplies a formatter and filters recipients.<br>
|
||||
* Extends Keyed for identification purposes.
|
||||
@@ -47,7 +43,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
|
||||
*
|
||||
* @param carbonPlayer the player attempting to speak
|
||||
* @return if the player may speak
|
||||
* @since 2.0.0
|
||||
* @since 3.0.0
|
||||
*/
|
||||
ChannelPermissionResult speechPermitted(CarbonPlayer carbonPlayer);
|
||||
|
||||
@@ -56,7 +52,7 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
|
||||
*
|
||||
* @param player the player that's receiving messages
|
||||
* @return if the player may receive messages
|
||||
* @since 2.0.0
|
||||
* @since 3.0.0
|
||||
*/
|
||||
ChannelPermissionResult hearingPermitted(CarbonPlayer player);
|
||||
|
||||
@@ -128,56 +124,4 @@ public interface ChatChannel extends Keyed, ChatComponentRenderer {
|
||||
*/
|
||||
boolean emptyRadiusRecipientsMessage();
|
||||
|
||||
/**
|
||||
* Represents the result of a channel permission check.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
record ChannelPermissionResult(boolean permitted, Component reason) {
|
||||
|
||||
private static final ChannelPermissionResult ALLOWED =
|
||||
new ChannelPermissionResult(true, empty());
|
||||
|
||||
/**
|
||||
* Returns a result denoting that the player is permitted for the action.
|
||||
*
|
||||
* @return that the action is allowed
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public static ChannelPermissionResult allowed() {
|
||||
return ALLOWED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a result denoting that the action is denied for the player.
|
||||
*
|
||||
* @param reason the reason the action was denied
|
||||
* @return that the action is denied
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public static ChannelPermissionResult denied(final Component reason) {
|
||||
return new ChannelPermissionResult(false, reason);
|
||||
}
|
||||
|
||||
/**
|
||||
* If True is supplied, returns a result denoting that the action was allowed. Otherwise, a result denoting that the action was denied.
|
||||
*
|
||||
* @param reason the reason, if any, that the action was allowed
|
||||
* @param supplier the supplier to check
|
||||
* @return an allowed result if true, otherwise a denied result
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public static ChannelPermissionResult allowedIf(
|
||||
final Component reason,
|
||||
final Supplier<Boolean> supplier
|
||||
) {
|
||||
if (supplier.get()) {
|
||||
return ALLOWED;
|
||||
} else {
|
||||
return denied(reason);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user