-3

So I'm trying to code an escort command for my game using case switch. Basically I have tons of other commands but I've never done one where it makes a target user follow the session user. Basically the person using the command would type :escorts username and it would make the other user stand either in front of the person using the command. or behind them. Any help would be amazing.

#region Escorts User
case "escorting":
{
    #region Generate Instances / Sessions / Vars
    if (!RoleplayManager.ParamsMet(Params, 1))
    {
        Session.SendWhisper("Invalid syntax: :stun x");
        return true;
    }
    string Target = Convert.ToString(Params[1]);
    GameClient TargetSession = null;
    RoomUser Actor = null;
    RoomUser Targ = null;
    TargetSession = RoleplayManager.GenerateSession(Target);

    if (TargetSession == null)
    {
        Session.SendWhisper("The user was not found in this room!");
        return true;
    }

    if (TargetSession.JobManager() == null)
    {
        Session.SendWhisper("The user was not found in this room!");
        return true;
    }

    if (TargetSession.JobManager().GetRoomUser() == null)
    {
        Session.SendWhisper("The user was not found in this room!");
        return true;
    }

    if (TargetSession.JobManager().GetRoomUser().RoomId != Session.JobManager().GetRoomUser().RoomId)
    {
        Session.SendWhisper("The user was not found in this room!");
        return true;
    }

    Targ = TargetSession.JobManager().GetRoomUser();
    Actor = Session.JobManager().GetRoomUser();

    int MyJobId = Session.GetRoleplay().JobId;
    int MyJobRank = Session.GetRoleplay().JobRank;

    Vector2D Pos1 = new Vector2D(Actor.X, Actor.Y);
    Vector2D Pos2 = new Vector2D(Targ.X, Targ.Y);

    #endregion
    #region Police Conditions
    if (Params.Length == 1)
    {
        Session.SendWhisper("Opa, você esqueceu de inserir um nome de usuário!");
        return true;
    }

    GameClient TargetClient = Plus.GetGame().GetClientManager().GetClientByUserName(Params[1]);
    if (TargetClient == null)
    {
        Session.SendWhisper("Ocorreu um erro ao tentar encontrar esse usuário, talvez ele esteja offline.");
        return true;
    }

    RoomUser RoomUser = Session.JobManager().CurrentRoom.GetRoomUserManager().GetRoomUserByHabbo(Session.JobManager().UserName);
    
    if (!JobManager.validJob(Session.GetRoleplay().JobId, Session.GetRoleplay().JobRank) && Session.GetRoleplay().inCR == false)
    {
        Session.SendWhisper("Your job cannot do this!", false, 34);
        return true;
    }
    bool isclose = false;
    if (!Session.GetRoleplay().JobHasRights("police")
        && !Session.GetRoleplay().JobHasRights("gov")
        && !Session.GetRoleplay().JobHasRights("swat")
        && !Session.GetRoleplay().JobHasRights("service")
        && RoleplayManager.CR == false)
    {
        Session.SendWhisper("Your job cannot do this!");
        return true;
    }
    if (!Session.GetRoleplay().Working && RoleplayManager.CR == false)
    {
        Session.SendWhisper("You must be working to do this!");
        return true;
    }
    if (Session.GetRoleplay().Dead)
    {
        Session.SendWhisper("You cannot do this while you are dead!");
        return true;
    }
    if (Session.GetRoleplay().Jailed)
    {
        Session.SendWhisper("You cannot do this while you are in jail!");
        return true;
    }
    if (Targ.Frozen)
    {
        Session.SendWhisper("This user is already stunned!");
        return true;
    }
    if (Session.JobManager().CurrentRoom.RoomData.Description.Contains("NOCOP"))
    {
        Session.SendWhisper("Can't do this in 'NOCOP' rooms.");
        return true;
    }
    if (JobManager.validJob(Session.GetRoleplay().JobId, Session.GetRoleplay().JobRank))
    {


        if (Session.JobManager().CurrentRoom.RoomData.Description.Contains("WESTERN") && Session.GetRoleplay().JobHasRights("police"))
        {
            Session.SendWhisper("Can't do this in 'WESTERN' rooms.");
            return true;
        }
        if (!Session.JobManager().CurrentRoom.RoomData.Description.Contains("WESTERN") && Session.GetRoleplay().JobHasRights("western"))
        {
            Session.SendWhisper("Can only do this in 'WESTERN' rooms.");
            return true;
        }
    }

    #endregion

    #region Execute
    Point ClientPos = new Point(RoomUser.X, RoomUser.Y);
   
    double Distance = RoleplayManager.Distance(Pos1, Pos2);

    if (Distance <= 1)
    {
        if (Session.GetRoleplay().Cop == true && Session.GetRoleplay().inCR == true)
        {
            RoleplayManager.Shout(Session, "*Fires their stun-gun at " + TargetSession.JobManager().UserName + "*");
            TargetSession.GetRoleplay().EffectSeconds = 10;
            TargetSession.GetRoleplay().StunnedSeconds = 10;
            Targ.ApplyEffect(590);
            Targ.CanWalk = true;
            Targ.Frozen = false;
            Targ.ClearMovement();

            LevelManager.AddLevelEXP(Session, 30);
            Session.GetRoleplay().SaveQuickStat("currentxp", +30);
            return true;
        }
    }
    else
    {
        Session.SendWhisper("Você deve se aproximar desse cidadão para escoltá-lo!");
        return true;
    }
}
#endregion

