2024-04-08 04:51:17 +01:00
|
|
|
package cc.fascinated.controller;
|
2024-04-06 04:10:15 +01:00
|
|
|
|
2024-04-08 06:28:43 +01:00
|
|
|
import cc.fascinated.model.player.Player;
|
|
|
|
import cc.fascinated.model.player.Skin;
|
2024-04-08 07:37:22 +01:00
|
|
|
import cc.fascinated.model.response.impl.InvalidPartResponse;
|
2024-04-08 06:48:21 +01:00
|
|
|
import cc.fascinated.model.response.impl.PlayerNotFoundResponse;
|
2024-04-08 06:28:43 +01:00
|
|
|
import cc.fascinated.service.PlayerService;
|
2024-04-08 19:47:20 +01:00
|
|
|
import cc.fascinated.common.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;
|
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.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 {
|
|
|
|
|
2024-04-06 19:16:54 +01:00
|
|
|
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
|
2024-04-06 18:57:09 +01:00
|
|
|
public ResponseEntity<?> getPlayer(@PathVariable String id) {
|
2024-04-06 04:10:15 +01:00
|
|
|
Player player = playerManagerService.getPlayer(id);
|
2024-04-08 06:48:21 +01:00
|
|
|
if (player == null) { // No player with that id was found
|
|
|
|
return new PlayerNotFoundResponse().toResponseEntity();
|
2024-04-06 04:10:15 +01:00
|
|
|
}
|
2024-04-08 06:48:21 +01:00
|
|
|
// Return the player
|
2024-04-06 19:16:54 +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}")
|
2024-04-08 07:37:22 +01:00
|
|
|
public ResponseEntity<?> getPlayerHead(@PathVariable String part,
|
2024-04-08 04:51:17 +01:00
|
|
|
@PathVariable String id,
|
2024-04-08 23:40:47 +01:00
|
|
|
@RequestParam(required = false, defaultValue = "256") 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);
|
2024-04-08 07:37:22 +01:00
|
|
|
if (skinPart == null) { // Unknown part name
|
|
|
|
return new InvalidPartResponse().toResponseEntity();
|
2024-04-08 07:33:14 +01:00
|
|
|
}
|
2024-04-08 07:37:22 +01:00
|
|
|
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 06:48:21 +01:00
|
|
|
// Return the part image
|
2024-04-06 05:45:50 +01:00
|
|
|
return ResponseEntity.ok()
|
2024-04-06 19:16:54 +01:00
|
|
|
.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
|
|
|
}
|