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

52 lines
1.8 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 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;
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 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(player);
}
2024-04-06 05:45:50 +01:00
@GetMapping(value = "/avatar/{id}")
public ResponseEntity<byte[]> getPlayerHead(@PathVariable String id) {
Player player = playerManagerService.getPlayer(id);
if (player == null) {
return null;
}
2024-04-06 18:42:52 +01:00
2024-04-06 05:45:50 +01:00
Skin skin = player.getSkin();
SkinPart head = skin.getHead();
return ResponseEntity.ok()
2024-04-06 18:42:52 +01:00
.cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic())
2024-04-06 05:45:50 +01:00
.contentType(MediaType.IMAGE_PNG)
.body(head.getPartData());
}
2024-04-06 04:10:15 +01:00
}