Tutorial Changing the MOTD for specific players without packets.

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

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

    Zombie_Striker

    While looking through the docs, I found the event "SereverPingListEvent". This event is called when a player tries to ping the server. With this event, you are able to change the player count, MOTD, and server icon for an individual player.

    To set up the event, you will need to use the ServerListPingEvent.
    Code:
    @EventHandler
        public void onPing(ServerListPingEvent event){
    • Then, to set the max players, use the line "event.setMaxPlayers(int)" to change the Maximum players to "int".
    • Then, to set the MOTD, use the line "event.setMOTD(String)" to change the MOTD to "String"
    • Then, to set the server icon, use the line "event.setServerIcon(CachedServerIcon)" to change the server icon. To get a "CachedServerIcon" object, use Bukkit.loadServerIcon( Image) where Image is equal to either a File or a BufferedImage.
    Currently, you can only change the MaxPlayers, and not the actual player count using this event.

    Below is an example of what you can do with this event. What this does is display the player's name (if they have already joined), set the player count to the total amount of players that have joined, and sets the image equal to a randomly selected image.
    Code:java
    1.  
    2. @EventHandler
    3. public void onPing(ServerListPingEvent e){
    4. /**
    5.   * This will check if the player has joined before, and if so, display their name.
    6.   */
    7. //This will see if the config cotnains there IP. The player's name will be stored here.
    8. if(getConfig().contains("address."+e.getAddress().getHostAddress())){
    9. //The next two lines will set "name" equal to the stored player name at that address.
    10. //Note: If two players share the same IP, then this will get the name of the last player who joined
    11. //Which may not be the current player joining.
    12. String name = getConfig().getString("address."+e.getAddress().getHostName());
    13. e.setMotd("Wellcome back "+name+"!");
    14. }else{
    15. //If the player has not joined before, we do not know the name of the player, so we will just welcome a "new player"
    16. e.setMotd("Welcome new player!");
    17. }
    18. /**
    19.   * Setting the max player count to the amount of players that have ever joined your server.
    20.   **/
    21. e.setMaxPlayers(Bukkit.getOfflinePlayers().length);
    22.  
    23. /**
    24.   * Changing the server image to a random image
    25.   */
    26. //These next few lines will find the folder /plugins/<plugin name>/images/ and make sure it exists.
    27. //All images for the server will be stored there.
    28. File folder = new File(getDataFolder(),"images");
    29. if(!folder.exists())
    30. folder.mkdirs();
    31.  
    32. //This line will get all the images stored at that directory.
    33. File[] images = folder.listFiles();
    34.  
    35. //This line will pick a random image from that array.
    36. int id = ThreadLocalRandom.current().nextInt(images.length);
    37.  
    38. //This line load the image as a "CachedServerIcon" (the way MC stores icons)
    39. //and set the server icon equal to that icon.
    40. e.setServerIcon(Bukkit.loadServerIcon(images[id]));
    41. }
     
    Last edited: Sep 8, 2016
    I Al Istannen and ChipDev like this.
  2. Nice but I think you should explain how it works, it's rather an example than a tutorial..


    Code:java
    1. new File(getDataFolder(), "images");
    ;)
     
    ArsenArsen likes this.
  3. Offline

    ArsenArsen

    I like it so I stole this code and I am writing a gigantic plugin by not doing anything other than copy pasting.

    NO!
    I was wondering how it all works, but was too lazy to decompile existing plugins or CTRL + F quiet a few terms. Thanks
     
  4. Offline

    Zombie_Striker

    I have added comment to each of the lines and explanations to what each method does and how it works. ;)
     
    FisheyLP likes this.
  5. Offline

    MCMastery

    nice!

    btw adress is supposed to be address ;)
     
Thread Status:
Not open for further replies.

Share This Page