mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-24 09:37:42 +01:00
Deprecate getLastLogin, replace with java 8 getLastLoginTime
(Resolves #1421)
This commit is contained in:
parent
26a69297ce
commit
6f52449d49
@ -12,11 +12,14 @@ import fr.xephi.authme.security.crypts.HashedPassword;
|
|||||||
import fr.xephi.authme.service.GeoIpService;
|
import fr.xephi.authme.service.GeoIpService;
|
||||||
import fr.xephi.authme.service.ValidationService;
|
import fr.xephi.authme.service.ValidationService;
|
||||||
import fr.xephi.authme.util.PlayerUtils;
|
import fr.xephi.authme.util.PlayerUtils;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -159,18 +162,38 @@ public class AuthMeApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the last login date of a player.
|
* Get the last (AuthMe) login date of a player.
|
||||||
*
|
*
|
||||||
* @param playerName The name of the player to process
|
* @param playerName The name of the player to process
|
||||||
|
*
|
||||||
* @return The date of the last login, or null if the player doesn't exist or has never logged in
|
* @return The date of the last login, or null if the player doesn't exist or has never logged in
|
||||||
|
* @Deprecated Use Java 8's Instant method {@link #getLastLoginTime(String)}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public Date getLastLogin(String playerName) {
|
public Date getLastLogin(String playerName) {
|
||||||
|
Long lastLogin = getLastLoginMillis(playerName);
|
||||||
|
return lastLogin == null ? null : new Date(lastLogin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the last (AuthMe) login timestamp of a player.
|
||||||
|
*
|
||||||
|
* @param playerName The name of the player to process
|
||||||
|
*
|
||||||
|
* @return The timestamp of the last login, or null if the player doesn't exist or has never logged in
|
||||||
|
*/
|
||||||
|
public Instant getLastLoginTime(String playerName) {
|
||||||
|
Long lastLogin = getLastLoginMillis(playerName);
|
||||||
|
return lastLogin == null ? null : Instant.ofEpochMilli(lastLogin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Long getLastLoginMillis(String playerName) {
|
||||||
PlayerAuth auth = playerCache.getAuth(playerName);
|
PlayerAuth auth = playerCache.getAuth(playerName);
|
||||||
if (auth == null) {
|
if (auth == null) {
|
||||||
auth = dataSource.getAuth(playerName);
|
auth = dataSource.getAuth(playerName);
|
||||||
}
|
}
|
||||||
if (auth != null && auth.getLastLogin() != null) {
|
if (auth != null) {
|
||||||
return new Date(auth.getLastLogin());
|
return auth.getLastLogin();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,7 @@ import java.util.Arrays;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo;
|
import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.Matchers.contains;
|
||||||
@ -202,7 +203,7 @@ public class AuthMeApiTest {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(result, not(nullValue()));
|
assertThat(result, not(nullValue()));
|
||||||
assertThat(result, equalTo(new Date(1501597979)));
|
assertThat(result, equalTo(new Date(1501597979L)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -222,6 +223,40 @@ public class AuthMeApiTest {
|
|||||||
verify(dataSource).getAuth(name);
|
verify(dataSource).getAuth(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldGetLastLoginTime() {
|
||||||
|
// given
|
||||||
|
String name = "David";
|
||||||
|
PlayerAuth auth = PlayerAuth.builder().name(name)
|
||||||
|
.lastLogin(1501597979L)
|
||||||
|
.build();
|
||||||
|
given(playerCache.getAuth(name)).willReturn(auth);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Instant result = api.getLastLoginTime(name);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, not(nullValue()));
|
||||||
|
assertThat(result, equalTo(Instant.ofEpochMilli(1501597979L)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldHandleNullLastLoginTime() {
|
||||||
|
// given
|
||||||
|
String name = "John";
|
||||||
|
PlayerAuth auth = PlayerAuth.builder().name(name)
|
||||||
|
.lastLogin(null)
|
||||||
|
.build();
|
||||||
|
given(dataSource.getAuth(name)).willReturn(auth);
|
||||||
|
|
||||||
|
// when
|
||||||
|
Instant result = api.getLastLoginTime(name);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, nullValue());
|
||||||
|
verify(dataSource).getAuth(name);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldReturnNullForUnavailablePlayer() {
|
public void shouldReturnNullForUnavailablePlayer() {
|
||||||
// given
|
// given
|
||||||
|
Loading…
Reference in New Issue
Block a user