Solved Automatically change server's MOTD

Discussion in 'Plugin Development' started by JjPwN1, Oct 26, 2012.

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

    JjPwN1

    Is there any API in Bukkit for changing the MOTD, as I haven't found any. Do I need to use the minecraft server API instead?
     
  2. Offline

    Loogeh

    ServerListPingEvent (i think)
    There is a event.setMOTD method.
    This event is called when a player pings the server from their multiplayer server list.
     
    jtjj222 and JjPwN1 like this.
  3. Offline

    JjPwN1

    Yep, that's it. Thanks.
     
  4. Offline

    Techtony96

    How would i do this but have it count down?

    Like the hunger games so it counts down to when the match starts.

    Loogeh
     
  5. Offline

    JjPwN1

    You'd set up an int, and set up a repeating task as well. For example:
    Code:
        @EventHandler
        public void countDown(final ServerListPingEvent event){
            if(gameHasEnded == true){ //if the game is in where it should countdown (lobby)
                this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable(){
                    public void run(){
                        int countdown = 60; //count down for a minute (60 seconds)
                        if(countdown > 0){ //if the countdown isn't at 0
                            countdown--; //take one away from the countdown
                            event.setMotd("Next Game: " + countdown + " seconds"); //set the motd as the time until the countdown hits 0
                        }
                        if(countdown == 0){
                            gameHasEnded = false;
                            event.setMotd("A game is currently in session!");
                            //here you put your code to start the hunger games
                        }
                    }
                }, 20, 20);
            }
        }
     
  6. this example is correct because its creates an repeating task for every server list ping, and it do'st store the variable for multiple invocions, and its not stopping the schadular after the game has started, whits can result in the start methode called more than 1 time
     
  7. Offline

    Comphenix

    @Techtony96
    This is not a very good idea.

    First off, you shouldn't use asynchronous tasks (that is, separate threads) for very small operations like this. Use synchronous tasks. Secondly, there's no need to use the scheduler at all. Simply record the time the game starts in a field, and then calculate the remaining time in the event handler (download sample code):
    Code:java
    1. package com.comphenix.example;
    2.  
    3. import java.text.DateFormat;
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. import java.util.TimeZone;
    7.  
    8. import org.apache.commons.lang.time.DateUtils;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.server.ServerListPingEvent;
    12. import org.bukkit.plugin.PluginManager;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class ExampleMod extends JavaPlugin implements Listener {
    16. // The time the game starts
    17. private Date launchTime;
    18.  
    19. @Override
    20. public void onEnable() {
    21. PluginManager manager = getServer().getPluginManager();
    22. manager.registerEvents(this, this);
    23.  
    24. // Start the game in one hour.
    25. launchTime = DateUtils.addHours(new Date(), 1);
    26. }
    27.  
    28. @EventHandler
    29. public void onServerListPing(ServerListPingEvent event) {
    30. Date currentTime = new Date();
    31. DateFormat formatter = SimpleDateFormat.getTimeInstance();
    32. formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
    33.  
    34. // See if the game has event started
    35. if (launchTime == null) {
    36. event.setMotd("Game has not started.");
    37. } else {
    38. // Note: Doesn't take into account leap seconds, ect.
    39. long milliDelta = launchTime.getTime() - currentTime.getTime();
    40. Date dateDelta = new Date(Math.abs(milliDelta));
    41.  
    42. if (milliDelta > 0) {
    43. event.setMotd("Game will start in " + formatter.format(dateDelta));
    44. } else {
    45. // You can also print the duration of the game
    46. //event.setMotd("Game has been running for " + formatter.format(dateDelta));
    47. event.setMotd("Game has started.");
    48. }
    49. }
    50. }
    51. }
     
  8. Offline

    Techtony96

    @JjPwN1
    @ferrybig
    @Comphenix


    Ok, i have the code set up to announce the thing ingame, i just need a way to set the motd. I get an error on event, "event cannot be resolved" Here is my code:

    Line 46
    https://github.com/Techtony96/Apoca...calyptic/src/Apocalyptic/ApocalypticGame.java

    Not sure if this was right or not, but i changed event to Event, and imported it. Now i get an error on setMotd saying "The method setMotd(String) is undefined for the type Event"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page