#endregion
CarenRose
  • 1,266
  • 1
  • 12
  • 24
xMufaisax
  • 1
  • 1
  • 1
    Do realise that you should generally add a `break;` at the end of each `case`? – Sweeper Apr 14 '20 at 19:24
  • 3
    First of all, don't make cases this big. Refactor the code into multiple functions and call them within the switch. Secondly, all cases should generally end with a break unless you have something else like a `throw` or `return` statement. Only when you want to go to the next case you can omit the `break`. I think once you've refactored the code, your question will solve itself. If not, ask with the refactored code. As it stands right now I don't understand what your question is. – Joelius Apr 14 '20 at 19:29
  • Is this *entire thing* one `case`? Sounds like a great time for some refactoring to pull out smaller operations into well-named methods. Aside from that, what's the actual problem? The question title implies that you're getting an error? Can you elaborate/clarify? – David Apr 14 '20 at 19:29
  • 3
    To @David's point, if you find yourself needing to define multiple (or even just _any_) `#region`s within a single method, let alone a single `case`, you've probably got too much going on there. – Lance U. Matthews Apr 14 '20 at 19:38
  • Ive sorted thank you very much. I did forget my break; haha. – xMufaisax Apr 16 '20 at 18:35

1 Answers1

0

The error says that each case block cannot fall through another one.

This means that each case must have a return or break at their ending.

Keep in mind that you may group multiple cases into one (technically this is falling through) when they contain no code.

For more information, see MSDN.

Your switch statement is massive and probably needs some refactoring.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
  • 1
    Multiple cases (with no statements in between), for a single code block has always been possible – Hans Kesting Apr 14 '20 at 20:27
  • 2
    "must have" should include `goto case` – devio Apr 14 '20 at 20:39
  • @devio you are right, but goto isn't really a good practice and I didn't want to give a bad suggestion. – Yennefer Apr 14 '20 at 21:30
  • @hans I fixed the answer. Thank you. – Yennefer Apr 14 '20 at 21:32
  • 1
    This answer is basically correct, but I note that there are some subtleties here. Suppose for instance we have `case X: if (y) { return; } else { break; } case Y: ...` The case section does not end with a break, even though it looks like it does -- that break is *inside* the `if`. Since *both* branches of the `if` have unreachable end points, so does the `if` statement itself, so it is allowed to be the sole contents of a case section. The rule in C# is that the end of the case section must not be reachable; any code that meets that constraint is good. – Eric Lippert Apr 14 '20 at 22:51
  • @Yennefer "goto isn't really a good practice" - citation needed. I heard it's perfectly fine if using it specifically for fall through. – shoopi Apr 03 '21 at 14:59