Solved Set ban reason in PlayerLoginEvent

Discussion in 'Plugin Development' started by Josh014, Oct 22, 2013.

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

    Josh014

    Hey,
    How can I get a ban reason from a config and set it if a player logs on he sees the reason why he/she is banned? I wrote a code but it didn't work because Essentials overwrite it each time.

    My code:
    Code:java
    1. @EventHandler
    2. public void onFirstJoin(PlayerLoginEvent event){
    3. Player player = event.getPlayer();
    4. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    5. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    6. }
     
  2. Offline

    Chinwe

    Check that event.getResult() == KICK_BANNED, and that bm isn't null before setting the kick message to it :)
    And you may want to set the priority to normal/high to override essentials:
    @EventHander (priority = EventPriority.HIGH)

    GusGold
    Actually PlayerLoginEvent, which does it all simply with event.getReason(), .setKickMessage() and .disallow()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  3. Offline

    Josh014

    Chinwe
    So I only need to do:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGH)
    2. public void onFirstJoin(PlayerLoginEvent event){
    3. Player player = event.getPlayer();
    4. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    5. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    6. }

    If I've read the last message correct?
     
  4. Offline

    Chinwe

    Josh014 That will override Essentials, though you also need to:
    Code:
    if (event.getReason() == Result.KICK_BANNED)
    {
      String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
      if (bm != null)
          event.setKickMessage(bm);
    }
     
  5. Offline

    Josh014

    Chinwe
    The only thing what I need to add is the:
    Code:java
    1. if (event.getReason() == Result.KICK_BANNED){
     
  6. Offline

    Jalau

    I use this: (Also, has 2 Lines in the Ban Message ^^)
    Code:
        //Ban Join
        @EventHandler(priority = EventPriority.HIGH)
        public void onPlayerBanned (PlayerJoinEvent e) {
        Player player = e.getPlayer();
        String msg = ChatColor.RED + "Your account has been permantly banned!" + ChatColor.RESET + "\n" + ChatColor.GRAY + "If you feel like this is an error talk to us on the forum.";
        if(this.getConfig().contains("ban." + player.getName()))
            if(!player.isOp())
            player.kickPlayer(msg);
          }
     
    
     
  7. Offline

    Josh014

    Chinwe
    Alright I use this but I get a red error line under "event.getReason()" it says "Add cast to 'event'"

    Code:java
    1. public void onFirstJoin(PlayerLoginEvent event){
    2. if (event.getReason() == Result.KICK_BANNED){
    3. Player player = event.getPlayer();
    4. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    5. if(bm != null){
    6. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    7. }
    8. }
    9. }


    Chinwe
    I think I have it almost but if I join when I'm banned I still see the Essentials message...
    Code:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGH)
    2. public void onPlayerBanned(PlayerLoginEvent event){
    3. Player player = event.getPlayer();
    4. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    5. if (event.getResult() == Result.KICK_BANNED){
    6. if(bm != null){
    7. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    8. }
    9. }
    10. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. just asking, did you registered your class as an event listener?
    if yes, try priority HIGHEST and print out a message to the console from the event handler
    - if you cant see your message, your class doesn't know it has t handle the event
    if no, lookup the wiki.bukkit.org how to register a listener class
     
  9. Offline

    Josh014

    ferrybig
    I did registered the event listener & I did HIGHEST and nothing happends.
     
  10. Offline

    whitehooder

    The eventpriority system is kind of upside down, but not really. I'll explain why...

    EventPriority.HIGHEST is not the one being run first, it is in fact the exact opposite, so that it can make use of the servers resources (where the word Highest comes from) after all the small plugins have ran their events.

    The name at the end is therefore the amount of server resources the plugin is allowed to use, and is named this way to make the server run more smoothly.

    Note: If any of this has changed in the last year, please prove me wrong, as I might not be up to date :)
     
  11. Offline

    Josh014

    whitehooder
    Do you know why my event isn't working then?
     
  12. Offline

    whitehooder

    Josh014 I suggest you try to set the priority to LOWEST, as it will be ran first (or hopefully first)
     
  13. Offline

    Josh014

    whitehooder
    I'm gonna test it out.

    whitehooder
    I tested it out but without succes >.>

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  14. Offline

    whitehooder

    I'll have a shot at it. Will post here later :)
     
  15. Offline

    Josh014

    whitehooder
    Thank you :). do you need my code?

    Code:
    Code:java
    1. @EventHandler (priority = EventPriority.LOWEST)
    2. public void onPlayerBanned(PlayerLoginEvent event){
    3. Player player = event.getPlayer();
    4. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    5. if (event.getResult() == Result.KICK_BANNED){
    6. if(bm != null){
    7. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    8. }
    9. }
    10. }
     
  16. Offline

    whitehooder

    Well, this worked out great on the first try:
    Code:java
    1. @EventHandler (priority = EventPriority.LOWEST)
    2. public void onPlayerBanned(PlayerLoginEvent event) {
    3. String bm = ChatColor.YELLOW + "You burned his house down... on valentines day...";
    4. if (event.getResult() == Result.KICK_BANNED) {
    5. if(bm != null){
    6. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    7. }
    8. }
    9. }


    Edit: Ahh, no sorry. Installed essentials to test it out and it displayed the essentials ban msg. I'll fool around with it a bit more ;)

    Okay, just thought about it for a few seconds, and it's all just basic logics really :)

    You see... The lowest priority is run first, which means you will have to use a higher priority to override the message already set by essentials. I basically just switched it out with EventPriority.HIGHEST and it worked like a charm.

    Just for the sake of copy&paste :)
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onPlayerBanned(PlayerLoginEvent event) {
    3. String bm = ChatColor.YELLOW + "You burned his house down... on valentines day...";
    4. if (event.getResult() == Result.KICK_BANNED) {
    5. if(bm != null){
    6. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    7. }
    8. }
    9. }


    Seems like you have already tried this (with HIGHEST) and I think the reason it didn't work out for you was that bm was null since the configuration failed, so if it doesn't work out of the box for you with config loading, I suggest you add in some debugging messages. Just a basic syso of bm after loading the value from the config would help you out.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  17. Offline

    Josh014

    whitehooder
    Can you explain debugging messages? I never used them before lol.
     
  18. Offline

    whitehooder

    Basically just output to the console so you can see what is happening. If you think there is a problem when getting the config string you would simply just print out the string to see it for yourself. Example:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void onPlayerBanned(PlayerLoginEvent event) {
    3. String bm = ChatColor.YELLOW + getConfig().getString("Players." + player.getName() + ".Reason");
    4. System.out.println("The value of bm is: " + bm);
    5. if (event.getResult() == Result.KICK_BANNED) {
    6. if(bm != null){
    7. event.setKickMessage(ChatColor.RED + "Banned: " + bm);
    8. }
    9. }
    10. }
    11.  


    If a message saying "The value of bm is: ......" the error is not there, if it prints noting more or you receive a null pointer exception in the console the error lies in that piece of code. Just basic debugging.
     
  19. Offline

    Josh014

    whitehooder
    I don't know what happened but I added the debugging thing and now it works lol... Thank you :3
     
  20. Offline

    whitehooder

    Just remove the debugging line then.
     
  21. Offline

    iMangoDev

    I get an error under "KICK_BANNED" help?
     
  22. Offline

    XxArdaPxX

    whitehooder I got an error while doing "Result.KICK_BANNED " so can you help me? And here is the code:
    Code:java
    1. @EventHandler (priority = EventPriority.HIGHEST)
    2. public void Login(PlayerLoginEvent e){
    3. String bm = "§cYou are banned";
    4. if(e.getResult() == Result.KICK_BANNED){
    5. if(bm != null){
    6. e.setKickMessage(bm);
    7. }
    8. }
    9. }
     
  23. Offline

    whitehooder

    You would have to tell us what the error is :) Is it an error in the server log when the code is executed or does Eclipse (or whatever IDE you use) show a red line beneath it? Your code looks ok and should be working. (Haven't checked if there is any Result.KICK_BANNED)
     
  24. Offline

    XxArdaPxX

    whitehooder nvm i fixed that :D i impored the wronk KICK_BANNED :D
     
Thread Status:
Not open for further replies.

Share This Page