diff --git a/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java b/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java index 41af99ec..1e31629a 100644 --- a/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/FriendsCommand.java @@ -2,70 +2,73 @@ package net.Indyuce.mmocore.command; import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.event.MMOCommandEvent; -import net.Indyuce.mmocore.manager.InventoryManager; -import net.Indyuce.mmocore.api.player.OfflinePlayerData; import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.social.FriendRequest; import net.Indyuce.mmocore.api.player.social.Request; +import net.Indyuce.mmocore.manager.InventoryManager; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; import java.util.UUID; public class FriendsCommand extends BukkitCommand { - public FriendsCommand(ConfigurationSection config) { - super(config.getString("main")); - - setAliases(config.getStringList("aliases")); - setDescription("Opens the friends menu."); - } + public FriendsCommand(ConfigurationSection config) { + super(config.getString("main")); - @Override - public boolean execute(CommandSender sender, String label, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "This command is for players only."); - return true; - } + setAliases(config.getStringList("aliases")); + setDescription("Opens the friends menu."); + } - PlayerData data = PlayerData.get((Player) sender); - MMOCommandEvent event = new MMOCommandEvent(data, "friends"); - Bukkit.getServer().getPluginManager().callEvent(event); - if(event.isCancelled()) return true; - - if (args.length > 1) { - UUID uuid; - try { - uuid = UUID.fromString(args[1]); - } catch (Exception e) { - return true; - } + @Override + public boolean execute(CommandSender sender, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "This command is for players only."); + return true; + } - Request request = MMOCore.plugin.requestManager.getRequest(uuid); - if (!(request instanceof FriendRequest)) - return true; + PlayerData data = PlayerData.get((Player) sender); + MMOCommandEvent event = new MMOCommandEvent(data, "friends"); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) return true; - if (request.isTimedOut()) { - MMOCore.plugin.requestManager.unregisterRequest(uuid); - return true; - } + if (args.length > 1) { - if (OfflinePlayerData.get(((Player) sender).getUniqueId()).hasFriend(uuid)) { - MMOCore.plugin.requestManager.unregisterRequest(uuid); - return true; - } + final @Nullable FriendRequest invite; + if (args.length > 1) - if (args[0].equalsIgnoreCase("accept")) - request.accept(); - if (args[0].equalsIgnoreCase("deny")) - request.deny(); - return true; - } + // Search by request ID + try { + final UUID uuid = UUID.fromString(args[1]); + final Request req = MMOCore.plugin.requestManager.getRequest(uuid); + Validate.isTrue(!req.isTimedOut() && req instanceof FriendRequest); + Validate.isTrue(!data.hasFriend(req.getCreator().getUniqueId())); + invite = (FriendRequest) req; + } catch (Exception exception) { + return true; + } - InventoryManager.FRIEND_LIST.newInventory(data).open(); - return true; - } + // Search by target player + else + invite = MMOCore.plugin.requestManager.findRequest(data, FriendRequest.class); + + // No invite found with given identifier/target player + if (invite == null) + return true; + + if (args[0].equalsIgnoreCase("accept")) + invite.accept(); + if (args[0].equalsIgnoreCase("deny")) + invite.deny(); + return true; + } + + InventoryManager.FRIEND_LIST.newInventory(data).open(); + return true; + } } diff --git a/src/main/java/net/Indyuce/mmocore/command/GuildCommand.java b/src/main/java/net/Indyuce/mmocore/command/GuildCommand.java index c67f8b85..f067ffbc 100644 --- a/src/main/java/net/Indyuce/mmocore/command/GuildCommand.java +++ b/src/main/java/net/Indyuce/mmocore/command/GuildCommand.java @@ -1,10 +1,12 @@ package net.Indyuce.mmocore.command; -import java.util.UUID; - import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.api.event.MMOCommandEvent; +import net.Indyuce.mmocore.api.player.PlayerData; +import net.Indyuce.mmocore.api.player.social.Request; +import net.Indyuce.mmocore.guild.provided.GuildInvite; import net.Indyuce.mmocore.manager.InventoryManager; +import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; @@ -12,65 +14,66 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; -import net.Indyuce.mmocore.api.player.PlayerData; -import net.Indyuce.mmocore.api.player.social.Request; -import net.Indyuce.mmocore.guild.provided.GuildInvite; +import java.util.UUID; public class GuildCommand extends BukkitCommand { - public GuildCommand(ConfigurationSection config) { - super(config.getString("main")); - - setAliases(config.getStringList("aliases")); - setDescription("Opens the guilds menu."); - } + public GuildCommand(ConfigurationSection config) { + super(config.getString("main")); - @Override - public boolean execute(CommandSender sender, String label, String[] args) { - if (!(sender instanceof Player)) { - sender.sendMessage(ChatColor.RED + "This command is for players only."); - return true; - } + setAliases(config.getStringList("aliases")); + setDescription("Opens the guilds menu."); + } - PlayerData data = PlayerData.get((OfflinePlayer) sender); - MMOCommandEvent event = new MMOCommandEvent(data, "guild"); - Bukkit.getServer().getPluginManager().callEvent(event); - if(event.isCancelled()) return true; - - if (args.length > 1) { - UUID uuid; - try { - uuid = UUID.fromString(args[1]); - } catch (Exception e) { - return true; - } + @Override + public boolean execute(CommandSender sender, String label, String[] args) { + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "This command is for players only."); + return true; + } - Request request = MMOCore.plugin.requestManager.getRequest(uuid); - if (!(request instanceof GuildInvite)) - return true; + PlayerData data = PlayerData.get((OfflinePlayer) sender); + MMOCommandEvent event = new MMOCommandEvent(data, "guild"); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) return true; - if (request.isTimedOut()) { - MMOCore.plugin.requestManager.unregisterRequest(uuid); - return true; - } + if (args.length > 1) { - if (!MMOCore.plugin.dataProvider.getGuildManager().isRegistered(((GuildInvite) request).getGuild())) { - MMOCore.plugin.requestManager.unregisterRequest(uuid); - return true; - } + final @Nullable GuildInvite invite; + if (args.length > 1) - if (args[0].equalsIgnoreCase("accept")) - request.accept(); - if (args[0].equalsIgnoreCase("deny")) - request.deny(); - return true; - } + // Search by request ID + try { + final UUID uuid = UUID.fromString(args[1]); + final Request req = MMOCore.plugin.requestManager.getRequest(uuid); + Validate.isTrue(!req.isTimedOut() && req instanceof GuildInvite); + invite = (GuildInvite) req; + Validate.isTrue(MMOCore.plugin.dataProvider.getGuildManager().isRegistered(invite.getGuild())); + } catch (Exception exception) { + return true; + } - if (data.inGuild()) - InventoryManager.GUILD_VIEW.newInventory(data).open(); - else - InventoryManager.GUILD_CREATION.newInventory(data).open(); - return true; - } + // Search by target player + else + invite = MMOCore.plugin.requestManager.findRequest(data, GuildInvite.class); + + // No invite found with given identifier/target player + if (invite == null) + return true; + + if (args[0].equalsIgnoreCase("accept")) + invite.accept(); + if (args[0].equalsIgnoreCase("deny")) + invite.deny(); + return true; + } + + if (data.inGuild()) + InventoryManager.GUILD_VIEW.newInventory(data).open(); + else + InventoryManager.GUILD_CREATION.newInventory(data).open(); + return true; + } }