Guest User

Untitled

a guest
Mar 9th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.40 KB | None | 0 0
  1. package org.hyperion.rs2.content.clan;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Objects;
  7. import java.util.Optional;
  8. import java.util.function.Consumer;
  9. import java.util.function.Predicate;
  10.  
  11. import org.hyperion.rs2.Misc.SystemClock;
  12. import org.hyperion.rs2.model.Player;
  13. import org.hyperion.rs2.model.World;
  14.  
  15. /**
  16.  * A clan
  17.  *
  18.  * @author Dan0194
  19.  */
  20. public final class Clan {
  21.  
  22.         /**
  23.          * The players clan list
  24.          */
  25.         @SuppressWarnings("serial")
  26.         public ClanList<Player> players = new ClanList<Player>(18144, 18244) {
  27.                
  28.                 /**
  29.                  *
  30.                  * Makes it update for everyone in the clan
  31.                  *
  32.                  * (non-Javadoc)
  33.                  * @see org.hyperion.rs2.content.clan.ClanList#update(org.hyperion.rs2.model.Player)
  34.                  */
  35.                 @Override
  36.                 public void update(Player owner) {
  37.                         stream().forEach((Player p) -> super.update(p));
  38.                 }
  39.                
  40.                 /**
  41.                  * (non-Javadoc)
  42.                  * @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
  43.                  */
  44.                 @Override
  45.                 public void onUpdate(Player owner, Optional<Player> value, int slot) {
  46.                        
  47.                         owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get().getName()).get().getRank().getPosition()  + "> " + value.get().getName() : "");
  48.                        
  49.                 }
  50.         };
  51.        
  52.         /**
  53.          * The banned players clan list
  54.          */
  55.         @SuppressWarnings("serial")
  56.         private ClanList<String> bannedPlayers = new ClanList<String>(18424, 18503) {
  57.  
  58.                 /**
  59.                  * (non-Javadoc)
  60.                  * @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
  61.                  */
  62.                 @Override
  63.                 public void onUpdate(Player owner, Optional<String> value, int slot) {
  64.                         owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get()).get().getRank().getPosition() + "> " + value.get() : "");
  65.                 }
  66.         };
  67.        
  68.         @SuppressWarnings("serial")
  69.         private ClanList<ClanPlayer> clanPlayers = new ClanList<ClanPlayer>(18323, 18422) {
  70.  
  71.                 /**
  72.                  * (non-Javadoc)
  73.                  * @see org.hyperion.rs2.content.clan.ClanList#onUpdate(org.hyperion.rs2.model.Player, java.util.Optional, int)
  74.                  */
  75.                 @Override
  76.                 public void onUpdate(Player owner, Optional<ClanPlayer> value, int slot) {
  77.                         owner.getActionSender().sendString(slot, value.isPresent() ? "<clan=" + getClanPlayerByName(value.get().getName()).get().getRank().getPosition() + "> " + value.get().getName() : "");
  78.                 }
  79.         };
  80.        
  81.         /**
  82.          * The map of all the kicked players in the clan chat
  83.          */
  84.         private Map<String, SystemClock> kickedPlayers = new HashMap<>(ClanConstants.MAX_PLAYERS_IN_CLAN);
  85.  
  86.         /**
  87.          * The rank able enter the chat
  88.          */
  89.         private ClanRank enterRank = ClanRank.NORMAL;
  90.        
  91.         /**
  92.          * The rank able to talk in the chat
  93.          */
  94.         private ClanRank talkRank = ClanRank.NORMAL;
  95.        
  96.         /**
  97.          * The rank able to kick people in the chat
  98.          */
  99.         private ClanRank kickRank = ClanRank.OWNER;
  100.        
  101.         /**
  102.          * The rank able to ban people in the chat
  103.          */
  104.         private ClanRank banRank = ClanRank.OWNER;
  105.        
  106.  
  107.         /**
  108.          * The owner of the clan
  109.          */
  110.         private String owner;
  111.        
  112.        
  113.         /**
  114.          * The title of the clan
  115.          */
  116.         private String title;
  117.        
  118.         /**
  119.          * If the clan is on loot share
  120.          */
  121.         private boolean lootshare;
  122.        
  123.         /**
  124.          * @param owner The owner of the clan
  125.          * @param title The title of the clan
  126.          */
  127.         public Clan(String owner, String title) {
  128.                 this.title = title;
  129.                 this.owner = owner;
  130.         }
  131.        
  132.         /**
  133.          * Gets an optional of a clan player depending on the name
  134.          *
  135.          * @return an optional with the value of clan player or an optional that is empty
  136.          */
  137.         public Optional<ClanPlayer> getClanPlayerByName(String name) {
  138.                 Predicate<ClanPlayer> player = (ClanPlayer c) -> c.getName().equals(name);
  139.                 return clanPlayers.stream().filter(player).findFirst();
  140.         }
  141.        
  142.         /**
  143.          * Gets an optional of a player in a clan chat depending on name
  144.          *
  145.          * @return an optional with the value of the player or an optional that is empty
  146.          */
  147.         public Optional<Player> getPlayerByName(String name) {
  148.                 Predicate<Player> player = (Player p) -> p.getName().equals(name);
  149.                 return players.stream().filter(player).findFirst();
  150.         }
  151.        
  152.         /**
  153.          * Kicks a player out of the clan chat
  154.          *
  155.          * @param player The player kicking the player out of the chat
  156.          * @param kickedPlayer The player being kicked out of the chat
  157.          */
  158.         public void kick(Player player, Player kickedPlayer) {
  159.                
  160.                 /** Gets the clan player depending on the name */
  161.                 ClanPlayer clanPlayer = getClanPlayerByName(player.getName()).get();
  162.                
  163.                 /** If the player is too low to kick people out of the clan chat */
  164.                 if(clanPlayer.getRank().getPosition() < kickRank.getPosition()) {
  165.                         player.getActionSender().sendMessage("Your too low of a rank to kick someone out.");
  166.                         return;
  167.                 }
  168.                
  169.                 /** If the player they are trying to kick out is themselves*/
  170.                 if(kickedPlayer.equals(player)) {
  171.                         player.getActionSender().sendMessage("You can't kick yourself out of the chat.");
  172.                         return;
  173.                 }
  174.                
  175.                 /** If the player is trying to kick out the creator of the clan chat */
  176.                 if(kickedPlayer.getName().equals(owner)) {
  177.                         player.getActionSender().sendMessage("You can't kick the creator of the clan.");
  178.                         return;
  179.                 }
  180.                
  181.                 /** Kicks them out of the clan chat */
  182.                 eject(kickedPlayer, "You have been kicked out of the chat.");
  183.                 kickedPlayer.getClanManager().clear();
  184.                 SystemClock clock = new SystemClock(ClanConstants.KICK_TIMER * 1000);
  185.                 kickedPlayers.put(kickedPlayer.getName(), clock);
  186.                 clock.reset();
  187.         }
  188.        
  189.         /**
  190.          * Bans a player from the clan
  191.          *
  192.          * @param the player The player banning the other player
  193.          * @param bannedPlayer The player being banned
  194.          */
  195.         public void ban(Player player, Player bannedPlayer) {
  196.                
  197.                 /** Gets the clan player */
  198.                 ClanPlayer clanPlayer = this.getClanPlayerByName(player.getName()).get();
  199.                
  200.                 /** If the player is too low to ban someone from the chat */
  201.                 if(clanPlayer.getRank().getPosition() < banRank.getPosition()) {
  202.                         player.getActionSender().sendMessage("Your rank is too low to ban someone.");
  203.                         return;
  204.                 }
  205.                
  206.                 /** If the player is trying to ban themselves */
  207.                 if(player.equals(bannedPlayer)) {
  208.                         player.getActionSender().sendMessage("You can't ban yourself.");
  209.                         return;
  210.                 }
  211.                
  212.                 /** If the player is trying to ban the creator of the chat */
  213.                 if(bannedPlayer.getName().equals(owner)) {
  214.                         player.getActionSender().sendMessage("You can't ban the creator of the clan.");
  215.                         return;
  216.                 }
  217.                
  218.                 /** Bans the player from the clan chat*/
  219.                 bannedPlayers.add(bannedPlayer.getName());
  220.                 eject(bannedPlayer, "You have been banned from the chat.");
  221.                 bannedPlayer.getClanManager().clear();
  222.         }
  223.        
  224.         /**
  225.          * Unbans a player from the clan
  226.          *
  227.          * @param owner The creator unbanning the player from the chat
  228.          * @param bannedPlayer The name of the player being unbanned
  229.          */
  230.         public void unban(Player owner, String bannedPlayer) {
  231.                 bannedPlayers.remove(bannedPlayer);
  232.                 bannedPlayers.update(owner);
  233.         }
  234.        
  235.        
  236.         /**
  237.          * Sends message to entire clan chat
  238.          *
  239.          * @param player The player sending the message
  240.          * @param message being sent to entire clan chat
  241.          */
  242.         public void message(Player player, String message) {
  243.                
  244.                 /** Gets the clan player from the player's name */
  245.                 ClanPlayer rank = getClanPlayerByName(player.getName()).get();
  246.                
  247.                 /** If the player is too low rank to talk in the chat */
  248.                 if(rank.getRank().getPosition() < talkRank.getPosition()) {
  249.                         player.getActionSender().sendMessage("Your rank is too low to talk in the chat.");
  250.                         return;
  251.                 }
  252.                
  253.                 /** Sends the message to everyone in the chat */
  254.                 players.stream().filter(Objects::nonNull).forEach((Player p) -> p.getActionSender().sendClanMessage(player.getName(), message, title, player.getRights().toInteger()));
  255.         }
  256.        
  257.         /**
  258.          * Ranks up a player in the clan
  259.          *
  260.          * @param owner The owner who is ranking the player
  261.          * @param clanPlayer the player in the clan who is being ranked
  262.          * @param rank The new rank
  263.          */
  264.         public void rank(Player owner, ClanPlayer clanPlayer, ClanRank rank) {
  265.                
  266.                
  267.                 /** If the owner is trying to rank himself */
  268.                 if(this.owner.equals(clanPlayer.getName())) {
  269.                         owner.getActionSender().sendMessage("You cannot set a rank for yourself!");
  270.                         return;
  271.                 }
  272.                        
  273.                 /** If the owner is trying to demote an admin */
  274.                 if(clanPlayer.getRank().equals(ClanRank.ADMIN)) {
  275.                         owner.getActionSender().sendMessage("You cannot demote an admin.");
  276.                         return;
  277.                 }
  278.                
  279.                 /** If the rank is the same as the Clan player's rank */
  280.                 if(rank.equals(clanPlayer.getRank())) {
  281.                         owner.getActionSender().sendMessage("That player is already that rank.");
  282.                         return;
  283.                 }
  284.                
  285.                 /** Makes sure your not setting the rank to a friend */
  286.                 rank = rank.equals(ClanRank.FRIEND) ? ClanRank.NORMAL : rank;
  287.                        
  288.                 /** Sets the rank and updates all the players in the clan */
  289.                 clanPlayer.setRank(rank);
  290.                 clanPlayers.update(owner);
  291.                
  292.                 /** Gets an optional of the clan player */
  293.                 Optional<Player> rankedPlayer = getPlayerByName(clanPlayer.getName());
  294.                
  295.                 /** If the clan player is in the chat update the entire chat */
  296.                 if(rankedPlayer.isPresent()) {
  297.                         players.update();
  298.                 }
  299.         }
  300.        
  301.         /**
  302.          * Deletes the entire clan
  303.          *
  304.          * @param owner The owner deleting the clan
  305.          */
  306.         public void delete(Player owner) {
  307.                
  308.                 /** Creates a new list of the entire clan to be looped through */
  309.                 new ArrayList<Player>(players).stream().forEach(new Consumer<Player>() {
  310.  
  311.                         @Override
  312.                         public void accept(Player t) {
  313.                                
  314.                                 /** The message the player will be sent */
  315.                                 String message = t.equals(owner) ? "Deleting clan..." : "The clan you were in has been deleted.";
  316.                                
  317.                                 /** Ejects the player out of the clan */
  318.                                 eject(t, message);
  319.                                 t.getClanManager().clear();
  320.                                
  321.                         }
  322.                        
  323.                 });
  324.                
  325.                 /** Removes the clan from the global list of all clans */
  326.                 World.getWorld().getClans().remove(this);
  327.                 owner.getActionSender().sendMessage("You delete the clan.");
  328.                 owner.getClanManager().clear();
  329.         }
  330.        
  331.        
  332.         /**
  333.          * Ejects a player out of the clan
  334.          *
  335.          * @param player The player being ejected out of clan
  336.          * @param message The message that will be sent to the player
  337.          */
  338.         public void eject(Player player, String message) {
  339.                 player.getActionSender().sendMessage(message);
  340.                 players.remove(player);
  341.                 player.getClanManager().setCurrentClan(null);
  342.                 players.update();
  343.         }
  344.        
  345.         /**
  346.          * Sets the new enter rank
  347.          *
  348.          * @param enterRank to the enterRank
  349.          */
  350.         public void setEnterRank(ClanRank enterRank) {
  351.                 this.enterRank = enterRank;
  352.         }
  353.        
  354.         /**
  355.          * Sets the new talk rank
  356.          *
  357.          * @param talkRank to the talkRank
  358.          */
  359.         public void setTalkRank(ClanRank talkRank) {
  360.                 this.talkRank = talkRank;
  361.         }
  362.        
  363.         /**
  364.          * Sets the new kick rank
  365.          *
  366.          * @param kickRank to the kickRank
  367.          */
  368.         public void setKickRank(ClanRank kickRank) {
  369.                 this.kickRank = kickRank;
  370.         }
  371.        
  372.         /**
  373.          * Sets the new ban rank
  374.          *
  375.          * @param banRank to banRank
  376.          */
  377.         public void setBanRank(ClanRank banRank) {
  378.                 this.banRank = banRank;
  379.         }
  380.  
  381.        
  382.         /**
  383.          * Gets the owner of the clan
  384.          *
  385.          * @return owner
  386.          */
  387.         public String getOwner() {
  388.                 return owner;
  389.         }
  390.  
  391.         /**
  392.          * Gets the title of the clan
  393.          *
  394.          * @return title
  395.          */
  396.         public String getTitle() {
  397.                 return title;
  398.         }
  399.        
  400.         /**
  401.          * Gets if the clan is on lootshare
  402.          *
  403.          * @return lootshare
  404.          */
  405.         public boolean onLootshare() {
  406.                 return lootshare;
  407.         }
  408.        
  409.         /**
  410.          * Sets the new title of the clan
  411.          *
  412.          * @param title to the title
  413.          */
  414.         public void setTitle(String title) {
  415.                 this.title = title;
  416.         }
  417.        
  418.         /**
  419.          * Sets if the clan is on lootshare
  420.          *
  421.          * @param lootshare to lootshare
  422.          */
  423.         public void setLootshare(boolean lootshare) {
  424.                 this.lootshare = lootshare;
  425.         }
  426.        
  427.         /**
  428.          * Gets the clan list of all players
  429.          *
  430.          * @return clanPlayers
  431.          */
  432.         public ClanList<ClanPlayer> getClanPlayers() {
  433.                 return clanPlayers;
  434.         }
  435.        
  436.         /**
  437.          * Gets the players in the chat
  438.          *
  439.          * @return players
  440.          */
  441.         public ClanList<Player> getPlayers() {
  442.                 return players;
  443.         }
  444.        
  445.         /**
  446.          * Gets the banned players list
  447.          *
  448.          * @return bannedPlayers
  449.          */
  450.         public ClanList<String> getBannedPlayers() {
  451.                 return bannedPlayers;
  452.         }
  453.  
  454.         /**
  455.          * Gets the enter rank
  456.          *
  457.          * @return enterRank
  458.          */
  459.         public ClanRank getEnterRank() {
  460.                 return enterRank;
  461.         }
  462.        
  463.         /**
  464.          * Gets the talk rank
  465.          *
  466.          * @return talkRank
  467.          */
  468.         public ClanRank getTalkRank() {
  469.                 return talkRank;
  470.         }
  471.        
  472.         /**
  473.          * Gets the kick rank
  474.          *
  475.          * @return kickRank
  476.          */
  477.         public ClanRank getKickRank() {
  478.                 return kickRank;
  479.         }
  480.        
  481.         /**
  482.          * Gets the ban rank
  483.          *
  484.          * @return banRank
  485.          */
  486.         public ClanRank getBanRank() {
  487.                 return banRank;
  488.         }
  489.        
  490.         /**
  491.          * Gets the kicked players
  492.          *
  493.          * @return kickedPlayers
  494.          */
  495.         public Map<String, SystemClock> getKickedPlayers() {
  496.                 return kickedPlayers;
  497.         }
  498.        
  499.  
  500. }
Advertisement
Add Comment
Please, Sign In to add comment