mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +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
@@ -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);
|
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 {
|
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 the permissions handler
|
||||||
* @return if the player may join
|
|
||||||
* @since 3.0.0
|
* @since 3.0.0
|
||||||
*/
|
*/
|
||||||
ChannelPermissionResult joinPermitted(CarbonPlayer carbonPlayer);
|
ChannelPermissions permissions();
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 a list of all recipients that will receive messages from the sender.
|
* 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();
|
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.
|
* The text that can be used to refer to this channel in commands.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
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.ChannelRegistry;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.event.CarbonEventHandler;
|
import net.draycia.carbon.api.event.CarbonEventHandler;
|
||||||
@@ -364,12 +365,12 @@ public class CarbonChannelRegistry extends ChatListenerInternal implements Chann
|
|||||||
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
channel.commandAliases(), commandManager.createDefaultCommandMeta())
|
||||||
.optional("message", signedGreedyStringParser());
|
.optional("message", signedGreedyStringParser());
|
||||||
|
|
||||||
if (!channel.dynamicPermission()) {
|
if (!channel.permissions().dynamic()) {
|
||||||
builder = builder.permission(PredicatePermission.of(sender -> {
|
builder = builder.permission(PredicatePermission.of(sender -> {
|
||||||
if (!(sender instanceof CarbonPlayer player)) {
|
if (!(sender instanceof CarbonPlayer player)) {
|
||||||
return true;
|
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.Objects;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import net.draycia.carbon.api.CarbonServer;
|
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.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
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 org.spongepowered.configurate.objectmapping.meta.Setting;
|
||||||
|
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult;
|
|
||||||
|
|
||||||
@ConfigSerializable
|
@ConfigSerializable
|
||||||
@DefaultQualifier(NonNull.class)
|
@DefaultQualifier(NonNull.class)
|
||||||
@@ -161,32 +160,8 @@ public class ConfigChatChannel implements ChatChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return new ChannelPermissionsImpl(this.permission(), this.messages);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -194,7 +169,7 @@ public class ConfigChatChannel implements ChatChannel {
|
|||||||
final List<Audience> recipients = new ArrayList<>();
|
final List<Audience> recipients = new ArrayList<>();
|
||||||
|
|
||||||
for (final CarbonPlayer player : this.server.players()) {
|
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);
|
recipients.add(player);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,23 +19,20 @@
|
|||||||
*/
|
*/
|
||||||
package net.draycia.carbon.common.channels;
|
package net.draycia.carbon.common.channels;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.UUID;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.Party;
|
import net.draycia.carbon.api.users.Party;
|
||||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
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.messages.SourcedAudience;
|
||||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||||
import net.kyori.adventure.audience.Audience;
|
import net.kyori.adventure.audience.Audience;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.text.Component;
|
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.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||||
@@ -50,8 +47,6 @@ public class PartyChatChannel extends ConfigChatChannel {
|
|||||||
|
|
||||||
public static final String FILE_NAME = "partychat.conf";
|
public static final String FILE_NAME = "partychat.conf";
|
||||||
|
|
||||||
private transient @MonotonicNonNull @Inject CarbonMessages messages;
|
|
||||||
|
|
||||||
public PartyChatChannel() {
|
public PartyChatChannel() {
|
||||||
this.key = Key.key("carbon", "partychat");
|
this.key = Key.key("carbon", "partychat");
|
||||||
this.commandAliases = List.of("pc");
|
this.commandAliases = List.of("pc");
|
||||||
@@ -63,27 +58,11 @@ public class PartyChatChannel extends ConfigChatChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
player.party().join() != null,
|
player.party().join() != null,
|
||||||
() -> this.messages.cannotUsePartyChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
package net.draycia.carbon.common.command.commands;
|
package net.draycia.carbon.common.command.commands;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
import java.util.Objects;
|
||||||
import net.draycia.carbon.api.channels.ChannelPermissionResult;
|
import net.draycia.carbon.api.channels.ChannelPermissionResult;
|
||||||
import net.draycia.carbon.api.channels.ChatChannel;
|
import net.draycia.carbon.api.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
@@ -71,12 +72,18 @@ public final class JoinCommand extends CarbonCommand {
|
|||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
|
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
|
||||||
.required("channel", greedyStringParser(), SuggestionProvider.blocking(
|
.required("channel", greedyStringParser(), SuggestionProvider.blocking((context, s) -> {
|
||||||
(context, s) -> {
|
final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer();
|
||||||
final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer();
|
return sender.leftChannels().stream()
|
||||||
return sender.leftChannels().stream().map(Key::value).map(Suggestion::suggestion).toList();
|
.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")
|
.permission("carbon.join")
|
||||||
.senderType(PlayerCommander.class)
|
.senderType(PlayerCommander.class)
|
||||||
.commandDescription(richDescription(this.carbonMessages.commandJoinDescription()))
|
.commandDescription(richDescription(this.carbonMessages.commandJoinDescription()))
|
||||||
@@ -87,7 +94,7 @@ public final class JoinCommand extends CarbonCommand {
|
|||||||
this.carbonMessages.channelNotFound(sender);
|
this.carbonMessages.channelNotFound(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ChannelPermissionResult permitted = channel.joinPermitted(sender);
|
final ChannelPermissionResult permitted = channel.permissions().joinPermitted(sender);
|
||||||
if (!permitted.permitted()) {
|
if (!permitted.permitted()) {
|
||||||
sender.sendMessage(permitted.reason());
|
sender.sendMessage(permitted.reason());
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
package net.draycia.carbon.common.command.commands;
|
package net.draycia.carbon.common.command.commands;
|
||||||
|
|
||||||
import com.google.inject.Inject;
|
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.channels.ChatChannel;
|
||||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||||
@@ -75,7 +75,9 @@ public final class LeaveCommand extends CarbonCommand {
|
|||||||
final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer();
|
final CarbonPlayer sender = ((PlayerCommander) context.sender()).carbonPlayer();
|
||||||
return this.channelRegistry.keys().stream()
|
return this.channelRegistry.keys().stream()
|
||||||
.map(this.channelRegistry::channel)
|
.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(x -> x.key().value())
|
||||||
.map(Suggestion::suggestion)
|
.map(Suggestion::suggestion)
|
||||||
.toList();
|
.toList();
|
||||||
@@ -90,11 +92,6 @@ public final class LeaveCommand extends CarbonCommand {
|
|||||||
this.carbonMessages.channelNotFound(sender);
|
this.carbonMessages.channelNotFound(sender);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final ChannelPermissionResult permitted = channel.joinPermitted(sender);
|
|
||||||
if (!permitted.permitted()) {
|
|
||||||
sender.sendMessage(permitted.reason());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (sender.leftChannels().contains(channel.key())) {
|
if (sender.leftChannels().contains(channel.key())) {
|
||||||
this.carbonMessages.channelAlreadyLeft(sender);
|
this.carbonMessages.channelAlreadyLeft(sender);
|
||||||
return;
|
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) {
|
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()) {
|
if (!permitted.permitted()) {
|
||||||
sender.sendMessage(permitted.reason());
|
sender.sendMessage(permitted.reason());
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.startsWith(prefix) && chatChannel.speechPermitted(this).permitted()) {
|
if (text.startsWith(prefix) && chatChannel.permissions().speechPermitted(this).permitted()) {
|
||||||
channel = chatChannel;
|
channel = chatChannel;
|
||||||
formattedMessage = formattedMessage.replaceText(TextReplacementConfig.builder()
|
formattedMessage = formattedMessage.replaceText(TextReplacementConfig.builder()
|
||||||
.once()
|
.once()
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public final class ChannelUtils {
|
|||||||
// TODO: Emit events
|
// TODO: Emit events
|
||||||
|
|
||||||
for (final CarbonPlayer recipient : CarbonChatProvider.carbonChat().server().players()) {
|
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);
|
recipient.sendMessage(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-22
@@ -28,11 +28,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.UserManager;
|
import net.draycia.carbon.api.users.UserManager;
|
||||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
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.audience.Audience;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -50,7 +49,6 @@ public class AllianceChannel extends AbstractFactionsChannel {
|
|||||||
|
|
||||||
public static final String FILE_NAME = "factionsuuid-alliancechat.conf";
|
public static final String FILE_NAME = "factionsuuid-alliancechat.conf";
|
||||||
|
|
||||||
private transient @MonotonicNonNull @Inject CarbonMessages messages;
|
|
||||||
private transient @MonotonicNonNull @Inject UserManager<?> users;
|
private transient @MonotonicNonNull @Inject UserManager<?> users;
|
||||||
|
|
||||||
public AllianceChannel() {
|
public AllianceChannel() {
|
||||||
@@ -65,27 +63,11 @@ public class AllianceChannel extends AbstractFactionsChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
this.hasRelations(player, Relation.ALLY),
|
this.hasRelations(player, Relation.ALLY),
|
||||||
() -> this.messages.cannotUseFactionAllianceChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
@@ -25,11 +25,10 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.UserManager;
|
import net.draycia.carbon.api.users.UserManager;
|
||||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
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.audience.Audience;
|
||||||
import net.kyori.adventure.key.Key;
|
import net.kyori.adventure.key.Key;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@@ -47,7 +46,6 @@ public class FactionChannel extends AbstractFactionsChannel {
|
|||||||
|
|
||||||
public static final String FILE_NAME = "factionsuuid-factionchat.conf";
|
public static final String FILE_NAME = "factionsuuid-factionchat.conf";
|
||||||
|
|
||||||
private transient @MonotonicNonNull @Inject CarbonMessages messages;
|
|
||||||
private transient @MonotonicNonNull @Inject UserManager<?> users;
|
private transient @MonotonicNonNull @Inject UserManager<?> users;
|
||||||
|
|
||||||
public FactionChannel() {
|
public FactionChannel() {
|
||||||
@@ -62,27 +60,11 @@ public class FactionChannel extends AbstractFactionsChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
this.faction(player) != null,
|
this.faction(player) != null,
|
||||||
() -> this.messages.cannotUseFactionChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.UserManager;
|
import net.draycia.carbon.api.users.UserManager;
|
||||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
||||||
@@ -65,27 +65,11 @@ public class TruceChannel extends AbstractFactionsChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
this.hasRelations(player, Relation.TRUCE),
|
this.hasRelations(player, Relation.TRUCE),
|
||||||
() -> this.messages.cannotUseTruceChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
+4
-20
@@ -26,7 +26,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.UserManager;
|
import net.draycia.carbon.api.users.UserManager;
|
||||||
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
||||||
@@ -65,27 +65,11 @@ public class McmmoPartyChannel extends ConfigChatChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
this.party(player) != null,
|
this.party(player) != null,
|
||||||
() -> this.messages.cannotUseMcmmoPartyChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
+4
-20
@@ -25,7 +25,7 @@ import com.palmergames.bukkit.towny.object.ResidentList;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
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.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.users.UserManager;
|
import net.draycia.carbon.api.users.UserManager;
|
||||||
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
import net.draycia.carbon.common.channels.ConfigChatChannel;
|
||||||
@@ -53,27 +53,11 @@ abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatCha
|
|||||||
protected abstract @Nullable T residentList(CarbonPlayer player);
|
protected abstract @Nullable T residentList(CarbonPlayer player);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelPermissionResult joinPermitted(final CarbonPlayer player) {
|
public ChannelPermissions permissions() {
|
||||||
return channelPermissionResult(
|
return ChannelPermissions.uniformDynamic(player -> channelPermissionResult(
|
||||||
this.residentList(player) != null,
|
this.residentList(player) != null,
|
||||||
() -> this.cannotUseChannel(player)
|
() -> 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
|
@Override
|
||||||
|
|||||||
Reference in new issue
Block a user