MOTDTimer

Discussion in 'Spigot Plugin Development' started by Just_Jitse, Jun 6, 2015.

  1. Hello people so when I was making my motdtimer I ran into a problem, I was able to set set a timer by just using the dateUtils. But when I made a command it was not working anymore, I have been buzy with this problem for a week now and I hope that someone could help me :)

    Commands.java:
    Code (Text):
    package me.Just_Jitse.Server;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    public class Commands implements CommandExecutor{

        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
     
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
         
            if(cmd.getName().equalsIgnoreCase("motdtimer")){
             
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "[ERROR] Set a time!");
                }
             
                else if(args.length == 1){
                    timer_minutes = Integer.parseInt(args[0]);
                 
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + timer_minutes + " minute(s)!");
                 
                }
             
                else if(args.length == 2){
                    timer_minutes = Integer.parseInt(args[0]);
                    timer_hours = Integer.parseInt(args[1]);
                 
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + args[0] + " minute(s) and +" + args[1] + " hour(s)!");
                 
                }
             
                else if(args.length == 3){
                    timer_minutes = Integer.parseInt(args[0]);
                    timer_hours = Integer.parseInt(args[1]);
                    timer_days = Integer.parseInt(args[2]);
                 
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + args[0] + " minute(s), " + args[1] + " hour(s) and " + args[2] + " day(s)!");
                }
            }
         
        return false;

        }

     
    }
     
    Ping.java:
    Code (Text):

    package me.Just_Jitse.Server;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;

    import org.apache.commons.lang.time.DateUtils;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.server.ServerListPingEvent;

    public class Ping implements Listener {
       
        public static void Date(){
           
            days = DateUtils.addDays(new Date(), Commands.timer_days);
            hours = DateUtils.addDays(days, Commands.timer_hours);
            launchTime = DateUtils.addMinutes(hours, Commands.timer_minutes);
           
        }
       
        private static Date launchTime;
        private static Date days;
        private static Date hours;
       
        @EventHandler
        public void onPing(ServerListPingEvent e) {
           
            Date currentTime = new Date();
            DateFormat formatter = SimpleDateFormat.getTimeInstance();
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
           
            if(launchTime != null){
               
                long milliDelta = launchTime.getTime() - currentTime.getTime();
                Date dateDelta = new Date(Math.abs(milliDelta));
                   
                if (milliDelta > 0) {
                    e.setMotd(ChatColor.GOLD + "" + ChatColor.BOLD + "Server launch in " + formatter.format(dateDelta) + " hours!");
                    }
                else{
                    e.setMotd(ChatColor.GREEN + "" + ChatColor.BOLD + "SERVER HAS BEEN LAUNCHED!!!");
                   
                    System.out.println(milliDelta);
                   
                    Commands.timer_hours = 0;
                    Commands.timer_minutes = 0;
                    Ping.launchTime = null;
                    Ping.days = null;
                    Ping.hours = null;
                    }
                }
            else{
                String str = Main.plugin.getConfig().getString("motd1");
                str = ChatColor.translateAlternateColorCodes('&', str);
               
                String str2 = Main.plugin.getConfig().getString("motd2");
                str2 = ChatColor.translateAlternateColorCodes('&', str2);
                e.setMotd(ChatColor.RED + str + "\n" + ChatColor.BLUE + str2);
               
                }
            }
        }
       

     
     
    #1 Just_Jitse, Jun 6, 2015
    Last edited: Jun 6, 2015
  2. You used the wrong method there.

    You should add a

    Code (Text):
    return true;
    at the end of this if clause:

    Code (Text):
    if(cmd.getName().equalsIgnoreCase("motdtimer")){
    to not get the help message displayed.

    At the end of the above if clause you should apply your variables

    Code (Text):
        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
    to your timer.
     
  3. @Friwi if I apply the variables at the end of the if clause they are just int's, then I can't call them again in an another class :(
    Your are going a little bit to fast for me could you explain a bit more? :S
     
  4. Try this: (I commented what has been changed)

    Code (Text):
    package me.Just_Jitse.Server;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;

    public class Commands implements CommandExecutor{

        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
     
            if(cmd.getName().equalsIgnoreCase("motdtimer")){
         
                if(args.length == 0){
                    sender.sendMessage(ChatColor.RED + "[ERROR] Set a time!");
                }
         
                else if(args.length == 1){
                    timer_minutes = Integer.parseInt(args[0]);
             
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + timer_minutes + " minute(s)!");
             
                }
         
                else if(args.length == 2){
                    timer_minutes = Integer.parseInt(args[0]);
                    timer_hours = Integer.parseInt(args[1]);
             
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + args[0] + " minute(s) and +" + args[1] + " hour(s)!");
             
                }
         
                else if(args.length == 3){
                    timer_minutes = Integer.parseInt(args[0]);
                    timer_hours = Integer.parseInt(args[1]);
                    timer_days = Integer.parseInt(args[2]);
             
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + args[0] + " minute(s), " + args[1] + " hour(s) and " + args[2] + " day(s)!");
                }
              //Apply values to Ping Listener
              Ping.Date();
              //Set the command as ok, dont display the command not found help message
              return true;
            }
     
        return false;

        }

    }
    Code (Text):
    package me.Just_Jitse.Server;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.TimeZone;

    import org.apache.commons.lang.time.DateUtils;
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.server.ServerListPingEvent;

    public class Ping implements Listener {

        public static void Date(){
       
            days = DateUtils.addDays(new Date(), Commands.timer_days);
            //Use addHours instead of addDays
            hours = DateUtils.addHours(days, Commands.timer_hours);
            launchTime = DateUtils.addMinutes(hours, Commands.timer_minutes);
       
        }

        private static Date launchTime;
        private static Date days;
        private static Date hours;

        @EventHandler
        public void onPing(ServerListPingEvent e) {
       
            Date currentTime = new Date();
            DateFormat formatter = SimpleDateFormat.getTimeInstance();
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
       
            if(launchTime != null){
           
                long milliDelta = launchTime.getTime() - currentTime.getTime();
                Date dateDelta = new Date(Math.abs(milliDelta));
               
                if (milliDelta > 0) {
                    e.setMotd(ChatColor.GOLD + "" + ChatColor.BOLD + "Server launch in " + formatter.format(dateDelta) + " hours!");
                    }
                else{
                    e.setMotd(ChatColor.GREEN + "" + ChatColor.BOLD + "SERVER HAS BEEN LAUNCHED!!!");
               
                    System.out.println(milliDelta);
               
                    Commands.timer_hours = 0;
                    Commands.timer_minutes = 0;
                    Ping.launchTime = null;
                    Ping.days = null;
                    Ping.hours = null;
                    }
                }
            else{
                String str = Main.plugin.getConfig().getString("motd1");
                str = ChatColor.translateAlternateColorCodes('&', str);
           
                String str2 = Main.plugin.getConfig().getString("motd2");
                str2 = ChatColor.translateAlternateColorCodes('&', str2);
                e.setMotd(ChatColor.RED + str + "\n" + ChatColor.BLUE + str2);
           
                }
            }
        }
     
  5. @Friwi What did you changed(I can't see bold in code tags xD), it works but I really want to know why it works now so I can learn from it ;)

    EDIT: ah there, ty for changing and helping me out! :D
     
  6. Just didn't knew i could not add (b) tags within (code) tags :D
     
  7. @Just_Jitse also, the static is not necessary in your case. Create a non static method(s) in your Ping class which allow updating of the time, and pass the instance of the Ping class to the CommandExecutor for access.
     
  8. It's better, yes.
    Aside it being a bad habit (static is not meant for lazy access, after all)
     
  9. Create the instance of your Ping class before you create an instance of your Commands class, then pass this instance through the constructor of Commands. Then in Commands, store it in a field in your class (a non static one, of type Ping), and you will be able to call methods of the Ping class from the Commands class through that Ping field.
     
    • Agree Agree x 1
  10. [​IMG]
    The static abuse D:
     
    • Like Like x 1
  11. urg, I don't get it ><