PlayTime plugin errors

Discussion in 'Plugin Development' started by IanAsf, Oct 30, 2018.

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

    IanAsf

    So im working on a plugin that does /playtime and it tells you your time being on the server, BUT...
    When ever i type /playtime it just says the message i set up, doesn't show the time.

    Code:
    /*
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour PlayTime."));
                player.getStatistic(Statistic.PLAY_ONE_TICK);
            }
            else {
                sender.sendMessage("Hey! You can't use this in the terminal!");
            }
            return true;
        }
    
    }
    
    */
    Does anybody know a way to fix this?
     
  2. Online

    timtower Administrator Administrator Moderator

    @IanAsf Why do you cast to Player BEFORE the check? And you are getting the statistic, but you are never using it...
     
    Last edited: Oct 31, 2018
  3. Offline

    IanAsf

    I don't really know what you mean can you help?
     
  4. Online

    timtower Administrator Administrator Moderator

    You need to use the value of this line: player.getStatistic(Statistic.PLAY_ONE_TICK);
     
  5. Offline

    KarimAKL

    @IanAsf I'm pretty sure what timtower is saying is that you need to use the statistic, an example could be sending a message with the value, like this:
    Code:Java
    1. player.sendMessage(player.getStatistic(Statistic.PLAY_ONE_TICK));

    You are currently just writing the value without doing anything with it. Hope i explained it well enough.

    Also you are casting the sender to a player before checking if it's a player, if you don't know what we mean then i'll try to explain it. This is your code:
    Code:Java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. Player player = (Player) sender;
    3. if (sender instanceof Player){
    4. player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour PlayTime."));
    5. player.getStatistic(Statistic.PLAY_ONE_TICK);
    6. }
    7. else {
    8. sender.sendMessage("Hey! You can't use this in the terminal!");
    9. }
    10. return true;
    11. }

    This is the part you are casting sender to player:
    Code:Java
    1. Player player = (Player) sender;

    But when you are doing this you are not sure it's a player, therefore you need to check if it's a player before doing this, you do this at this part:
    Code:Java
    1. if (sender instanceof Player){

    But if you look at your code then you are checking AFTER you've cast sender to player, you should cast after checking. Hope i helped.
     
  6. Offline

    IanAsf

    I tried your line of code and it didnt work.
     
  7. Offline

    KarimAKL

    @IanAsf Haven't tried that exact line, i think you should make it a string, something like this:
    Code:Java
    1. player.sendMessage(player.getStatistic(Statistic.PLAY_ONE_TICK).toString());

    If that doesn't work try something like this:
    Code:Java
    1. player.sendMessage(""+player.getStatistic(Statistic.PLAY_ONE_TICK));

    Also i don't recommend copy and pasting the code i wrote, it's just to show you an example of what to do.
     
  8. Offline

    IanAsf

    Do i put a message in the " "? like "Your play time is "+player.getStatistic(Statistic.PLAY_ONE_TICK));
     
  9. @IanAsf

    That is definitely a step in the right direction and possibly the solution. (At least if player.getStatistic(Statistic.PLAY_ONE_TICK) gives a nice and readable String).
    You should try it!
     
  10. Offline

    IanAsf

    It worked! Thank you so much! But another question how do i convert ticks into Days-Hours-Min-Seconds
     
  11. Offline

    KarimAKL

  12. Offline

    IanAsf

    I saw that but i didnt know what line to use and where to put it any help? Cause i wanna type /playtime and the the ammount of time you been on the server pop up. It pops up in tick form and i want it to pop up as Min if your under an hour, if your over an hour it say hour and if your a day or older it will say day then amount of hours. example (1 day 22 hours) (2 days 15 hours) (45 minutes) (2 hours) and such. This is what i got so far.
    Code:
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour play time is "+player.getStatistic(Statistic.PLAY_ONE_TICK)));
            }
            else {
                sender.sendMessage("Hey! You can't use this in the terminal!");
                }
            return true;
        }
    
    }
    
     
    Last edited: Nov 1, 2018
  13. @IanAsf

    This is all spoonfeed you are going to get: (at least from me)

    Code:
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    I hope you can figure out how to get the hours on your own.
    Now use some if and else statements to send to the player what you want to send (hours? minutes?)
     
  14. Offline

    IanAsf

    Where do i put the if/else statement? and what do i put in it?
     
  15. @IanAsf

    You put the if and else statements after calculating the seconds, minutes, hours...

    In the if condition, you check if the hours and days are greater than or equal to 1 (has the player played at least 1 hour or at least 1 day?). In the blocks of the if and else, you use player.sendMessage(...) to send what you want to send to them (the minutes, hours and/or days they have played).
     
  16. Offline

    IanAsf

    Im sorry i dont understand do i edit my if statement already? if so do i put this line in?
    Code:
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    [code]
     
  17. @IanAsf
    You need to put the if's and else's after calculating the seconds, minutes, hours and days. (After the code you just sent.)
     
  18. Offline

    IanAsf

    So create another if and else statement im guessing...

    Code:
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
    
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
      Player player = (Player) sender;
      if (sender instanceof Player){
      player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour play time is "+player.getStatistic(Statistic.PLAY_ONE_TICK)));
      }
      else {
      sender.sendMessage("Hey! You can't use this in the terminal!");
      }
      return true;
      }
    if(sender instanceof Player){
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    int hours = minutes / 60;
    }
    else{
    sender.sendMessage("Hey! You can't use this in the terminal!")
      return true;
      }
    
    }
    
    Im not on my PC im on my phone so I cant try this right now but I will soon but real quick is this the way you do it?
     
    Last edited: Nov 3, 2018
  19. @IanAsf

    That is not what I meant, you need to replace the sendMessage code by the code I sent you. Then, after calculating ticks, seconds, minutes and hours, you use if's to check if the play time is less than an hour or less than a day. Send the player the right message depending on the situation.
     
  20. Offline

    IanAsf

    Ok so replace t
    sender.sendMessage("Hey! You can't use this in the terminal!");
    and put
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    int hours = minutes / 60;

    Ok so i did that and the code now looks like this
    Code:
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour PlayTime is &2"+player.getStatistic(Statistic.PLAY_ONE_TICK)));
            }
            else {
                int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
                int seconds = ticks / 20;
                int minutes = seconds / 60;
                int hours = minutes / 60;
                }
            return true;
        }
    
    }
    
    And i have a warning message under hours and it still shows ticks not converted. Did i put it in wrong?
    Maybe you can help?
     
    Last edited: Nov 4, 2018
  21. @IanAsf

    The code you put in your else block should be located in your if block.
    Your old else block was fine.

    You need to create new if blocks in your current if blocks.

    You have a warning under hours because you calculate the amount of hours the player has played, but you don't use it. You should use the seconds, minutes and hours in the sendMessage to send them to the player.
     
  22. Offline

    IanAsf

    So put
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    int hours = minutes / 60;
    in the if? I honestly dont understand can you put the line of code in my code? this is honestly the only thing i have probloms with
     
    Last edited: Nov 5, 2018
  23. @IanAsf

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    int hours = minutes / 60;
                player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour play time is "+player.getStatistic(Statistic.PLAY_ONE_TICK)));
            }
            else {
                sender.sendMessage("Hey! You can't use this in the terminal!");
                }
            return true;
        }
    Was that really so hard?
    Now to the next part:
    player.sendMessage("You played " + hours + " hours")
    You should really figure the rest out for yourself...
     
  24. Offline

    IanAsf

    Wait whats player.sendMessage("You played " + hours + " hours") for?
    I had it so it popped up here
    player.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bYour play time is "+player.getStatistic(Statistic.PLAY_ONE_TICK)));

    I did it and it worked! One prob its going over... 150 minutes right now i think theres an issue.
    Code:
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player) sender;
            if (sender instanceof Player){
    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
    int seconds = ticks / 20;
    int minutes = seconds / 60;
    int hours = minutes / 60;
    int days = hours / 24;
                player.sendMessage("You've spent " + days +" Days " + hours + " Hours " + minutes + " Minutes On MineHunt! " );
            }
            else {
                sender.sendMessage("Hey! You can't use this in the terminal!");
                }
            return true;
        }
    }
    Another question i have is im going to add another command to this plugin and its going to be /playtime (player) how do i name the class? What would the line of code be?
     

    Attached Files:

    Last edited by a moderator: Nov 5, 2018
  25. @IanAsf

    The minutes go over 150 because the minutes are the total amount of minutes the player has played. If you want to fix the value, use minutes % 60 instead of just minutes in the message.
    Sending the amount of hours was just an example of how to send the hours to the player.

    If you want to add the command /playtime (player) as well, you will have to distinct 2 cases:
    args.length = 0 and args.length = 1

    You will have to put all the code in the same class as now, so you will have to handle both the cases in your onCommand method. So when args.length is 0, use the sender as player. When args.length is 1, try to use Bukkit.getPlayer(args[0]) to get the player you want and send the playtime of that player instead.

    You will need more programming skills to implement that properly though...
     
  26. Offline

    IanAsf

    I fixxed it and heres the line of code i used.
    Code:
    package me.FatalAura.PlayTime.Command1;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Statistic;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class PlayTime implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                Player player = (Player) sender;
                if (sender instanceof Player) {
                    int ticks = player.getStatistic(Statistic.PLAY_ONE_TICK);
                   
                    int s = (int) (ticks / 20);
                    String time = formatTime(s);
                    player.sendMessage(time);
                } else {
                    sender.sendMessage("Hey! You can't use this in the terminal!");
                }
                return true;
            }
         
            public static String formatIntoMMSS(int secs) {
                int remainder = secs % 3600;
                int minutes = remainder / 60;
                int seconds = remainder % 60;
         
                return ((minutes < 10 ? "0" : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds);
            }
         
            public static String formatTime(int secs) {
                int remainder = secs % 86400;
         
                int days = secs / 86400;
                int hours = remainder / 3600;
                int minutes = (remainder / 60) - (hours * 60);
                int seconds = (remainder % 3600) - (minutes * 60);
         
                if (days > 0) {
                    return ChatColor.translateAlternateColorCodes('&', "&fYou've spent &l" + days + " days, " + hours + " hours, " + minutes + " minutes on &6Mine&eHunt!");
                } else if (hours > 0) {
                    return ChatColor.translateAlternateColorCodes('&', "&fYou've spent &l" + hours + " hours, " + minutes + " minutes on &6Mine&eHunt!");
                } else if (minutes > 0) {
                    return ChatColor.translateAlternateColorCodes('&', "&fYou've spent &l" + minutes + " minutes on &6Mine&eHunt!");
                } else {
                    return String.valueOf(seconds + "s");
                }
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page