MOTDtimer

Discussion in 'Plugin Help/Development/Requests' started by Just_Jitse, Jun 3, 2015.

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

    Just_Jitse

    Hey people!

    Could someone help me, I am trying to make a MOTDtimer with command but it isn't working.
    Could someone please check my code and say what's wrong?

    Thanks!
    ps. I have registered the command in the main class ;)

    Commands:
    Code:
    package me.Just_Jitse.Server;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor{
    
        public static boolean timer_on_off = false;
        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2,
                String[] args) {
            if (cmd.getName().equalsIgnoreCase("motdtimer")){
                if(args[0] == null){
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "[ERROR] Set a time!");
                }
                else{
                    String string = args[0];
                    int min = Integer.parseInt(string);
                    timer_on_off = true;
                    timer_days = min;
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "Timer +" + string + " minutes!");
                }
                if(args[1] != null){
                    String string = args[1];
                    int hour = Integer.parseInt(string);
                    timer_hours = hour;
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "Timer +" + string + "hours!");
                }
                if(args[2] != null){
                    String string = args[2];
                    int day = Integer.parseInt(string);
                    timer_hours = day;
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "Timer +" + string + "days!");
                }
            }
       
           
           
            return false;
        }
    
       
    }
    
    Ping:
    Code:
    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(){
            one = DateUtils.addDays(new Date(), Commands.timer_days);
            two = DateUtils.addHours(one, Commands.timer_hours);
            launchTime = DateUtils.addMinutes(two, Commands.timer_minutes);
           
        }
       
        private static Date launchTime;
        private static Date one;
        private static Date two;
       
        @EventHandler
        public void onServerListPing(ServerListPingEvent e) {
           
            Date currentTime = new Date();
            DateFormat formatter = SimpleDateFormat.getTimeInstance();
            formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
           
            if(Commands.timer_on_off == false){
                if (launchTime == null) {
                    e.setMotd(ChatColor.RED + "" + ChatColor.BOLD + "Wait, it's coming soon ;)");
                }
                else {
                    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 FOR " + formatter.format(dateDelta) + " MINUTES");
                    }
                }
            }
           
            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);
            }
        }
       
    }
    
     
  2. Offline

    BizarrePlatinum

    @Just_Jitse This isn't the cause of your problem, however, I recommend naming your variables something that describes what it's used for, you have a lot of strings named string in your commands class. Also, did you make it so you have to enter more than one arg to get to minutes and hours on purpose?
     
  3. Offline

    Just_Jitse

    Yep, @BizarrePlatinum, this way it's also possible to set the timer to just one minute ^^

    Edit: Do you want the error log? What do you think btw of this?

    Code:
    package me.Just_Jitse.Server;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor{
    
        public static boolean timer_on_off = false;
        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2,
                String[] args) {
           
            Player p = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("motdtimer")){
                if(args[0] == null){
                    p.sendMessage(ChatColor.RED + "[ERROR] Set a time!");
                }
                else{
                    timer_on_off = true;
                    timer_days = Integer.parseInt(args[0]);
                    p.sendMessage(ChatColor.RED + "Timer +" + args[0] + " minutes!");
                }
                if(args[1] != null){
                    timer_hours = Integer.parseInt(args[1]);
                    p.sendMessage(ChatColor.RED + "Timer +" + args[1] + "hours!");
                }
                if(args[2] != null){
                    timer_hours = Integer.parseInt(args[2]);
                    p.sendMessage(ChatColor.RED + "Timer +" + args[2] + "days!");
                }
            }
       
           
           
            return false;
        }
    
       
    }
    
     
  4. Offline

    Zombie_Striker

    @Just_Jitse
    Why is it always so hard for people to say how it's not working? To just give some background as to what the problem is.

    Did you debug? Did you add any checks to test what is at fault? Did you look at the error log (since what you have will generate an error)?

    If you would have done those three things you would have solved your problem.
     
  5. Offline

    Just_Jitse

    @Zombie_Striker I have had a look at the error log but it sais this:
    Code:
    org.Bukkit.craftbukkit.w1_8_R3.command.ColouredConsoleSender cannot be cast to org.Bukkit.entity.Player
    Do you know what this means?
     
  6. Offline

    BizarrePlatinum

    @Just_Jitse probably because you cast the sender to player before you checked if it was a player or not.
     
    Googlelover1234 likes this.
  7. Offline

    Just_Jitse

    @BizarrePlatinum Yup, already found that.
    Here is the new code on commands:
    Code:
    package me.Just_Jitse.Server;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    
    public class Commands implements CommandExecutor{
    
        public static boolean timer_on_off = false;
        public static int timer_days;
        public static int timer_hours;
        public static int timer_minutes;
      
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2,
                String[] args) {
          
            if (cmd.getName().equalsIgnoreCase("motdtimer")){
                if(args.length == 0){
                    sender.sendMessage("[ERROR] Set a time!");
                }
                else{
                    timer_on_off = true;
                    timer_days = Integer.parseInt(args[0]);
                    sender.sendMessage("Timer +" + args[0] + " minutes!");
                }
                if(args[1] != null){
                    timer_hours = Integer.parseInt(args[1]);
                    sender.sendMessage("Timer +" + args[1] + "hours!");
                }
                if(args[2] != null){
                    timer_hours = Integer.parseInt(args[2]);
                    sender.sendMessage("Timer +" + args[2] + "days!");
                }
            }
            return false;
        }
    
      
    }
    
    But still getting an error tho :(
     
  8. Offline

    BizarrePlatinum

  9. Offline

    Just_Jitse

  10. Offline

    Zombie_Striker

  11. Offline

    Just_Jitse

    @Zombie_Striker please help me out, come on man :(

    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  12. @Just_Jitse I'm afraid @Zombie_Striker has it right here. You need to learn the basics of Java before you can create plugins, as Bukkit is written in Java. If you don't, you will always run into problems such as this, and problems you haven't noticed, and will have no idea how to solve them.
     
  13. Offline

    Just_Jitse

    Okay, overlooked my code, yeah it was a bit of a mess. But what do you think of this @AdamQpzm?

    My new commands.java
    Code:
    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 boolean timer_on_off;
        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_on_off = true;
                    timer_minutes = Integer.parseInt(args[0]);
                   
                    sender.sendMessage(ChatColor.GREEN + "Timer plus " + args[0] + " minute(s)!");
                   
                }
               
                else if(args.length == 2){
                    timer_on_off = true;
                    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_on_off = true;
                    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;
    
        }
    
       
    }
    
     
  14. @Just_Jitse I still think you need to learn more Java :) It's not just what you've fixed, but a couple of other things that indicate you do not have sufficient knowledge. You don't handle any exceptions here, and there's definitely one that you should, and your fields are public static for what reason? And they have unconventional naming. Finally, you only ever return false, why is that?
     
  15. Moved to Bukkit Alternates.
     
  16. Offline

    Zombie_Striker

    @Just_Jitse
    You still have not learned anything. Please stop working on your plugin, because you do not know what you're doing.

    Please look at my link, learn Java, and come back to find the simple mistake that you are still overlooking.
     
  17. Offline

    Just_Jitse

  18. There's a key principle called "encapsulation". Fields should not be public without good reason to do so, and just so they can be accessed from another class is not a good one.
     
Thread Status:
Not open for further replies.

Share This Page