Actually use ChannelPermissionResult reason (#335)

This commit is contained in:
Jason Penilla authored and GitHub committed 2023-11-10 09:03:08 -07:00
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);
}
}
}
}
@@ -27,11 +27,13 @@ import java.util.List;
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.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessages;
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.common.messages.SourcedAudience;
import net.draycia.carbon.common.messages.SourcedMessageSender;
import net.draycia.carbon.common.messages.SourcedReceiverResolver;
@@ -59,8 +61,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Setting;
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.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult;
@ConfigSerializable
@DefaultQualifier(NonNull.class)
@@ -68,6 +69,7 @@ public class ConfigChatChannel implements ChatChannel {
protected transient @MonotonicNonNull @Inject CarbonServer server;
private transient @MonotonicNonNull @Inject CarbonMessageRenderer renderer;
protected transient @MonotonicNonNull @Inject CarbonMessages messages;
@Comment("""
The channel's key, used to track the channel.
@@ -156,14 +158,19 @@ public class ConfigChatChannel implements ChatChannel {
}
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer carbonPlayer) {
return ChannelPermissionResult.allowedIf(text("Insufficient permissions!"), () ->
carbonPlayer.hasPermission(this.permission() + ".speak")); // carbon.channels.local.speak
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.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this.key));
return channelPermissionResult(
player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this.key),
() -> this.messages.channelNoPermission(player)
);
}
@Override
@@ -26,6 +26,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChannelPermissionResult;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.Party;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
@@ -42,6 +43,8 @@ import org.checkerframework.framework.qual.DefaultQualifier;
import org.jetbrains.annotations.NotNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult;
@ConfigSerializable
@DefaultQualifier(NonNull.class)
public class PartyChatChannel extends ConfigChatChannel {
@@ -64,16 +67,18 @@ public class PartyChatChannel extends ConfigChatChannel {
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return player.party().join() != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
return channelPermissionResult(
player.party().join() != null,
() -> this.messages.cannotUsePartyChannel(player)
);
}
@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return player.party().join() != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
return channelPermissionResult(
player.party().join() != null,
() -> this.messages.cannotUsePartyChannel(player)
);
}
@Override
@@ -82,7 +87,7 @@ public class PartyChatChannel extends ConfigChatChannel {
final @Nullable UUID party = wrapped.partyId();
if (party == null) {
if (sender.online()) {
this.messages.cannotUsePartyChannel(sender);
sender.sendMessage(this.messages.cannotUsePartyChannel(sender));
}
return new ArrayList<>();
}
@@ -24,6 +24,7 @@ import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import net.draycia.carbon.api.channels.ChannelPermissionResult;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
@@ -82,8 +83,9 @@ public final class JoinCommand extends CarbonCommand {
this.carbonMessages.channelNotFound(sender);
return;
}
if (!channel.speechPermitted(sender).permitted()) {
this.carbonMessages.channelNoPermission(sender);
final ChannelPermissionResult permitted = channel.speechPermitted(sender);
if (!permitted.permitted()) {
sender.sendMessage(permitted.reason());
return;
}
if (!sender.leftChannels().contains(channel.key())) {
@@ -24,6 +24,7 @@ import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
import cloud.commandframework.minecraft.extras.RichDescription;
import com.google.inject.Inject;
import net.draycia.carbon.api.channels.ChannelPermissionResult;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
@@ -86,8 +87,9 @@ public final class LeaveCommand extends CarbonCommand {
this.carbonMessages.channelNotFound(sender);
return;
}
if (!channel.speechPermitted(sender).permitted()) {
this.carbonMessages.channelNoPermission(sender);
final ChannelPermissionResult permitted = channel.speechPermitted(sender);
if (!permitted.permitted()) {
sender.sendMessage(permitted.reason());
return;
}
if (sender.leftChannels().contains(channel.key())) {
@@ -48,7 +48,7 @@ public interface CarbonMessages {
void channelAlreadyLeft(final Audience audience);
@Message("channel.no_permission")
void channelNoPermission(final Audience audience);
Component channelNoPermission(Audience audience);
@Message("channel.left")
void channelLeft(final Audience audience);
@@ -514,7 +514,7 @@ public interface CarbonMessages {
void playerLeftParty(Audience audience, Component partyName, Component displayName);
@Message("party.cannot_use_channel")
void cannotUsePartyChannel(Audience audience);
Component cannotUsePartyChannel(Audience audience);
@Message("deletemessage.prefix")
Component deleteMessagePrefix();
@@ -538,12 +538,12 @@ public interface CarbonMessages {
*/
@Message("integrations.towny.cannot_use_alliance_channel")
void cannotUseAllianceChannel(Audience audience);
Component cannotUseAllianceChannel(Audience audience);
@Message("integrations.towny.cannot_use_nation_channel")
void cannotUseNationChannel(Audience audience);
Component cannotUseNationChannel(Audience audience);
@Message("integrations.towny.cannot_use_town_channel")
void cannotUseTownChannel(Audience audience);
Component cannotUseTownChannel(Audience audience);
}
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import net.kyori.adventure.audience.Audience;
import net.kyori.moonshine.Moonshine;
import net.kyori.moonshine.annotation.Placeholder;
import net.kyori.moonshine.annotation.meta.ThreadSafe;
@@ -79,7 +80,8 @@ public final class StandardPlaceholderResolverStrategyButDifferent<R, I, F> impl
final Parameter parameter = methodParameters[idx];
final @Nullable Object value = parameters[idx];
if (value == null) {
// Don't resolve Audiences for now
if (value == null || parameter.getType() == Audience.class) {
// Nothing to resolve with.
continue;
}
@@ -27,6 +27,7 @@ import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.config.ConfigHeader;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -59,8 +60,8 @@ public class AllianceChannel extends NationChannel {
}
@Override
protected void cannotUseChannel(final CarbonPlayer player) {
this.messages.cannotUseAllianceChannel(player);
protected Component cannotUseChannel(final CarbonPlayer player) {
return this.messages.cannotUseAllianceChannel(player);
}
}
@@ -28,6 +28,7 @@ import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.config.ConfigHeader;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -64,8 +65,8 @@ public class NationChannel extends ResidentListChannel<Nation> {
}
@Override
protected void cannotUseChannel(final CarbonPlayer player) {
this.messages.cannotUseNationChannel(player);
protected Component cannotUseChannel(final CarbonPlayer player) {
return this.messages.cannotUseNationChannel(player);
}
}
@@ -25,10 +25,10 @@ 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.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
@@ -37,6 +37,8 @@ import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult;
@DefaultQualifier(NonNull.class)
abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatChannel {
@@ -46,23 +48,24 @@ abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatCha
""";
protected final static TownyAPI TOWNY_API = TownyAPI.getInstance();
protected transient @MonotonicNonNull @Inject CarbonMessages messages;
protected transient @MonotonicNonNull @Inject UserManager<?> users;
protected abstract @Nullable T residentList(CarbonPlayer player);
@Override
public ChannelPermissionResult speechPermitted(final CarbonPlayer player) {
return this.residentList(player) != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
return channelPermissionResult(
this.residentList(player) != null,
() -> this.cannotUseChannel(player)
);
}
@Override
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
return this.residentList(player) != null
? ChannelPermissionResult.allowed()
: ChannelPermissionResult.denied(Component.empty());
return channelPermissionResult(
this.residentList(player) != null,
() -> this.cannotUseChannel(player)
);
}
@Override
@@ -71,7 +74,7 @@ abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatCha
if (residentList == null) {
if (sender.online()) {
this.cannotUseChannel(sender);
sender.sendMessage(this.cannotUseChannel(sender));
}
return Collections.emptyList();
@@ -94,6 +97,6 @@ abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatCha
return TOWNY_API.getOnlinePlayers(residentList);
}
protected abstract void cannotUseChannel(CarbonPlayer player);
protected abstract Component cannotUseChannel(CarbonPlayer player);
}
@@ -28,6 +28,7 @@ import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.config.ConfigHeader;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -64,8 +65,8 @@ public class TownChannel extends ResidentListChannel<Town> {
}
@Override
protected void cannotUseChannel(final CarbonPlayer player) {
this.messages.cannotUseTownChannel(player);
protected Component cannotUseChannel(final CarbonPlayer player) {
return this.messages.cannotUseTownChannel(player);
}
}