Using AsyncPlayerPreLoginEvent to kick a banned player

Discussion in 'Plugin Development' started by Th3Controller, Dec 29, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Th3Controller

    Hello everyone,

    I'm having a little bit of trouble today trying to "kick" a banned player. I'm reading from a ban list, this is how the ban list looks like:
    Code:
    banlist:
      notch:
        reason: ' you are thirsty'
      thecontroller:
        reason: ' you are hungry'
    
    How would I go on about doing this?
     
  2. Offline

    MP5K

    hello Th3Controller,
    1. onEnable: load the Config and get a list of all banned players. (or an HashMap<String, String> Name , Reason)
    2. onPlayerLogin: check if the player is in the hashmap / list if he is kick him

    if you need a sample code message / tag me ;3
     
  3. Offline

    Th3Controller

    I already have the config.yml loaded, the list gets read when a player pre-login event occurs.

    I prefer to use AsyncPlayerPreLoginEvent. Also don't prefer to use HashMaps in this occassion :3
     
  4. Offline

    raGan.

    I'm not entirely sure about this, but it may be better to use synchronized class in Async event, like Hashtable.

    Edit: wait, are you having troubles with kicking the player ?
     
  5. Offline

    MP5K

    ok but it's a performance loss if you read the list every time a players Join's try to cache the data.
     
  6. Offline

    Th3Controller

    No I need help setting it up, I tried doing it yes but it didn't work.

    Good idea, I'll just cache/add the a attempting player that's already banned into a HashMap/ArrayList. so whenever he tries to spam join it won't be a performance lost :)
     
  7. Offline

    fireblast709

    As it is an Async event, mind thread-safety
     
  8. Offline

    Th3Controller

    What do you mean by "mind thread-safety".
    Is this method unsafe? Should I use a different method using the main thread instead?
     
  9. Offline

    fireblast709

    No, I mean that you should mind how you access Lists/Sets/HashMaps across threads. One way to go about it is handling Concurrency yourself using synchronized blocks/the synchronized keyword. Or use a CopyOnWriteArrayList
     
  10. Offline

    tommycake50

    use PlayerPreLoginEvent.
    and
    if they are banned.
    event.disAllow(event.result.KICK_BANNED, message);
     
  11. Offline

    Th3Controller

    Are you sure?
    If someone can whip up a batch of code that would help, I'm having the trouble of reading from the file also (not that the plugin can't read it, I don't know how to read it).
     
  12. Offline

    fireblast709

    Why not use the PlayerLoginEvent? Nonetheless if you want a thread-safe method:
    The config
    Code:
    default-reason: You have been banned!
    Banned:
      - Playername;reason
    The HashMap
    Code:java
    1. java.util.concurrent.ConcurrentHashMap<String, String> banned = new java.util.concurrent.ConcurrentHashMap<String, String>();
    2.  

    The onEnable()
    Code:java
    1. public void onEnable()
    2. {
    3. List<String> banList = getConfig().getStringList("Banned");
    4. if(banList == null)
    5. {
    6. banList = new ArrayList<String>();
    7. getConfig().set("Banned", banList);
    8. saveConfig();
    9. }
    10. for(String bannedPlayer : banList)
    11. {
    12. String[] pr = bannedPlayer.split(";", 2);
    13. if(pr.length == 0)
    14. {
    15. continue;
    16. }
    17. if(pr.length == 1)
    18. {
    19. pr = new String[]{pr[0], getConfig().getString("default-reason", "You have been banned")};
    20. }
    21. banned.put(pr[0], pr[1]);
    22. }
    23. }

    The event:
    Code:java
    1. @EventHandler(priority=EventPriority.HIGHEST)
    2. public void onPreLogin(AsyncPlayerPreLoginEvent event)
    3. {
    4. String name = event.getName();
    5. if(banned.containsKey(name))
    6. {
    7. String reason = banned.get(name);
    8. event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, reason == null ? "You have been banned" : reason);
    9. }
    10. }
     
  13. Offline

    Th3Controller

    fireblast709 Lol that was my old format for the player and reason. But my friend had to tell me its better that way >.>. Anyways I'll continue with yours, I'll be back later or tomorrow on my result, I'm tired.

    Btw you made making a config file look intimidating.

    Why not use (making this on the top of my head, I forgot)
    getConfig.CopyDefaults(true);
    saveConfig;

    :p
     
  14. Offline

    fireblast709

    Yea you could obviously use a default here ;3. And if your friend is talking about the async part, he has a point there
    (as that part of the code would be executed on another thread and would not directly interfere with the main threads execution flow).

    Sorry for the intimidating code xD
     
  15. Offline

    tommycake50

    not that it would matter too much and theres a low risk but nonetheless i agree with you its bad practice anyway.
     
  16. Offline

    fireblast709

    Wait I don't follow you there xD
     
Thread Status:
Not open for further replies.

Share This Page