unify vanish and offline messages

This commit is contained in:
Draycia committed 2021-10-20 20:39:42 -07:00
1 parent e37bad3d92
commit d79b1fd24d
5 files changed
+56 -11

No files matched your search

@@ -24,6 +24,7 @@ import net.draycia.carbon.common.CarbonCommonModule;
import net.draycia.carbon.common.ForCarbon; import net.draycia.carbon.common.ForCarbon;
import net.draycia.carbon.common.command.Commander; import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.messages.CarbonMessageService; import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.util.CloudUtils;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.util.ComponentMessageThrowable; import net.kyori.adventure.util.ComponentMessageThrowable;
@@ -38,7 +39,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
public final class CarbonChatBukkitModule extends AbstractModule { public final class CarbonChatBukkitModule extends AbstractModule {
private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[^\\s\\w\\-]"); private static final Pattern SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[^\\s\\w\\-]");
private static final Component NULL = Component.text("null");
private final Logger logger = LogManager.getLogger("CarbonChat"); private final Logger logger = LogManager.getLogger("CarbonChat");
private final CarbonChatBukkit carbonChat; private final CarbonChatBukkit carbonChat;
@@ -101,7 +101,7 @@ public final class CarbonChatBukkitModule extends AbstractModule {
final CarbonMessageService messageService final CarbonMessageService messageService
) { ) {
commandManager.registerExceptionHandler(ArgumentParseException.class, (sender, exception) -> { commandManager.registerExceptionHandler(ArgumentParseException.class, (sender, exception) -> {
final var throwableMessage = message(exception.getCause()); final var throwableMessage = CloudUtils.message(exception.getCause());
messageService.errorCommandArgumentParsing(sender, throwableMessage); messageService.errorCommandArgumentParsing(sender, throwableMessage);
}); });
@@ -128,7 +128,7 @@ public final class CarbonChatBukkitModule extends AbstractModule {
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
cause.printStackTrace(new PrintWriter(writer)); cause.printStackTrace(new PrintWriter(writer));
final String stackTrace = writer.toString().replaceAll("\t", " "); final String stackTrace = writer.toString().replaceAll("\t", " ");
final @Nullable Component throwableMessage = message(cause); final @Nullable Component throwableMessage = CloudUtils.message(cause);
messageService.errorCommandCommandExecution(sender, throwableMessage, stackTrace); messageService.errorCommandCommandExecution(sender, throwableMessage, stackTrace);
}); });
@@ -145,9 +145,4 @@ public final class CarbonChatBukkitModule extends AbstractModule {
this.bind(CarbonServer.class).to(CarbonServerBukkit.class); this.bind(CarbonServer.class).to(CarbonServerBukkit.class);
} }
private static Component message(final Throwable throwable) {
final @Nullable Component msg = ComponentMessageThrowable.getOrConvertMessage(throwable);
return msg == null ? NULL : msg;
}
} }
@@ -10,7 +10,9 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
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.PlayerCommander;
import net.draycia.carbon.common.command.argument.CarbonPlayerArgument;
import net.draycia.carbon.common.messages.CarbonMessageService; import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.util.CloudUtils;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
@@ -47,7 +49,10 @@ public class ContinueCommand {
} }
if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.continue")) { if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.continue")) {
messageService.whisperTargetOffline(sender, CarbonPlayer.renderName(sender)); final var rawNameInput = CloudUtils.rawInputByMatchingName(handler.getRawInput(), recipient);
final var exception = new CarbonPlayerArgument.PlayerParseException(rawNameInput);
messageService.errorCommandArgumentParsing(sender, CloudUtils.message(exception));
return; return;
} }
@@ -10,7 +10,9 @@ import net.draycia.carbon.api.users.ComponentPlayerResult;
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.PlayerCommander;
import net.draycia.carbon.common.command.argument.CarbonPlayerArgument;
import net.draycia.carbon.common.messages.CarbonMessageService; import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.util.CloudUtils;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; 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;
@@ -49,7 +51,10 @@ public class ReplyCommand {
} }
if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.reply")) { if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.reply")) {
messageService.whisperTargetOffline(sender, CarbonPlayer.renderName(sender)); final var rawNameInput = CloudUtils.rawInputByMatchingName(handler.getRawInput(), recipient);
final var exception = new CarbonPlayerArgument.PlayerParseException(rawNameInput);
messageService.errorCommandArgumentParsing(sender, CloudUtils.message(exception));
return; return;
} }
@@ -9,6 +9,7 @@ import net.draycia.carbon.common.command.Commander;
import net.draycia.carbon.common.command.PlayerCommander; import net.draycia.carbon.common.command.PlayerCommander;
import net.draycia.carbon.common.command.argument.CarbonPlayerArgument; import net.draycia.carbon.common.command.argument.CarbonPlayerArgument;
import net.draycia.carbon.common.messages.CarbonMessageService; import net.draycia.carbon.common.messages.CarbonMessageService;
import net.draycia.carbon.common.util.CloudUtils;
public class WhisperCommand { public class WhisperCommand {
@@ -40,7 +41,10 @@ public class WhisperCommand {
} }
if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.whisper")) { if (!sender.awareOf(recipient) && !sender.hasPermission("carbon.seevanish.whisper")) {
messageService.whisperTargetOffline(sender, CarbonPlayer.renderName(sender)); final var rawNameInput = CloudUtils.rawInputByMatchingName(handler.getRawInput(), recipient);
final var exception = new CarbonPlayerArgument.PlayerParseException(rawNameInput);
messageService.errorCommandArgumentParsing(sender, CloudUtils.message(exception));
return; return;
} }
@@ -0,0 +1,36 @@
package net.draycia.carbon.common.util;
import java.util.LinkedList;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.ComponentMessageThrowable;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public final class CloudUtils {
private static final Component NULL = Component.text("null");
private CloudUtils() {
}
public static Component message(final Throwable throwable) {
final @Nullable Component msg = ComponentMessageThrowable.getOrConvertMessage(throwable);
return msg == null ? NULL : msg;
}
public static String rawInputByMatchingName(
final LinkedList<String> rawInput,
final CarbonPlayer recipient
) {
return rawInput
.stream()
.filter(it -> it.equalsIgnoreCase(recipient.username()))
.findFirst()
.orElse(recipient.username());
}
}