1+ package net .onelitefeather .labyrinth .commands ;
2+
3+ import net .kyori .adventure .text .minimessage .MiniMessage ;
4+ import net .kyori .adventure .text .minimessage .tag .resolver .Placeholder ;
5+ import net .onelitefeather .labyrinth .service .api .ValidationService ;
6+ import net .onelitefeather .labyrinth .utils .Constants ;
7+ import org .bukkit .Location ;
8+ import org .bukkit .Material ;
9+ import org .bukkit .entity .Player ;
10+ import org .jetbrains .annotations .NotNull ;
11+ import org .junit .jupiter .api .*;
12+ import org .mockbukkit .mockbukkit .world .WorldMock ;
13+
14+ class CenterCommandTest extends CommandPluginTestBase {
15+
16+ private CenterCommand command ;
17+ private MockValidationService validationService ;
18+
19+ public static class MockValidationService implements ValidationService {
20+ private boolean isValid ;
21+
22+ @ Override
23+ public boolean validateZoneInput (@ NotNull Player player , @ NotNull String zone ) {
24+ return isValid ;
25+ }
26+
27+ public void setValid (boolean valid ) {
28+ isValid = valid ;
29+ }
30+
31+ public boolean isValid () {
32+ return isValid ;
33+ }
34+ }
35+
36+ @ Override
37+ @ BeforeEach
38+ void setUp () {
39+ super .setUp ();
40+ validationService = new MockValidationService ();
41+ command = new CenterCommand (plugin , validationService );
42+ }
43+
44+
45+ @ DisplayName ("Test if the player location is not zero" )
46+ @ Test
47+ void testIsYLocationFromPlayerNotZero () {
48+ var player = server .addPlayer ();
49+ var location = new Location (new WorldMock (Material .GRASS_BLOCK , 64 ), 120 , 64 , 120 );
50+ player .setLocation (location );
51+
52+ command .centerCommand (player , "Test" );
53+ Assertions .assertNotEquals (0 , location .getY ());
54+ }
55+
56+ @ Test
57+ void testValidationWrong () {
58+ var player = server .addPlayer ();
59+ var location = new Location (new WorldMock (Material .GRASS_BLOCK , 64 ), 120 , 64 , 120 );
60+ player .setLocation (location );
61+
62+ validationService .setValid (false );
63+ command .centerCommand (player , "Test" );
64+ var playerMessage = player .nextComponentMessage ();
65+ Assertions .assertNotNull (playerMessage );
66+ var expectedMessage = MiniMessage .miniMessage ().deserialize (Constants .ZONE_INVALID_MESSAGE ,
67+ Placeholder .component ("prefix" , Constants .PREFIX ));
68+ Assertions .assertEquals (expectedMessage , playerMessage );
69+ Assertions .assertFalse (plugin .getConfig ().contains (Constants .CONFIG_ZONE_CENTER_PATH .formatted ("Test" )));
70+ }
71+
72+ @ Test
73+ void testValidationTrue () {
74+ var player = server .addPlayer ();
75+ var location = new Location (new WorldMock (Material .GRASS_BLOCK , 64 ), 120 , 64 , 120 );
76+ var zoneName = "Test" ;
77+ var expectedMessage = MiniMessage .miniMessage ().deserialize (Constants .CENTER_COMMAND_MESSAGE_SUCCESS ,
78+ Placeholder .unparsed ("zone" , zoneName ),
79+ Placeholder .component ("prefix" , Constants .PREFIX ));
80+
81+ player .setLocation (location );
82+ validationService .setValid (true );
83+ command .centerCommand (player , zoneName );
84+ location .setY (0 );
85+ var playerMessage = player .nextComponentMessage ();
86+
87+ Assertions .assertNotNull (playerMessage );
88+ Assertions .assertEquals (expectedMessage , playerMessage );
89+ Assertions .assertTrue (plugin .getConfig ().contains (Constants .CONFIG_ZONE_CENTER_PATH .formatted (zoneName )));
90+ Assertions .assertEquals (location , plugin .getConfig ().getLocation (Constants .CONFIG_ZONE_CENTER_PATH .formatted (zoneName )));
91+ }
92+
93+ }
0 commit comments