Skip to content

Commit

Permalink
You can cancel an invitation
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya committed Jan 5, 2025
1 parent ff5cbc2 commit c81add5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@ public class InviteCacheExecution {

private static final ConcurrentHashMap<UUID, CopyOnWriteArrayList<UUID>> invitePending = new ConcurrentHashMap<>();

public static boolean isInvitedCache(UUID islandId, UUID playerId) {
CopyOnWriteArrayList<UUID> playerIdList = invitePending.get(islandId);
return playerIdList != null && playerIdList.contains(playerId);
public static synchronized boolean isInvitedCache(UUID islandId, UUID playerId) {
return invitePending.computeIfAbsent(islandId, k -> new CopyOnWriteArrayList<>()).contains(playerId);
}

public static synchronized void addInviteCache(UUID islandId, UUID playerId) {
invitePending.computeIfAbsent(islandId, k -> new CopyOnWriteArrayList<>()).add(playerId);
}

public static synchronized void removeInviteCache(UUID islandId, UUID playerId) {
CopyOnWriteArrayList<UUID> playerIdList = invitePending.get(islandId);
if (playerIdList != null) {
playerIdList.remove(playerId);
if (playerIdList.isEmpty()) {
invitePending.remove(islandId);
}
}
invitePending.computeIfAbsent(islandId, k -> new CopyOnWriteArrayList<>()).remove(playerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,43 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
return true;
}
String type = args[0];
Main skyblock = Main.getPlugin(Main.class);
if (type.equalsIgnoreCase("add")) {
if (args.length < 2) {
LanguageToml.sendMessage(player, LanguageToml.messageInviteAddCommandNotEnoughArgs);
return true;
}
String playerOrOwner = args[1];
invitePlayer(Main.getPlugin(Main.class), player, playerOrOwner);
invitePlayer(skyblock, player, playerOrOwner);
} else if (type.equalsIgnoreCase("accept")) {
if (args.length < 2) {
LanguageToml.sendMessage(player, LanguageToml.messageInviteAcceptCommandNotEnoughArgs);
return true;
}
String playerOrOwner = args[1];
acceptPlayer(Main.getPlugin(Main.class), player, playerOrOwner);
acceptPlayer(skyblock, player, playerOrOwner);
} else if (type.equalsIgnoreCase("decline")) {
if (args.length < 2) {
LanguageToml.sendMessage(player, LanguageToml.messageInviteDeclineCommandNotEnoughArgs);
return true;
}
String playerOrOwner = args[1];
declinePlayer(Main.getPlugin(Main.class), player, playerOrOwner);
declinePlayer(skyblock, player, playerOrOwner);
} else if (type.equalsIgnoreCase("delete")) {
if (args.length < 2) {
LanguageToml.sendMessage(player, LanguageToml.messageInviteRemoveCommandNotEnoughArgs);
return true;
}
String playerOrOwner = args[1];
deleteInvitePlayer(skyblock, player, playerOrOwner);
}
return true;
}

@Override
public @NotNull List<String> onTabComplete(@NotNull Plugin plugin, @NotNull CommandSender sender, @NotNull String[] args) {
if (args.length == 1) {
List<String> possible = List.of("accept", "decline", "add");
List<String> possible = List.of("accept", "decline", "add", "delete");
String partial = args[0].trim().toLowerCase();
return possible.stream()
.filter(cmd -> cmd.toLowerCase().startsWith(partial))
Expand All @@ -89,9 +97,32 @@ public boolean onCommand(@NotNull Plugin plugin, @NotNull CommandSender sender,
return Collections.emptyList();
}

private void deleteInvitePlayer(Main plugin, Player ownerIsland, String playerInvited) {
SkyblockManager skyblockManager = plugin.getInterneAPI().getSkyblockManager();
Island island = skyblockManager.getIslandByPlayerId(ownerIsland.getUniqueId()).join();
if (island == null) {
LanguageToml.sendMessage(ownerIsland, LanguageToml.messagePlayerHasNotIsland);
return;
}
Players executorPlayer = island.getMember(ownerIsland.getUniqueId());

if (!PermissionsManagers.testPermissions(executorPlayer, ownerIsland, island, PermissionsCommandIsland.INVITE, false)) {
return;
}

UUID playerInvitedId = Bukkit.getPlayerUniqueId(playerInvited);
if (playerInvitedId == null) {
LanguageToml.sendMessage(ownerIsland, LanguageToml.messagePlayerNotFound);
return;
}

InviteCacheExecution.removeInviteCache(island.getId(), playerInvitedId);
LanguageToml.sendMessage(ownerIsland, LanguageToml.messageInviteDeletePlayerInvited.formatted(playerInvited));
}

private void invitePlayer(Main plugin, Player ownerIsland, String playerInvited) {
try {
SkyblockManager skyblockManager = Main.getPlugin(Main.class).getInterneAPI().getSkyblockManager();
SkyblockManager skyblockManager = plugin.getInterneAPI().getSkyblockManager();
Island island = skyblockManager.getIslandByPlayerId(ownerIsland.getUniqueId()).join();
if (island == null) {
LanguageToml.sendMessage(ownerIsland, LanguageToml.messagePlayerHasNotIsland);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public class LanguageToml {
public static String messageInviteAlreadyIsland = "You are already on an island!";
public static String messageInviteCommandNotEnoughArgs = "The command is incomplete: /skyllia invite <add/accept/decline> <player/island_owner>";
public static String messageInviteAcceptCommandNotEnoughArgs = "You must specify which island you want to join: /skyllia invite accept <island_owner>";
public static String messageInviteRemoveCommandNotEnoughArgs = "The command is incomplete: /skyllia invite remove <island_owner>";
public static String messageInviteDeclineCommandNotEnoughArgs = "You must specify which island you want to decline: /skyllia invite decline <island_owner>";
public static String messageInviteAddCommandNotEnoughArgs = "You must specify which island you want to decline: /skyllia invite add <player>";
public static String messageInvitePlayerInvited = "The player %s has been invited. Awaiting a response...";
public static String messageInviteDeletePlayerInvited = "You have deleted %s's invitation.";
public static String messageInvitePlayerNotification = "The player %player_invite% has invited you to their island. To accept: /skyllia invite accept %player_invite%. To decline: /skyllia invite decline %player_invite%";
public static String messageInviteAcceptOwnerHasNotIsland = "The island of the player %s was not found.";
public static String messageInviteDeclineOwnerHasNotIsland = "The island of the player %s was not found.";
Expand Down

0 comments on commit c81add5

Please sign in to comment.