Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.hardcorefactions.hcfactions.faction.argument;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.List;
- import java.util.UUID;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.event.Event;
- import com.hardcorefactions.hcfactions.HCF;
- import com.hardcorefactions.hcfactions.faction.event.FactionRelationCreateEvent;
- import com.hardcorefactions.hcfactions.faction.struct.Relation;
- import com.hardcorefactions.hcfactions.faction.struct.Role;
- import com.hardcorefactions.hcfactions.faction.type.Faction;
- import com.hardcorefactions.hcfactions.faction.type.PlayerFaction;
- import com.hardcorefactions.hcfactions.utilities.ConfigurationService;
- import com.hardcorefactions.util.command.CommandArgument;
- public class FactionAllyArgument extends CommandArgument
- {
- private static final Relation RELATION;
- private final HCF plugin;
- static {
- RELATION = Relation.ALLY;
- }
- public FactionAllyArgument(final HCF plugin) {
- super("ally", "Make an ally pact with other factions.", new String[] { "alliance" });
- this.plugin = plugin;
- }
- @Override
- public String getUsage(final String label) {
- return String.valueOf('/') + label + ' ' + this.getName() + " <factionName>";
- }
- @SuppressWarnings("deprecation")
- @Override
- public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
- if (!(sender instanceof Player)) {
- sender.sendMessage(ChatColor.RED + "This command is only executable by players.");
- return true;
- }
- if (ConfigurationService.MAX_ALLIES_PER_FACTION <= 0) {
- sender.sendMessage(ChatColor.RED + "Sorry but allies are disabled this map.");
- return true;
- }
- if (args.length < 2) {
- sender.sendMessage(ChatColor.YELLOW + "Usage: " + this.getUsage(label));
- return true;
- }
- final Player player = (Player)sender;
- final PlayerFaction playerFaction = this.plugin.getFactionManager().getPlayerFaction(player);
- if (playerFaction == null) {
- sender.sendMessage(ChatColor.RED + "You are not in a faction.");
- return true;
- }
- if (playerFaction.getMember(player.getUniqueId()).getRole() == Role.MEMBER) {
- sender.sendMessage(ChatColor.RED + "You must be an officer to send ally requests.");
- return true;
- }
- final Faction containingFaction = this.plugin.getFactionManager().getContainingFaction(args[1]);
- if (!(containingFaction instanceof PlayerFaction)) {
- sender.sendMessage(ChatColor.RED + "Player faction named or containing member with IGN or UUID " + args[1] + " not found.");
- return true;
- }
- final PlayerFaction targetFaction = (PlayerFaction)containingFaction;
- if (playerFaction == targetFaction) {
- sender.sendMessage(ChatColor.RED + "You cannot send " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.RED + " requests to your own faction.");
- return true;
- }
- final Collection<UUID> allied = playerFaction.getAllied();
- if (allied.size() >= ConfigurationService.MAX_ALLIES_PER_FACTION) {
- sender.sendMessage(ChatColor.RED + "Your faction already has reached the alliance limit, which is " + ConfigurationService.MAX_ALLIES_PER_FACTION + '.');
- return true;
- }
- if (targetFaction.getAllied().size() >= ConfigurationService.MAX_ALLIES_PER_FACTION) {
- sender.sendMessage(String.valueOf(targetFaction.getDisplayName(sender)) + ChatColor.RED + " has reached their maximum alliance limit, which is " + ConfigurationService.MAX_ALLIES_PER_FACTION + '.');
- return true;
- }
- if (allied.contains(targetFaction.getUniqueID())) {
- sender.sendMessage(ChatColor.RED + "Your faction already is " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.RED + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.RED + '.');
- return true;
- }
- if (targetFaction.getRequestedRelations().remove(playerFaction.getUniqueID()) != null) {
- final FactionRelationCreateEvent event = new FactionRelationCreateEvent(playerFaction, targetFaction, FactionAllyArgument.RELATION);
- Bukkit.getPluginManager().callEvent((Event)event);
- targetFaction.getRelations().put(playerFaction.getUniqueID(), FactionAllyArgument.RELATION);
- targetFaction.broadcast(ChatColor.YELLOW + "Your faction is now " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.YELLOW + " with " + playerFaction.getDisplayName(targetFaction) + ChatColor.YELLOW + '.');
- playerFaction.getRelations().put(targetFaction.getUniqueID(), FactionAllyArgument.RELATION);
- playerFaction.broadcast(ChatColor.YELLOW + "Your faction is now " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.YELLOW + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.YELLOW + '.');
- return true;
- }
- if (playerFaction.getRequestedRelations().putIfAbsent(targetFaction.getUniqueID(), FactionAllyArgument.RELATION) != null) {
- sender.sendMessage(ChatColor.YELLOW + "Your faction has already requested to " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.YELLOW + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.YELLOW + '.');
- return true;
- }
- playerFaction.broadcast(String.valueOf(targetFaction.getDisplayName(playerFaction)) + ChatColor.YELLOW + " were informed that you wish to be " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.YELLOW + '.');
- targetFaction.broadcast(String.valueOf(playerFaction.getDisplayName(targetFaction)) + ChatColor.YELLOW + " has sent a request to be " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.YELLOW + ". Use " + ConfigurationService.ALLY_COLOUR + "/faction " + this.getName() + ' ' + playerFaction.getName() + ChatColor.YELLOW + " to accept.");
- return true;
- }
- @SuppressWarnings("deprecation")
- @Override
- public List<String> onTabComplete(final CommandSender sender, final Command command, final String label, final String[] args) {
- if (args.length != 2 || !(sender instanceof Player)) {
- return Collections.emptyList();
- }
- final Player player = (Player)sender;
- final PlayerFaction playerFaction = this.plugin.getFactionManager().getPlayerFaction(player);
- if (playerFaction == null) {
- return Collections.emptyList();
- }
- final List<String> results = new ArrayList<String>();
- Player[] onlinePlayers;
- for (int length = (onlinePlayers = Bukkit.getOnlinePlayers()).length, i = 0; i < length; ++i) {
- final Player target = onlinePlayers[i];
- if (!target.equals(player) && player.canSee(target) && !results.contains(target.getName())) {
- final Faction targetFaction = this.plugin.getFactionManager().getPlayerFaction(target);
- if (targetFaction != null && playerFaction != targetFaction && playerFaction.getRequestedRelations().get(targetFaction.getUniqueID()) != FactionAllyArgument.RELATION && playerFaction.getRelations().get(targetFaction.getUniqueID()) != FactionAllyArgument.RELATION) {
- results.add(targetFaction.getName());
- }
- }
- }
- return results;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement