Skip to content

Commit 9c369da

Browse files
committed
Added function to save the user login
1 parent b569b2c commit 9c369da

File tree

6 files changed

+31
-6
lines changed

6 files changed

+31
-6
lines changed

multiworld-api/multiworld-api-core/src/main/java/com/dev7ex/multiworld/api/user/WorldUser.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
/**
1010
* @author Dev7ex
1111
* @since 18.06.2023
12-
*
12+
* <p>
1313
* Represents a User
14-
*
1514
*/
1615
public interface WorldUser {
1716

@@ -28,6 +27,10 @@ public interface WorldUser {
2827

2928
void setLastLocation(@Nullable final WorldLocation location);
3029

30+
long getLastLogin();
31+
32+
void setLastLogin(final long lastLogin);
33+
3134
void sendMessage(@NotNull final String message);
3235

3336
}

multiworld-api/multiworld-api-core/src/main/java/com/dev7ex/multiworld/api/user/WorldUserProperty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public enum WorldUserProperty {
1313

1414
UNIQUE_ID("unique-id"),
1515
NAME("name"),
16-
LAST_LOCATION("last-location");
16+
LAST_LOCATION("last-location"),
17+
LAST_LOGIN("last-login");
1718

1819
private final String storagePath;
1920

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/listener/player/PlayerConnectionListener.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ public void handlePlayerLogin(final PlayerLoginEvent event) {
3333
final Player player = event.getPlayer();
3434
final WorldUser user = new User(player.getUniqueId(), player.getName());
3535
final WorldUserConfiguration userConfiguration = new UserConfiguration(user);
36-
final ParsedMap<WorldUserProperty, Object> userData = userConfiguration.read(WorldUserProperty.LAST_LOCATION);
36+
final ParsedMap<WorldUserProperty, Object> userData = userConfiguration.read();
3737

3838
user.setLastLocation(userData.getValue(WorldUserProperty.LAST_LOCATION));
39+
user.setLastLogin(userData.getLong(WorldUserProperty.LAST_LOGIN));
3940
user.setConfiguration(userConfiguration);
4041

4142
super.getUserProvider().registerUser(user);

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/user/User.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class User implements WorldUser, EntityResolver<Player> {
2525
private final String name;
2626
private WorldUserConfiguration configuration;
2727
private WorldLocation lastLocation;
28+
private long lastLogin;
2829

2930
public User(@NotNull final UUID uniqueId, @NotNull final String name) {
3031
this.uniqueId = uniqueId;

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/user/UserConfiguration.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public UserConfiguration(@NotNull final WorldUser user) {
3636
this.fileConfiguration.addDefault(WorldUserProperty.UNIQUE_ID.getStoragePath(), user.getUniqueId().toString());
3737
this.fileConfiguration.addDefault(WorldUserProperty.NAME.getStoragePath(), user.getName());
3838
this.fileConfiguration.addDefault(WorldUserProperty.LAST_LOCATION.getStoragePath(), null);
39+
this.fileConfiguration.addDefault(WorldUserProperty.LAST_LOGIN.getStoragePath(), System.currentTimeMillis());
3940
this.fileConfiguration.options().copyDefaults(true);
4041
this.saveFile();
4142
}
@@ -57,6 +58,10 @@ public ParsedMap<WorldUserProperty, Object> read() {
5758
case LAST_LOCATION:
5859
userData.put(property, this.fileConfiguration.getSerializable(property.getStoragePath(), BukkitWorldLocation.class));
5960
break;
61+
62+
case LAST_LOGIN:
63+
userData.put(property, this.fileConfiguration.getLong(property.getStoragePath(), System.currentTimeMillis()));
64+
break;
6065
}
6166
});
6267
return userData;
@@ -83,6 +88,10 @@ public ParsedMap<WorldUserProperty, Object> read(final WorldUserProperty... prop
8388
userData.put(property, this.fileConfiguration.getSerializable(property.getStoragePath(), BukkitWorldLocation.class));
8489
break;
8590

91+
case LAST_LOGIN:
92+
userData.put(property, this.fileConfiguration.getLong(property.getStoragePath()));
93+
break;
94+
8695
default:
8796
break;
8897
}
@@ -105,6 +114,10 @@ public void write(final ParsedMap<WorldUserProperty, Object> userData) {
105114
case LAST_LOCATION:
106115
this.fileConfiguration.set(property.getStoragePath(), userData.get(property));
107116
break;
117+
118+
case LAST_LOGIN:
119+
this.fileConfiguration.set(property.getStoragePath(), userData.getLong(property));
120+
break;
108121
}
109122
}
110123
this.saveFile();

multiworld-bukkit/src/main/java/com/dev7ex/multiworld/user/UserService.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public void onEnable() {
3131
for (final Player player : Bukkit.getOnlinePlayers()) {
3232
final WorldUser user = new User(player.getUniqueId(), player.getName());
3333
final WorldUserConfiguration userConfiguration = new UserConfiguration(user);
34-
final ParsedMap<WorldUserProperty, Object> userData = userConfiguration.read(WorldUserProperty.LAST_LOCATION);
34+
final ParsedMap<WorldUserProperty, Object> userData = userConfiguration.read();
3535

36+
user.setLastLogin(userData.getLong(WorldUserProperty.LAST_LOGIN));
3637
user.setLastLocation(userData.getValue(WorldUserProperty.LAST_LOCATION));
3738
user.setConfiguration(userConfiguration);
3839

@@ -72,7 +73,8 @@ public Optional<WorldUser> getUser(@NotNull final String name) {
7273
public void saveUser(@NotNull final WorldUser user) {
7374
this.saveUser(user, WorldUserProperty.UNIQUE_ID,
7475
WorldUserProperty.NAME,
75-
WorldUserProperty.LAST_LOCATION);
76+
WorldUserProperty.LAST_LOCATION,
77+
WorldUserProperty.LAST_LOGIN);
7678
}
7779

7880
@Override
@@ -93,6 +95,10 @@ public void saveUser(@NotNull final WorldUser user, @NotNull final WorldUserProp
9395
data.put(property, user.getLastLocation());
9496
break;
9597

98+
case LAST_LOGIN:
99+
data.put(property, user.getLastLogin());
100+
break;
101+
96102
default:
97103
this.saveUser(user);
98104
break;

0 commit comments

Comments
 (0)