From 6144a2e1f6e3b33daca9512cb0832e8c700d0fea Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 8 Apr 2024 07:37:22 +0100 Subject: [PATCH] fix issue if bad part name is given it shows the default head --- .../cc/fascinated/controller/PlayerController.java | 8 +++++--- .../model/response/impl/InvalidPartResponse.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/fascinated/model/response/impl/InvalidPartResponse.java diff --git a/src/main/java/cc/fascinated/controller/PlayerController.java b/src/main/java/cc/fascinated/controller/PlayerController.java index 338f465..50a8ac6 100644 --- a/src/main/java/cc/fascinated/controller/PlayerController.java +++ b/src/main/java/cc/fascinated/controller/PlayerController.java @@ -2,6 +2,7 @@ package cc.fascinated.controller; import cc.fascinated.model.player.Player; import cc.fascinated.model.player.Skin; +import cc.fascinated.model.response.impl.InvalidPartResponse; import cc.fascinated.model.response.impl.PlayerNotFoundResponse; import cc.fascinated.service.PlayerService; import cc.fascinated.util.PlayerUtils; @@ -39,7 +40,7 @@ public class PlayerController { } @GetMapping(value = "/{part}/{id}") - public ResponseEntity getPlayerHead(@PathVariable String part, + public ResponseEntity getPlayerHead(@PathVariable String part, @PathVariable String id, @RequestParam(required = false, defaultValue = "250") int size) { Player player = playerManagerService.getPlayer(id); @@ -47,9 +48,10 @@ public class PlayerController { if (player != null) { // The player exists Skin skin = player.getSkin(); Skin.Parts skinPart = Skin.Parts.fromName(part); - if (skinPart != null) { // The part exists - partBytes = PlayerUtils.getSkinPartBytes(skin, skinPart, size); + if (skinPart == null) { // Unknown part name + return new InvalidPartResponse().toResponseEntity(); } + partBytes = PlayerUtils.getSkinPartBytes(skin, skinPart, size); } if (partBytes == null) { // Fallback to the default head partBytes = PlayerUtils.getSkinPartBytes(Skin.DEFAULT_SKIN, Skin.Parts.HEAD, size); diff --git a/src/main/java/cc/fascinated/model/response/impl/InvalidPartResponse.java b/src/main/java/cc/fascinated/model/response/impl/InvalidPartResponse.java new file mode 100644 index 0000000..132e643 --- /dev/null +++ b/src/main/java/cc/fascinated/model/response/impl/InvalidPartResponse.java @@ -0,0 +1,11 @@ +package cc.fascinated.model.response.impl; + +import cc.fascinated.model.response.Response; +import org.springframework.http.HttpStatus; + +public class InvalidPartResponse extends Response { + + public InvalidPartResponse() { + super(HttpStatus.NOT_FOUND, "Invalid part name."); + } +}