Whispers part 2/3

3/3 will have /reply
This commit is contained in:
Draycia committed 2021-09-18 19:39:08 -07:00
1 parent 4693c0d55a
commit 0cfe8f16a2
7 files changed
+141 -17

No files matched your search

@@ -2,6 +2,7 @@ package net.draycia.carbon.common;
import com.google.inject.Injector;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.common.command.commands.WhisperCommand;
import net.draycia.carbon.common.listeners.DeafenHandler;
import net.draycia.carbon.common.listeners.MuteHandler;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -11,8 +12,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public abstract class CarbonChatCommon implements CarbonChat {
public final void initialize(final Injector injector) {
injector.getInstance(MuteHandler.class);
injector.getInstance(DeafenHandler.class);
// lol, bukkit module can't extend Common and it can't call this
}
}
@@ -0,0 +1,98 @@
package net.draycia.carbon.common.command.arguments;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.captions.Caption;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import com.google.inject.Inject;
import com.google.inject.Injector;
import java.util.List;
import java.util.Queue;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.command.Commander;
import org.checkerframework.checker.nullness.qual.NonNull;
public class CarbonPlayerArgument {
final Injector injector;
@Inject
public CarbonPlayerArgument(final Injector injector) {
this.injector = injector;
}
public CommandArgument<Commander, CarbonPlayer> newInstance(boolean required, final String name) {
return new CarbonPlayerArgumentImpl(required, name, this.injector.getInstance(CarbonPlayerArgumentParser.class), CarbonPlayer.class);
}
public static final class CarbonPlayerArgumentImpl extends CommandArgument<Commander, CarbonPlayer> {
CarbonPlayerArgumentImpl(boolean required, @NonNull String name, @NonNull ArgumentParser<Commander, CarbonPlayer> parser, @NonNull Class<CarbonPlayer> valueType) {
super(required, name, parser, valueType);
}
}
private static final class CarbonPlayerArgumentParser implements ArgumentParser<Commander, CarbonPlayer> {
final CarbonChat carbonChat;
@Inject
public CarbonPlayerArgumentParser(final CarbonChat carbonChat) {
this.carbonChat = carbonChat;
}
@Override
public @NonNull ArgumentParseResult<@NonNull CarbonPlayer> parse(@NonNull CommandContext<@NonNull Commander> commandContext, @NonNull Queue<@NonNull String> inputQueue) {
final String input = inputQueue.poll();
if (input == null) {
return ArgumentParseResult.failure(new NoInputProvidedException(CarbonPlayerArgumentParser.class, commandContext));
}
for (var player : this.carbonChat.server().players()) {
if (player.username().equalsIgnoreCase(input)) {
return ArgumentParseResult.success(player);
}
}
return ArgumentParseResult.failure(new PlayerParseException(input, commandContext));
}
@Override
public @NonNull List<@NonNull String> suggestions(@NonNull CommandContext<Commander> commandContext, @NonNull String input) {
return ArgumentParser.super.suggestions(commandContext, input);
}
}
public static final class PlayerParseException extends ParserException {
private static final long serialVersionUID = -8703083769083215214L;
private final String input;
public PlayerParseException(
final @NonNull String input,
final @NonNull CommandContext<?> context
) {
super(
CarbonPlayerArgumentParser.class,
context,
Caption.of("No player with that name was found!"),
CaptionVariable.of("input", input)
);
this.input = input;
}
public @NonNull String input() {
return this.input;
}
}
}
@@ -7,6 +7,8 @@ import com.google.inject.Injector;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.util.SourcedAudience;
import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.command.arguments.CarbonPlayerArgument;
import net.draycia.carbon.common.messages.CarbonMessageService;
public class WhisperCommand {
@@ -15,28 +17,30 @@ public class WhisperCommand {
public WhisperCommand(
final Injector injector,
final CommandManager<Commander> commandManager,
final CarbonMessageService messageService
) {
final CarbonMessageService messageService,
final CarbonPlayerArgument carbonPlayerArgument
) {
var command = commandManager.commandBuilder("whisper", "w", "message", "msg")
.argument(carbonPlayerArgument.newInstance(true, "recipient"))
.argument(StringArgument.greedy("message"))
.argument(/* Audience recipient argument */)
.permission("carbon.whisper.send") // TODO: carbon.whisper.spy
.senderType(CarbonPlayer.class)
.senderType(PlayerCommander.class)
.handler(handler -> {
final CarbonPlayer sender = ((PlayerCommander)handler.getSender()).carbonPlayer();
final String message = handler.get("message");
final CarbonPlayer sender = (CarbonPlayer) handler.getSender();
final CarbonPlayer recipient = handler.get("recipient");
if (sender.equals(recipient)) {
// TODO: send error message "you cannot whisper yourself!"
return;
//return;
}
messageService.whisperSender(new SourcedAudience(handler.getSender(), handler.getSender()),
sender.displayName(), recipient.displayName());
messageService.whisperSender(new SourcedAudience(sender, sender),
sender.displayName(), recipient.displayName(), message);
messageService.whisperRecipient(new SourcedAudience(handler.getSender(), recipient),
sender.displayName(), recipient.displayName());
messageService.whisperRecipient(new SourcedAudience(sender, recipient),
sender.displayName(), recipient.displayName(), message);
sender.whisperReplyTarget(recipient.uuid());
recipient.whisperReplyTarget(sender.uuid());
@@ -46,14 +46,16 @@ public interface CarbonMessageService {
void whisperSender(
final SourcedAudience audience,
@Placeholder Component senderDisplayName,
@Placeholder Component recipientDisplayName
@Placeholder Component recipientDisplayName,
@Placeholder String message
);
@Message("whisper.from")
void whisperRecipient(
final SourcedAudience audience,
@Placeholder Component senderDisplayName,
@Placeholder Component recipientDisplayName
@Placeholder Component recipientDisplayName,
@Placeholder String message
);
}
@@ -26,6 +26,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
protected boolean spying = false;
protected @MonotonicNonNull String username;
protected @MonotonicNonNull UUID uuid;
protected @Nullable UUID whisperReplyTarget = null;
public CarbonPlayerCommon(
final @Nullable Component displayName,
@@ -130,6 +131,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
return false;
}
@Override
public @Nullable UUID whisperReplyTarget() {
return this.whisperReplyTarget;
}
@Override
public void whisperReplyTarget(@Nullable UUID uuid) {
this.whisperReplyTarget = uuid;
}
@Override
public Identity identity() {
return Identity.identity(this.uuid);