Tutorial How to temp-ban / ban a player using just one line.

Discussion in 'Resources' started by Zombie_Striker, Sep 23, 2016.

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

    Zombie_Striker

    Introduction
    What if I told you that you can create a banning system, complete with customize-able ban reasons, the ability to add expiration dates, and the option to choose banning Ips, that only requires one line?

    This tutorial will show you how to use the built-in Banning system to complete all of the above. This tutorial will be broken down into three parts: Setting up the banlist, setting the custom reasons, and an example.

    Getting the banlist.
    There are two banlists that you can choose from: the Name banlist and the IP banlist.

    The Name banlist will ban a user with a specific name. To get this banlist, you would use
    Code:
    Bukkit.getBanList(Banlist.Type.NAME)
    To get the IP banlist, all you need to do is change "NAME" to "IP".
    Code:
    Bukkit.getBanList(Banlist.Type.IP)
    After you set up the banlist, you will need to use the "addBan" method to ban the player or IP. The parameters for the method are "String [target], String [reason], Date [expires], String [source]".
    • [Target] can either be the player's name or the player's IP.
    • [Reason] is the message that will be displayed if they try to join the server while they are banned.
    • [Expires] is the date the ban will be lifted. Set this to null if you want the player to be permanently banned.
    • [Source] is the source of the ban. You can set this to null.
    Here is an examples of how you can use it; This will permanently ban the player, displaying the message "you were hacking" if they try to join the server:
    Code:
    //ip
    BanList#addBan(target.getAddress().getHostName(),"You were hacking", null, null);
    
    //name
    BanList#addBan(target.getName(),"You were hacking", null, null);
    
    If you want to temperately ban the player, you will have to create a new date object. For example, if you wanted to ban the player for one hour, you would use the following date object instead:
    Code:
    Date date = new Date(System.currentTimeMillis()+60*60*1000);
    //Since there are 1000 miliseconds in one second, 60 seconds in one minute, and 60 minutes in an hour, by adding 60*60*1000 to the current time, you are adding an hour to the bantime. 
    There are many ways to create the date object, but for this example we are using milliseconds to add an hour to ban time.

    If you wanted to re-add this to the addBan method above, it should look like
    Code:
    BanList#addBan(target,"You where hacking",new Date(System.currentTimeMillis()+60*60*1000), null);
    
    Important Note:
    This will only ban the player, and will not kick them off the server. If you want to kick the player off the server, use the following line:
    Code:
    player.kickPlayer("Reason");
    
    Where you will replace "reason" with the message you display to the player once they have been kicked. It may be best to use the same message you use above.


    Custom Ban reason.
    The default ban message may not be what you want. If you want to create your own custom ban message, you will need to create a "Bumper" to push the old ban reason off the screen. For most screens, the following bumper should move the default message off the screen.
    Code:
        String bumper = org.apache.commons.lang.StringUtils.repeat("\n", 35);
    After creating this object, you can add this before and after your custom reason in order to move the default message of the screen. An example of this would be
    Code:
    BanList#addBan(target.getName(),bumper+"You where hacking!"+bumper, null, null);
    
    Example:
    For the people who would want to see how to, as the title says, ban a player using just one line, this would be it:
    Code:
           Bukkit.getBanList(BanList.Type.NAME).addBan(player.getName(), org.apache.commons.lang.StringUtils.repeat("\n", 35)+" You have been banned by "+sender.getName()+" \nFor one hour"+org.apache.commons.lang.StringUtils.repeat("\n", 35)",new Date(System.currentTimeMillis()+60*60*1000),null);
    What this will do is ban the player for one hour, and display the a custom ban message to the player.
     
    Last edited: Sep 24, 2016
  2. Offline

    Tecno_Wizard

    @Zombie_Striker, interesting question... Has this been made UUID safe? The way you say it makes it seem as if it's not, which is a serious flaw in the system Spigot needs to be alerted about.
     
  3. Offline

    Zombie_Striker

    @Tecno_Wizard
    It should UUID safe, though you have to provide the player's name if you want to ban them. The UUID is stored along with the rest of the ban data, so it should be used (though you would need to look at the source code/test it if you want to be sure).
     
  4. Offline

    Tecno_Wizard

    @Zombie_Striker, odd that they didn't overload that to allow for both.
     
    Zombie_Striker and bwfcwalshy like this.
  5. Offline

    ArsenArsen

  6. Nice, I didn't know that all that existed :)

    found a few things:
    lip lip lip lip

    apache's StringUtils is available in bukkit:
    Code:java
    1. String bumper = StringUtils.repeat("\n", 35);
     
    Last edited: Sep 24, 2016
    Zombie_Striker likes this.
  7. Offline

    Zombie_Striker

    @ArsenArsen
    Those issues have been fixed.

    @FisheyLP
    lip. Also, I have made the change to bumper.
     
    ArsenArsen likes this.
  8. Offline

    Goksi

    Thanks that helped me a lot :)
    Edit:How could i unban player using this metode ?
     
    Last edited: Apr 22, 2017
  9. Offline

    ThePandaPlayer

    @Goksi

    It is possible:

    Code:
    Bukkit.getBanList(BanList.Type.NAME).pardon(target);
    As you can see, instead of the .addBan() method, we are using the .pardon() method. This method takes the player's name as a string.


     
  10. Offline

    Zombie_Striker

  11. Offline

    xXkguyXx

    Quick Question, If I used MySQL in bukkit, would it store that information in there?
    This might be off topic but, what is stored in Sql by bukkit If I enabled that? (Just out of curiosity)
     
  12. @xXkguyXx
    No, it'd still use the same ban-files.

    Nothing is stored in the SQL database automatically if you enable it, it's only for plugins as far as I know.
     
Thread Status:
Not open for further replies.

Share This Page