Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more menus and checks #59

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions src/main/java/fr/communaywen/core/commands/TeamCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <create|list|invite|accept|leave|kick>", 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 <nom de la team>", false);
return CommandUtils.sendMessage(sender, ChatColor.WHITE + "Usage: /team create <nom de la team>", true);
}
if (args[0].equalsIgnoreCase("list")) {
Menu menu = new TeamListMenu(player, teamManager);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/fr/communaywen/core/teams/Team.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -44,10 +46,18 @@ public List<UUID> 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) {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/fr/communaywen/core/teams/TeamManager.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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) {
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/fr/communaywen/core/teams/menu/ConfirmationMenu.java
Original file line number Diff line number Diff line change
@@ -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<InventoryClickEvent> toConfirm;

public ConfirmationMenu(Player owner, Consumer<InventoryClickEvent> 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<Integer, ItemStack> getContent() {
Map<Integer, ItemStack> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -45,9 +49,35 @@ public void onInventoryClick(InventoryClickEvent inventoryClickEvent) {
@Override
public @NotNull Map<Integer, ItemStack> getContent() {
Map<Integer, ItemStack> 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<UUID> firstSixPlayers = team.getPlayers(0, 7);
List<UUID> secondSixPlayers = team.getPlayers(7, 14);
List<UUID> thirdFivePlayers = team.getPlayers(14, 20);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.communaywen.core.teams.utils;

public enum MethodState {

VALID,
WARNING,
INVALID,

}
29 changes: 29 additions & 0 deletions src/main/java/fr/communaywen/core/teams/utils/TeamUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading