Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. package com.hardcorefactions.hcfactions.faction.argument;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.UUID;
  8.  
  9. import org.bukkit.Bukkit;
  10. import org.bukkit.ChatColor;
  11. import org.bukkit.command.Command;
  12. import org.bukkit.command.CommandSender;
  13. import org.bukkit.entity.Player;
  14. import org.bukkit.event.Event;
  15.  
  16. import com.hardcorefactions.hcfactions.HCF;
  17. import com.hardcorefactions.hcfactions.faction.event.FactionRelationCreateEvent;
  18. import com.hardcorefactions.hcfactions.faction.struct.Relation;
  19. import com.hardcorefactions.hcfactions.faction.struct.Role;
  20. import com.hardcorefactions.hcfactions.faction.type.Faction;
  21. import com.hardcorefactions.hcfactions.faction.type.PlayerFaction;
  22. import com.hardcorefactions.hcfactions.utilities.ConfigurationService;
  23. import com.hardcorefactions.util.command.CommandArgument;
  24.  
  25. public class FactionAllyArgument extends CommandArgument
  26. {
  27. private static final Relation RELATION;
  28. private final HCF plugin;
  29.  
  30. static {
  31. RELATION = Relation.ALLY;
  32. }
  33.  
  34. public FactionAllyArgument(final HCF plugin) {
  35. super("ally", "Make an ally pact with other factions.", new String[] { "alliance" });
  36. this.plugin = plugin;
  37. }
  38.  
  39. @Override
  40. public String getUsage(final String label) {
  41. return String.valueOf('/') + label + ' ' + this.getName() + " <factionName>";
  42. }
  43.  
  44. @SuppressWarnings("deprecation")
  45. @Override
  46. public boolean onCommand(final CommandSender sender, final Command command, final String label, final String[] args) {
  47. if (!(sender instanceof Player)) {
  48. sender.sendMessage(ChatColor.RED + "This command is only executable by players.");
  49. return true;
  50. }
  51. if (ConfigurationService.MAX_ALLIES_PER_FACTION <= 0) {
  52. sender.sendMessage(ChatColor.RED + "Sorry but allies are disabled this map.");
  53. return true;
  54. }
  55. if (args.length < 2) {
  56. sender.sendMessage(ChatColor.YELLOW + "Usage: " + this.getUsage(label));
  57. return true;
  58. }
  59. final Player player = (Player)sender;
  60. final PlayerFaction playerFaction = this.plugin.getFactionManager().getPlayerFaction(player);
  61. if (playerFaction == null) {
  62. sender.sendMessage(ChatColor.RED + "You are not in a faction.");
  63. return true;
  64. }
  65. if (playerFaction.getMember(player.getUniqueId()).getRole() == Role.MEMBER) {
  66. sender.sendMessage(ChatColor.RED + "You must be an officer to send ally requests.");
  67. return true;
  68. }
  69. final Faction containingFaction = this.plugin.getFactionManager().getContainingFaction(args[1]);
  70. if (!(containingFaction instanceof PlayerFaction)) {
  71. sender.sendMessage(ChatColor.RED + "Player faction named or containing member with IGN or UUID " + args[1] + " not found.");
  72. return true;
  73. }
  74. final PlayerFaction targetFaction = (PlayerFaction)containingFaction;
  75. if (playerFaction == targetFaction) {
  76. sender.sendMessage(ChatColor.RED + "You cannot send " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.RED + " requests to your own faction.");
  77. return true;
  78. }
  79. final Collection<UUID> allied = playerFaction.getAllied();
  80. if (allied.size() >= ConfigurationService.MAX_ALLIES_PER_FACTION) {
  81. sender.sendMessage(ChatColor.RED + "Your faction already has reached the alliance limit, which is " + ConfigurationService.MAX_ALLIES_PER_FACTION + '.');
  82. return true;
  83. }
  84. if (targetFaction.getAllied().size() >= ConfigurationService.MAX_ALLIES_PER_FACTION) {
  85. sender.sendMessage(String.valueOf(targetFaction.getDisplayName(sender)) + ChatColor.RED + " has reached their maximum alliance limit, which is " + ConfigurationService.MAX_ALLIES_PER_FACTION + '.');
  86. return true;
  87. }
  88. if (allied.contains(targetFaction.getUniqueID())) {
  89. sender.sendMessage(ChatColor.RED + "Your faction already is " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.RED + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.RED + '.');
  90. return true;
  91. }
  92. if (targetFaction.getRequestedRelations().remove(playerFaction.getUniqueID()) != null) {
  93. final FactionRelationCreateEvent event = new FactionRelationCreateEvent(playerFaction, targetFaction, FactionAllyArgument.RELATION);
  94. Bukkit.getPluginManager().callEvent((Event)event);
  95. targetFaction.getRelations().put(playerFaction.getUniqueID(), FactionAllyArgument.RELATION);
  96. targetFaction.broadcast(ChatColor.YELLOW + "Your faction is now " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.YELLOW + " with " + playerFaction.getDisplayName(targetFaction) + ChatColor.YELLOW + '.');
  97. playerFaction.getRelations().put(targetFaction.getUniqueID(), FactionAllyArgument.RELATION);
  98. playerFaction.broadcast(ChatColor.YELLOW + "Your faction is now " + FactionAllyArgument.RELATION.getDisplayName() + 'd' + ChatColor.YELLOW + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.YELLOW + '.');
  99. return true;
  100. }
  101. if (playerFaction.getRequestedRelations().putIfAbsent(targetFaction.getUniqueID(), FactionAllyArgument.RELATION) != null) {
  102. sender.sendMessage(ChatColor.YELLOW + "Your faction has already requested to " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.YELLOW + " with " + targetFaction.getDisplayName(playerFaction) + ChatColor.YELLOW + '.');
  103. return true;
  104. }
  105. playerFaction.broadcast(String.valueOf(targetFaction.getDisplayName(playerFaction)) + ChatColor.YELLOW + " were informed that you wish to be " + FactionAllyArgument.RELATION.getDisplayName() + ChatColor.YELLOW + '.');
  106. 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.");
  107. return true;
  108. }
  109.  
  110. @SuppressWarnings("deprecation")
  111. @Override
  112. public List<String> onTabComplete(final CommandSender sender, final Command command, final String label, final String[] args) {
  113. if (args.length != 2 || !(sender instanceof Player)) {
  114. return Collections.emptyList();
  115. }
  116. final Player player = (Player)sender;
  117. final PlayerFaction playerFaction = this.plugin.getFactionManager().getPlayerFaction(player);
  118. if (playerFaction == null) {
  119. return Collections.emptyList();
  120. }
  121. final List<String> results = new ArrayList<String>();
  122. Player[] onlinePlayers;
  123. for (int length = (onlinePlayers = Bukkit.getOnlinePlayers()).length, i = 0; i < length; ++i) {
  124. final Player target = onlinePlayers[i];
  125. if (!target.equals(player) && player.canSee(target) && !results.contains(target.getName())) {
  126. final Faction targetFaction = this.plugin.getFactionManager().getPlayerFaction(target);
  127. if (targetFaction != null && playerFaction != targetFaction && playerFaction.getRequestedRelations().get(targetFaction.getUniqueID()) != FactionAllyArgument.RELATION && playerFaction.getRelations().get(targetFaction.getUniqueID()) != FactionAllyArgument.RELATION) {
  128. results.add(targetFaction.getName());
  129. }
  130. }
  131. }
  132. return results;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement