Added /friend accept and /guild accept

This commit is contained in:
Indyuce 2022-08-01 14:36:09 +02:00
parent a4eada2593
commit 32dd296eaa
2 changed files with 104 additions and 98 deletions

View File

@ -2,17 +2,18 @@ package net.Indyuce.mmocore.command;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.MMOCommandEvent; 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.PlayerData;
import net.Indyuce.mmocore.api.player.social.FriendRequest; import net.Indyuce.mmocore.api.player.social.FriendRequest;
import net.Indyuce.mmocore.api.player.social.Request; 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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import java.util.UUID; import java.util.UUID;
@ -37,31 +38,33 @@ public class FriendsCommand extends BukkitCommand {
if (event.isCancelled()) return true; if (event.isCancelled()) return true;
if (args.length > 1) { if (args.length > 1) {
UUID uuid;
final @Nullable FriendRequest invite;
if (args.length > 1)
// Search by request ID
try { try {
uuid = UUID.fromString(args[1]); final UUID uuid = UUID.fromString(args[1]);
} catch (Exception e) { 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; return true;
} }
Request request = MMOCore.plugin.requestManager.getRequest(uuid); // Search by target player
if (!(request instanceof FriendRequest)) else
return true; invite = MMOCore.plugin.requestManager.findRequest(data, FriendRequest.class);
if (request.isTimedOut()) { // No invite found with given identifier/target player
MMOCore.plugin.requestManager.unregisterRequest(uuid); if (invite == null)
return true; return true;
}
if (OfflinePlayerData.get(((Player) sender).getUniqueId()).hasFriend(uuid)) {
MMOCore.plugin.requestManager.unregisterRequest(uuid);
return true;
}
if (args[0].equalsIgnoreCase("accept")) if (args[0].equalsIgnoreCase("accept"))
request.accept(); invite.accept();
if (args[0].equalsIgnoreCase("deny")) if (args[0].equalsIgnoreCase("deny"))
request.deny(); invite.deny();
return true; return true;
} }

View File

@ -1,10 +1,12 @@
package net.Indyuce.mmocore.command; package net.Indyuce.mmocore.command;
import java.util.UUID;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.MMOCommandEvent; 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 net.Indyuce.mmocore.manager.InventoryManager;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -12,10 +14,9 @@ import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;
import net.Indyuce.mmocore.api.player.PlayerData; import java.util.UUID;
import net.Indyuce.mmocore.api.player.social.Request;
import net.Indyuce.mmocore.guild.provided.GuildInvite;
public class GuildCommand extends BukkitCommand { public class GuildCommand extends BukkitCommand {
@ -39,31 +40,33 @@ public class GuildCommand extends BukkitCommand {
if (event.isCancelled()) return true; if (event.isCancelled()) return true;
if (args.length > 1) { if (args.length > 1) {
UUID uuid;
final @Nullable GuildInvite invite;
if (args.length > 1)
// Search by request ID
try { try {
uuid = UUID.fromString(args[1]); final UUID uuid = UUID.fromString(args[1]);
} catch (Exception e) { 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; return true;
} }
Request request = MMOCore.plugin.requestManager.getRequest(uuid); // Search by target player
if (!(request instanceof GuildInvite)) else
return true; invite = MMOCore.plugin.requestManager.findRequest(data, GuildInvite.class);
if (request.isTimedOut()) { // No invite found with given identifier/target player
MMOCore.plugin.requestManager.unregisterRequest(uuid); if (invite == null)
return true; return true;
}
if (!MMOCore.plugin.dataProvider.getGuildManager().isRegistered(((GuildInvite) request).getGuild())) {
MMOCore.plugin.requestManager.unregisterRequest(uuid);
return true;
}
if (args[0].equalsIgnoreCase("accept")) if (args[0].equalsIgnoreCase("accept"))
request.accept(); invite.accept();
if (args[0].equalsIgnoreCase("deny")) if (args[0].equalsIgnoreCase("deny"))
request.deny(); invite.deny();
return true; return true;
} }