Files
minecraft-helper/src/main/java/cc/fascinated/api/controller/PlayerController.java

61 lines
2.2 KiB
Java
Raw Normal View History

2024-04-06 04:10:15 +01:00
package cc.fascinated.api.controller;
import cc.fascinated.player.PlayerManagerService;
import cc.fascinated.player.impl.Player;
2024-04-06 05:45:50 +01:00
import cc.fascinated.player.impl.Skin;
import cc.fascinated.player.impl.SkinPart;
2024-04-06 19:13:40 +01:00
import lombok.NonNull;
2024-04-06 04:10:15 +01:00
import org.springframework.beans.factory.annotation.Autowired;
2024-04-06 18:42:52 +01:00
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
2024-04-06 04:10:15 +01:00
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
2024-04-06 18:42:52 +01:00
import java.util.Map;
2024-04-06 19:13:40 +01:00
import java.util.Objects;
2024-04-06 18:42:52 +01:00
import java.util.concurrent.TimeUnit;
2024-04-06 04:10:15 +01:00
@RestController
2024-04-06 05:45:50 +01:00
@RequestMapping(value = "/")
2024-04-06 04:10:15 +01:00
public class PlayerController {
private final CacheControl cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();
2024-04-06 19:13:40 +01:00
@NonNull private final SkinPart defaultHead = Objects.requireNonNull(Skin.getDefaultHead(), "Default head is null");
2024-04-06 04:10:15 +01:00
private final PlayerManagerService playerManagerService;
@Autowired
public PlayerController(PlayerManagerService playerManagerService) {
this.playerManagerService = playerManagerService;
}
2024-04-06 05:45:50 +01:00
@GetMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody
public ResponseEntity<?> getPlayer(@PathVariable String id) {
2024-04-06 04:10:15 +01:00
Player player = playerManagerService.getPlayer(id);
if (player == null) {
return new ResponseEntity<>(Map.of("error", "Player not found"), HttpStatus.NOT_FOUND);
2024-04-06 04:10:15 +01:00
}
return ResponseEntity.ok()
.cacheControl(cacheControl)
.body(player);
2024-04-06 04:10:15 +01:00
}
2024-04-06 05:45:50 +01:00
@GetMapping(value = "/avatar/{id}")
public ResponseEntity<byte[]> getPlayerHead(@PathVariable String id) {
Player player = playerManagerService.getPlayer(id);
2024-04-06 19:13:40 +01:00
byte[] headBytes;
2024-04-06 05:45:50 +01:00
if (player == null) {
2024-04-06 19:13:40 +01:00
headBytes = defaultHead.getPartData();
} else {
Skin skin = player.getSkin();
SkinPart head = skin.getHead();
headBytes = head.getPartData();
2024-04-06 05:45:50 +01:00
}
return ResponseEntity.ok()
.cacheControl(cacheControl)
2024-04-06 05:45:50 +01:00
.contentType(MediaType.IMAGE_PNG)
2024-04-06 19:13:40 +01:00
.body(headBytes);
2024-04-06 05:45:50 +01:00
}
2024-04-06 04:10:15 +01:00
}