Skip to content

Commit ebe0a18

Browse files
committed
Move GameProfile validation into a kotlin utils file for cleaner code
1 parent 1f2faed commit ebe0a18

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed
Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
package me.cael.capes.mixins;
22

3-
import com.google.gson.JsonElement;
4-
import com.google.gson.JsonParser;
5-
import com.google.gson.JsonSyntaxException;
63
import com.mojang.authlib.GameProfile;
7-
import com.mojang.authlib.properties.Property;
84
import me.cael.capes.CapeConfig;
95
import me.cael.capes.Capes;
106
import me.cael.capes.handler.PlayerHandler;
7+
import me.cael.capes.util.UtilsKt;
118
import net.minecraft.client.network.PlayerListEntry;
129
import net.minecraft.client.util.SkinTextures;
1310
import net.minecraft.util.Identifier;
1411
import org.spongepowered.asm.mixin.Final;
1512
import org.spongepowered.asm.mixin.Mixin;
1613
import org.spongepowered.asm.mixin.Shadow;
17-
import org.spongepowered.asm.mixin.Unique;
1814
import org.spongepowered.asm.mixin.injection.At;
1915
import org.spongepowered.asm.mixin.injection.Inject;
2016
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2117

22-
import java.nio.charset.StandardCharsets;
23-
import java.util.Base64;
24-
import java.util.Optional;
2518
import java.util.function.Supplier;
2619

2720
@Mixin(PlayerListEntry.class)
@@ -30,14 +23,14 @@ public abstract class MixinPlayerListEntry {
3023

3124
@Inject(method = "texturesSupplier", at = @At("HEAD"))
3225
private static void loadTextures(GameProfile profile, CallbackInfoReturnable<Supplier<SkinTextures>> cir) {
33-
if (capes$isValidProfile(profile)) {
26+
if (UtilsKt.isValidProfile(profile)) {
3427
PlayerHandler.Companion.onLoadTexture(profile);
3528
}
3629
}
3730

3831
@Inject(method = "getSkinTextures", at = @At("TAIL"), cancellable = true)
3932
private void getCapeTexture(CallbackInfoReturnable<SkinTextures> cir) {
40-
if (!capes$isValidProfile(profile)) return;
33+
if (!UtilsKt.isValidProfile(profile)) return;
4134
PlayerHandler handler = PlayerHandler.Companion.fromProfile(profile);
4235
if (handler.getHasCape()) {
4336
CapeConfig config = Capes.INSTANCE.getCONFIG();
@@ -52,25 +45,4 @@ private void getCapeTexture(CallbackInfoReturnable<SkinTextures> cir) {
5245
}
5346
}
5447

55-
@Unique
56-
private static boolean capes$isValidProfile(GameProfile profile) {
57-
Optional<Property> property = profile.getProperties().get("textures").stream().findFirst();
58-
if (property.isEmpty()) return false;
59-
60-
String textures = property.get().value();
61-
String json = new String(Base64.getDecoder().decode(textures), StandardCharsets.UTF_8);
62-
JsonElement root;
63-
try {
64-
root = JsonParser.parseString(json);
65-
} catch (JsonSyntaxException e) {
66-
return false;
67-
}
68-
if (!root.isJsonObject()) return false;
69-
JsonElement element = root.getAsJsonObject().get("profileName");
70-
if (element == null || element.isJsonNull()) return false;
71-
72-
String profileName = element.getAsString();
73-
return profile.getId().version() == 4 || (profile.getId().version() == 2 && profile.getName().equals(profileName));
74-
}
75-
7648
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package me.cael.capes.util
2+
3+
import com.google.gson.JsonParser
4+
import com.mojang.authlib.GameProfile
5+
import java.nio.charset.StandardCharsets
6+
import java.util.*
7+
8+
fun GameProfile.isValidProfile(): Boolean {
9+
val profileName = runCatching {
10+
val textures = this.properties.get("textures").stream().findFirst().get().value
11+
val json = String(Base64.getDecoder().decode(textures), StandardCharsets.UTF_8)
12+
JsonParser.parseString(json).asJsonObject.get("profileName").asString
13+
}.getOrNull() ?: return false
14+
15+
return this.id.version() == 4 || (this.id.version() == 2 && this.name == profileName)
16+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ neoforge_yarn_mappings=1.21+build.4
77
enabled_platforms=fabric
88

99
archives_base_name=capes
10-
mod_version=1.5.8+1.21.8
10+
mod_version=1.5.9+1.21.8
1111
maven_group=me.capes
1212

1313
fabric_loader_version=0.16.14

0 commit comments

Comments
 (0)