Skip to content

Commit

Permalink
config rework
Browse files Browse the repository at this point in the history
  • Loading branch information
MeiNanziiii committed Oct 9, 2024
1 parent a5013a0 commit d3a0c1d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 62 deletions.
15 changes: 3 additions & 12 deletions src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,19 +19,11 @@
public class PlayerManagerMixin {
@Inject(method = "checkCanJoin", at = @At("RETURN"), cancellable = true)
private void minekord$checkRoles(SocketAddress socketAddress, GameProfile gameProfile, CallbackInfoReturnable<Text> 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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/ua/mei/minekord/Minekord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -28,7 +28,7 @@ object Minekord : ModInitializer {

config.validateRequired()

if (config[AuthSpec.loginByIp]) IPCache.load()
if (config[ExperimentalSpec.DiscordSpec.loginByIp]) IPCache.load()

MinekordBot.launchBot()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,7 +32,7 @@ object DiscordUtils {
return runBlocking {
MinekordBot.guild?.getMembers(nickname)?.filter { member ->
val trueRoleIds: List<ULong> = member.roleIds.map { it.value }
config[AuthSpec.requiredRoles].all { it in trueRoleIds }
config[ExperimentalSpec.DiscordSpec.requiredRoles].all { it in trueRoleIds }
}?.firstOrNull()
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ object MinekordBot : CoroutineScope {
}
}

bot.start()
try {
bot.start()
} catch (_: Throwable) {
println("LOX")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 0 additions & 10 deletions src/main/kotlin/ua/mei/minekord/config/AuthSpec.kt

This file was deleted.

12 changes: 12 additions & 0 deletions src/main/kotlin/ua/mei/minekord/config/ExperimentalSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ua.mei.minekord.config

import com.uchuhimo.konf.ConfigSpec

object ExperimentalSpec : ConfigSpec() {
object DiscordSpec : ConfigSpec() {
val enabled by required<Boolean>()
val allowOfflinePlayers by required<Boolean>()
val requiredRoles by required<List<ULong>>()
val loginByIp by required<Boolean>()
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ua/mei/minekord/config/MinekordConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
34 changes: 8 additions & 26 deletions src/main/resources/minekord.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d3a0c1d

Please sign in to comment.