-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Peaches_MLG <[email protected]>
- Loading branch information
1 parent
a205725
commit 88f7ef0
Showing
4 changed files
with
220 additions
and
27 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/com/iridium/iridiumteams/commands/ConfirmableCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.iridium.iridiumteams.commands; | ||
|
||
import com.iridium.iridiumteams.IridiumTeams; | ||
import com.iridium.iridiumteams.database.IridiumUser; | ||
import com.iridium.iridiumteams.database.Team; | ||
import com.iridium.iridiumteams.gui.ConfirmationGUI; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public abstract class ConfirmableCommand<T extends Team, U extends IridiumUser<T>> extends Command<T, U> { | ||
public final boolean requiresConfirmation; | ||
|
||
public ConfirmableCommand() { | ||
super(); | ||
this.requiresConfirmation = false; | ||
} | ||
|
||
public ConfirmableCommand(@NotNull List<String> aliases, @NotNull String description, @NotNull String syntax, | ||
@NotNull String permission, long cooldownInSeconds, boolean requiresConfirmation) { | ||
super(aliases, description, syntax, permission, cooldownInSeconds); | ||
this.requiresConfirmation = requiresConfirmation; | ||
} | ||
|
||
@Override | ||
public final boolean execute(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams) { | ||
if (!isCommandValid(user, team, arguments, iridiumTeams)) { | ||
return false; | ||
} | ||
|
||
if (requiresConfirmation) { | ||
Player player = user.getPlayer(); | ||
|
||
player.openInventory(new ConfirmationGUI<>(() -> { | ||
executeAfterConfirmation(user, team, arguments, iridiumTeams); | ||
}, iridiumTeams).getInventory()); | ||
return true; | ||
} | ||
|
||
executeAfterConfirmation(user, team, arguments, iridiumTeams); | ||
return true; | ||
} | ||
|
||
protected abstract boolean isCommandValid(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams); | ||
|
||
protected abstract void executeAfterConfirmation(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/test/java/com/iridium/iridiumteams/commands/ConfirmableCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package com.iridium.iridiumteams.commands; | ||
|
||
import be.seeseemelk.mockbukkit.MockBukkit; | ||
import be.seeseemelk.mockbukkit.ServerMock; | ||
import be.seeseemelk.mockbukkit.entity.PlayerMock; | ||
import com.iridium.iridiumteams.IridiumTeams; | ||
import com.iridium.iridiumteams.TeamBuilder; | ||
import com.iridium.iridiumteams.UserBuilder; | ||
import com.iridium.iridiumteams.gui.ConfirmationGUI; | ||
import com.iridium.testplugin.TestPlugin; | ||
import com.iridium.testplugin.TestTeam; | ||
import com.iridium.testplugin.User; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.inventory.Inventory; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ConfirmableCommandTest { | ||
private ServerMock serverMock; | ||
private TestConfirmableCommand command; | ||
private IridiumTeams<TestTeam, User> iridiumTeams; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
this.serverMock = MockBukkit.mock(); | ||
MockBukkit.load(TestPlugin.class); | ||
this.iridiumTeams = TestPlugin.getInstance(); | ||
this.command = new TestConfirmableCommand(true); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
MockBukkit.unmock(); | ||
} | ||
|
||
@Test | ||
public void testExecuteWithoutConfirmation() { | ||
TestConfirmableCommand command = new TestConfirmableCommand(false); | ||
PlayerMock playerMock = new UserBuilder(serverMock).build(); | ||
User user = TestPlugin.getInstance().getUserManager().getUser(playerMock); | ||
TestTeam team = new TeamBuilder().build(); | ||
|
||
command.execute(user, team, new String[]{}, iridiumTeams); | ||
|
||
assertTrue(command.isCommandValidCalled); | ||
assertTrue(command.executeAfterConfirmationCalled); | ||
} | ||
|
||
@Test | ||
public void testExecuteWithConfirmation() { | ||
TestConfirmableCommand command = new TestConfirmableCommand(true); | ||
PlayerMock player = new UserBuilder(serverMock).build(); | ||
User user = TestPlugin.getInstance().getUserManager().getUser(player); | ||
TestTeam team = new TeamBuilder().build(); | ||
|
||
command.execute(user, team, new String[]{}, iridiumTeams); | ||
|
||
assertTrue(command.isCommandValidCalled); | ||
|
||
serverMock.getScheduler().performOneTick(); | ||
assertTrue(player.getOpenInventory().getTopInventory().getHolder() instanceof ConfirmationGUI); | ||
|
||
assertFalse(command.executeAfterConfirmationCalled); | ||
} | ||
|
||
@Test | ||
public void testConfirmationGUI_Accept() { | ||
TestConfirmableCommand command = new TestConfirmableCommand(true); | ||
PlayerMock player = new UserBuilder(serverMock).build(); | ||
User user = TestPlugin.getInstance().getUserManager().getUser(player); | ||
TestTeam team = new TeamBuilder().build(); | ||
|
||
command.execute(user, team, new String[]{}, iridiumTeams); | ||
|
||
assertFalse(command.executeAfterConfirmationCalled); | ||
|
||
serverMock.getScheduler().performOneTick(); | ||
|
||
Inventory openInventory = player.getOpenInventory().getTopInventory(); | ||
assertTrue(openInventory.getHolder() instanceof ConfirmationGUI); | ||
|
||
InventoryClickEvent confirmClickEvent = player.simulateInventoryClick(TestPlugin.getInstance().getInventories().confirmationGUI.yes.slot); | ||
assertTrue(confirmClickEvent.isCancelled()); | ||
assertNull(player.getOpenInventory().getTopInventory()); | ||
assertTrue(command.executeAfterConfirmationCalled); | ||
} | ||
|
||
@Test | ||
public void testConfirmationGUI_Deny() { | ||
TestConfirmableCommand command = new TestConfirmableCommand(true); | ||
PlayerMock player = new UserBuilder(serverMock).build(); | ||
User user = TestPlugin.getInstance().getUserManager().getUser(player); | ||
TestTeam team = new TeamBuilder().build(); | ||
|
||
command.execute(user, team, new String[]{}, iridiumTeams); | ||
|
||
assertFalse(command.executeAfterConfirmationCalled); | ||
|
||
serverMock.getScheduler().performOneTick(); | ||
|
||
Inventory openInventory = player.getOpenInventory().getTopInventory(); | ||
assertTrue(openInventory.getHolder() instanceof ConfirmationGUI); | ||
|
||
InventoryClickEvent denyClickEvent = player.simulateInventoryClick(TestPlugin.getInstance().getInventories().confirmationGUI.no.slot); | ||
assertTrue(denyClickEvent.isCancelled()); | ||
assertNull(player.getOpenInventory().getTopInventory()); | ||
assertFalse(command.executeAfterConfirmationCalled); | ||
} | ||
|
||
private class TestConfirmableCommand extends ConfirmableCommand<TestTeam, User> { | ||
boolean isCommandValidCalled = false; | ||
boolean executeAfterConfirmationCalled = false; | ||
|
||
public TestConfirmableCommand(boolean requiresConfirmation) { | ||
super(Collections.emptyList(), "", "", "", 0, requiresConfirmation); | ||
} | ||
|
||
@Override | ||
protected boolean isCommandValid(User user, TestTeam team, String[] arguments, IridiumTeams<TestTeam, User> iridiumTeams) { | ||
isCommandValidCalled = true; | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void executeAfterConfirmation(User user, TestTeam team, String[] arguments, IridiumTeams<TestTeam, User> iridiumTeams) { | ||
executeAfterConfirmationCalled = true; | ||
} | ||
} | ||
} |