Skip to content

Commit

Permalink
Add confirmable commands (#85)
Browse files Browse the repository at this point in the history
Co-authored-by: Peaches_MLG <[email protected]>
  • Loading branch information
Faun471 and PeachesMLG authored Jul 28, 2024
1 parent a205725 commit 88f7ef0
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 27 deletions.
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);
}
62 changes: 36 additions & 26 deletions src/main/java/com/iridium/iridiumteams/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@
import com.iridium.iridiumteams.Rank;
import com.iridium.iridiumteams.database.IridiumUser;
import com.iridium.iridiumteams.database.Team;
import com.iridium.iridiumteams.gui.ConfirmationGUI;
import lombok.NoArgsConstructor;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Optional;

@NoArgsConstructor
public class DeleteCommand<T extends Team, U extends IridiumUser<T>> extends Command<T, U> {
public class DeleteCommand<T extends Team, U extends IridiumUser<T>> extends ConfirmableCommand<T, U> {
public String adminPermission;

public DeleteCommand(List<String> args, String description, String syntax, String permission, long cooldownInSeconds, String adminPermission) {
super(args, description, syntax, permission, cooldownInSeconds);
public DeleteCommand(List<String> args, String description, String syntax, String permission, long cooldownInSeconds, String adminPermission, boolean requiresConfirmation) {
super(args, description, syntax, permission, cooldownInSeconds, requiresConfirmation);
this.adminPermission = adminPermission;
}

Expand All @@ -31,53 +30,64 @@ public boolean execute(U user, String[] arguments, IridiumTeams<T, U> iridiumTea
));
return false;
}

Optional<T> team = iridiumTeams.getTeamManager().getTeamViaNameOrPlayer(arguments[0]);
if (!team.isPresent()) {
player.sendMessage(StringUtils.color(iridiumTeams.getMessages().teamDoesntExistByName
.replace("%prefix%", iridiumTeams.getConfiguration().prefix))
);
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
));
return false;
}
deleteTeam(user, team.get(), iridiumTeams, true);
return true;
return execute(user, team.get(), arguments, iridiumTeams);
}
return super.execute(user, arguments, iridiumTeams);
}

@Override
public boolean execute(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams) {
protected boolean isCommandValid(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams) {
Player player = user.getPlayer();
if (arguments.length == 1) {
return true;
}

if (user.getUserRank() != Rank.OWNER.getId() && !user.isBypassing()) {
player.sendMessage(StringUtils.color(iridiumTeams.getMessages().cannotDeleteTeam
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
));
return false;
}
deleteTeam(user, team, iridiumTeams, false);
return true;
}

@Override
protected void executeAfterConfirmation(U user, T team, String[] arguments, IridiumTeams<T, U> iridiumTeams) {
if (arguments.length == 1) {
deleteTeam(user, team, iridiumTeams, true);
}

deleteTeam(user, team, iridiumTeams, false);
}

private void deleteTeam(U user, T team, IridiumTeams<T, U> iridiumTeams, boolean admin) {
Player player = user.getPlayer();
player.openInventory(new ConfirmationGUI<>(() -> {
if (!iridiumTeams.getTeamManager().deleteTeam(team, user)) return;
for (U member : iridiumTeams.getTeamManager().getTeamMembers(team)) {
member.setTeamID(0);
Player teamMember = member.getPlayer();
if (teamMember != null) {
teamMember.sendMessage(StringUtils.color(iridiumTeams.getMessages().teamDeleted
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
.replace("%player%", player.getName())
));
}
}
if (admin) {
player.sendMessage(StringUtils.color(iridiumTeams.getMessages().deletedPlayerTeam
if (!iridiumTeams.getTeamManager().deleteTeam(team, user)) return;

for (U member : iridiumTeams.getTeamManager().getTeamMembers(team)) {
member.setTeamID(0);
Player teamMember = member.getPlayer();
if (teamMember != null) {
teamMember.sendMessage(StringUtils.color(iridiumTeams.getMessages().teamDeleted
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
.replace("%name%", team.getName())
.replace("%player%", player.getName())
));
}
}, iridiumTeams).getInventory());
}
if (admin) {
player.sendMessage(StringUtils.color(iridiumTeams.getMessages().deletedPlayerTeam
.replace("%prefix%", iridiumTeams.getConfiguration().prefix)
.replace("%name%", team.getName())
));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Commands(String permissionBase, String team, String commandBase) {
trustsCommand = new TrustsCommand<>(Collections.singletonList("trusts"), "View all active trusted members for your " + team, "%prefix% &7/" + commandBase + " trusts", "", 0);
kickCommand = new KickCommand<>(Collections.singletonList("kick"), "Kick a player from your " + team, "%prefix% &7/" + commandBase + " kick <player>", "", 0);
leaveCommand = new LeaveCommand<>(Collections.singletonList("leave"), "Leave your current " + team, "%prefix% &7/" + commandBase + " leave", "", 0);
deleteCommand = new DeleteCommand<>(Collections.singletonList("delete"), "Delete your " + team, "%prefix% &7/" + commandBase + " delete (player)", "", 0, permissionBase + ".delete.others");
deleteCommand = new DeleteCommand<>(Collections.singletonList("delete"), "Delete your " + team, "%prefix% &7/" + commandBase + " delete (player)", "", 0, permissionBase + ".delete.others", true);
infoCommand = new InfoCommand<>(Arrays.asList("info", "value"), "View information about a " + team, "%prefix% &7/" + commandBase + " info <" + team.toLowerCase() + ">", "", 0);
descriptionCommand = new DescriptionCommand<>(Collections.singletonList("description"), "Set your " + team + " description.", "%prefix% &7/" + commandBase + " description (" + team + ") <description>", "", 0, permissionBase + ".description.others");
renameCommand = new RenameCommand<>(Collections.singletonList("rename"), "Rename your " + team, "%prefix% &7/" + commandBase + " rename (" + team + ") <name>", "", 0, permissionBase + ".rename.others");
Expand Down
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;
}
}
}

0 comments on commit 88f7ef0

Please sign in to comment.