1

I am trying to work on some methods and a response for an action/"command" being able to be used one time a week (spam prevention). My only problem is how to set my java time to set as a week. This is what I have:

public static long displayTime;

Methods:

    public static boolean setDisplayName(Player player, String displayName) {
    synchronized (cachedNames) {
        if((SerializableFilesManager.containsPlayer(Utils.formatPlayerNameForProtocol(displayName)) || cachedNames.contains(displayName) || !cachedNames.add(displayName)))
            return false;
        if(player.hasDisplayName())
            cachedNames.remove(player.getDisplayName());
    }
    displayTime = System.currentTimeMillis() + (1000*60*60);
    String displayname = player.getDisplayName();
    player.setDisplayName(displayName);
    FriendChatsManager.refreshChat(player);
    Highscores.highscores(player, displayname);
    player.getAppearence().generateAppearenceData();
    return true;
}

public static String convertToTime() {
    String time = "You have to wait "+(getTime() == 0 ? "few more seconds" :      getTime()+" minutes")+" to change your display name again!";
    return time;
}

public static int getTime() {
    return (int) (displayTime-System.currentTimeMillis()/60000);
}    

What I am trying to change into the "week's" time is displayTime = System.currentTimeMillis() + (1000*60*60);. I believe (1000*60*60) is 24 hours, corrrect? This is my only problem. When this is solved, the user should only be able to use the command once a week. The command is right here:

case "setdisplay":
            if (DisplayNames.displayTime > System.currentTimeMillis()) {
                player.getPackets().sendGameMessage(DisplayNames.convertToTime());
                return true;
            }
            if (!player.isDonator() && !player.isExtremeDonator()) {
                player.getPackets().sendGameMessage(
                        "You do not have the privileges to use this.");
                return true;
            }

DisplayNames is the class that the methods and everything is in. Thank you for all of the help and I am sure this is just a stupid question :P.

~Derek

2 Answers2

1

You can use Calendar class, it has method to modify date, such as adding day. And it could be converted to Date. It solves most of my problems regarding Time. Hope it works for you too.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
  • Well, would you mind even confirming that the displayTime = System.currentTimeMillis() + (1000*60*60); is 24 hours? If not, what time would this convert to? – user2619426 Sep 11 '13 at 15:40
  • Also, I would not like this to be set as happening once, but every 7 days or 1 week. I don't think the calandar would work because it can be used at different days, which may not work correctly. – user2619426 Sep 11 '13 at 16:22
0

I used Calendar.DAY_OF_WEEK for days in a week

fileyfood500
  • 1,199
  • 1
  • 13
  • 29