[Help] Total played time

Discussion in 'Plugin Development' started by Cheesepro, Nov 18, 2014.

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

    Cheesepro

    I am trying to make a player id card feature for my server and the played time just doesn't seemed to be working.. since all the played time are 1000 hours above... so here is what I have. and if you know what is wrong please do help me :D Thank you!
    Code:java
    1. package me.cheesepro.ce.mainpack;
    2.  
    3. import me.cheesepro.ce.extra.ConsoleSender;
    4. import me.cheesepro.ce.extra.Messenger;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.Material;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandExecutor;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.inventory.ItemStack;
    14. import org.bukkit.inventory.meta.ItemMeta;
    15.  
    16. import java.lang.reflect.Array;
    17. import java.util.ArrayList;
    18. import java.util.Arrays;
    19. import java.util.StringTokenizer;
    20.  
    21. /**
    22. * Created by Mark on 15/11/2014.
    23. */
    24. public class cmdID implements CommandExecutor{
    25.  
    26. private CEMain plugin;
    27. private final Messenger msg;
    28.  
    29. public cmdID(CEMain plugin){
    30. this.plugin = plugin;
    31. this.msg = new Messenger(plugin);
    32. }
    33.  
    34. @Override
    35. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    36. if (sender instanceof Player) {
    37. Player p = (Player) sender;
    38. if (label.equalsIgnoreCase("id")) {
    39. ItemStack item = new ItemStack(Material.PAPER, 1);
    40. ItemMeta im = item.getItemMeta();
    41. long tTickPlayed = p.getPlayerTime();
    42. long tSecondPlayed1 = tTickPlayed / 20L;
    43. long tMinPlayed1 = tSecondPlayed1 / 60L;
    44. long tHPlayed1 = tMinPlayed1 / 60L;
    45. long tMinPlayed2 = tMinPlayed1 - tHPlayed1 * 60L;
    46. long tSecondPlayed2 = tSecondPlayed1 - tMinPlayed1 * 60L;
    47. im.setDisplayName(ChatColor.WHITE.toString() + ChatColor.BOLD + "==========[" + ChatColor.LIGHT_PURPLE.toString() + ChatColor.BOLD + "ID" + ChatColor.WHITE.toString() + ChatColor.BOLD + "]==========");
    48. ArrayList<String> lore = new ArrayList<String>();
    49. lore.add(ChatColor.GREEN.toString() + ChatColor.BOLD + "Name: " + p.getName());
    50. lore.add(ChatColor.YELLOW.toString() + ChatColor.BOLD + "Played time: " + tHPlayed1 + "h " + tMinPlayed2 + "min " + tSecondPlayed2 + "s");
    51. lore.add(ChatColor.WHITE.toString() + ChatColor.BOLD + "========================");
    52. im.setLore(lore);
    53. item.setItemMeta(im);
    54. p.getInventory().addItem(item);
    55. msg.m(p, "a", "ID card get!");
    56. }
    57. } else {
    58. ConsoleSender.noConsole();
    59. }
    60. return false;
    61. }
    62. }
    63.  
     
  2. Offline

    Skionz

    I believe Player#getPlayerTime() returns their minecraft time as in night or day not the amount of time they have been on the server. You would have to setup a scheduler and increment each players time every 5 minutes or so and either save it to some type of file or a database.
     
  3. Offline

    Cheesepro

    Skionz Thanks :D so the hours I got is basically how many "Minecraft hour" I've played. Is it?
     
  4. Offline

    Skionz

    No its the players client side time.
     
  5. Offline

    Cheesepro

    Skionz so.. how long since the player first joined?
     
  6. Offline

    Skionz

    I told you. Setup a scheduler and save your data using some method of serialization.
     
  7. Offline

    msnijder30

    Cheesepro
    If you are trying to get their total online time you could do the following:
    store System.currentTimeMilliSeconds() when they log in and when they log out. then store the difference between those two times in a config file, now you have their total online-play time in milliseconds. I think you can make calculations from there :)
    Is this what you want? Or did I not understand it right.
     
Thread Status:
Not open for further replies.

Share This Page