|
| 1 | +package io.github.underscore11code.ccord.client; |
| 2 | + |
| 3 | +import io.github.underscore11code.ccord.client.config.CarbonCordClientConfig; |
| 4 | +import io.github.underscore11code.ccord.client.listeners.AdventureListener; |
| 5 | +import io.github.underscore11code.ccord.common.Config; |
| 6 | +import io.github.underscore11code.ccord.common.VersionChecker; |
| 7 | +import io.github.underscore11code.ccord.messaging.MessageService; |
| 8 | +import io.github.underscore11code.ccord.messaging.RedisMessagingService; |
| 9 | +import io.github.underscore11code.ccord.messaging.packets.ClientConnectPacket; |
| 10 | +import io.github.underscore11code.ccord.messaging.packets.PingPacket; |
| 11 | +import io.github.underscore11code.ccord.messaging.packets.PingResponsePacket; |
| 12 | +import io.github.underscore11code.ccord.messaging.packets.RawChatPacket; |
| 13 | +import net.kyori.adventure.identity.Identified; |
| 14 | +import net.kyori.adventure.identity.Identity; |
| 15 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +public class CarbonClient { |
| 23 | + private static @Nullable CarbonClient instance = null; |
| 24 | + private static final Logger logger = LoggerFactory.getLogger(CarbonClient.class); |
| 25 | + |
| 26 | + private final File configDir; |
| 27 | + private final Config<CarbonCordClientConfig> config; |
| 28 | + private final MessageService messageService; |
| 29 | + private final ClientPlatformAdapter adapter; |
| 30 | + |
| 31 | + @SuppressWarnings({"assignment.type.incompatible", "argument.type.incompatible"}) |
| 32 | + public CarbonClient(final File configDir, final ClientPlatformAdapter adapter) throws Exception { |
| 33 | + if (instance != null) { |
| 34 | + throw new IllegalStateException("Attempted to load CarbonClient when it's already running!"); |
| 35 | + } |
| 36 | + instance = this; |
| 37 | + |
| 38 | + if (!VersionChecker.checkJvmVersion()) { |
| 39 | + throw new Exception(); |
| 40 | + } |
| 41 | + |
| 42 | + this.adapter = adapter; |
| 43 | + |
| 44 | + this.configDir = configDir; |
| 45 | + this.config = new Config<>(CarbonCordClientConfig.class, new File(configDir, "client.conf")); |
| 46 | + this.config.load(); |
| 47 | + this.config.save(); |
| 48 | + |
| 49 | + // TODO Remove when a second messaging service is added |
| 50 | + //noinspection SwitchStatementWithTooFewBranches |
| 51 | + switch (this.config.get().messaging().serviceType()) { |
| 52 | + case "redis": |
| 53 | + this.messageService = new RedisMessagingService(this.config.get().messaging().redis().toRedisUri()); |
| 54 | + break; |
| 55 | + |
| 56 | + default: |
| 57 | + logger.error("Invalid messaging service type \"{}\"!", this.config.get().messaging().serviceType()); |
| 58 | + throw new Exception(); |
| 59 | + } |
| 60 | + |
| 61 | + this.messageService.connect(); |
| 62 | + |
| 63 | + this.messageService.bus().register(PingPacket.class, p -> { |
| 64 | + if (!p.isFromThis()) this.messageService.send(new PingResponsePacket(p)); |
| 65 | + }); |
| 66 | + new AdventureListener(this); |
| 67 | + this.messageService.send(new ClientConnectPacket(this.config.get().serverName())); |
| 68 | + } |
| 69 | + |
| 70 | + public void shutdown() { |
| 71 | + this.messageService.disconnect(); |
| 72 | + instance = null; |
| 73 | + } |
| 74 | + |
| 75 | + public void handleMessage(final Identified player, final String channel, final String message) { |
| 76 | + this.messageService.send(new RawChatPacket(player.identity().uuid(), channel, message)); |
| 77 | + } |
| 78 | + |
| 79 | + public void handleMessage(final Identity identity, final String channel, final String message) { |
| 80 | + this.messageService.send(new RawChatPacket(identity.uuid(), channel, message)); |
| 81 | + } |
| 82 | + |
| 83 | + public void handleMessage(final UUID uuid, final String channel, final String message) { |
| 84 | + this.messageService.send(new RawChatPacket(uuid, channel, message)); |
| 85 | + } |
| 86 | + |
| 87 | + public File configDir() { |
| 88 | + return this.configDir; |
| 89 | + } |
| 90 | + |
| 91 | + public Config<CarbonCordClientConfig> config() { |
| 92 | + return this.config; |
| 93 | + } |
| 94 | + |
| 95 | + public MessageService messageService() { |
| 96 | + return this.messageService; |
| 97 | + } |
| 98 | + |
| 99 | + public ClientPlatformAdapter adapter() { |
| 100 | + return this.adapter; |
| 101 | + } |
| 102 | + |
| 103 | + public static @Nullable CarbonClient instance() { |
| 104 | + return instance; |
| 105 | + } |
| 106 | +} |
0 commit comments