1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Simple whitelist

Discussion in 'Scripts' started by Xenius, Jan 2, 2017.

  1. Xenius

    Xenius Member Donator

    Messages:
    48
    Likes Received:
    22
    Joined:
    Sep 26, 2016
    This is a whitelist script.

    Code:
    using System;
    using System.Collections.Generic;
    using GTANetworkServer;
    using GTANetworkShared;
    
    public class Whitelist : Script
    {
        public Dictionary<string, bool> whitelist_players = new Dictionary<string, bool>();
        
        public Whitelist()
        {
            API.onPlayerBeginConnect += OnPlayerBeginConnectHandler;
            
            whitelist_players["SocialClubUsername1"] = true;
            whitelist_players["SocialClubUsername2"] = true;
            whitelist_players["SocialClubUsername3"] = true;
            whitelist_players["SocialClubUsername4"] = true;
        }
        
        private void OnPlayerBeginConnectHandler(Client player, CancelEventArgs e)
        {
            string scname = player.socialClubName;
            if (!whitelist_players.ContainsKey(scname))
                API.kickPlayer(player, "You are not on the whitelist! ("+ scname +")");
        }
    }
     
  2. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    You should use a List<string>, rather than the Dictionary<string, bool>. The bool is redundant since you can just check if a list contains something, and you're not even using it anyway. Other than that, good job :=)

    EDIT: New code:
    Code:
    using System;
    using System.Collections.Generic;
    using GTANetworkServer;
    using GTANetworkShared;
    
    public class Whitelist : Script
    {
        public List<string> Players;
    
        public Whitelist()
        {
            API.onPlayerBeginConnect += OnPlayerBeginConnectHandler;
    
            Players = new List<string>
            {
                "SocialClubUsername1",
                "SocialClubUsername2",
                "SocialClubUsername3",
                "SocialClubUsername4"
            };
        }
    
        private void OnPlayerBeginConnectHandler(Client player, CancelEventArgs e)
        {
            var socialClubName = player.socialClubName;
    
            if (!Players.Contains((socialClubName)))
                API.kickPlayer(player, $"You are not on the whitelist! ({socialClubName})");
        }
    }
     
    Last edited: Jan 10, 2017
    darkcha0s and Jack8814 like this.
  3. darkcha0s

    darkcha0s Member

    Messages:
    7
    Likes Received:
    5
    Joined:
    Jan 10, 2017
    Even though the bool is redundant, the dictionary is actually a faster lookup than list; Seeing as he only has to lookup the users, I would actually recommend using the dictionary, as it is based on a hashtable.
     
    WhiteFoxMax and unSatisfied like this.
  4. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    I think efficiency doesn't really matter here unless you have a LOT of players in the whitelist :=P
     
    WhiteFoxMax and unSatisfied like this.
  5. Alex_Wells

    Alex_Wells Member

    Messages:
    23
    Likes Received:
    1
    Joined:
    Dec 21, 2016
    Wow wait, how did you make the server compile C# 6.0?
     
  6. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    Are you using VS2015? C# 6.0 has nothing to do with .NET framework, so in fact, I'm using C# 7.0 with VS2017. Native tuples are nice :=)
     
  7. Alex_Wells

    Alex_Wells Member

    Messages:
    23
    Likes Received:
    1
    Joined:
    Dec 21, 2016
    I'm using Rider IDE from Intelli, cuz all of microsoft products are buggy and uncomfortable. Anyway, does it change something? I've tried changing C# to 6.0/7.0, also .NET to 4.6.1, but server just doesnt care. It always tries to compile it with 4.5, because itself was built with 4.5..

    It gives me errors that $"" is an undefined symbol, also cant define default properties..

    Update: nope, it doesnt work. At all. http://prntscr.com/dvw9rj
    http://prntscr.com/dvw9xy
     
    Last edited: Jan 15, 2017
  8. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    Again, .NET framework has NOTHING to do with the C# version, which is why I can use C# 7.0 perfectly fine with GTA:N while targeting 4.5.2. I'm not really sure where you came up with "all of microsoft products are buggy and uncomfortable", but to each their own I suppose.
     
  9. Alex_Wells

    Alex_Wells Member

    Messages:
    23
    Likes Received:
    1
    Joined:
    Dec 21, 2016
    I do understand what you said, but how to switch C# version?

    Also C# is very uncomfortable comparing to Java. Just type "C# vs Java" on wiki and find comparasion table.. Java wins in each category, that is somehow-similar to C#.
     
  10. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    I don't see how Java is relevant to this conversation, but I highly disagree with you along with the majority of people that has worked in both languages extensively. I'm not your personal Google, you can easily look up how to use C# 6.0.
     
  11. Alex_Wells

    Alex_Wells Member

    Messages:
    23
    Likes Received:
    1
    Joined:
    Dec 21, 2016
    Did I even ask "how to use it"? Really, I've got this useless VS2015, Roslyn, set C# version, all the latest things, but all of those are just ignored, because server compiles it and not me.
     
    Last edited: Jan 15, 2017
  12. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    Why don't you compile it and reference the .dll in your meta.xml o_O ? You're the issue, not VS2015 or Roslyn.
     
  13. Alex_Wells

    Alex_Wells Member

    Messages:
    23
    Likes Received:
    1
    Joined:
    Dec 21, 2016
    Oh god, u're saying like this is a normal behaviour.. Also how server will know where to put this all? By default it uses resource's folder name.

    Roslyn is an absolutely another topic.. Making 100500 libraries just to make basic stuff work, this is something I'll never get on with..
    Any other language wins there.
     
  14. unSatisfied

    unSatisfied Member

    Messages:
    22
    Likes Received:
    4
    Joined:
    Dec 18, 2016
    It is completely normal behavior considering you have no clue what the fuck you're doing. You can easily change the build output directory to your resource folder, and you don't need 100500 libraries to make basic stuff work at all. It's clear that you just dived into something you have no knowledge about, and are blaming the tech rather than yourself.
     
  15. MissMelissa

    MissMelissa New Member

    Messages:
    0
    Likes Received:
    0
    Joined:
    Dec 26, 2016
    Guys, this isn't the point of the thread. Please get back on topic.
     
    unSatisfied likes this.

Share This Page