mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 14:38:48 +00:00
leave & join
This commit is contained in:
1 parent
f62350ab68
commit
46ba7512ac
22 files changed
+402
-4
No files matched your search
@@ -114,7 +114,7 @@ public final class BasicChatChannel implements ChatChannel {
|
||||
|
||||
@Override
|
||||
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
|
||||
return ChannelPermissionResult.allowed();
|
||||
return ChannelPermissionResult.allowedIf(Component.empty(), () -> !player.leftChannels().contains(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -209,7 +209,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
|
||||
@Override
|
||||
public ChannelPermissionResult hearingPermitted(final CarbonPlayer player) {
|
||||
return ChannelPermissionResult.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see"));
|
||||
return ChannelPermissionResult.allowedIf(empty(), () -> player.hasPermission(this.permission() + ".see") && !player.leftChannels().contains(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2021 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.command.commands;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
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.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.command.CarbonCommand;
|
||||
import net.draycia.carbon.common.command.CommandSettings;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.util.ChannelUtils;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class JoinCommand extends CarbonCommand {
|
||||
|
||||
final CarbonChat carbonChat;
|
||||
final CommandManager<Commander> commandManager;
|
||||
final CarbonMessages carbonMessages;
|
||||
final PlayerSuggestions playerSuggestions;
|
||||
|
||||
@Inject
|
||||
public JoinCommand(
|
||||
final CarbonChat carbonChat,
|
||||
final CommandManager<Commander> commandManager,
|
||||
final CarbonMessages carbonMessages,
|
||||
final PlayerSuggestions playerSuggestions
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.commandManager = commandManager;
|
||||
this.carbonMessages = carbonMessages;
|
||||
this.playerSuggestions = playerSuggestions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandSettings _commandSettings() {
|
||||
return new CommandSettings("join");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key key() {
|
||||
return Key.key("carbon", "join");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
|
||||
.argument(StringArgument.greedy("channel"),
|
||||
RichDescription.of(this.carbonMessages.commandWhisperArgumentMessage()))
|
||||
.permission("carbon.join")
|
||||
.senderType(PlayerCommander.class)
|
||||
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandIgnoreDescription())
|
||||
.handler(handler -> {
|
||||
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
||||
final @Nullable ChatChannel channel = ChannelUtils.locateChannel(handler.get("channel"));
|
||||
if (channel == null) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
if (!channel.speechPermitted(sender).permitted()) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
|
||||
if (!sender.leftChannels().contains(channel.key())) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
sender.joinChannel(channel);
|
||||
// send a message
|
||||
|
||||
})
|
||||
.build();
|
||||
|
||||
this.commandManager.command(command);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2021 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.command.commands;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
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.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.command.CarbonCommand;
|
||||
import net.draycia.carbon.common.command.CommandSettings;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.command.argument.PlayerSuggestions;
|
||||
import net.draycia.carbon.common.messages.CarbonMessages;
|
||||
import net.draycia.carbon.common.util.ChannelUtils;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class LeaveCommand extends CarbonCommand {
|
||||
|
||||
final CarbonChat carbonChat;
|
||||
final CommandManager<Commander> commandManager;
|
||||
final CarbonMessages carbonMessages;
|
||||
final PlayerSuggestions playerSuggestions;
|
||||
|
||||
@Inject
|
||||
public LeaveCommand(
|
||||
final CarbonChat carbonChat,
|
||||
final CommandManager<Commander> commandManager,
|
||||
final CarbonMessages carbonMessages,
|
||||
final PlayerSuggestions playerSuggestions
|
||||
) {
|
||||
this.carbonChat = carbonChat;
|
||||
this.commandManager = commandManager;
|
||||
this.carbonMessages = carbonMessages;
|
||||
this.playerSuggestions = playerSuggestions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CommandSettings _commandSettings() {
|
||||
return new CommandSettings("leave");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Key key() {
|
||||
return Key.key("carbon", "leave");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
final var command = this.commandManager.commandBuilder(this.commandSettings().name(), this.commandSettings().aliases())
|
||||
.argument(StringArgument.greedy("channel"),
|
||||
RichDescription.of(this.carbonMessages.commandWhisperArgumentMessage()))
|
||||
.permission("carbon.join")
|
||||
.senderType(PlayerCommander.class)
|
||||
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, this.carbonMessages.commandIgnoreDescription())
|
||||
.handler(handler -> {
|
||||
final CarbonPlayer sender = ((PlayerCommander) handler.getSender()).carbonPlayer();
|
||||
final @Nullable ChatChannel channel = ChannelUtils.locateChannel(handler.get("channel"));
|
||||
if (channel == null) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
if (!channel.speechPermitted(sender).permitted()) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
|
||||
if (sender.leftChannels().contains(channel.key())) {
|
||||
// send a message
|
||||
return;
|
||||
}
|
||||
sender.joinChannel(channel);
|
||||
// send a message
|
||||
|
||||
})
|
||||
.build();
|
||||
|
||||
this.commandManager.command(command);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -67,6 +67,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
// Punishments
|
||||
protected List<UUID> ignoredPlayers = new ArrayList<>();
|
||||
|
||||
protected List<Key> leftChannels = new ArrayList<>();
|
||||
|
||||
public CarbonPlayerCommon(
|
||||
final boolean muted,
|
||||
final boolean deafened,
|
||||
@@ -281,6 +283,27 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Key> leftChannels() {
|
||||
return this.leftChannels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void joinChannel(final ChatChannel channel) {
|
||||
this.leftChannels.remove(channel.key());
|
||||
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
|
||||
userManager.removeLeftChannel(this.uuid(), channel.key());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leaveChannel(final ChatChannel channel) {
|
||||
this.leftChannels.add(channel.key());
|
||||
if (this.carbonChat.server().userManager() instanceof SaveOnChange userManager) {
|
||||
userManager.addLeftChannel(this.uuid(), channel.key());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identity identity() {
|
||||
return Identity.identity(this.uuid);
|
||||
|
||||
@@ -47,4 +47,8 @@ public interface SaveOnChange {
|
||||
|
||||
int removeIgnore(final UUID id, final UUID ignoredPlayer);
|
||||
|
||||
int addLeftChannel(final UUID id, final Key channel);
|
||||
|
||||
int removeLeftChannel(final UUID id, final Key channel);
|
||||
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import java.util.concurrent.CompletableFuture;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
@@ -59,7 +60,15 @@ public abstract class AbstractUserManager implements UserManager<CarbonPlayerCom
|
||||
|
||||
batch.execute();
|
||||
}
|
||||
if (!player.leftChannels().isEmpty()) {
|
||||
final PreparedBatch batch = handle.prepareBatch(this.locator.query("save-leftchannels"));
|
||||
|
||||
for (final Key leftChannel : player.leftChannels()) {
|
||||
batch.bind("id", player.uuid()).bind("channel", leftChannel).add();
|
||||
}
|
||||
|
||||
batch.execute();
|
||||
}
|
||||
// TODO: save ignoredplayers
|
||||
return new ComponentPlayerResult<>(player, empty());
|
||||
}));
|
||||
|
||||
@@ -58,4 +58,9 @@ public interface MySQLSaveOnChange extends SaveOnChange {
|
||||
@SqlUpdate("DELETE FROM carbon_ignores WHERE id = UNHEX(REPLACE(:id, '-', '')) AND ignoredplayer = UNHEX(REPLACE(:ignoredPlayer, '-', ''))")
|
||||
int removeIgnore(final UUID id, final UUID ignoredPlayer);
|
||||
|
||||
@SqlUpdate("INSERT IGNORE INTO carbon_leftchannels (id, channel) VALUES (UNHEX(REPLACE(:id, '-', '')), :channel)")
|
||||
int addLeftChannel(final UUID id, final Key channel);
|
||||
|
||||
@SqlUpdate("DELETE FROM carbon_leftchannels WHERE id = UNHEX(REPLACE(:id, '-', '')) AND channel = :channel")
|
||||
int removeLeftChannel(final UUID id, final Key channel);
|
||||
}
|
||||
+14
-1
@@ -128,7 +128,10 @@ public final class MySQLUserManager extends AbstractUserManager implements SaveO
|
||||
.bind("id", uuid)
|
||||
.mapTo(UUID.class)
|
||||
.forEach(ignoredPlayer -> carbonPlayerCommon.get().ignoredPlayers().add(ignoredPlayer));
|
||||
|
||||
handle.createQuery(this.locator.query("select-leftchannels"))
|
||||
.bind("id", uuid)
|
||||
.mapTo(Key.class)
|
||||
.forEach(channel -> carbonPlayerCommon.get().leftChannels().add(channel));
|
||||
return carbonPlayerCommon.get();
|
||||
}));
|
||||
|
||||
@@ -204,4 +207,14 @@ public final class MySQLUserManager extends AbstractUserManager implements SaveO
|
||||
return this.jdbi.withExtension(MySQLSaveOnChange.class, changeSaver -> changeSaver.removeIgnore(id, ignoredPlayer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addLeftChannel(final UUID id, final Key channel) {
|
||||
return this.jdbi.withExtension(MySQLSaveOnChange.class, changeSaver -> changeSaver.addLeftChannel(id, channel));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeLeftChannel(final UUID id, final Key channel) {
|
||||
return this.jdbi.withExtension(MySQLSaveOnChange.class, changeSaver -> changeSaver.removeLeftChannel(id, channel));
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -58,4 +58,9 @@ public interface PostgreSQLSaveOnChange extends SaveOnChange {
|
||||
@SqlUpdate("DELETE FROM carbon_ignores WHERE id = :id, ignoredplayer = :ignoredPlayer")
|
||||
int removeIgnore(final UUID id, final UUID ignoredPlayer);
|
||||
|
||||
@SqlUpdate("INSERT INTO carbon_leftchannels VALUES id = :id, channel = :channel")
|
||||
int addLeftChannel(final UUID id, final Key channel);
|
||||
|
||||
@SqlUpdate("DELETE FROM carbon_leftchannels WHERE id = :id, channel = :channel")
|
||||
int removeLeftChannel(final UUID id, final Key channel);
|
||||
}
|
||||
+10
@@ -206,4 +206,14 @@ public final class PostgreSQLUserManager extends AbstractUserManager implements
|
||||
return this.jdbi.withExtension(PostgreSQLSaveOnChange.class, changeSaver -> changeSaver.removeIgnore(id, ignoredPlayer));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int addLeftChannel(final UUID id, final Key channel) {
|
||||
return this.jdbi.withExtension(PostgreSQLSaveOnChange.class, changeSaver -> changeSaver.addLeftChannel(id, channel));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int removeLeftChannel(final UUID id, final Key channel) {
|
||||
return this.jdbi.withExtension(PostgreSQLSaveOnChange.class, changeSaver -> changeSaver.removeLeftChannel(id, channel));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,6 +44,8 @@ import net.draycia.carbon.common.command.commands.ContinueCommand;
|
||||
import net.draycia.carbon.common.command.commands.DebugCommand;
|
||||
import net.draycia.carbon.common.command.commands.HelpCommand;
|
||||
import net.draycia.carbon.common.command.commands.IgnoreCommand;
|
||||
import net.draycia.carbon.common.command.commands.JoinCommand;
|
||||
import net.draycia.carbon.common.command.commands.LeaveCommand;
|
||||
import net.draycia.carbon.common.command.commands.MuteCommand;
|
||||
import net.draycia.carbon.common.command.commands.MuteInfoCommand;
|
||||
import net.draycia.carbon.common.command.commands.NicknameCommand;
|
||||
@@ -73,7 +75,7 @@ public final class CloudUtils {
|
||||
public static final List<Class<? extends CarbonCommand>> COMMAND_CLASSES = List.of(ClearChatCommand.class,
|
||||
ContinueCommand.class, DebugCommand.class, HelpCommand.class, IgnoreCommand.class, MuteCommand.class,
|
||||
MuteInfoCommand.class, NicknameCommand.class, ReloadCommand.class, ReplyCommand.class, UnignoreCommand.class,
|
||||
UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class);
|
||||
UnmuteCommand.class, UpdateUsernameCommand.class, WhisperCommand.class, JoinCommand.class, LeaveCommand.class);
|
||||
|
||||
private static final List<CarbonCommand> CONSTRUCTED_COMMANDS = new ArrayList<>();
|
||||
|
||||
|
||||
Reference in new issue
Block a user