Skip to content

Commit

Permalink
Fix resolving the default Cape and some other changes regarding textu…
Browse files Browse the repository at this point in the history
…re API
  • Loading branch information
CaptainRexPL committed Jan 17, 2024
1 parent 9a90465 commit 5c8a17f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.104.Final</version>
<version>4.1.105.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
4 changes: 0 additions & 4 deletions client/src/main/java/com/collarmc/client/Collar.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,7 @@ public static Collar create(CollarConfiguration configuration) {
*/
public void connect() {
try {
LOGGER.info("Before checkServerCompatibility");
checkServerCompatibility(configuration);
LOGGER.info("After checkServerCompatibility");
LOGGER.info("Before UrlBuilding" + configuration.collarServerURL);
String url = UrlBuilder.fromUrl(configuration.collarServerURL).withPath("/api/1/listen").toString();
LOGGER.info("Connecting to server " + url);
webSocket = Http.client().webSocket(Request.url(url).ws(), new CollarWebSocket(this));
Expand Down Expand Up @@ -276,7 +273,6 @@ public Player player() {
* @param configuration of the client
*/
private static void checkServerCompatibility(CollarConfiguration configuration) {
LOGGER.info("Before api discover: " + configuration.collarServerURL);
DiscoverResponse response;
try {
response = Http.client().execute(url(UrlBuilder.fromUrl(configuration.collarServerURL).withPath("/api/discover")).get(), Response.json(DiscoverResponse.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static DebugConfiguration load(HomeDirectory home) throws IOException {
String mode = properties.getProperty("session.mode", null);
MinecraftSession.Mode sessionMode = mode == null ? null : MinecraftSession.Mode.valueOf(mode.toUpperCase());
return new DebugConfiguration(
(boolean) properties.getOrDefault("tracers", false),
(boolean) properties.getOrDefault("waypoints", false),
Boolean.parseBoolean(properties.getProperty("tracers", "false")),
Boolean.parseBoolean(properties.getProperty("waypoints", "false")),
sessionMode,
url == null ? null : new URL(url)
);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.104.Final</version>
<version>4.1.105.Final</version>
<scope>compile</scope>
</dependency>
<!-- a database -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.collarmc.server.protocol;

import com.collarmc.api.groups.Group;
import com.collarmc.api.http.HttpException.NotFoundException;
import com.collarmc.api.http.RequestContext;
import com.collarmc.api.identity.ClientIdentity;
Expand All @@ -20,6 +21,7 @@
import org.eclipse.jetty.websocket.api.Session;

import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class TexturesProtocolHandler extends ProtocolHandler {

Expand All @@ -42,11 +44,11 @@ public boolean handleRequest(CollarServer collar, ClientIdentity identity, Proto
if (request.type == TextureType.CAPE) {
texture = findDefaultCape(request, sessionState);
}
// otherwise fall back to fetching any cape cape the player owns
// otherwise fall back to fetching any cape the player owns
if (texture == null) {
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(null, sessionState.identity.id(), request.group, request.type)).texture;
texture = findTexture(request, sessionState);
}
response = new GetTextureResponse(texture.id, null, sessionState.toPlayer(), texture.url, texture.type);
response = new GetTextureResponse(texture == null ? null : texture.id, null, sessionState.toPlayer(), texture == null ? null : texture.url, texture == null ? request.type : texture.type);
} catch (NotFoundException ignored) {
LOGGER.info("Could not find texture " + request.type + " for player " + request.player);
response = new GetTextureResponse(null, null, sessionState.toPlayer(), null, request.type);
Expand Down Expand Up @@ -81,17 +83,61 @@ private TextureService.Texture findDefaultCape(GetTextureRequest request, Sessio
PublicProfile playerProfile = services.profileCache.getById(sessionState.identity.id()).orElseThrow(() -> new IllegalStateException("could not find profile " + sessionState.identity.id())).toPublic();
if (playerProfile.cape != null) {
try {
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(playerProfile.cape.texture, null, null, null)).texture;
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(playerProfile.cape.texture, sessionState.identity.id(), null, null)).texture;
} catch (NotFoundException ignored) {
LOGGER.info("Could not find texture " + playerProfile.cape.texture + " for player " + request.player);
texture = null;
}
if(texture == null) {
try {
for (Group group : services.groupStore.findGroupsContaining(sessionState.toPlayer()).collect(Collectors.toSet())) {
try {
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(playerProfile.cape.texture, null, group.id, null)).texture;
if (texture != null) {
break; // Exit the loop once texture is found in the first group
}
} catch (NotFoundException ignored) {
LOGGER.info("Could not find texture " + playerProfile.cape.texture + " for player " + request.player + " in group " + group.id);
}
}
} catch (NotFoundException ignored) {
LOGGER.info("Could not find groups for player");
}
}
} else {
texture = null;
}
return texture;
}

private TextureService.Texture findTexture(GetTextureRequest request, SessionManager.SessionState sessionState) {
TextureService.Texture texture;
try {
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(null, sessionState.identity.id(), null, request.type)).texture;
} catch (NotFoundException ignored) {
LOGGER.info("Could not find profile texture for player " + request.player);
texture = null;
}
if(texture == null) {
try {
for (Group group : services.groupStore.findGroupsContaining(sessionState.toPlayer()).collect(Collectors.toSet())) {
try {
texture = services.textures.getTexture(RequestContext.ANON, new TextureService.GetTextureRequest(null, null, group.id, request.type)).texture;
if (texture != null) {
break; // Exit the loop once texture is found in the first group
}

} catch (NotFoundException ignored) {
LOGGER.info("Could not find texture " + request.type + " for group " + group.id);
}
}
} catch (NotFoundException ignored) {
LOGGER.info("Could not find texture " + request.type + " for any group");
}
}
return texture;
}

@Override
public void onSessionStopping(ClientIdentity identity, Player player, BiConsumer<Session, ProtocolResponse> sender) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ public GetTextureResponse getTexture(RequestContext context, GetTextureRequest r
context.assertAnonymous();
Texture texture;
if (req.texture != null) {
texture = docs.find(and(eq(FIELD_OWNER, req.profile), eq(FIELD_TEXTURE_ID, req.texture))).map(this::map).first();
if (req.group != null) {
texture = docs.find(and(eq(FIELD_TEXTURE_GROUP, req.group), eq(FIELD_TEXTURE_ID, req.texture))).map(this::map).first();
} else {
texture = docs.find(eq(FIELD_TEXTURE_ID, req.texture)).map(this::map).first();
}
} else if (req.profile != null) {
texture = docs.find(and(eq(FIELD_OWNER, req.profile), eq(FIELD_TYPE, req.type.name()))).map(this::map).first();
} else if (req.group != null) {
Expand Down
2 changes: 1 addition & 1 deletion shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.104.Final</version>
<version>4.1.105.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down

0 comments on commit 5c8a17f

Please sign in to comment.