Complete some TODOs

This commit is contained in:
Draycia committed 2020-10-15 02:14:52 -07:00
1 parent 548955134e
commit 4d547f80c7
27 files changed
+155 -67

No files matched your search

@@ -1,5 +1,6 @@
package net.draycia.carbon.api;
import cloud.commandframework.CommandManager;
import net.draycia.carbon.api.config.CarbonTranslations;
import net.draycia.carbon.api.adventure.MessageProcessor;
import net.draycia.carbon.api.channels.ChannelRegistry;
@@ -8,6 +9,7 @@ import net.draycia.carbon.api.config.CarbonSettings;
import net.draycia.carbon.api.config.ChannelSettings;
import net.draycia.carbon.api.config.ModerationSettings;
import net.draycia.carbon.api.messaging.MessageService;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.api.users.PlayerUser;
import net.draycia.carbon.api.users.UserService;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
@@ -53,4 +55,6 @@ public interface CarbonChat {
@NonNull GsonComponentSerializer gsonSerializer();
@NonNull CommandManager<CarbonUser> commandManager();
}
@@ -0,0 +1,20 @@
package net.draycia.carbon.api;
import net.kyori.registry.DefaultedRegistryGetter;
import org.checkerframework.checker.nullness.qual.NonNull;
/**
* An extension of the DefaultedRegistryGetter that declares a defaultValue method
* @param <K>
* @param <V>
*/
public interface DefaultedKeyValueRegistry<K,V> extends DefaultedRegistryGetter<K, V> {
/**
* Gets the default value.
*
* @return the default value
*/
@NonNull V defaultValue();
}
@@ -1,6 +1,6 @@
package net.draycia.carbon.api.channels;
import net.kyori.registry.DefaultedRegistry;
import net.draycia.carbon.api.DefaultedKeyValueRegistry;
import net.kyori.registry.Registry;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -11,7 +11,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ChannelRegistry implements Registry<String, ChatChannel>, DefaultedRegistry<String, ChatChannel> {
public class ChannelRegistry implements Registry<String, ChatChannel>, DefaultedKeyValueRegistry<String, ChatChannel> {
private @NonNull final Map<@NonNull String, @NonNull ChatChannel> registry = new HashMap<>();
@@ -37,6 +37,12 @@ public class ChannelRegistry implements Registry<String, ChatChannel>, Defaulted
@Override
public @Nullable String key(@NonNull final ChatChannel value) {
for (final Map.Entry<String, ChatChannel> entry : this.registry.entrySet()) {
if (entry.getValue().equals(value)) {
return entry.getKey();
}
}
return null;
}
@@ -50,17 +56,18 @@ public class ChannelRegistry implements Registry<String, ChatChannel>, Defaulted
return this.registry.values().iterator();
}
public @MonotonicNonNull ChatChannel defaultChannel() {
return this.defaultChannel;
}
@Override
public @NonNull String defaultKey() {
return this.defaultChannel.key();
}
@Override
public @NonNull ChatChannel defaultValue() {
return this.defaultChannel;
}
@Override
public @NonNull ChatChannel getOrDefault(@NonNull final String key) {
return this.registry.getOrDefault(key, this.defaultChannel());
return this.registry.getOrDefault(key, this.defaultValue());
}
}
@@ -10,7 +10,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List;
// TODO: rename this to something that's less... weird?
public interface TextChannel extends ChatChannel, ForwardingAudience {
@Override
@@ -16,7 +16,7 @@ import java.util.Map;
import java.util.regex.Pattern;
@ConfigSerializable
public class CarbonSettings {
public final class CarbonSettings {
private static final ObjectMapper<CarbonSettings> MAPPER;
@@ -7,7 +7,7 @@ import org.spongepowered.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ConfigSerializable
public class CarbonTranslations {
public final class CarbonTranslations {
private static final ObjectMapper<CarbonTranslations> MAPPER;
@@ -27,8 +27,9 @@ public class CarbonTranslations {
MAPPER.save(this, node);
}
@Setting
private String reloaded = "<yellow>Chat config has been reloaded!";
private ExceptionMessages exceptionMessages = new ExceptionMessages();
@Setting private String reloaded = "<yellow>Chat config has been reloaded!";
@Setting private String otherPlayerOffline = "<red>That player is offline!";
@Setting private String spyWhispers = "<yellow>Spy [<gray><sender><reset> <gold>-> <gray><target><reset><yellow>] <message>";
@Setting private String noReplyTarget = "<red>You have no one to reply to!";
@@ -64,6 +65,10 @@ public class CarbonTranslations {
@Setting private String clearExempt = "<yellow>You were exempt from the clear due to your permissions!";
@Setting private String cannotWhisperSelf = "<yellow>You cannot whisper yourself!";
public ExceptionMessages exceptionMessages() {
return this.exceptionMessages;
}
public String reloaded() {
return this.reloaded;
}
@@ -15,7 +15,7 @@ import java.util.List;
import java.util.Map;
@ConfigSerializable
public class ChannelOptions {
public final class ChannelOptions {
@Setting
@Comment("What this channel is identified as. This will be what's typed ingame to use the channel.")
@@ -11,7 +11,7 @@ import java.util.Collections;
import java.util.Map;
@ConfigSerializable
public class ChannelSettings {
public final class ChannelSettings {
private static final ObjectMapper<ChannelSettings> MAPPER;
@@ -0,0 +1,26 @@
package net.draycia.carbon.api.config;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ConfigSerializable
public final class ExceptionMessages {
@Setting private @NonNull String channelNotFound = "Channel not found!";
@Setting private @NonNull String channelNotPublic = "Channel is not public!";
@Setting private @NonNull String playerNotFound = "Player not found!";
public String channelNotFound() {
return this.channelNotFound;
}
public String channelNotPublic() {
return this.channelNotPublic;
}
public String playerNotFound() {
return this.playerNotFound;
}
}
@@ -14,7 +14,7 @@ import java.util.Map;
import java.util.regex.Pattern;
@ConfigSerializable
public class ModerationSettings {
public final class ModerationSettings {
private static final ObjectMapper<ModerationSettings> MAPPER;
@@ -34,8 +34,8 @@ public class ModerationSettings {
MAPPER.save(this, node);
}
// TODO: comment this
@Setting
@Comment("The text that's prepended to someone else's shadowmuted messages.")
private String shadowMutePrefix = "[SM] ";
public String shadowMutePrefix() {
@@ -86,19 +86,19 @@ public class ModerationSettings {
@ConfigSerializable
public static class CapsProtection {
@Setting
@Comment("If caps protection is enabled")
@Comment("If caps protection is enabled")
private boolean enabled = true;
@Setting
@Comment("The minimum message length for caps protection to activate")
@Comment("The minimum message length for caps protection to activate")
private int minimumLength = 10;
@Setting
@Comment("The amount of letters in the message for it to trigger the protection")
@Comment("The amount of letters in the message for it to trigger the protection")
private float percentCaps = 0.80F;
@Setting
@Comment("If true, stops message from sending. If false, simply changes the message to lowercase")
@Comment("If true, stops message from sending. If false, simply changes the message to lowercase")
private boolean blockMessage = false;
public boolean enabled() {
@@ -127,12 +127,11 @@ public class ModerationSettings {
@ConfigSerializable
public static class Filters {
@Setting
private boolean enabled = true;
@Setting
@Comment("The keys (\"****\" for example) are what the text is replaced with.\n" +
@Comment("The keys (\"****\" for example) are what the text is replaced with.\n" +
"The strings in the lists (\"lag\" etc) are what's replaced.\n" +
"Set to filters: {} if you want to disable the filter feature.\n" +
"Set the key to \"_\" for the replacement to be blank (remove the filtered pattern).")
@@ -143,7 +142,7 @@ public class ModerationSettings {
};
@Setting
@Comment("Anything in blocked-words will prevent the message from being sent at all.\n" +
@Comment("Anything in blocked-words will prevent the message from being sent at all.\n" +
"Set to blocked-words: [] if you want to disable the blocked words feature.")
private List<Pattern> blockedPatterns = Collections.singletonList(Pattern.compile("pineapple doesn't belong on pizza"));
@@ -158,7 +157,6 @@ public class ModerationSettings {
public List<Pattern> blockedPatterns() {
return this.blockedPatterns;
}
}
}
@@ -6,7 +6,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ConfigSerializable
public class RedisCredentials {
public final class RedisCredentials {
@Setting private @NonNull String host = "localhost";
@Setting private @Nullable String password = "";
@@ -5,7 +5,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ConfigSerializable
public class SQLCredentials {
public final class SQLCredentials {
@Setting private @NonNull String username = "username";
@Setting private @NonNull String password = "password";
@@ -12,7 +12,7 @@ import java.util.List;
import java.util.Map;
@ConfigSerializable
public class SharedChannelOptions {
public final class SharedChannelOptions {
@Setting private String color = "#FFFFFF";
@Setting private Map<String, Context> contexts = new HashMap<>(); // TODO: set defaults
@@ -7,7 +7,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
import org.spongepowered.configurate.objectmapping.meta.Setting;
@ConfigSerializable
public class WhisperOptions {
public final class WhisperOptions {
@Setting
@Comment("Plays a sound when receiving private messages with /whisper /msg")
@@ -1,5 +1,6 @@
package net.draycia.carbon;
import cloud.commandframework.CommandManager;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.paper.PaperCommandManager;
import net.draycia.carbon.api.CarbonChat;
@@ -91,16 +92,14 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
private ChannelManager channelManager;
private AdventureManager messageProcessor;
private CommandSettingsRegistry commandSettings;
private ModerationSettings moderationSettings;
private CarbonSettings carbonSettings;
private ChannelSettings channelSettings;
private UserService<BukkitPlayerUser> userService;
private MessageManager messageManager;
private CarbonTranslations translations;
private CommandManager<CarbonUser> commandManager;
private Logger logger;
@@ -393,6 +392,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
return this.commandSettings;
}
@Override
public @NonNull CommandManager<CarbonUser> commandManager() {
return this.commandManager;
}
@Override
public @NonNull MessageProcessor messageProcessor() {
return this.messageProcessor;
@@ -31,7 +31,7 @@ public class PAPIContext {
final String[] pieces = context.asString().split("=");
if (pieces.length != 2) {
return; // TODO: maybe alert of misconfiguration?
return;
}
final Player receiver = Bukkit.getPlayer(event.recipient().uuid());
@@ -59,7 +59,7 @@ public class PAPIContext {
final String[] pieces = context.asString().split("=");
if (pieces.length != 2) {
return; // TODO: maybe alert of misconfiguration?
return;
}
final Player receiver = Bukkit.getPlayer(event.user().uuid());
@@ -38,11 +38,11 @@ public class BukkitChatListener implements Listener {
ChatChannel channel = user.selectedChannel();
if (channel == null) {
if (this.carbonChat.channelRegistry().defaultChannel() == null) {
if (this.carbonChat.channelRegistry().defaultValue() == null) {
return;
}
channel = this.carbonChat.channelRegistry().defaultChannel();
channel = this.carbonChat.channelRegistry().defaultValue();
}
// if (channel.shouldCancelChatEvent()) {
@@ -197,7 +197,7 @@ public class BukkitPlayerUser implements PlayerUser, ForwardingAudience {
@Override
public void clearSelectedChannel() {
this.selectedChannel(this.carbonChat.channelRegistry().defaultChannel());
this.selectedChannel(this.carbonChat.channelRegistry().defaultValue());
}
@Override
@@ -79,7 +79,7 @@ public class CarbonPlaceholders extends PlaceholderExpansion {
} else if (key.startsWith("selected_channel")) {
final ChatChannel channel = this.carbonChat.userService().wrap(player.getUniqueId()).selectedChannel();
return channel == null ? this.carbonChat.channelRegistry().defaultChannel().name() : channel.name();
return channel == null ? this.carbonChat.channelRegistry().defaultValue().name() : channel.name();
}
return null;
@@ -60,6 +60,10 @@ public class AdventureManager implements MessageProcessor {
return this.carbonChat.gsonSerializer().deserialize(input);
}
public FormatType formatType() {
return this.formatType;
}
@Override
public @NonNull AudienceProvider audiences() {
return this.provider;
@@ -182,7 +182,7 @@ public class CarbonChatChannel implements TextChannel {
"phase", Long.toString(System.currentTimeMillis() % 25),
"message", consoleFormatEvent.message());
// TODO: add ConsoleUser to users map
// TODO: add ConsoleUser to users map. Requires "users" to be CarbonUser and not PlayerUser.
final ChatComponentEvent consoleEvent = new ChatComponentEvent(user, null, this, consoleFormat,
consoleFormatEvent.message());
@@ -7,6 +7,7 @@ import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.events.misc.CarbonEvents;
import net.draycia.carbon.api.events.ChannelRegisterEvent;
import net.draycia.carbon.common.commands.AliasedChannelCommand;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -96,11 +97,11 @@ public class ChannelManager {
if (channel != null) {
this.registerChannel(channel);
// TODO: register AliasedChannelCommand for each channel
String prefix = null;
if (channel instanceof TextChannel) {
prefix = ((TextChannel) channel).messagePrefix();
new AliasedChannelCommand(this.carbonChat.commandManager(), (TextChannel) channel);
}
if (prefix != null && !prefix.trim().isEmpty()) {
@@ -108,7 +109,6 @@ public class ChannelManager {
} else {
prefix = "";
}
this.carbonChat.logger().info("Registering channel: " + prefix + channel.name());
}
}
@@ -37,7 +37,6 @@ public class ChannelListCommand {
commandManager.createDefaultCommandMeta())
.senderType(CarbonUser.class) // console & player
.permission("carbonchat.channellist")
// TODO: make this return a PlayerUser
.argument(PlayerUserArgument.optionalPlayerUserArgument()) // carbonchat.channellist.other
.handler(context -> {
if (context.getOptional("user").isPresent()) {
@@ -114,7 +113,6 @@ public class ChannelListCommand {
private void makeList(@NonNull final Iterator<@NonNull ChatChannel> iterator, final TextComponent.@NonNull Builder list) {
final String listSeparator = this.carbonChat.translations().channelListSeparator();
// TODO: Larry, why did you double assign the listSeparatorComponent?
final Component listSeparatorComponent = Component.text(listSeparator);
while (iterator.hasNext()) {
@@ -6,6 +6,7 @@ import cloud.commandframework.context.CommandContext;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.channels.TextChannel;
import net.draycia.carbon.api.config.ExceptionMessages;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;
@@ -16,7 +17,7 @@ public final class ChannelArgument<C> extends CommandArgument<C, TextChannel> {
private ChannelArgument(final boolean required) {
super(required, "channel", ChannelArgument::parser,
CarbonChatProvider.carbonChat().channelRegistry().defaultChannel().key(),
CarbonChatProvider.carbonChat().channelRegistry().defaultValue().key(),
TextChannel.class, ChannelArgument::suggestions);
}
@@ -24,9 +25,10 @@ public final class ChannelArgument<C> extends CommandArgument<C, TextChannel> {
final @NonNull Queue<String> inputs) {
final String input = inputs.poll();
final ExceptionMessages exceptions = CarbonChatProvider.carbonChat().translations().exceptionMessages();
if (input == null) {
// TODO: make exception messages configurable
return ArgumentParseResult.failure(new IllegalArgumentException("Channel cannot be null"));
return ArgumentParseResult.failure(new IllegalArgumentException(exceptions.channelNotFound()));
}
final ChatChannel channel = CarbonChatProvider.carbonChat()
@@ -35,9 +37,9 @@ public final class ChannelArgument<C> extends CommandArgument<C, TextChannel> {
if (channel instanceof TextChannel) {
return ArgumentParseResult.success((TextChannel) channel);
} else if (channel != null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Channel is not public!"));
return ArgumentParseResult.failure(new IllegalArgumentException(exceptions.channelNotPublic()));
} else {
return ArgumentParseResult.failure(new IllegalArgumentException("Channel does not exist"));
return ArgumentParseResult.failure(new IllegalArgumentException(exceptions.channelNotFound()));
}
}
@@ -25,21 +25,23 @@ public final class PlayerUserArgument<C> extends CommandArgument<C, PlayerUser>
final @NonNull Queue<String> inputs) {
final String input = inputs.poll();
final String playerNotFound = CarbonChatProvider.carbonChat().translations().exceptionMessages()
.playerNotFound();
if (input == null) {
// TODO: make exception messages configurable
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
}
final UUID uuid = CarbonChatProvider.carbonChat().userService().resolve(input);
if (uuid == null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
}
final PlayerUser user = CarbonChatProvider.carbonChat().userService().wrap(uuid);
if (user == null) {
return ArgumentParseResult.failure(new IllegalArgumentException("Player not found!"));
return ArgumentParseResult.failure(new IllegalArgumentException(playerNotFound));
}
return ArgumentParseResult.success(user);
@@ -28,7 +28,7 @@ public class PlayerJoinListener {
}
if (channel.equals("DEFAULT")) {
user.selectedChannel(carbonChat.channelRegistry().defaultChannel());
user.selectedChannel(carbonChat.channelRegistry().defaultValue());
return;
}
@@ -1,6 +1,10 @@
package net.draycia.carbon.common.utils;
import net.draycia.carbon.api.CarbonChatProvider;
import net.draycia.carbon.api.adventure.MessageProcessor;
import net.draycia.carbon.api.users.CarbonUser;
import net.draycia.carbon.common.adventure.AdventureManager;
import net.draycia.carbon.common.adventure.FormatType;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
@@ -27,6 +31,7 @@ public final class ColorUtils {
}
// TODO: find out way to do this
// TODO: add parsePlaceholders() to PlayerUser class
// if (user != null) {
// final Player player = Bukkit.getPlayer(user.uuid());
@@ -62,26 +67,40 @@ public final class ColorUtils {
// TODO: check if MiniMessage or MineDown
String output = input;
// Legacy RGB
output = spigotLegacyRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
String hexReplacement = "<#$1$2$3$4$5$6>";
boolean legacyCapable = false;
// Alternate RGB, TAB (neznamy) && KiteBoard
output = pluginRGB.matcher(output).replaceAll("<#$1$2$3$4$5$6>");
final MessageProcessor messageProcessor = CarbonChatProvider.carbonChat().messageProcessor();
// Legacy Colors
for (final char c : "0123456789abcdef".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextColor color = format.color();
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
if (messageProcessor instanceof AdventureManager) {
if (((AdventureManager) messageProcessor).formatType() == FormatType.MINEDOWN) {
hexReplacement = "&#$1$2$3$4$5$6&";
legacyCapable = true;
}
}
// Legacy Formatting
for (final char c : "klmno".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextDecoration decoration = format.decoration();
// Legacy RGB
output = spigotLegacyRGB.matcher(output).replaceAll(hexReplacement);
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
// Alternate RGB, TAB (neznamy) && KiteBoard
output = pluginRGB.matcher(output).replaceAll(hexReplacement);
if (!legacyCapable) {
// Legacy Colors
for (final char c : "0123456789abcdef".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextColor color = format.color();
output = output.replaceAll("[§&]" + c, "<" + color.asHexString() + ">");
}
// Legacy Formatting
for (final char c : "klmno".toCharArray()) {
final LegacyFormat format = LegacyComponentSerializer.parseChar(c);
final TextDecoration decoration = format.decoration();
output = output.replaceAll("[§&]" + c, "<" + decoration.name() + ">");
}
}
return output;