diff --git a/src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java b/src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java index bfe6e2c..7b6be54 100644 --- a/src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java +++ b/src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java @@ -7,9 +7,8 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import ua.mei.minekord.bot.DiscordUtils; import ua.mei.minekord.cache.IPCache; -import ua.mei.minekord.config.AuthSpec; +import ua.mei.minekord.config.ExperimentalSpec; import ua.mei.minekord.config.MinekordConfigKt; import ua.mei.minekord.event.IPCheckEvent; @@ -20,19 +19,11 @@ public class PlayerManagerMixin { @Inject(method = "checkCanJoin", at = @At("RETURN"), cancellable = true) private void minekord$checkRoles(SocketAddress socketAddress, GameProfile gameProfile, CallbackInfoReturnable cir) { - if (cir.getReturnValue() == null) { - boolean uuidFromSnowflake = MinekordConfigKt.getConfig().get(AuthSpec.INSTANCE.getUuidFromSnowflake()); - boolean requiredRoles = !MinekordConfigKt.getConfig().get(AuthSpec.INSTANCE.getRequiredRoles()).isEmpty(); - boolean loginByIp = MinekordConfigKt.getConfig().get(AuthSpec.INSTANCE.getLoginByIp()); + if (cir.getReturnValue() == null && MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getEnabled())) { + boolean loginByIp = MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getLoginByIp()); String playerName = gameProfile.getName(); String cachedIp = IPCache.INSTANCE.getFromCache(playerName); - if (!uuidFromSnowflake && requiredRoles) { - if (DiscordUtils.INSTANCE.getPlayer(playerName) == null) { - cir.setReturnValue(Text.translatable("multiplayer.disconnect.generic")); - } - } - if (loginByIp && socketAddress instanceof InetSocketAddress inet && !cachedIp.equals(inet.getHostName())) { IPCheckEvent.Companion.getEvent().invoker().request(socketAddress, gameProfile); cir.setReturnValue(Text.translatable("multiplayer.disconnect.generic")); diff --git a/src/main/java/ua/mei/minekord/mixin/ServerLoginNetworkHandlerMixin.java b/src/main/java/ua/mei/minekord/mixin/ServerLoginNetworkHandlerMixin.java index 1a6ac95..7bbf97c 100644 --- a/src/main/java/ua/mei/minekord/mixin/ServerLoginNetworkHandlerMixin.java +++ b/src/main/java/ua/mei/minekord/mixin/ServerLoginNetworkHandlerMixin.java @@ -12,8 +12,8 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import ua.mei.minekord.bot.DiscordUtils; -import ua.mei.minekord.config.AuthSpec; +import ua.mei.minekord.bot.ExperimentalUtils; +import ua.mei.minekord.config.ExperimentalSpec; import ua.mei.minekord.config.MinekordConfigKt; import java.util.UUID; @@ -36,13 +36,13 @@ public abstract class ServerLoginNetworkHandlerMixin { @Inject(method = "onHello", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/MinecraftServer;isOnlineMode()Z"), cancellable = true) private void minekord$trueUuids(LoginHelloC2SPacket loginHelloC2SPacket, CallbackInfo ci) { - if (MinekordConfigKt.getConfig().get(AuthSpec.INSTANCE.getUuidFromSnowflake())) { - if (this.server.isOnlineMode() && !MinekordConfigKt.getConfig().get(AuthSpec.INSTANCE.getAllowOfflinePlayers()) && !DiscordUtils.INSTANCE.premiumPlayer(loginHelloC2SPacket.comp_907())) { + if (MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getEnabled())) { + if (this.server.isOnlineMode() && !MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getAllowOfflinePlayers()) && !ExperimentalUtils.INSTANCE.premiumPlayer(loginHelloC2SPacket.comp_907())) { this.disconnect(Text.translatable("multiplayer.disconnect.generic")); ci.cancel(); } - UUID trueUuid = DiscordUtils.INSTANCE.generateFromNickname(loginHelloC2SPacket.comp_765()); + UUID trueUuid = ExperimentalUtils.INSTANCE.generateFromNickname(loginHelloC2SPacket.comp_765()); if (trueUuid != null) { this.profile = new GameProfile(trueUuid, loginHelloC2SPacket.comp_765()); diff --git a/src/main/kotlin/ua/mei/minekord/Minekord.kt b/src/main/kotlin/ua/mei/minekord/Minekord.kt index c8811cf..c24aa8c 100644 --- a/src/main/kotlin/ua/mei/minekord/Minekord.kt +++ b/src/main/kotlin/ua/mei/minekord/Minekord.kt @@ -6,8 +6,8 @@ import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger import ua.mei.minekord.bot.MinekordBot import ua.mei.minekord.cache.IPCache -import ua.mei.minekord.config.AuthSpec import ua.mei.minekord.config.CONFIG_PATH +import ua.mei.minekord.config.ExperimentalSpec import ua.mei.minekord.config.config import java.nio.file.Files @@ -28,7 +28,7 @@ object Minekord : ModInitializer { config.validateRequired() - if (config[AuthSpec.loginByIp]) IPCache.load() + if (config[ExperimentalSpec.DiscordSpec.loginByIp]) IPCache.load() MinekordBot.launchBot() } diff --git a/src/main/kotlin/ua/mei/minekord/bot/DiscordUtils.kt b/src/main/kotlin/ua/mei/minekord/bot/ExperimentalUtils.kt similarity index 93% rename from src/main/kotlin/ua/mei/minekord/bot/DiscordUtils.kt rename to src/main/kotlin/ua/mei/minekord/bot/ExperimentalUtils.kt index 75fcf70..3d48217 100644 --- a/src/main/kotlin/ua/mei/minekord/bot/DiscordUtils.kt +++ b/src/main/kotlin/ua/mei/minekord/bot/ExperimentalUtils.kt @@ -8,12 +8,12 @@ import io.ktor.http.HttpStatusCode import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.runBlocking -import ua.mei.minekord.config.AuthSpec +import ua.mei.minekord.config.ExperimentalSpec import ua.mei.minekord.config.config import java.util.UUID import kotlin.experimental.inv -object DiscordUtils { +object ExperimentalUtils { private val client: HttpClient = HttpClient() fun premiumPlayer(uuid: UUID): Boolean { @@ -32,7 +32,7 @@ object DiscordUtils { return runBlocking { MinekordBot.guild?.getMembers(nickname)?.filter { member -> val trueRoleIds: List = member.roleIds.map { it.value } - config[AuthSpec.requiredRoles].all { it in trueRoleIds } + config[ExperimentalSpec.DiscordSpec.requiredRoles].all { it in trueRoleIds } }?.firstOrNull() } } diff --git a/src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt b/src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt index 3d39b9d..9dd88f3 100644 --- a/src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt +++ b/src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt @@ -27,7 +27,11 @@ object MinekordBot : CoroutineScope { } } - bot.start() + try { + bot.start() + } catch (_: Throwable) { + println("LOX") + } } } diff --git a/src/main/kotlin/ua/mei/minekord/bot/extensions/IPCheckExtension.kt b/src/main/kotlin/ua/mei/minekord/bot/extensions/IPCheckExtension.kt index 897349f..1f0ac3a 100644 --- a/src/main/kotlin/ua/mei/minekord/bot/extensions/IPCheckExtension.kt +++ b/src/main/kotlin/ua/mei/minekord/bot/extensions/IPCheckExtension.kt @@ -14,9 +14,11 @@ import dev.kordex.core.time.toDiscord import io.ktor.util.network.address import kotlinx.coroutines.launch import kotlinx.datetime.Clock -import ua.mei.minekord.bot.DiscordUtils +import ua.mei.minekord.bot.ExperimentalUtils import ua.mei.minekord.bot.MinekordBot import ua.mei.minekord.cache.IPCache +import ua.mei.minekord.config.ExperimentalSpec +import ua.mei.minekord.config.config import ua.mei.minekord.event.IPCheckEvent class IPCheckExtension : Extension() { @@ -26,7 +28,9 @@ class IPCheckExtension : Extension() { IPCheckEvent.event.register { address, profile -> MinekordBot.launch { try { - val discordId: ULong = DiscordUtils.uuidToDiscord(profile.id) + if (!config[ExperimentalSpec.DiscordSpec.enabled]) return@launch + + val discordId: ULong = ExperimentalUtils.uuidToDiscord(profile.id) val user: User? = kord.getUser(Snowflake(discordId)) user?.getDmChannelOrNull()?.createMessage { @@ -36,10 +40,12 @@ class IPCheckExtension : Extension() { field { name = "> IP" value = "> ${address.address}" + inline = true } field { name = "> Time" value = "> ${Clock.System.now().toDiscord(TimestampType.Default)}" + inline = true } } components { diff --git a/src/main/kotlin/ua/mei/minekord/config/AuthSpec.kt b/src/main/kotlin/ua/mei/minekord/config/AuthSpec.kt deleted file mode 100644 index 74a644b..0000000 --- a/src/main/kotlin/ua/mei/minekord/config/AuthSpec.kt +++ /dev/null @@ -1,10 +0,0 @@ -package ua.mei.minekord.config - -import com.uchuhimo.konf.ConfigSpec - -object AuthSpec : ConfigSpec() { - val uuidFromSnowflake by required() - val allowOfflinePlayers by required() - val requiredRoles by required>() - val loginByIp by required() -} diff --git a/src/main/kotlin/ua/mei/minekord/config/ExperimentalSpec.kt b/src/main/kotlin/ua/mei/minekord/config/ExperimentalSpec.kt new file mode 100644 index 0000000..9810098 --- /dev/null +++ b/src/main/kotlin/ua/mei/minekord/config/ExperimentalSpec.kt @@ -0,0 +1,12 @@ +package ua.mei.minekord.config + +import com.uchuhimo.konf.ConfigSpec + +object ExperimentalSpec : ConfigSpec() { + object DiscordSpec : ConfigSpec() { + val enabled by required() + val allowOfflinePlayers by required() + val requiredRoles by required>() + val loginByIp by required() + } +} diff --git a/src/main/kotlin/ua/mei/minekord/config/MinekordConfig.kt b/src/main/kotlin/ua/mei/minekord/config/MinekordConfig.kt index db76111..3bff1da 100644 --- a/src/main/kotlin/ua/mei/minekord/config/MinekordConfig.kt +++ b/src/main/kotlin/ua/mei/minekord/config/MinekordConfig.kt @@ -6,6 +6,6 @@ import net.fabricmc.loader.api.FabricLoader const val CONFIG_PATH: String = "minekord.toml" -val config: Config = Config { addSpec(BotSpec); addSpec(AuthSpec) } +val config: Config = Config { addSpec(BotSpec); addSpec(ExperimentalSpec) } .from.toml.resource(CONFIG_PATH) .from.toml.watchFile(FabricLoader.getInstance().configDir.resolve(CONFIG_PATH).toFile()) diff --git a/src/main/resources/minekord.toml b/src/main/resources/minekord.toml index 96a774e..f928d63 100644 --- a/src/main/resources/minekord.toml +++ b/src/main/resources/minekord.toml @@ -4,29 +4,11 @@ token = "" guild = 0 chat = 0 -[Auth] - -# If true, player UUID will be generated by they Discord ID. -# If user doens't exists in guild with player nickname, player will be kicked. -# Works with offline and online mode. -uuidFromSnowflake = false - -# If true, offline players can join server if online mode enabled. -# If online mode disabled, ignore. -# Made for servers which uses EasyAuth with premium-auto-login. -allowOfflinePlayers = false - -# Roles which required for server join. -# Can be empty. -requiredRoles = [] - -# When player trying to join server, bot will send embed to player DM, -# embed contains IP from which player are joining, player can confirm -# is this ip are they, and then successful join server. -# -# Every new IP, bot will request confirmation. -# Player can join only with last confirmed IP. -# -# Option made for players, who are lazy to type /login and /register, -# or for best safety. -loginByIp = false +# Experimental and unstable features. +# USE AT YOUR OWN RISK!!! +[Experimental] + [Experimental.Discord] + enabled = false + allowOfflinePlayers = false + requiredRoles = [] + loginByIp = false