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

63 lines
2.4 KiB
Java
Raw Normal View History

2024-04-08 04:51:17 +01:00
package cc.fascinated.controller;
2024-04-06 04:10:15 +01:00
2024-04-08 05:29:05 +01:00
import cc.fascinated.service.PlayerService;
import cc.fascinated.model.player.Player;
import cc.fascinated.model.player.Skin;
2024-04-08 06:13:03 +01:00
import cc.fascinated.util.PlayerUtils;
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-07 00:31:33 +01:00
@RequestMapping(value = "/player/")
2024-04-06 04:10:15 +01:00
public class PlayerController {
private final CacheControl cacheControl = CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic();
2024-04-08 04:51:17 +01:00
private final PlayerService playerManagerService;
2024-04-06 04:10:15 +01:00
@Autowired
2024-04-08 04:51:17 +01:00
public PlayerController(PlayerService playerManagerService) {
2024-04-06 04:10:15 +01:00
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
2024-04-08 04:51:17 +01:00
@GetMapping(value = "/{part}/{id}")
public ResponseEntity<byte[]> getPlayerHead(@PathVariable String part,
@PathVariable String id,
@RequestParam(required = false, defaultValue = "250") int size) {
2024-04-06 05:45:50 +01:00
Player player = playerManagerService.getPlayer(id);
2024-04-08 06:13:03 +01:00
byte[] partBytes = new byte[0];
2024-04-08 05:29:05 +01:00
if (player != null) { // The player exists
2024-04-06 19:13:40 +01:00
Skin skin = player.getSkin();
2024-04-08 06:13:03 +01:00
Skin.Parts skinPart = Skin.Parts.fromName(part);
partBytes = PlayerUtils.getSkinPartBytes(skin, skinPart, size);
2024-04-08 04:51:17 +01:00
}
2024-04-08 06:13:03 +01:00
if (partBytes == null) { // Fallback to the default head
partBytes = PlayerUtils.getSkinPartBytes(Skin.DEFAULT_SKIN, Skin.Parts.HEAD, size);
2024-04-06 05:45:50 +01:00
}
2024-04-08 04:51:17 +01:00
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-08 06:13:03 +01:00
.body(partBytes);
2024-04-06 05:45:50 +01:00
}
2024-04-06 04:10:15 +01:00
}