diff --git a/src/main/java/fr/communaywen/core/commands/TeamCommand.java b/src/main/java/fr/communaywen/core/commands/TeamCommand.java index 42ce294f..9562c90d 100644 --- a/src/main/java/fr/communaywen/core/commands/TeamCommand.java +++ b/src/main/java/fr/communaywen/core/commands/TeamCommand.java @@ -5,6 +5,9 @@ import fr.communaywen.core.teams.Team; import fr.communaywen.core.teams.TeamManager; import fr.communaywen.core.teams.menu.TeamListMenu; +import fr.communaywen.core.teams.menu.TeamMenu; +import fr.communaywen.core.teams.utils.MethodState; +import fr.communaywen.core.teams.utils.TeamUtils; import fr.communaywen.core.utils.CommandUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -28,11 +31,16 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return CommandUtils.sendMessage(sender, "Vous devez être un joueur pour exécuter cette commande !", true); } if (args.length == 0) { - return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team ", false); + Team team = teamManager.isInTeam(player.getUniqueId()); + if (team == null) { + return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true); + } + TeamMenu teamMenu = new TeamMenu(player, team, false); + teamMenu.open(); } if (args.length == 1) { if (args[0].equalsIgnoreCase("create")) { - return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team create ", false); + return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team create ", true); } if (args[0].equalsIgnoreCase("list")) { Menu menu = new TeamListMenu(player, teamManager); @@ -66,17 +74,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St if (team == null) { return CommandUtils.sendMessage(sender, "Vous n'êtes pas dans une team !", true); } - boolean notDeleted = team.removePlayer(player.getUniqueId()); - CommandUtils.sendMessage(sender, "Vous avez quitté la team !", false); - if (!notDeleted) { - CommandUtils.sendMessage(sender, ChatColor.DARK_RED + "La team a été supprimée !", false); - } - for (UUID teamPlayer : team.getPlayers()) { - Player teamPlayerOnline = Bukkit.getPlayer(teamPlayer); - if (teamPlayerOnline != null) { - CommandUtils.sendMessage(teamPlayerOnline, player.getName() + " a quitté la team !", false); - } - } + return TeamUtils.quit(team, player); } if (args[0].equalsIgnoreCase("inventory")) { Team team = teamManager.isInTeam(player.getUniqueId()); @@ -138,15 +136,15 @@ public boolean onCommand(CommandSender sender, Command command, String label, St } UUID target = team.getPlayerByUsername(args[1]); if (target != null) { - boolean notDeleted = team.removePlayer(target); - CommandUtils.sendMessage(sender, "Le joueur " + args[1] + " a été kické de la team !", false); + MethodState state = team.removePlayer(target); + if (state == MethodState.VALID || state == MethodState.WARNING) CommandUtils.sendMessage(sender, "Le joueur " + args[1] + " a été kické de la team !", false); + if (state == MethodState.INVALID) return CommandUtils.sendMessage(sender, ChatColor.DARK_RED + "Impossible de kick, la team serait supprimée et il reste des items dans l'inventaire !", true); Player targetPlayer = Bukkit.getPlayer(target); if (targetPlayer != null) { CommandUtils.sendMessage(Objects.requireNonNull(targetPlayer), ChatColor.DARK_RED + "Vous avez été kické de la team !", false); } - if (!notDeleted) { - CommandUtils.sendMessage(sender, ChatColor.DARK_RED + "La team a été supprimée !", false); - } + if (state == MethodState.WARNING) CommandUtils.sendMessage(sender, ChatColor.DARK_RED + "La team a été supprimée !", false); + } else { return CommandUtils.sendMessage(sender, "Le joueur " + args[1] + " n'est pas dans la team !", true); } diff --git a/src/main/java/fr/communaywen/core/teams/Team.java b/src/main/java/fr/communaywen/core/teams/Team.java index 40d241dc..94ec657b 100644 --- a/src/main/java/fr/communaywen/core/teams/Team.java +++ b/src/main/java/fr/communaywen/core/teams/Team.java @@ -1,6 +1,8 @@ package fr.communaywen.core.teams; import fr.communaywen.core.AywenCraftPlugin; +import fr.communaywen.core.teams.utils.MethodState; +import fr.communaywen.core.utils.CommandUtils; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; @@ -44,10 +46,18 @@ public List getPlayers(int first, int last) { return result; } + public boolean isIn(UUID player) { + return players.contains(player); + } + public void openInventory(Player player) { player.openInventory(inventory); } + public Inventory getInventory() { + return inventory; + } + public UUID getPlayerByUsername(String username) { Player bukkitPlayer = Bukkit.getPlayer(username); if (bukkitPlayer == null) { @@ -76,16 +86,19 @@ public boolean addPlayer(UUID player) { return true; } - public boolean removePlayer(UUID player) { - players.remove(player); - if (players.isEmpty()) { - AywenCraftPlugin.getInstance().getTeamManager().deleteTeam(this); - return false; + public MethodState removePlayer(UUID player) { + if (players.size() - 1 == 0) { + players.remove(player); + if (!AywenCraftPlugin.getInstance().getTeamManager().deleteTeam(this)) { + players.add(player); + return MethodState.INVALID; + } + return MethodState.WARNING; } if (isOwner(player)) { owner = getRandomPlayer(); } - return true; + return MethodState.VALID; } public boolean isOwner(UUID player) { diff --git a/src/main/java/fr/communaywen/core/teams/TeamManager.java b/src/main/java/fr/communaywen/core/teams/TeamManager.java index ba29d30a..e5466cff 100644 --- a/src/main/java/fr/communaywen/core/teams/TeamManager.java +++ b/src/main/java/fr/communaywen/core/teams/TeamManager.java @@ -1,9 +1,13 @@ package fr.communaywen.core.teams; import fr.communaywen.core.utils.Queue; +import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.OfflinePlayer; +import org.bukkit.inventory.ItemStack; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.UUID; @@ -25,8 +29,18 @@ public Team createTeam(UUID owner, String name) { return team; } - public void deleteTeam(Team team) { + public boolean deleteTeam(Team team) { + int items = 0; + for (ItemStack item : team.getInventory().getContents()) { + if (item != null && item.getType() != Material.AIR) { + items++; + } + } + if (items > 0) { + return false; + } teams.remove(team); + return true; } public Team isInTeam(UUID player) { diff --git a/src/main/java/fr/communaywen/core/teams/menu/ConfirmationMenu.java b/src/main/java/fr/communaywen/core/teams/menu/ConfirmationMenu.java new file mode 100644 index 00000000..78c82f89 --- /dev/null +++ b/src/main/java/fr/communaywen/core/teams/menu/ConfirmationMenu.java @@ -0,0 +1,51 @@ +package fr.communaywen.core.teams.menu; + +import dev.xernas.menulib.Menu; +import dev.xernas.menulib.utils.InventorySize; +import dev.xernas.menulib.utils.ItemBuilder; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.function.Consumer; + +public class ConfirmationMenu extends Menu { + + private final Consumer toConfirm; + + public ConfirmationMenu(Player owner, Consumer toConfirm) { + super(owner); + this.toConfirm = toConfirm; + } + + @Override + public @NotNull String getName() { + return ChatColor.GOLD + "Confirmation"; + } + + @Override + public @NotNull InventorySize getInventorySize() { + return InventorySize.SMALLEST; + } + + @Override + public void onInventoryClick(InventoryClickEvent inventoryClickEvent) { + + } + + @Override + public @NotNull Map getContent() { + Map content = fill(Material.GRAY_STAINED_GLASS_PANE); + content.put(2, new ItemBuilder(this, Material.GREEN_CONCRETE, itemMeta -> { + itemMeta.setDisplayName(ChatColor.GREEN + "Confirmer"); + }).setOnClick(toConfirm)); + content.put(6, new ItemBuilder(this, Material.RED_CONCRETE, itemMeta -> { + itemMeta.setDisplayName(ChatColor.RED + "Annuler"); + }).setBackButton()); + return content; + } +} diff --git a/src/main/java/fr/communaywen/core/teams/menu/TeamListMenu.java b/src/main/java/fr/communaywen/core/teams/menu/TeamListMenu.java index eaae1532..b32fe6ac 100644 --- a/src/main/java/fr/communaywen/core/teams/menu/TeamListMenu.java +++ b/src/main/java/fr/communaywen/core/teams/menu/TeamListMenu.java @@ -1,6 +1,5 @@ package fr.communaywen.core.teams.menu; -import dev.xernas.menulib.Menu; import dev.xernas.menulib.PaginatedMenu; import dev.xernas.menulib.utils.ItemBuilder; import dev.xernas.menulib.utils.ItemUtils; @@ -11,6 +10,7 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -48,9 +48,10 @@ public TeamListMenu(Player owner, TeamManager manager) { itemMeta.setDisplayName(ChatColor.GOLD + manager.getTeams().get(finalI).getName()); itemMeta.setLore(List.of( ChatColor.DARK_RED + "Propriétaire: " + Bukkit.getOfflinePlayer(manager.getTeams().get(finalI).getOwner()).getName(), - ChatColor.GRAY + "Membres: " + manager.getTeams().get(finalI).getPlayers().size() + ChatColor.GRAY + "■ Membres: " + manager.getTeams().get(finalI).getPlayers().size(), + ChatColor.GRAY + "■ Cliquez pour voir les détails" )); - }).setNextMenu(new MemberListMenu(getOwner(), manager.getTeams().get(i)))); + }).setNextMenu(new TeamMenu(getOwner(), manager.getTeams().get(i), true))); } return items; } diff --git a/src/main/java/fr/communaywen/core/teams/menu/MemberListMenu.java b/src/main/java/fr/communaywen/core/teams/menu/TeamMenu.java similarity index 58% rename from src/main/java/fr/communaywen/core/teams/menu/MemberListMenu.java rename to src/main/java/fr/communaywen/core/teams/menu/TeamMenu.java index 9c4c574e..17ed28cc 100644 --- a/src/main/java/fr/communaywen/core/teams/menu/MemberListMenu.java +++ b/src/main/java/fr/communaywen/core/teams/menu/TeamMenu.java @@ -5,10 +5,12 @@ import dev.xernas.menulib.utils.ItemBuilder; import dev.xernas.menulib.utils.ItemUtils; import fr.communaywen.core.teams.Team; +import fr.communaywen.core.teams.utils.MethodState; +import fr.communaywen.core.teams.utils.TeamUtils; +import fr.communaywen.core.utils.CommandUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; -import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; @@ -18,13 +20,15 @@ import java.util.Map; import java.util.UUID; -public class MemberListMenu extends Menu { +public class TeamMenu extends Menu { private final Team team; + private final boolean fromList; - public MemberListMenu(Player owner, Team team) { + public TeamMenu(Player owner, Team team, boolean fromList) { super(owner); this.team = team; + this.fromList = fromList; } @Override @@ -45,9 +49,35 @@ public void onInventoryClick(InventoryClickEvent inventoryClickEvent) { @Override public @NotNull Map getContent() { Map content = fill(Material.GRAY_STAINED_GLASS_PANE); - content.put(49, new ItemBuilder(this, Material.ARROW, itemMeta -> { - itemMeta.setDisplayName(ChatColor.GRAY + "Retour"); - }).setBackButton()); + if (fromList) { + content.put(49, new ItemBuilder(this, Material.ARROW, itemMeta -> { + itemMeta.setDisplayName(ChatColor.GRAY + "Retour"); + }).setBackButton()); + } else { + content.put(49, new ItemBuilder(this, Material.BARRIER, itemMeta -> { + itemMeta.setDisplayName(ChatColor.RED + "Fermer"); + }).setCloseButton()); + } + if (team.isIn(getOwner().getUniqueId())) { + content.put(45, new ItemBuilder(this, Material.LIME_WOOL, itemMeta -> { + itemMeta.setDisplayName(ChatColor.GREEN + "Vous êtes dans cette équipe"); + })); + content.put(46, new ItemBuilder(this, Material.RED_DYE, itemMeta -> { + itemMeta.setDisplayName(ChatColor.RED + "Quitter l'équipe"); + }).setNextMenu(new ConfirmationMenu(getOwner(), event -> { + TeamUtils.quit(team, getOwner()); + getOwner().closeInventory(); + }))); + content.put(53, new ItemBuilder(this, Material.CHEST, itemMeta -> { + itemMeta.setDisplayName(ChatColor.GOLD + "Inventaire de la team"); + }).setOnClick(event -> { + team.openInventory(getOwner()); + })); + } else { + content.put(45, new ItemBuilder(this, Material.RED_WOOL, itemMeta -> { + itemMeta.setDisplayName(ChatColor.RED + "Vous n'êtes pas dans cette équipe"); + })); + } List firstSixPlayers = team.getPlayers(0, 7); List secondSixPlayers = team.getPlayers(7, 14); List thirdFivePlayers = team.getPlayers(14, 20); diff --git a/src/main/java/fr/communaywen/core/teams/utils/MethodState.java b/src/main/java/fr/communaywen/core/teams/utils/MethodState.java new file mode 100644 index 00000000..7e313edf --- /dev/null +++ b/src/main/java/fr/communaywen/core/teams/utils/MethodState.java @@ -0,0 +1,9 @@ +package fr.communaywen.core.teams.utils; + +public enum MethodState { + + VALID, + WARNING, + INVALID, + +} diff --git a/src/main/java/fr/communaywen/core/teams/utils/TeamUtils.java b/src/main/java/fr/communaywen/core/teams/utils/TeamUtils.java new file mode 100644 index 00000000..ac3570e9 --- /dev/null +++ b/src/main/java/fr/communaywen/core/teams/utils/TeamUtils.java @@ -0,0 +1,29 @@ +package fr.communaywen.core.teams.utils; + +import fr.communaywen.core.teams.Team; +import fr.communaywen.core.utils.CommandUtils; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.entity.Player; + +import java.util.UUID; + +public class TeamUtils { + + public static boolean quit(Team team, Player player) { + MethodState state = team.removePlayer(player.getUniqueId()); + if (state == MethodState.WARNING || state == MethodState.VALID) + CommandUtils.sendMessage(player, "Vous avez quitté la team !", false); + if (state == MethodState.WARNING) + CommandUtils.sendMessage(player, ChatColor.DARK_RED + "La team a été supprimée !", false); + if (state == MethodState.INVALID) + return CommandUtils.sendMessage(player, ChatColor.DARK_RED + "Impossible de quitter, la team serait supprimée et il reste des items dans l'inventaire !", true); + for (UUID teamPlayer : team.getPlayers()) { + Player teamPlayerOnline = Bukkit.getPlayer(teamPlayer); + if (teamPlayerOnline != null) { + CommandUtils.sendMessage(teamPlayerOnline, player.getName() + " a quitté la team !", false); + } + } + return true; + } +}