mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
Update moonshine, add auto-nickname (#155)
This commit is contained in:
1 parent
74e83a08a0
commit
3b1987b5a2
71 files changed
+1022
-829
No files matched your search
@@ -4,7 +4,7 @@ import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.Moonshine;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.channels.ChannelRegistry;
|
||||
@@ -12,16 +12,21 @@ import net.draycia.carbon.api.users.UserManager;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.common.config.ConfigLoader;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageParser;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSender;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSource;
|
||||
import net.draycia.carbon.common.messages.ComponentPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.ServerReceiverResolver;
|
||||
import net.draycia.carbon.common.messages.ReceiverResolver;
|
||||
import net.draycia.carbon.common.messages.StringPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.UUIDPlaceholderResolver;
|
||||
import net.draycia.carbon.common.users.JSONUserManager;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.Moonshine;
|
||||
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
|
||||
import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy;
|
||||
import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -58,21 +63,24 @@ public final class CarbonCommonModule extends AbstractModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
public CarbonMessageService messageService(
|
||||
final ServerReceiverResolver serverReceiverResolver,
|
||||
final ReceiverResolver receiverResolver,
|
||||
final ComponentPlaceholderResolver<Audience> componentPlaceholderResolver,
|
||||
final UUIDPlaceholderResolver<Audience> uuidPlaceholderResolver,
|
||||
final StringPlaceholderResolver<Audience> stringPlaceholderResolver,
|
||||
final CarbonMessageSource carbonMessageSource,
|
||||
final CarbonMessageParser carbonMessageParser,
|
||||
final CarbonMessageSender carbonMessageSender
|
||||
) {
|
||||
return Moonshine.<Audience>builder()
|
||||
.receiver(serverReceiverResolver)
|
||||
.placeholder(Component.class, componentPlaceholderResolver)
|
||||
.placeholder(UUID.class, uuidPlaceholderResolver)
|
||||
.source(carbonMessageSource)
|
||||
.parser(carbonMessageParser)
|
||||
.sender(carbonMessageSender)
|
||||
.create(CarbonMessageService.class, this.getClass().getClassLoader());
|
||||
final CarbonMessageSender carbonMessageSender,
|
||||
final CarbonMessageRenderer carbonMessageRenderer
|
||||
) throws UnscannableMethodException {
|
||||
return Moonshine.<CarbonMessageService, Audience>builder(new TypeToken<>() {})
|
||||
.receiverLocatorResolver(receiverResolver, 0)
|
||||
.sourced(carbonMessageSource)
|
||||
.rendered(carbonMessageRenderer)
|
||||
.sent(carbonMessageSender)
|
||||
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
|
||||
.weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0)
|
||||
.weightedPlaceholderResolver(UUID.class, uuidPlaceholderResolver, 0)
|
||||
.weightedPlaceholderResolver(String.class, stringPlaceholderResolver, 0)
|
||||
.create(this.getClass().getClassLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+16
-16
@@ -17,6 +17,7 @@ import net.kyori.adventure.key.Key;
|
||||
import net.kyori.registry.DefaultedRegistry;
|
||||
import net.kyori.registry.RegistryImpl;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -29,14 +30,23 @@ import org.spongepowered.configurate.serialize.SerializationException;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implements ChannelRegistry, DefaultedRegistry<Key, ChatChannel> {
|
||||
|
||||
private static @MonotonicNonNull ObjectMapper<ConfigChatChannel> MAPPER;
|
||||
|
||||
static {
|
||||
try {
|
||||
MAPPER = ObjectMapper.factory().get(ConfigChatChannel.class);
|
||||
} catch (final SerializationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private final ConfigLoader configLoader;
|
||||
private final Path dataDirectory;
|
||||
private final Injector injector;
|
||||
private final Logger logger;
|
||||
private final PrimaryConfig primaryConfig;
|
||||
|
||||
private Key defaultKey;
|
||||
private ChatChannel basicChannel;
|
||||
private @MonotonicNonNull Key defaultKey;
|
||||
private @MonotonicNonNull ChatChannel basicChannel;
|
||||
|
||||
@Inject
|
||||
public CarbonChannelRegistry(
|
||||
@@ -102,16 +112,6 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implem
|
||||
}
|
||||
}
|
||||
|
||||
private static ObjectMapper<ConfigChatChannel> MAPPER;
|
||||
|
||||
static {
|
||||
try {
|
||||
MAPPER = ObjectMapper.factory().get(ConfigChatChannel.class);
|
||||
} catch (final SerializationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public @Nullable ChatChannel loadChannel(final Path channelFile) {
|
||||
final ConfigurationLoader<?> loader = this.configLoader.configurationLoader(channelFile);
|
||||
|
||||
@@ -125,17 +125,17 @@ public class CarbonChannelRegistry extends RegistryImpl<Key, ChatChannel> implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChatChannel defaultValue() {
|
||||
public ChatChannel defaultValue() {
|
||||
return Objects.requireNonNullElse(this.get(this.defaultKey), this.basicChannel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Key defaultKey() {
|
||||
public Key defaultKey() {
|
||||
return this.defaultKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull ChatChannel getOrDefault(@NonNull final Key key) {
|
||||
public ChatChannel getOrDefault(final Key key) {
|
||||
final @Nullable ChatChannel channel = this.get(key);
|
||||
|
||||
if (channel != null) {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package net.draycia.carbon.common.channels;
|
||||
|
||||
import com.proximyst.moonshine.Moonshine;
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.CarbonChatProvider;
|
||||
@@ -10,14 +11,19 @@ import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageService;
|
||||
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageParser;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageRenderer;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSender;
|
||||
import net.draycia.carbon.common.messages.ComponentPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.ServerReceiverResolver;
|
||||
import net.draycia.carbon.common.messages.ReceiverResolver;
|
||||
import net.draycia.carbon.common.messages.StringPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.UUIDPlaceholderResolver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.Moonshine;
|
||||
import net.kyori.moonshine.exception.scan.UnscannableMethodException;
|
||||
import net.kyori.moonshine.strategy.StandardPlaceholderResolverStrategy;
|
||||
import net.kyori.moonshine.strategy.supertype.StandardSupertypeThenInterfaceSupertypeStrategy;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
@@ -26,6 +32,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Setting;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
|
||||
@ConfigSerializable
|
||||
@@ -37,7 +44,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
The key can stay "carbon".
|
||||
The value is what's used in commands, this is probably what you want to change.
|
||||
""")
|
||||
private Key key = Key.key("carbon", "basic");
|
||||
private final Key key = Key.key("carbon", "basic");
|
||||
|
||||
@Comment("""
|
||||
The permission required to use the channel.
|
||||
@@ -45,11 +52,11 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
To send messages you must have the permission carbon.channel.basic.speak
|
||||
If you want to give both, grant carbon.channel.basic or carbon.channel.basic.*
|
||||
""")
|
||||
private String permission = "carbon.channel.basic";
|
||||
private final String permission = "carbon.channel.basic";
|
||||
|
||||
@Setting("format")
|
||||
@Comment("The chat formats for this channel.")
|
||||
private ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
|
||||
private final ConfigChannelMessageSource messageSource = new ConfigChannelMessageSource();
|
||||
|
||||
private transient @Nullable ConfigChannelMessageService messageService = null;
|
||||
|
||||
@@ -63,7 +70,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
return this.messageService().chatFormat(
|
||||
recipient,
|
||||
sender.uuid(),
|
||||
sender.displayName(),
|
||||
Objects.requireNonNullElseGet(sender.displayName(), () -> Component.text(sender.username())),
|
||||
sender.username(),
|
||||
message
|
||||
);
|
||||
@@ -113,21 +120,31 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
return this.key;
|
||||
}
|
||||
|
||||
private ConfigChannelMessageService createMessageService() {
|
||||
final ServerReceiverResolver serverReceiverResolver = new ServerReceiverResolver(CarbonChatProvider.carbonChat().server());
|
||||
private @Nullable ConfigChannelMessageService createMessageService() {
|
||||
final ReceiverResolver serverReceiverResolver = new ReceiverResolver();
|
||||
final ComponentPlaceholderResolver<Audience> componentPlaceholderResolver = new ComponentPlaceholderResolver<>();
|
||||
final UUIDPlaceholderResolver<Audience> uuidPlaceholderResolver = new UUIDPlaceholderResolver<>();
|
||||
final CarbonMessageParser carbonMessageParser = new CarbonMessageParser();
|
||||
final StringPlaceholderResolver<Audience> stringPlaceholderResolver = new StringPlaceholderResolver<>();
|
||||
final CarbonMessageRenderer carbonMessageRenderer = new CarbonMessageRenderer();
|
||||
final CarbonMessageSender carbonMessageSender = new CarbonMessageSender();
|
||||
|
||||
return Moonshine.<Audience>builder()
|
||||
.receiver(serverReceiverResolver)
|
||||
.placeholder(Component.class, componentPlaceholderResolver)
|
||||
.placeholder(UUID.class, uuidPlaceholderResolver)
|
||||
.source(this.messageSource)
|
||||
.parser(carbonMessageParser)
|
||||
.sender(carbonMessageSender)
|
||||
.create(ConfigChannelMessageService.class, this.getClass().getClassLoader());
|
||||
try {
|
||||
return Moonshine.<ConfigChannelMessageService, Audience>builder(new TypeToken<>() {
|
||||
})
|
||||
.receiverLocatorResolver(serverReceiverResolver, 0)
|
||||
.sourced(this.messageSource)
|
||||
.rendered(carbonMessageRenderer)
|
||||
.sent(carbonMessageSender)
|
||||
.resolvingWithStrategy(new StandardPlaceholderResolverStrategy<>(new StandardSupertypeThenInterfaceSupertypeStrategy(false)))
|
||||
.weightedPlaceholderResolver(Component.class, componentPlaceholderResolver, 0)
|
||||
.weightedPlaceholderResolver(UUID.class, uuidPlaceholderResolver, 0)
|
||||
.weightedPlaceholderResolver(String.class, stringPlaceholderResolver, 0)
|
||||
.create(this.getClass().getClassLoader());
|
||||
} catch (final UnscannableMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private ConfigChannelMessageService messageService() {
|
||||
@@ -135,7 +152,7 @@ public final class ConfigChatChannel implements ChatChannel {
|
||||
this.messageService = this.createMessageService();
|
||||
}
|
||||
|
||||
return this.messageService;
|
||||
return requireNonNull(this.messageService, "Channel message service must not be null!");
|
||||
}
|
||||
|
||||
}
|
||||
+7
-5
@@ -1,20 +1,22 @@
|
||||
package net.draycia.carbon.common.channels.messages;
|
||||
|
||||
import com.proximyst.moonshine.annotation.Message;
|
||||
import com.proximyst.moonshine.annotation.Placeholder;
|
||||
import com.proximyst.moonshine.annotation.Receiver;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.annotation.Message;
|
||||
import net.kyori.moonshine.annotation.Placeholder;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public interface ConfigChannelMessageService {
|
||||
|
||||
// TODO: locale placeholders?
|
||||
@Message("channel.format")
|
||||
Component chatFormat(
|
||||
@Receiver final Audience audience,
|
||||
final Audience audience,
|
||||
@Placeholder UUID uuid,
|
||||
@Placeholder Component displayname,
|
||||
@Placeholder("displayname") Component displayName,
|
||||
@Placeholder String username,
|
||||
@Placeholder Component message
|
||||
);
|
||||
|
||||
+23
-22
@@ -1,10 +1,10 @@
|
||||
package net.draycia.carbon.common.channels.messages;
|
||||
|
||||
import com.proximyst.moonshine.message.IMessageSource;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.moonshine.message.IMessageSource;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
@@ -13,7 +13,7 @@ import org.spongepowered.configurate.objectmapping.meta.Setting;
|
||||
|
||||
@ConfigSerializable
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ConfigChannelMessageSource implements IMessageSource<String, Audience> {
|
||||
public class ConfigChannelMessageSource implements IMessageSource<Audience, String> {
|
||||
|
||||
// Map<String, String> -> Map<Group, Format>
|
||||
// "default" key will be configurable but let's not worry about that for now
|
||||
@@ -24,7 +24,7 @@ public class ConfigChannelMessageSource implements IMessageSource<String, Audien
|
||||
The "console" format is what's shown to console.
|
||||
The keys are group names, the values are chat formats (MiniMessage).
|
||||
""")
|
||||
private Map<String, String> defaults = Map.of("default", "<displayname>: <message>",
|
||||
private final Map<String, String> defaults = Map.of("default", "<displayname>: <message>",
|
||||
"console", "<username> - <uuid>: <message>");
|
||||
|
||||
// TODO: Move the default to the advanced config?
|
||||
@@ -33,34 +33,35 @@ public class ConfigChannelMessageSource implements IMessageSource<String, Audien
|
||||
You can safely delete this section if you don't want to use this feature.
|
||||
Will fall back to the defaults section if no format was found for the player.
|
||||
""")
|
||||
private Map<Locale, Map<String, String>> locales = Map.of(Locale.US,
|
||||
private final Map<Locale, Map<String, String>> locales = Map.of(Locale.US,
|
||||
Map.of("default", "<displayname>: <message>"));
|
||||
|
||||
@Override
|
||||
public String message(final String key, final Audience receiver) {
|
||||
public String messageOf(final Audience receiver, final String messageKey) {
|
||||
if (receiver instanceof CarbonPlayer player) {
|
||||
return this.forPlayer(key, player);
|
||||
return this.forPlayer(messageKey, player);
|
||||
} else {
|
||||
return this.forAudience(key, receiver);
|
||||
return this.forAudience(messageKey, receiver);
|
||||
}
|
||||
}
|
||||
|
||||
private String forPlayer(final String key, final CarbonPlayer player) {
|
||||
final Map<String, String> formats = this.locales.get(player.locale());
|
||||
|
||||
if (formats != null) {
|
||||
final String format = formats.get(player.primaryGroup());
|
||||
|
||||
if (format != null) {
|
||||
return format;
|
||||
}
|
||||
|
||||
final String defaultFormat = formats.get("default");
|
||||
|
||||
if (defaultFormat != null) {
|
||||
return defaultFormat;
|
||||
}
|
||||
}
|
||||
// TODO: rewrite and reimplement
|
||||
// final Map<String, String> formats = this.locales.get(player.locale());
|
||||
//
|
||||
// if (formats != null) {
|
||||
// final String format = formats.get(player.primaryGroup());
|
||||
//
|
||||
// if (format != null) {
|
||||
// return format;
|
||||
// }
|
||||
//
|
||||
// final String defaultFormat = formats.get("default");
|
||||
//
|
||||
// if (defaultFormat != null) {
|
||||
// return defaultFormat;
|
||||
// }
|
||||
// }
|
||||
|
||||
final String format = this.defaults.get(player.primaryGroup());
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -15,24 +14,20 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
import org.spongepowered.configurate.loader.ConfigurationLoader;
|
||||
import org.spongepowered.configurate.objectmapping.ObjectMapper;
|
||||
import org.spongepowered.configurate.objectmapping.guice.GuiceObjectMapperProvider;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ConfigLoader {
|
||||
|
||||
private final Path dataDirectory;
|
||||
private final ObjectMapper.Factory mapper;
|
||||
private final LocaleSerializerConfigurate locale;
|
||||
|
||||
@Inject
|
||||
public ConfigLoader(
|
||||
@ForCarbon final Path dataDirectory,
|
||||
final Injector injector
|
||||
final LocaleSerializerConfigurate locale
|
||||
) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.mapper = ObjectMapper.factoryBuilder()
|
||||
.addDiscoverer(GuiceObjectMapperProvider.injectedObjectDiscoverer(injector))
|
||||
.build();
|
||||
this.locale = locale;
|
||||
}
|
||||
|
||||
public ConfigurationLoader<?> configurationLoader(final Path file) {
|
||||
@@ -43,8 +38,8 @@ public class ConfigLoader {
|
||||
ConfigurateComponentSerializer.configurate();
|
||||
|
||||
return opts.shouldCopyDefaults(true).serializers(serializerBuilder ->
|
||||
serializerBuilder.registerAll(serializer.serializers())
|
||||
.register(Locale.class, new LocaleSerializerConfigurate())
|
||||
serializerBuilder.registerAll(serializer.serializers())
|
||||
.register(Locale.class, this.locale)
|
||||
);
|
||||
})
|
||||
.path(file)
|
||||
@@ -62,7 +57,7 @@ public class ConfigLoader {
|
||||
|
||||
try {
|
||||
final var node = loader.load();
|
||||
final T config = node.get(clazz);
|
||||
final @Nullable T config = node.get(clazz);
|
||||
|
||||
if (!Files.exists(file)) {
|
||||
node.set(clazz, config);
|
||||
|
||||
@@ -12,22 +12,20 @@ import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
public class PrimaryConfig {
|
||||
|
||||
@Comment("The default locale for plugin messages.")
|
||||
private Locale defaultLocale = Locale.US;
|
||||
private final Locale defaultLocale = Locale.US;
|
||||
@Comment("The default channel that new players will be in when they join.")
|
||||
private final Key defaultChannel = Key.key("carbon", "basic");
|
||||
@Comment("The service that will be used to store and load player information.")
|
||||
private final StorageType storageType = StorageType.JSON;
|
||||
|
||||
public Locale defaultLocale() {
|
||||
return this.defaultLocale;
|
||||
}
|
||||
|
||||
@Comment("The default channel that new players will be in when they join.")
|
||||
private Key defaultChannel = Key.key("carbon", "basic");
|
||||
|
||||
public Key defaultChannel() {
|
||||
return this.defaultChannel;
|
||||
}
|
||||
|
||||
@Comment("The service that will be used to store and load player information.")
|
||||
private StorageType storageType = StorageType.JSON;
|
||||
|
||||
public StorageType storageType() {
|
||||
return this.storageType;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.KeyedRenderer;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -23,13 +21,12 @@ public class MuteHandler {
|
||||
|
||||
private final KeyedRenderer renderer =
|
||||
keyedRenderer(this.muteKey, (sender, recipient, message, originalMessage) ->
|
||||
this.messageService.muteSpyPrefix().append(message));
|
||||
this.messageService.muteSpyPrefix(recipient).append(message));
|
||||
|
||||
@Inject
|
||||
public MuteHandler(
|
||||
final CarbonChat carbonChat,
|
||||
final CarbonMessageService messageService,
|
||||
final CommandManager<Commander> commandManager
|
||||
final CarbonMessageService messageService
|
||||
) {
|
||||
this.messageService = messageService;
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageParser;
|
||||
import com.proximyst.moonshine.message.ParsingContext;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
|
||||
@Singleton
|
||||
public class CarbonMessageParser implements IMessageParser<String, Component, Audience> {
|
||||
|
||||
public Component parse(final String message, final ParsingContext<Audience> parsingContext) {
|
||||
return MiniMessage.get().parse(message, parsingContext.placeholders());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonMessageRenderer implements IMessageRenderer<Audience, String, Component, Component> {
|
||||
|
||||
@Override
|
||||
public Component render(
|
||||
final Audience receiver,
|
||||
final String intermediateMessage,
|
||||
final Map<String, ? extends Component> resolvedPlaceholders,
|
||||
final Method method,
|
||||
final Type owner
|
||||
) {
|
||||
final List<Template> templates = new ArrayList<>();
|
||||
|
||||
for (final var entry : resolvedPlaceholders.entrySet()) {
|
||||
templates.add(Template.of(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
return MiniMessage.get().parse(intermediateMessage, templates);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,17 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.message.IMessageSender;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.message.IMessageSender;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonMessageSender implements IMessageSender<Audience, Component> {
|
||||
|
||||
@Override
|
||||
public void sendMessage(final Audience receiver, final Component message) {
|
||||
receiver.sendMessage(message);
|
||||
public void send(final Audience receiver, final Component renderedMessage) {
|
||||
receiver.sendMessage(renderedMessage);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +1,31 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.annotation.Message;
|
||||
import com.proximyst.moonshine.annotation.Placeholder;
|
||||
import com.proximyst.moonshine.annotation.Receiver;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.annotation.Message;
|
||||
import net.kyori.moonshine.annotation.Placeholder;
|
||||
|
||||
public interface CarbonMessageService {
|
||||
|
||||
@Message("example.command.hello")
|
||||
void exampleCommandFeedback(
|
||||
@Receiver final Audience audience,
|
||||
final Audience audience,
|
||||
@Placeholder final Component plugin
|
||||
);
|
||||
|
||||
@Message("unsupported.legacy")
|
||||
void unsupportedLegacyChar(
|
||||
@Receiver final Audience audience,
|
||||
final Audience audience,
|
||||
@Placeholder final String message
|
||||
);
|
||||
|
||||
@Message("test.phrase")
|
||||
void localeTestMessage(@Receiver final Audience audience);
|
||||
void localeTestMessage(final Audience audience);
|
||||
|
||||
@Message("channel.format.basic")
|
||||
Component basicChatFormat(
|
||||
@Receiver final Audience audience,
|
||||
final Audience audience,
|
||||
@Placeholder UUID uuid,
|
||||
@Placeholder Component displayname,
|
||||
@Placeholder String username,
|
||||
@@ -34,9 +33,6 @@ public interface CarbonMessageService {
|
||||
);
|
||||
|
||||
@Message("mute.spy.prefix")
|
||||
Component muteSpyPrefix();
|
||||
|
||||
@Message("placeholders.plugin")
|
||||
Component pluginName();
|
||||
Component muteSpyPrefix(final Audience audience);
|
||||
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageSource;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -28,6 +27,7 @@ import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.translation.Translator;
|
||||
import net.kyori.moonshine.message.IMessageSource;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
@@ -35,7 +35,7 @@ import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class CarbonMessageSource implements IMessageSource<String, Audience> {
|
||||
public final class CarbonMessageSource implements IMessageSource<Audience, String> {
|
||||
|
||||
private final Locale defaultLocale;
|
||||
private final Map<Locale, Properties> locales = new HashMap<>();
|
||||
@@ -109,11 +109,11 @@ public final class CarbonMessageSource implements IMessageSource<String, Audienc
|
||||
}
|
||||
|
||||
@Override
|
||||
public String message(final String key, final Audience receiver) {
|
||||
public String messageOf(final Audience receiver, final String messageKey) {
|
||||
if (receiver instanceof CarbonPlayer player) {
|
||||
return this.forPlayer(key, player);
|
||||
return this.forPlayer(messageKey, player);
|
||||
} else {
|
||||
return this.forAudience(key, receiver);
|
||||
return this.forAudience(messageKey, receiver);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+16
-10
@@ -1,25 +1,31 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.proximyst.moonshine.component.placeholder.IPlaceholderResolver;
|
||||
import com.proximyst.moonshine.component.placeholder.PlaceholderContext;
|
||||
import com.proximyst.moonshine.component.placeholder.ResolveResult;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.moonshine.placeholder.ConclusionValue;
|
||||
import net.kyori.moonshine.placeholder.ContinuanceValue;
|
||||
import net.kyori.moonshine.placeholder.IPlaceholderResolver;
|
||||
import net.kyori.moonshine.util.Either;
|
||||
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 ComponentPlaceholderResolver<R> implements IPlaceholderResolver<R, Component> {
|
||||
public class ComponentPlaceholderResolver<R> implements IPlaceholderResolver<R, Component, Component> {
|
||||
|
||||
public ResolveResult resolve(
|
||||
@Override
|
||||
public @Nullable Map<String, Either<ConclusionValue<? extends Component>, ContinuanceValue<?>>>
|
||||
resolve(
|
||||
final String placeholderName,
|
||||
final Component value,
|
||||
final PlaceholderContext<R> ctx,
|
||||
final Multimap<String, @Nullable Object> flags
|
||||
final R receiver,
|
||||
final Type owner,
|
||||
final Method method,
|
||||
final @Nullable Object[] parameters
|
||||
) {
|
||||
return ResolveResult.ok(placeholderName, MiniMessage.get().serialize(value));
|
||||
return Map.of(placeholderName, Either.left(ConclusionValue.conclusionValue(value)));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Modified to better fit in Carbon
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import javax.inject.Singleton;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.moonshine.receiver.IReceiverLocator;
|
||||
import net.kyori.moonshine.receiver.IReceiverLocatorResolver;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public final class ReceiverResolver implements IReceiverLocatorResolver<Audience> {
|
||||
|
||||
@Override
|
||||
public IReceiverLocator<Audience> resolve(final Method method, final Type proxy) {
|
||||
return new Resolver();
|
||||
}
|
||||
|
||||
private static final class Resolver implements IReceiverLocator<Audience> {
|
||||
@Override
|
||||
public Audience locate(final Method method, final Object proxy, final @Nullable Object[] parameters) {
|
||||
for (final Object parameter : parameters) {
|
||||
if (parameter instanceof Audience audience) {
|
||||
return audience;
|
||||
}
|
||||
}
|
||||
|
||||
return Audience.empty();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ServerReceiver {
|
||||
String permission() default "";
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Modified to better fit in Carbon
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiver;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiverResolver;
|
||||
import com.proximyst.moonshine.component.receiver.ReceiverContext;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@Singleton
|
||||
public final class ServerReceiverResolver implements IReceiverResolver<Audience> {
|
||||
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
|
||||
@Inject
|
||||
public ServerReceiverResolver(final CarbonServer carbonServer) {
|
||||
this.carbonServer = carbonServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<IReceiver<Audience>> resolve(final Method method) {
|
||||
final ServerReceiver annotation = method.getAnnotation(ServerReceiver.class);
|
||||
if (annotation == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(new Resolver(this.carbonServer, annotation.permission()));
|
||||
}
|
||||
|
||||
private static final class Resolver implements IReceiver<Audience> {
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
private final @Nullable String permission;
|
||||
|
||||
private Resolver(final @NonNull CarbonServer carbonServer,
|
||||
final @Nullable String permission) {
|
||||
this.carbonServer = carbonServer;
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience find(final ReceiverContext ctx) {
|
||||
if (this.permission == null) {
|
||||
return this.carbonServer;
|
||||
}
|
||||
|
||||
return Audience.audience(Stream.concat(Stream.of(this.carbonServer.console()),
|
||||
Streams.stream(this.carbonServer.players())
|
||||
.filter(player -> player.hasPermission(this.permission)))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.placeholder.ConclusionValue;
|
||||
import net.kyori.moonshine.placeholder.ContinuanceValue;
|
||||
import net.kyori.moonshine.placeholder.IPlaceholderResolver;
|
||||
import net.kyori.moonshine.util.Either;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
public class StringPlaceholderResolver<R> implements IPlaceholderResolver<R, String, Component> {
|
||||
|
||||
@Override
|
||||
public @Nullable Map<String, Either<ConclusionValue<? extends Component>, ContinuanceValue<?>>>
|
||||
resolve(
|
||||
final String placeholderName,
|
||||
final String value,
|
||||
final R receiver,
|
||||
final Type owner,
|
||||
final Method method,
|
||||
final @Nullable Object[] parameters
|
||||
) {
|
||||
return Map.of(placeholderName, Either.left(ConclusionValue.conclusionValue(Component.text(value))));
|
||||
}
|
||||
|
||||
}
|
||||
+20
-9
@@ -1,21 +1,32 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.proximyst.moonshine.component.placeholder.IPlaceholderResolver;
|
||||
import com.proximyst.moonshine.component.placeholder.PlaceholderContext;
|
||||
import com.proximyst.moonshine.component.placeholder.ResolveResult;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.moonshine.placeholder.ConclusionValue;
|
||||
import net.kyori.moonshine.placeholder.ContinuanceValue;
|
||||
import net.kyori.moonshine.placeholder.IPlaceholderResolver;
|
||||
import net.kyori.moonshine.util.Either;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
public class UUIDPlaceholderResolver<R> implements IPlaceholderResolver<R, UUID> {
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class UUIDPlaceholderResolver<R> implements IPlaceholderResolver<R, UUID, Component> {
|
||||
|
||||
public ResolveResult resolve(
|
||||
@Override
|
||||
public @Nullable Map<String, Either<ConclusionValue<? extends Component>, ContinuanceValue<?>>>
|
||||
resolve(
|
||||
final String placeholderName,
|
||||
final UUID value,
|
||||
final PlaceholderContext<R> ctx,
|
||||
final Multimap<String, @Nullable Object> flags
|
||||
final R receiver,
|
||||
final Type owner,
|
||||
final Method method,
|
||||
final @Nullable Object[] parameters
|
||||
) {
|
||||
return ResolveResult.ok(placeholderName, value.toString());
|
||||
return Map.of(placeholderName, Either.left(ConclusionValue.conclusionValue(Component.text(value.toString()))));
|
||||
}
|
||||
|
||||
}
|
||||
+3
@@ -13,7 +13,10 @@ import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonPlayerSerializerGson implements JsonSerializer<CarbonPlayerCommon>, JsonDeserializer<CarbonPlayer> {
|
||||
|
||||
@Override
|
||||
|
||||
+2
-2
@@ -10,6 +10,7 @@ import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.intellij.lang.annotations.Subst;
|
||||
|
||||
import static net.kyori.adventure.key.Key.key;
|
||||
|
||||
@@ -29,9 +30,8 @@ public class ChatChannelSerializerGson extends TypeAdapter<ChatChannel> {
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("PatternValidation") // the input is variable
|
||||
public @Nullable ChatChannel read(final JsonReader in) throws IOException {
|
||||
final @Nullable String channelName = in.nextString();
|
||||
@Subst("namespace:value") final @Nullable String channelName = in.nextString();
|
||||
|
||||
if (channelName != null) {
|
||||
return this.registry.get(key(channelName));
|
||||
|
||||
+24
-5
@@ -1,29 +1,48 @@
|
||||
package net.draycia.carbon.common.serialisation.gson;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Locale;
|
||||
import net.kyori.adventure.translation.Translator;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.ConfigurationNode;
|
||||
import org.spongepowered.configurate.serialize.SerializationException;
|
||||
import org.spongepowered.configurate.serialize.TypeSerializer;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class LocaleSerializerConfigurate implements TypeSerializer<Locale> {
|
||||
|
||||
private final Logger logger;
|
||||
|
||||
@Inject
|
||||
public LocaleSerializerConfigurate(final Logger logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Locale deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
|
||||
final String value = node.getString();
|
||||
public Locale deserialize(final Type type, final ConfigurationNode node) {
|
||||
final @Nullable String value = node.getString();
|
||||
|
||||
if (value == null) {
|
||||
throw new SerializationException("value cannot be null!");
|
||||
this.logger.warn("value null for locale! defaulting to en_US");
|
||||
return Locale.ENGLISH;
|
||||
}
|
||||
|
||||
return Translator.parseLocale(value);
|
||||
return requireNonNull(Translator.parseLocale(value), "value locale cannot be null!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(final Type type, @Nullable final Locale obj, final ConfigurationNode node) throws SerializationException {
|
||||
node.set(obj.toLanguageTag());
|
||||
if (obj == null) {
|
||||
node.set(null);
|
||||
} else {
|
||||
node.set(obj.toLanguageTag());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,12 +9,11 @@ import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import static java.util.Objects.requireNonNullElseGet;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Single {
|
||||
@@ -24,8 +23,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
protected boolean muted = false;
|
||||
protected @Nullable ChatChannel selectedChannel;
|
||||
protected boolean spying = false;
|
||||
protected String username;
|
||||
protected UUID uuid;
|
||||
protected @MonotonicNonNull String username;
|
||||
protected @MonotonicNonNull UUID uuid;
|
||||
|
||||
public CarbonPlayerCommon(
|
||||
final @Nullable Component displayName,
|
||||
@@ -44,7 +43,7 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Audience audience() {
|
||||
public @NotNull Audience audience() {
|
||||
return Audience.empty();
|
||||
}
|
||||
|
||||
@@ -58,13 +57,13 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component displayName() {
|
||||
return requireNonNullElseGet(this.displayName, () -> text(this.username));
|
||||
public @Nullable Component displayName() {
|
||||
return this.displayName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayName(final @Nullable Component displayName) {
|
||||
this.displayName = requireNonNullElseGet(displayName, () -> text(this.username));
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,9 +9,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.ComponentPlayerResult;
|
||||
@@ -32,11 +34,10 @@ import static net.kyori.adventure.text.Component.text;
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class JSONUserManager implements UserManager {
|
||||
|
||||
private final Path dataDirectory;
|
||||
private final Injector injector;
|
||||
private final Logger logger;
|
||||
private final Gson serializer;
|
||||
private final Path userDirectory;
|
||||
private final CarbonChat carbonChat;
|
||||
|
||||
private final Map<UUID, CarbonPlayerCommon> userCache = new ConcurrentHashMap<>();
|
||||
|
||||
@@ -44,20 +45,20 @@ public class JSONUserManager implements UserManager {
|
||||
public JSONUserManager(
|
||||
final @ForCarbon Path dataDirectory,
|
||||
final Injector injector,
|
||||
final Logger logger
|
||||
) throws IOException {
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.injector = injector;
|
||||
final Logger logger,
|
||||
final CarbonChat carbonChat
|
||||
) throws IOException {
|
||||
this.logger = logger;
|
||||
this.userDirectory = this.dataDirectory.resolve("users");
|
||||
this.userDirectory = dataDirectory.resolve("users");
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
Files.createDirectories(this.userDirectory);
|
||||
|
||||
this.serializer = GsonComponentSerializer.gson().populator()
|
||||
.apply(new GsonBuilder())
|
||||
.registerTypeAdapter(CarbonPlayerCommon.class, this.injector.getInstance(CarbonPlayerSerializerGson.class))
|
||||
.registerTypeAdapter(ChatChannel.class, this.injector.getInstance(ChatChannelSerializerGson.class))
|
||||
.registerTypeAdapter(UUID.class, this.injector.getInstance(UUIDSerializerGson.class))
|
||||
.registerTypeAdapter(CarbonPlayerCommon.class, injector.getInstance(CarbonPlayerSerializerGson.class))
|
||||
.registerTypeAdapter(ChatChannel.class, injector.getInstance(ChatChannelSerializerGson.class))
|
||||
.registerTypeAdapter(UUID.class, injector.getInstance(UUIDSerializerGson.class))
|
||||
.create();
|
||||
}
|
||||
|
||||
@@ -86,7 +87,15 @@ public class JSONUserManager implements UserManager {
|
||||
}
|
||||
}
|
||||
|
||||
return new ComponentPlayerResult(null, text("No save file found for player."));
|
||||
final String name = Objects.requireNonNull(
|
||||
this.carbonChat.server().resolveName(uuid).join());
|
||||
|
||||
final CarbonPlayerCommon player = new CarbonPlayerCommon(null,
|
||||
null, name, uuid);
|
||||
|
||||
this.userCache.put(uuid, player);
|
||||
|
||||
return new ComponentPlayerResult(player, empty());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user