问题描述:

So I'm trying to make the System.currentTimeMillis(); a constant. It's difficult to explain this. What the code is supposed to do is add a value to the System.currentTimeMillis(); which it does and wait till the code returns true and then execute an action. So basically, I'm trying to make a "temp-ban" system. Because System.currentTimeMillis isn't a constant value, of course, this is going to return false. I'm wondering what would I exactly do to make this code become true

long timeleft = StaticMaps.muteMap.get(uuid).getTime() * 1000; //seconds to miliseconds

if (System.currentTimeMillis() >= timeleft + System.currentTimeMillis()) {

plugin.mutemanager.destructPlayerMute(uuid, "Expired", "Removed by Console, Expired!");

} else {

KTools.notify("debug");

e.setCancelled(true);

}

Solution I used

Store the value as a "Long" inside a map with the System.currentTimeMillis();

So what you would do is.

Map<UUID, Long> temp = new HashMap<>();

Long time = 3 * 1000; //3 * 1000 = 3 Seconds

temp.put(Identifier, System.currentTimeMillis() + timetoadd)

timetoadd needs to be a long.

Then check if System.currentTimeMillis() is >= the value in the hashmap.

 long HOUR = 3600000;

long DAY = 86400000;

long dayformula = Long.parseLong(parts[0]) * DAY;

long hourformula = Long.parseLong(parts[1]) * HOUR;

long totalmiliseconds = dayformula + hourformula;

网友答案:

You don't want to make that into a constant. You want to store off its value for a given moment in time and use that to check against the current system time.

That's easily accomplished thus:

long timeleft = StaticMaps.muteMap.get(uuid).getTime() * 1000; //seconds to miliseconds
long lastCheckedTime = System.currentTimeMillis();
if (System.currentTimeMillis() >= timeleft + lastCheckedTime) {
    plugin.mutemanager.destructPlayerMute(uuid, "Expired", "Removed by Console, Expired!");

} else {
    KTools.notify("debug");
    e.setCancelled(true);
}

...although to be fair, that statement is almost guaranteed to be false unless timeLeft is on the order of microseconds.

网友答案:

You can't make System.currentTimeMillis(); a constant (var/value) because that's not a var/value, but rather a function that returns one.


What you want to do is save timestamps of the current time (using that), plus ban/mute time, into a map, and then either schedule the lift of the ban/unmute, or manage it yourself by periodically checking System.currentTimeMillis(); and if it's past (above) any expected ban-lift/unmute time (the ones you saved), applying the necessary actions (lifting ban or unmuting).


I'm currently on my phone, and I can't give an example of the schedule method out of my (not very reliable) memory...

But, if it's the managed one, I can try:

    //How to "schedule" the lift...
    public /*static ?*/ void mutePlayer(??? uuid, long muteTime){
    StaticMaps.muteMap.put(uuid, System.currentTimeMillis()+muteTime); //I'm assuming how the syntax is...Adapt as needed.
     }

    //Somewhere else, inside a loop that runs periodically...
    for(Entry<uuid, long> scheduled : Staticamaps.muteMap.entrySet()){
    if (System.currentTimeMillis() >= scheduled.value() /*lift timestamp*/)) {
    plugin.mutemanager.destructPlayerMute(scheduled.key() /*uuid*/, "Expired", "Mute has expired!");
    } else {
    KTools.notify("debug");
    e.setCancelled(true);
    }
    }

Note that, since I can't know the syntax or the functions available to muteMap, some of the ones I used should be considered pseudo-code.

相关阅读:
Top