public void enterChannel(Player p, string owner) {
		    if (p.getClan() != null) {
			    return;
		    }
		    p.getPackets().sendMessage("Attempting to join channel...:clan:");
		    if (owner.Equals(p.getLoginDetails().getUsername())) {
			    Clan newClan = new Clan(p, owner, owner);
			    addChannel(newClan);
		    }
            Event enterChannelEvent = new Event(700);
            enterChannelEvent.setAction(() => {
				enterChannelEvent.stop();
				foreach(Clan c in clans) {
					if (c != null) {
						if (c.getClanOwner().Equals(owner)) {
							if(c.getUserList().Count >= 100) {
								p.getPackets().sendMessage("The channel is full.");
								return;
							}
							if (!owner.Equals(p.getLoginDetails().getUsername())) {
								if (c.getEnterRights() != Clan.ClanRank.NO_RANK) {
									if (c.getEnterRights() == Clan.ClanRank.FRIEND) {
										if (!c.isFriendOfOwner(p) && !c.userHasRank(p.getLoginDetails().getUsername())) {
											p.getPackets().sendMessage("You do not have a high enough rank to enter this clan chat.");
											return;
										}
									} else {
										bool canEnter = true;
										foreach (KeyValuePair<string, Clan.ClanRank> u in c.getUsersWithRank()) {
											if(u.Key.Equals(p.getLoginDetails().getUsername())) {
												if (u.Value < c.getEnterRights()) {
													canEnter = false;
													break;
												}
											}
										}
										if (!canEnter) {
											p.getPackets().sendMessage("You do not have a high enough rank to enter this clan chat.");
											return;
										}
									}
								}
							}
							c.addUser(p);
							updateClan(c);
							p.getPackets().sendMessage("Now talking in channel : " + misc.formatPlayerNameForDisplay(c.getClanName() + ":clan:"));
							p.getPackets().sendMessage("To talk, start each line of chat with the / symbol. :clan:");
							return;
						}
					}
				}
				p.getPackets().sendMessage("The channel you tried to join does not exist. :clan:");
		    });
            Server.registerEvent(enterChannelEvent);
	    }
 public void setClanRights(Clan.ClanRank rights)
 {
     if (rights == Clan.ClanRank.NO_RANK)
     {
         if (clan.getOwnerFriends().Contains(p.getLoginDetails().getLongName()))
         {
             rights = Clan.ClanRank.FRIEND;
         }
     }
     else
     {
         if(!clan.getUsersWithRank().ContainsKey(p.getLoginDetails().getUsername()))
             clan.getUsersWithRank().Add(p.getLoginDetails().getUsername(), rights);
         if (clan.getUsersWithRank().Count >= 250)
         {
             misc.WriteError("Clan 'usersWithRank' map size needs increasing!");
         }
     }
     this.rights = rights;
 }
 public ClanUser(Player p, Clan clan)
 {
     this.p = p;
     this.clan = clan;
     this.rights = Clan.ClanRank.NO_RANK;
 }
	    public void newClanMessage(Clan c, ChatMessage m) {
            Player p = m.getPlayer();
            if (!c.getClanOwner().Equals(p.getLoginDetails().getUsername())) {
	            if (c.getTalkRights() != Clan.ClanRank.NO_RANK) {
		            if (c.getTalkRights() == 0) {
			            if (!c.isFriendOfOwner(p) && !c.userHasRank(p.getLoginDetails().getUsername())) {
				            p.getPackets().sendMessage("You do not have a high enough rank to talk in this clan chat.");
				            return;
			            }
		            } else {
                        foreach (KeyValuePair<string, Clan.ClanRank> u in c.getUsersWithRank()) {
				            if (u.Key.Equals(p.getLoginDetails().getUsername())) {
					            if (u.Value < c.getTalkRights()) {
						            p.getPackets().sendMessage("You do not have a high enough rank to talk in this clan chat.");
						            return;
					            }
				            }
			            }
		            }
	            }
            }

            //Send the clan chat message to all clan members.
		    foreach(ClanUser cu in c.getUserList()) {
                p = cu.getClanMember();
                if(p != null)
                    p.getPackets().newClanMessage(c, m);
            }
	    }
	    public void updateClan(Clan c) {
		    foreach(ClanUser cu in c.getUserList()) {
                Player p = cu.getClanMember();

                if (p != null)
                    p.getPackets().updateClan(c);
		    }
	    }
	    private void addChannel(Clan clan) {
		    lock(clans) {
			    clans.Add(clan);
		    }
	    }
	    public void deleteChannel(Clan clan) {
            lock (clans)
            {
			    clans.Remove(clan);
		    }
	    }
 public void setClan(Clan clan)
 {
     this.clan = clan;
 }
	    public void openClanSetup(Player p) {
		    p.getPackets().displayInterface(590);
		    Clan clan = getClanByOwner(p.getLoginDetails().getUsername());
		    if (clan == null) {
                Clan newClan = new Clan(p, "Clan name", p.getLoginDetails().getUsername());
			    addChannel(newClan);
                p.getPackets().sendMessage("Your clan chat has been succesfully set up.");
			    return;
		    }
            p.getPackets().modifyText(misc.formatPlayerNameForDisplay(clan.getClanName()), 590, 22);
            p.getPackets().modifyText(clan.getRankString(clan.getEnterRights()), 590, 23);
            p.getPackets().modifyText(clan.getRankString(clan.getTalkRights()), 590, 24);
            p.getPackets().modifyText(clan.getRankString(clan.getKickRights()), 590, 25);
            p.getPackets().modifyText(clan.getRankString(clan.getLootRights()), 590, 26);
	    }
 private void addChannel(Clan clan)
 {
     lock (clans) {
         clans.Add(clan);
     }
 }
        public void enterChannel(Player p, string owner)
        {
            if (p.getClan() != null)
            {
                return;
            }
            p.getPackets().sendMessage("Attempting to join channel...:clan:");
            if (owner.Equals(p.getLoginDetails().getUsername()))
            {
                Clan newClan = new Clan(p, owner, owner);
                addChannel(newClan);
            }
            Event enterChannelEvent = new Event(700);

            enterChannelEvent.setAction(() => {
                enterChannelEvent.stop();
                foreach (Clan c in clans)
                {
                    if (c != null)
                    {
                        if (c.getClanOwner().Equals(owner))
                        {
                            if (c.getUserList().Count >= 100)
                            {
                                p.getPackets().sendMessage("The channel is full.");
                                return;
                            }
                            if (!owner.Equals(p.getLoginDetails().getUsername()))
                            {
                                if (c.getEnterRights() != Clan.ClanRank.NO_RANK)
                                {
                                    if (c.getEnterRights() == Clan.ClanRank.FRIEND)
                                    {
                                        if (!c.isFriendOfOwner(p) && !c.userHasRank(p.getLoginDetails().getUsername()))
                                        {
                                            p.getPackets().sendMessage("You do not have a high enough rank to enter this clan chat.");
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        bool canEnter = true;
                                        foreach (KeyValuePair <string, Clan.ClanRank> u in c.getUsersWithRank())
                                        {
                                            if (u.Key.Equals(p.getLoginDetails().getUsername()))
                                            {
                                                if (u.Value < c.getEnterRights())
                                                {
                                                    canEnter = false;
                                                    break;
                                                }
                                            }
                                        }
                                        if (!canEnter)
                                        {
                                            p.getPackets().sendMessage("You do not have a high enough rank to enter this clan chat.");
                                            return;
                                        }
                                    }
                                }
                            }
                            c.addUser(p);
                            updateClan(c);
                            p.getPackets().sendMessage("Now talking in channel : " + misc.formatPlayerNameForDisplay(c.getClanName() + ":clan:"));
                            p.getPackets().sendMessage("To talk, start each line of chat with the / symbol. :clan:");
                            return;
                        }
                    }
                }
                p.getPackets().sendMessage("The channel you tried to join does not exist. :clan:");
            });
            Server.registerEvent(enterChannelEvent);
        }
 public void updateClan(Clan c)
 {
     PacketBuilder pb = new PacketBuilder();
     pb.setId(55).setSize(Packet.Size.VariableShort);
     pb.addLong(misc.playerNameToLong(c.getClanOwner()));
     pb.addLong(misc.playerNameToLong(c.getClanName()));
     pb.addByte((byte)c.getKickRights());
     pb.addByte((byte)c.getUserList().Count);
     foreach (ClanUser list in c.getUserList())
     {
         Player p = list.getClanMember();
         pb.addLong(p.getLoginDetails().getLongName());
         pb.addUShort(p.getWorld());
         int rights = Convert.ToInt32(list.getClanRights());
         pb.addByte((byte)rights);
         pb.addString("Server " + p.getWorld());
     }
     connection.SendPacket(pb.toPacket());
 }
        public void newClanMessage(Clan c, ChatMessage chatMessage) {
			PacketBuilder pb = new PacketBuilder();
            pb.setId(54).setSize(Packet.Size.VariableByte);
            pb.addLong(chatMessage.getPlayer().getLoginDetails().getLongName());
            pb.addByte((byte)1); // dummy
            pb.addLong(misc.playerNameToLong(c.getClanName()));
            pb.addUShort(0); // some message counter bs
            string message = chatMessage.getChatText();
			int messageCounter = player.getFriends().getNextUniqueId();
            pb.addThreeBytes(messageCounter);
            pb.addByte((byte)chatMessage.getPlayer().getRights());
            pb.addBytes(chatMessage.getPacked());
            connection.SendPacket(pb.toPacket());
        }
 public ClanUser(Player p, Clan clan)
 {
     this.p      = p;
     this.clan   = clan;
     this.rights = Clan.ClanRank.NO_RANK;
 }