From 3ffe19dd220f35f20f1f7ab38863ab3081d7e9c2 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sat, 20 Jan 2024 13:31:45 -0700 Subject: [PATCH] fix(bukkit): improve invalid user handling in OfflinePlayerParser --- .../bukkit/parser/OfflinePlayerParser.java | 16 ++-- .../parser/OfflinePlayerArgumentTest.java | 90 ------------------- 2 files changed, 9 insertions(+), 97 deletions(-) delete mode 100644 cloud-bukkit/src/test/java/cloud/commandframework/bukkit/parser/OfflinePlayerArgumentTest.java diff --git a/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/parser/OfflinePlayerParser.java b/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/parser/OfflinePlayerParser.java index da9deea1..5bb946f2 100644 --- a/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/parser/OfflinePlayerParser.java +++ b/cloud-bukkit/src/main/java/cloud/commandframework/bukkit/parser/OfflinePlayerParser.java @@ -28,7 +28,6 @@ import cloud.commandframework.arguments.parser.ArgumentParser; import cloud.commandframework.arguments.parser.ParserDescriptor; import cloud.commandframework.arguments.suggestion.BlockingSuggestionProvider; -import cloud.commandframework.arguments.suggestion.Suggestion; import cloud.commandframework.bukkit.BukkitCaptionKeys; import cloud.commandframework.bukkit.BukkitCommandContextKeys; import cloud.commandframework.captions.CaptionVariable; @@ -51,7 +50,7 @@ * * @param Command sender type */ -public final class OfflinePlayerParser implements ArgumentParser, BlockingSuggestionProvider { +public final class OfflinePlayerParser implements ArgumentParser, BlockingSuggestionProvider.Strings { /** * Creates a new offline player parser. @@ -84,10 +83,14 @@ public final class OfflinePlayerParser implements ArgumentParser 16) { + return ArgumentParseResult.failure(new OfflinePlayerParseException(input, commandContext)); + } - final OfflinePlayer player = Bukkit.getOfflinePlayer(input); - - if (!player.hasPlayedBefore() && !player.isOnline()) { + final OfflinePlayer player; + try { + player = Bukkit.getOfflinePlayer(input); + } catch (final Exception e) { return ArgumentParseResult.failure(new OfflinePlayerParseException(input, commandContext)); } @@ -95,7 +98,7 @@ public final class OfflinePlayerParser implements ArgumentParser suggestions( + public @NonNull Iterable<@NonNull String> stringSuggestions( final @NonNull CommandContext commandContext, final @NonNull CommandInput input ) { @@ -103,7 +106,6 @@ public final class OfflinePlayerParser implements ArgumentParser !(bukkit instanceof Player && !((Player) bukkit).canSee(player))) .map(Player::getName) - .map(Suggestion::simple) .collect(Collectors.toList()); } diff --git a/cloud-bukkit/src/test/java/cloud/commandframework/bukkit/parser/OfflinePlayerArgumentTest.java b/cloud-bukkit/src/test/java/cloud/commandframework/bukkit/parser/OfflinePlayerArgumentTest.java deleted file mode 100644 index 6b603d48..00000000 --- a/cloud-bukkit/src/test/java/cloud/commandframework/bukkit/parser/OfflinePlayerArgumentTest.java +++ /dev/null @@ -1,90 +0,0 @@ -// -// MIT License -// -// Copyright (c) 2024 Incendo -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -// SOFTWARE. -// -package cloud.commandframework.bukkit.parser; - -import cloud.commandframework.arguments.parser.ArgumentParseResult; -import cloud.commandframework.bukkit.util.ServerTest; -import cloud.commandframework.context.CommandInput; -import com.google.common.truth.Truth; -import org.bukkit.OfflinePlayer; -import org.bukkit.command.CommandSender; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; - -import static com.google.common.truth.Truth.assertThat; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -@SuppressWarnings("deprecation") -class OfflinePlayerArgumentTest extends ServerTest { - - @Mock - private OfflinePlayer player; - - @BeforeEach - void setup() { - when(this.server().getOfflinePlayer("player")).thenReturn(this.player); - } - - @Test - void Parse_HappyFlow_Success() { - // Arrange - when(this.player.hasPlayedBefore()).thenReturn(true); - final OfflinePlayerParser parser = new OfflinePlayerParser<>(); - final CommandInput commandInput = CommandInput.of("player"); - - // Act - final ArgumentParseResult result = parser.parse( - this.commandContext(), - commandInput - ); - - // Assert - assertThat(result.failure()).isEmpty(); - assertThat(result.parsedValue()).hasValue(player); - Truth.assertThat(commandInput.remainingInput()).isEmpty(); - - verify(this.server()).getOfflinePlayer("player"); - } - - @Test - void Parse_NonExistentPlayer_Failure() { - // Arrange - final OfflinePlayerParser parser = new OfflinePlayerParser<>(); - final CommandInput commandInput = CommandInput.of("player"); - - // Act - final ArgumentParseResult result = parser.parse( - this.commandContext(), - commandInput - ); - - // Assert - assertThat(result.failure()).isPresent(); - assertThat(result.parsedValue()).isEmpty(); - - verify(this.server()).getOfflinePlayer("player"); - } -}