mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Whispers part 2/3
3/3 will have /reply
This commit is contained in:
1 parent
4693c0d55a
commit
0cfe8f16a2
7 files changed
+141
-17
No files matched your search
@@ -18,6 +18,7 @@ import net.draycia.carbon.bukkit.listeners.BukkitPlayerJoinListener;
|
|||||||
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
import net.draycia.carbon.bukkit.users.CarbonPlayerBukkit;
|
||||||
import net.draycia.carbon.bukkit.util.BukkitMessageRenderer;
|
import net.draycia.carbon.bukkit.util.BukkitMessageRenderer;
|
||||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||||
|
import net.draycia.carbon.common.command.commands.WhisperCommand;
|
||||||
import net.draycia.carbon.common.listeners.DeafenHandler;
|
import net.draycia.carbon.common.listeners.DeafenHandler;
|
||||||
import net.draycia.carbon.common.listeners.MuteHandler;
|
import net.draycia.carbon.common.listeners.MuteHandler;
|
||||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||||
@@ -87,9 +88,12 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: abstract?
|
// Listeners
|
||||||
this.injector.getInstance(MuteHandler.class);
|
injector.getInstance(MuteHandler.class);
|
||||||
this.injector.getInstance(DeafenHandler.class);
|
injector.getInstance(DeafenHandler.class);
|
||||||
|
|
||||||
|
// Commands
|
||||||
|
injector.getInstance(WhisperCommand.class);
|
||||||
|
|
||||||
final long saveDelay = 5 * 60 * 20;
|
final long saveDelay = 5 * 60 * 20;
|
||||||
|
|
||||||
|
|||||||
@@ -265,4 +265,9 @@ public final class CarbonPlayerBukkit extends CarbonPlayerCommon implements Forw
|
|||||||
return this.player() != null;
|
return this.player() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable UUID whisperReplyTarget() {
|
||||||
|
return this.carbonPlayer.whisperReplyTarget();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package net.draycia.carbon.common;
|
|||||||
|
|
||||||
import com.google.inject.Injector;
|
import com.google.inject.Injector;
|
||||||
import net.draycia.carbon.api.CarbonChat;
|
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.DeafenHandler;
|
||||||
import net.draycia.carbon.common.listeners.MuteHandler;
|
import net.draycia.carbon.common.listeners.MuteHandler;
|
||||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
@@ -11,8 +12,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
|||||||
public abstract class CarbonChatCommon implements CarbonChat {
|
public abstract class CarbonChatCommon implements CarbonChat {
|
||||||
|
|
||||||
public final void initialize(final Injector injector) {
|
public final void initialize(final Injector injector) {
|
||||||
injector.getInstance(MuteHandler.class);
|
// lol, bukkit module can't extend Common and it can't call this
|
||||||
injector.getInstance(DeafenHandler.class);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+98
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+14
-10
@@ -7,6 +7,8 @@ import com.google.inject.Injector;
|
|||||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||||
import net.draycia.carbon.api.util.SourcedAudience;
|
import net.draycia.carbon.api.util.SourcedAudience;
|
||||||
import net.draycia.carbon.common.command.Commander;
|
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;
|
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||||
|
|
||||||
public class WhisperCommand {
|
public class WhisperCommand {
|
||||||
@@ -15,28 +17,30 @@ public class WhisperCommand {
|
|||||||
public WhisperCommand(
|
public WhisperCommand(
|
||||||
final Injector injector,
|
final Injector injector,
|
||||||
final CommandManager<Commander> commandManager,
|
final CommandManager<Commander> commandManager,
|
||||||
final CarbonMessageService messageService
|
final CarbonMessageService messageService,
|
||||||
) {
|
final CarbonPlayerArgument carbonPlayerArgument
|
||||||
|
) {
|
||||||
var command = commandManager.commandBuilder("whisper", "w", "message", "msg")
|
var command = commandManager.commandBuilder("whisper", "w", "message", "msg")
|
||||||
|
.argument(carbonPlayerArgument.newInstance(true, "recipient"))
|
||||||
.argument(StringArgument.greedy("message"))
|
.argument(StringArgument.greedy("message"))
|
||||||
.argument(/* Audience recipient argument */)
|
|
||||||
.permission("carbon.whisper.send") // TODO: carbon.whisper.spy
|
.permission("carbon.whisper.send") // TODO: carbon.whisper.spy
|
||||||
.senderType(CarbonPlayer.class)
|
.senderType(PlayerCommander.class)
|
||||||
.handler(handler -> {
|
.handler(handler -> {
|
||||||
|
final CarbonPlayer sender = ((PlayerCommander)handler.getSender()).carbonPlayer();
|
||||||
|
|
||||||
final String message = handler.get("message");
|
final String message = handler.get("message");
|
||||||
final CarbonPlayer sender = (CarbonPlayer) handler.getSender();
|
|
||||||
final CarbonPlayer recipient = handler.get("recipient");
|
final CarbonPlayer recipient = handler.get("recipient");
|
||||||
|
|
||||||
if (sender.equals(recipient)) {
|
if (sender.equals(recipient)) {
|
||||||
// TODO: send error message "you cannot whisper yourself!"
|
// TODO: send error message "you cannot whisper yourself!"
|
||||||
return;
|
//return;
|
||||||
}
|
}
|
||||||
|
|
||||||
messageService.whisperSender(new SourcedAudience(handler.getSender(), handler.getSender()),
|
messageService.whisperSender(new SourcedAudience(sender, sender),
|
||||||
sender.displayName(), recipient.displayName());
|
sender.displayName(), recipient.displayName(), message);
|
||||||
|
|
||||||
messageService.whisperRecipient(new SourcedAudience(handler.getSender(), recipient),
|
messageService.whisperRecipient(new SourcedAudience(sender, recipient),
|
||||||
sender.displayName(), recipient.displayName());
|
sender.displayName(), recipient.displayName(), message);
|
||||||
|
|
||||||
sender.whisperReplyTarget(recipient.uuid());
|
sender.whisperReplyTarget(recipient.uuid());
|
||||||
recipient.whisperReplyTarget(sender.uuid());
|
recipient.whisperReplyTarget(sender.uuid());
|
||||||
|
|||||||
@@ -46,14 +46,16 @@ public interface CarbonMessageService {
|
|||||||
void whisperSender(
|
void whisperSender(
|
||||||
final SourcedAudience audience,
|
final SourcedAudience audience,
|
||||||
@Placeholder Component senderDisplayName,
|
@Placeholder Component senderDisplayName,
|
||||||
@Placeholder Component recipientDisplayName
|
@Placeholder Component recipientDisplayName,
|
||||||
|
@Placeholder String message
|
||||||
);
|
);
|
||||||
|
|
||||||
@Message("whisper.from")
|
@Message("whisper.from")
|
||||||
void whisperRecipient(
|
void whisperRecipient(
|
||||||
final SourcedAudience audience,
|
final SourcedAudience audience,
|
||||||
@Placeholder Component senderDisplayName,
|
@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 boolean spying = false;
|
||||||
protected @MonotonicNonNull String username;
|
protected @MonotonicNonNull String username;
|
||||||
protected @MonotonicNonNull UUID uuid;
|
protected @MonotonicNonNull UUID uuid;
|
||||||
|
protected @Nullable UUID whisperReplyTarget = null;
|
||||||
|
|
||||||
public CarbonPlayerCommon(
|
public CarbonPlayerCommon(
|
||||||
final @Nullable Component displayName,
|
final @Nullable Component displayName,
|
||||||
@@ -130,6 +131,16 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @Nullable UUID whisperReplyTarget() {
|
||||||
|
return this.whisperReplyTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void whisperReplyTarget(@Nullable UUID uuid) {
|
||||||
|
this.whisperReplyTarget = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Identity identity() {
|
public Identity identity() {
|
||||||
return Identity.identity(this.uuid);
|
return Identity.identity(this.uuid);
|
||||||
|
|||||||
Reference in new issue
Block a user