Skip to content

Commit 70f9481

Browse files
committed
Updated AltManager
1 parent bb24020 commit 70f9481

7 files changed

Lines changed: 1259 additions & 74 deletions

File tree

src/main/java/net/wurstclient/altmanager/AltManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ public void edit(Alt oldAlt, String newNameOrEmail, String newPassword)
6464
oldAlt.isFavorite()));
6565
}
6666

67+
public void updateTokenAltName(TokenAlt tokenAlt, String newName)
68+
{
69+
if(tokenAlt == null)
70+
return;
71+
72+
for(Alt alt : alts)
73+
{
74+
if(alt != tokenAlt)
75+
continue;
76+
77+
tokenAlt.setName(newName);
78+
sortAlts();
79+
altsFile.save(this);
80+
return;
81+
}
82+
}
83+
6784
/**
6885
* Logs the user in with this Alt. Also updates the counter for checked alts
6986
* and saves the alt list file as necessary.

src/main/java/net/wurstclient/altmanager/AltRenderer.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ public final class AltRenderer
3636
private static final ConcurrentHashMap<String, Identifier> onlineSkins =
3737
new ConcurrentHashMap<>();
3838

39+
private static final ConcurrentHashMap<String, PlayerModelType> onlineModels =
40+
new ConcurrentHashMap<>();
41+
42+
private static final ConcurrentHashMap.KeySetView<String, Boolean> loadingSkins =
43+
ConcurrentHashMap.newKeySet();
44+
3945
private static final HashMap<String, Identifier> offlineSkins =
4046
new HashMap<>();
4147

@@ -46,15 +52,34 @@ private static Identifier getSkinTexture(String name)
4652

4753
Identifier offlineSkin = offlineSkins.get(name);
4854
if(offlineSkin == null)
49-
{
50-
queueOnlineSkinLoading(name);
5155
offlineSkin = loadOfflineSkin(name);
52-
}
5356

5457
Identifier onlineSkin = onlineSkins.get(name);
58+
if(onlineSkin == null)
59+
queueOnlineSkinLoading(name);
60+
5561
return onlineSkin != null ? onlineSkin : offlineSkin;
5662
}
5763

64+
public static void refreshSkin(String name)
65+
{
66+
if(name == null || name.isBlank())
67+
return;
68+
69+
onlineSkins.remove(name);
70+
onlineModels.remove(name);
71+
loadingSkins.remove(name);
72+
queueOnlineSkinLoading(name);
73+
}
74+
75+
public static boolean isSkinLoading(String name)
76+
{
77+
if(name == null || name.isBlank())
78+
return false;
79+
80+
return loadingSkins.contains(name);
81+
}
82+
5883
private static Identifier loadOfflineSkin(String name)
5984
{
6085
UUID uuid = UUIDUtil.createOfflinePlayerUUID(name);
@@ -67,6 +92,12 @@ private static Identifier loadOfflineSkin(String name)
6792

6893
private static void queueOnlineSkinLoading(String name)
6994
{
95+
if(name == null || name.isBlank())
96+
return;
97+
98+
if(!loadingSkins.add(name))
99+
return;
100+
70101
Minecraft mc = WurstClient.MC;
71102

72103
CompletableFuture.supplyAsync(() -> {
@@ -90,9 +121,17 @@ private static void queueOnlineSkinLoading(String name)
90121
}, BACKGROUND_THREAD).thenAcceptAsync(skinTextures -> {
91122

92123
if(skinTextures != null)
124+
{
93125
onlineSkins.put(name, skinTextures.body().texturePath());
126+
onlineModels.put(name, skinTextures.model());
127+
}
94128

95-
}, BACKGROUND_THREAD);
129+
loadingSkins.remove(name);
130+
131+
}, BACKGROUND_THREAD).exceptionally(error -> {
132+
loadingSkins.remove(name);
133+
return null;
134+
});
96135
}
97136

98137
public static void drawAltFace(GuiGraphics context, String name, int x,
@@ -132,9 +171,7 @@ public static void drawAltBody(GuiGraphics context, String name, int x,
132171
{
133172
Identifier texture = getSkinTexture(name);
134173

135-
boolean slim =
136-
DefaultPlayerSkin.get(UUIDUtil.createOfflinePlayerUUID(name))
137-
.model() == PlayerModelType.SLIM;
174+
boolean slim = getModelType(name) == PlayerModelType.SLIM;
138175

139176
// Face
140177
x = x + width / 4;
@@ -271,9 +308,7 @@ public static void drawAltBack(GuiGraphics context, String name, int x,
271308
{
272309
Identifier texture = getSkinTexture(name);
273310

274-
boolean slim =
275-
DefaultPlayerSkin.get(UUIDUtil.createOfflinePlayerUUID(name))
276-
.model() == PlayerModelType.SLIM;
311+
boolean slim = getModelType(name) == PlayerModelType.SLIM;
277312

278313
// Face
279314
x = x + width / 4;
@@ -402,4 +437,14 @@ public static void drawAltBack(GuiGraphics context, String name, int x,
402437
e.printStackTrace();
403438
}
404439
}
440+
441+
private static PlayerModelType getModelType(String name)
442+
{
443+
PlayerModelType onlineModel = onlineModels.get(name);
444+
if(onlineModel != null)
445+
return onlineModel;
446+
447+
return DefaultPlayerSkin.get(UUIDUtil.createOfflinePlayerUUID(name))
448+
.model();
449+
}
405450
}

src/main/java/net/wurstclient/altmanager/MicrosoftLoginManager.java

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,12 @@ public static void login(String email, String password)
105105

106106
public static void loginWithToken(String token) throws LoginException
107107
{
108-
if(token == null || token.isBlank())
109-
throw new LoginException("Token cannot be empty.");
110-
111-
String trimmedToken = token.trim();
112108
System.out.println("Logging in with token...");
113109
long startTime = System.nanoTime();
114110

115111
try
116112
{
117-
try
118-
{
119-
MinecraftProfile mcProfile = getMinecraftProfile(trimmedToken);
120-
setSession(mcProfile);
121-
System.out.println("Token login successful after "
122-
+ (System.nanoTime() - startTime) / 1e6D + " ms");
123-
return;
124-
125-
}catch(LoginException ignored)
126-
{
127-
// Token may be a Microsoft token instead of a Minecraft token.
128-
}
129-
130-
MinecraftProfile mcProfile =
131-
getAccountFromMicrosoftAccessToken(trimmedToken);
113+
MinecraftProfile mcProfile = authenticateTokenWithoutSession(token);
132114
setSession(mcProfile);
133115
System.out.println("Token login successful after "
134116
+ (System.nanoTime() - startTime) / 1e6D + " ms");
@@ -144,19 +126,13 @@ public static void loginWithToken(String token) throws LoginException
144126
public static void loginWithRefreshToken(String refreshToken)
145127
throws LoginException
146128
{
147-
if(refreshToken == null || refreshToken.isBlank())
148-
throw new LoginException("Refresh token cannot be empty.");
149-
150129
System.out.println("Logging in with refresh token...");
151130
long startTime = System.nanoTime();
152131

153132
try
154133
{
155-
String msftAccessToken =
156-
getMicrosoftAccessTokenFromRefreshToken(refreshToken.trim());
157-
158134
MinecraftProfile mcProfile =
159-
getAccountFromMicrosoftAccessToken(msftAccessToken);
135+
authenticateRefreshTokenWithoutSession(refreshToken);
160136
setSession(mcProfile);
161137

162138
System.out.println("Refresh-token login successful after "
@@ -170,6 +146,54 @@ public static void loginWithRefreshToken(String refreshToken)
170146
}
171147
}
172148

149+
public static MinecraftProfile authenticateTokenWithoutSession(String token)
150+
throws LoginException
151+
{
152+
if(token == null || token.isBlank())
153+
throw new LoginException("Token cannot be empty.");
154+
155+
String trimmedToken = token.trim();
156+
157+
try
158+
{
159+
return getMinecraftProfile(trimmedToken);
160+
161+
}catch(LoginException ignored)
162+
{
163+
// Token may be a Microsoft token instead of a Minecraft token.
164+
}
165+
166+
return getAccountFromMicrosoftAccessToken(trimmedToken);
167+
}
168+
169+
public static MinecraftProfile authenticateRefreshTokenWithoutSession(
170+
String refreshToken) throws LoginException
171+
{
172+
if(refreshToken == null || refreshToken.isBlank())
173+
throw new LoginException("Refresh token cannot be empty.");
174+
175+
String msftAccessToken =
176+
getMicrosoftAccessTokenFromRefreshToken(refreshToken.trim());
177+
return getAccountFromMicrosoftAccessToken(msftAccessToken);
178+
}
179+
180+
public static MinecraftProfile authenticateTokenAltWithoutSession(
181+
String token, String refreshToken) throws LoginException
182+
{
183+
String trimmedRefresh = refreshToken == null ? "" : refreshToken.trim();
184+
185+
if(!trimmedRefresh.isEmpty())
186+
return authenticateRefreshTokenWithoutSession(trimmedRefresh);
187+
188+
return authenticateTokenWithoutSession(token);
189+
}
190+
191+
public static MinecraftProfile getMinecraftProfileByAccessToken(
192+
String mcAccessToken) throws LoginException
193+
{
194+
return getMinecraftProfile(mcAccessToken);
195+
}
196+
173197
private static MinecraftProfile getAccount(String email, String password)
174198
throws LoginException
175199
{

0 commit comments

Comments
 (0)