Skip to content

Commit 9fa25ec

Browse files
committed
FakePlayer cleanup and better logic
1 parent b773dc8 commit 9fa25ec

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

src/main/kotlin/com/lambda/config/serializer/GameProfileCodec.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ object GameProfileCodec : Codec<GameProfile>, Stringifiable<GameProfile> {
5050
typeOfT: Type?,
5151
context: JsonDeserializationContext?,
5252
): GameProfile {
53-
val name = json.asJsonObject.get("name")?.asString ?: "nil"
54-
val id = json.asJsonObject.get("id")?.asString ?: "00000000-0000-0000-0000-000000000000"
53+
val name = json.asJsonObject.get("name").asString
54+
val id = json.asJsonObject.get("id").asString
5555
val parsedId =
5656
if (id.length == 32) id.replaceFirst(
5757
"(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})".toRegex(),

src/main/kotlin/com/lambda/module/modules/combat/FakePlayer.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@ import com.lambda.network.mojang.getProfile
2929
import com.lambda.threading.onShutdown
3030
import com.lambda.util.Timer
3131
import com.lambda.util.player.FakePlayerId
32-
import com.lambda.util.player.spawnFakePlayer
3332
import com.mojang.authlib.GameProfile
3433
import com.mojang.datafixers.util.Either
3534
import net.minecraft.client.network.OtherClientPlayerEntity
3635
import net.minecraft.client.network.PlayerListEntry
3736
import java.util.*
38-
import kotlin.jvm.optionals.getOrElse
3937
import kotlin.jvm.optionals.getOrNull
4038
import kotlin.time.Duration.Companion.seconds
4139

@@ -59,16 +57,16 @@ object FakePlayer : Module(
5957

6058
init {
6159
listen<TickEvent.Pre> {
62-
fakePlayer = cachedProfiles[playerName]
63-
?.let { spawnFakePlayer(it, fakePlayer ?: player, addToWorld = false) }
64-
?.takeUnless { it == fakePlayer?.gameProfile }
65-
?: fakePlayer?.takeIf { playerName == it.gameProfile.name }
66-
?: spawnFakePlayer(nilProfile, fakePlayer ?: player, addToWorld = false)
60+
val newFakePlayer = cachedProfiles[playerName]
61+
?.let { newFakePlayer(it) }
62+
?.takeUnless { it.gameProfile == fakePlayer?.gameProfile } // If the current profile equals the current fake player's profile, stop.
63+
64+
fakePlayer = newFakePlayer
65+
?: return@listen
6766
}
6867

6968
listenConcurrently<TickEvent.Pre>(priority = 1000) {
7069
if (!fetchTimer.timePassed(2.seconds)) return@listenConcurrently
71-
7270
cachedProfiles.getOrPut(playerName) { fetchProfile(playerName) }
7371
}
7472

@@ -77,24 +75,33 @@ object FakePlayer : Module(
7775
}
7876

7977
listen<ConnectionEvent.Connect.Pre> { disable() }
80-
onShutdown { disable() }
78+
79+
onShutdown { disable() } // FixMe: This doesn't work because the hook triggers after the modules are saved.
80+
8181
onDisable { fakePlayer?.discard(); fakePlayer = null }
8282
}
8383

84+
fun SafeContext.newFakePlayer(profile: GameProfile) =
85+
OtherClientPlayerEntity(world, profile).apply {
86+
copyFrom(player)
87+
id = FakePlayerId
88+
}
89+
8490
suspend fun SafeContext.fetchProfile(user: String): GameProfile {
85-
val requestedProfile = getProfile(user).getOrElse { return nilProfile }
91+
val requestedProfile = getProfile(user)
92+
.getOrElse { return nilProfile }
8693

8794
// Fetch the skin properties from mojang
8895
val properties = mc.apiServices.profileResolver
89-
.getProfile(Either.right(requestedProfile.id)).getOrNull()?.properties
96+
.getProfile(Either.right(requestedProfile.id))
97+
.getOrNull()
98+
?.properties
99+
?: return nilProfile
90100

91-
// We use the nil profile to avoid the nil username if something wrong happens
92-
// Check the GameProfile deserializer you'll understand
93-
val profile = nilProfile
94-
properties?.let { profile.properties.putAll(it) }
101+
val profile = GameProfile(requestedProfile.id, requestedProfile.name, properties)
95102

96-
mc.networkHandler?.playerListEntries?.put(profile.id, PlayerListEntry(profile, false))
103+
connection.playerListEntries[profile.id] = PlayerListEntry(profile, false)
97104

98-
return profile
105+
return profile
99106
}
100107
}

src/main/kotlin/com/lambda/util/player/PlayerUtils.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ package com.lambda.util.player
2020
import com.lambda.config.groups.BuildConfig
2121
import com.lambda.context.SafeContext
2222
import com.lambda.util.world.fastEntitySearch
23-
import com.mojang.authlib.GameProfile
2423
import net.minecraft.client.network.ClientPlayerEntity
25-
import net.minecraft.client.network.OtherClientPlayerEntity
26-
import net.minecraft.client.network.PlayerListEntry
27-
import net.minecraft.entity.player.PlayerEntity
2824
import net.minecraft.entity.projectile.FireworkRocketEntity
2925
import net.minecraft.item.ItemStack
3026
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket
@@ -58,23 +54,6 @@ fun SafeContext.copyPlayer(entity: ClientPlayerEntity) =
5854
isOnGround = entity.isOnGround
5955
}
6056

61-
fun SafeContext.spawnFakePlayer(
62-
profile: GameProfile,
63-
reference: PlayerEntity = player,
64-
addToWorld: Boolean = true
65-
): OtherClientPlayerEntity {
66-
val entity = OtherClientPlayerEntity(world, profile).apply {
67-
copyFrom(reference)
68-
69-
playerListEntry = PlayerListEntry(profile, false)
70-
id = FakePlayerId
71-
}
72-
73-
if (addToWorld) world.addEntity(entity)
74-
75-
return entity
76-
}
77-
7857
fun SafeContext.swingHand(swingType: BuildConfig.SwingType, hand: Hand) =
7958
when (swingType) {
8059
BuildConfig.SwingType.Vanilla -> {

0 commit comments

Comments
 (0)