mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 22:16:22 +00:00
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:
1 parent
a1c37ad84a
commit
2faaa2dcda
17 files changed
+239
-217
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -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<Audience> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user