Skip to content

Commit 94f44a2

Browse files
committed
some improvements
1 parent d3a0c1d commit 94f44a2

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

src/main/java/ua/mei/minekord/mixin/PlayerManagerMixin.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212
import ua.mei.minekord.config.MinekordConfigKt;
1313
import ua.mei.minekord.event.IPCheckEvent;
1414

15-
import java.net.InetSocketAddress;
1615
import java.net.SocketAddress;
1716

1817
@Mixin(PlayerManager.class)
1918
public class PlayerManagerMixin {
2019
@Inject(method = "checkCanJoin", at = @At("RETURN"), cancellable = true)
2120
private void minekord$checkRoles(SocketAddress socketAddress, GameProfile gameProfile, CallbackInfoReturnable<Text> cir) {
22-
if (cir.getReturnValue() == null && MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getEnabled())) {
23-
boolean loginByIp = MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getLoginByIp());
24-
String playerName = gameProfile.getName();
25-
String cachedIp = IPCache.INSTANCE.getFromCache(playerName);
26-
27-
if (loginByIp && socketAddress instanceof InetSocketAddress inet && !cachedIp.equals(inet.getHostName())) {
21+
if (cir.getReturnValue() == null && MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getEnabled()) && MinekordConfigKt.getConfig().get(ExperimentalSpec.DiscordSpec.INSTANCE.getLoginByIp())) {
22+
if (!IPCache.INSTANCE.haveInCache(gameProfile.getName(), socketAddress)) {
2823
IPCheckEvent.Companion.getEvent().invoker().request(socketAddress, gameProfile);
2924
cir.setReturnValue(Text.translatable("multiplayer.disconnect.generic"));
3025
}

src/main/kotlin/ua/mei/minekord/bot/MinekordBot.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.coroutines.launch
99
import ua.mei.minekord.bot.extensions.IPCheckExtension
1010
import ua.mei.minekord.bot.extensions.SetupExtension
1111
import ua.mei.minekord.config.BotSpec
12+
import ua.mei.minekord.config.ExperimentalSpec
1213
import ua.mei.minekord.config.config
1314
import kotlin.coroutines.CoroutineContext
1415

@@ -23,15 +24,13 @@ object MinekordBot : CoroutineScope {
2324
bot = ExtensibleBot(config[BotSpec.token]) {
2425
extensions {
2526
add(::SetupExtension)
26-
add(::IPCheckExtension)
27+
28+
if (config[ExperimentalSpec.DiscordSpec.enabled])
29+
add(::IPCheckExtension)
2730
}
2831
}
2932

30-
try {
31-
bot.start()
32-
} catch (_: Throwable) {
33-
println("LOX")
34-
}
33+
bot.start()
3534
}
3635
}
3736

src/main/kotlin/ua/mei/minekord/bot/extensions/IPCheckExtension.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class IPCheckExtension : Extension() {
2525
override val name: String = "IP Check Extension"
2626

2727
override suspend fun setup() {
28-
IPCheckEvent.event.register { address, profile ->
28+
IPCheckEvent.event.register { socketAddress, profile ->
2929
MinekordBot.launch {
3030
try {
3131
if (!config[ExperimentalSpec.DiscordSpec.enabled]) return@launch
@@ -39,7 +39,7 @@ class IPCheckExtension : Extension() {
3939

4040
field {
4141
name = "> IP"
42-
value = "> ${address.address}"
42+
value = "> ${socketAddress.address}"
4343
inline = true
4444
}
4545
field {
@@ -54,7 +54,7 @@ class IPCheckExtension : Extension() {
5454
style = ButtonStyle.Success
5555

5656
action {
57-
IPCache.putIntoCache(profile.name, address.address)
57+
IPCache.putIntoCache(profile.name, socketAddress.address)
5858

5959
edit {
6060
components {

src/main/kotlin/ua/mei/minekord/cache/IPCache.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package ua.mei.minekord.cache
33
import com.google.gson.Gson
44
import com.google.gson.GsonBuilder
55
import com.google.gson.stream.JsonReader
6+
import io.ktor.util.network.address
67
import net.fabricmc.loader.api.FabricLoader
78
import java.io.FileReader
9+
import java.net.SocketAddress
810
import java.nio.file.Files
911
import java.nio.file.Path
1012

@@ -37,7 +39,7 @@ object IPCache {
3739
save()
3840
}
3941

40-
fun getFromCache(nickname: String): String {
41-
return cache[nickname] ?: ""
42+
fun haveInCache(nickname: String, socketAddress: SocketAddress): Boolean {
43+
return cache[nickname] == socketAddress.address
4244
}
4345
}

src/main/kotlin/ua/mei/minekord/event/IPCheckEvent.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import net.fabricmc.fabric.api.event.EventFactory
66
import java.net.SocketAddress
77

88
fun interface IPCheckEvent {
9-
fun request(address: SocketAddress, profile: GameProfile)
9+
fun request(socketAddress: SocketAddress, profile: GameProfile)
1010

1111
companion object {
1212
val event: Event<IPCheckEvent> = EventFactory.createArrayBacked(IPCheckEvent::class.java) { listeners ->
13-
IPCheckEvent { address, profile ->
13+
IPCheckEvent { socketAddress, profile ->
1414
listeners.forEach { listener ->
15-
listener.request(address, profile)
15+
listener.request(socketAddress, profile)
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)