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,70 +2,73 @@ 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;
public class FriendsCommand extends BukkitCommand { public class FriendsCommand extends BukkitCommand {
public FriendsCommand(ConfigurationSection config) { public FriendsCommand(ConfigurationSection config) {
super(config.getString("main")); super(config.getString("main"));
setAliases(config.getStringList("aliases"));
setDescription("Opens the friends menu.");
}
@Override setAliases(config.getStringList("aliases"));
public boolean execute(CommandSender sender, String label, String[] args) { setDescription("Opens the friends menu.");
if (!(sender instanceof Player)) { }
sender.sendMessage(ChatColor.RED + "This command is for players only.");
return true;
}
PlayerData data = PlayerData.get((Player) sender); @Override
MMOCommandEvent event = new MMOCommandEvent(data, "friends"); public boolean execute(CommandSender sender, String label, String[] args) {
Bukkit.getServer().getPluginManager().callEvent(event); if (!(sender instanceof Player)) {
if(event.isCancelled()) return true; sender.sendMessage(ChatColor.RED + "This command is for players only.");
return true;
if (args.length > 1) { }
UUID uuid;
try {
uuid = UUID.fromString(args[1]);
} catch (Exception e) {
return true;
}
Request request = MMOCore.plugin.requestManager.getRequest(uuid); PlayerData data = PlayerData.get((Player) sender);
if (!(request instanceof FriendRequest)) MMOCommandEvent event = new MMOCommandEvent(data, "friends");
return true; Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return true;
if (request.isTimedOut()) { if (args.length > 1) {
MMOCore.plugin.requestManager.unregisterRequest(uuid);
return true;
}
if (OfflinePlayerData.get(((Player) sender).getUniqueId()).hasFriend(uuid)) { final @Nullable FriendRequest invite;
MMOCore.plugin.requestManager.unregisterRequest(uuid); if (args.length > 1)
return true;
}
if (args[0].equalsIgnoreCase("accept")) // Search by request ID
request.accept(); try {
if (args[0].equalsIgnoreCase("deny")) final UUID uuid = UUID.fromString(args[1]);
request.deny(); final Request req = MMOCore.plugin.requestManager.getRequest(uuid);
return true; 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(); // Search by target player
return true; 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;
}
} }

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,65 +14,66 @@ 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 {
public GuildCommand(ConfigurationSection config) { public GuildCommand(ConfigurationSection config) {
super(config.getString("main")); super(config.getString("main"));
setAliases(config.getStringList("aliases"));
setDescription("Opens the guilds menu.");
}
@Override setAliases(config.getStringList("aliases"));
public boolean execute(CommandSender sender, String label, String[] args) { setDescription("Opens the guilds menu.");
if (!(sender instanceof Player)) { }
sender.sendMessage(ChatColor.RED + "This command is for players only.");
return true;
}
PlayerData data = PlayerData.get((OfflinePlayer) sender); @Override
MMOCommandEvent event = new MMOCommandEvent(data, "guild"); public boolean execute(CommandSender sender, String label, String[] args) {
Bukkit.getServer().getPluginManager().callEvent(event); if (!(sender instanceof Player)) {
if(event.isCancelled()) return true; sender.sendMessage(ChatColor.RED + "This command is for players only.");
return true;
if (args.length > 1) { }
UUID uuid;
try {
uuid = UUID.fromString(args[1]);
} catch (Exception e) {
return true;
}
Request request = MMOCore.plugin.requestManager.getRequest(uuid); PlayerData data = PlayerData.get((OfflinePlayer) sender);
if (!(request instanceof GuildInvite)) MMOCommandEvent event = new MMOCommandEvent(data, "guild");
return true; Bukkit.getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) return true;
if (request.isTimedOut()) { if (args.length > 1) {
MMOCore.plugin.requestManager.unregisterRequest(uuid);
return true;
}
if (!MMOCore.plugin.dataProvider.getGuildManager().isRegistered(((GuildInvite) request).getGuild())) { final @Nullable GuildInvite invite;
MMOCore.plugin.requestManager.unregisterRequest(uuid); if (args.length > 1)
return true;
}
if (args[0].equalsIgnoreCase("accept")) // Search by request ID
request.accept(); try {
if (args[0].equalsIgnoreCase("deny")) final UUID uuid = UUID.fromString(args[1]);
request.deny(); final Request req = MMOCore.plugin.requestManager.getRequest(uuid);
return true; 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()) // Search by target player
InventoryManager.GUILD_VIEW.newInventory(data).open(); else
else invite = MMOCore.plugin.requestManager.findRequest(data, GuildInvite.class);
InventoryManager.GUILD_CREATION.newInventory(data).open();
return true; // 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;
}
} }