Optional migration

This commit is contained in:
games647
2018-03-05 21:27:48 +01:00
parent 8272aeac69
commit f250f8071f
8 changed files with 18 additions and 28 deletions

View File

@ -4,6 +4,8 @@ import com.github.games647.fastlogin.core.PlayerProfile;
import com.github.games647.fastlogin.core.mojang.SkinProperties;
import com.github.games647.fastlogin.core.shared.LoginSession;
import java.util.Optional;
import org.apache.commons.lang.ArrayUtils;
/**
@ -63,8 +65,8 @@ public class BukkitLoginSession extends LoginSession {
}
//todo: this should be optional for players without a skin at all
public synchronized SkinProperties getSkinProperty() {
return skinProperty;
public synchronized Optional<SkinProperties> getSkin() {
return Optional.ofNullable(skinProperty);
}
/**

View File

@ -36,7 +36,7 @@ public class CrackedCommand implements CommandExecutor {
plugin.getCore().sendLocaleMessage("remove-premium", sender);
profile.setPremium(false);
profile.setUuid(null);
profile.setId(null);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getCore().getStorage().save(profile);
});

View File

@ -44,11 +44,7 @@ public class SkinApplyListener implements Listener {
//loginEvent.getAddress is just a InetAddress not InetSocketAddress, so not unique enough
for (BukkitLoginSession session : plugin.getLoginSessions().values()) {
if (session.getUsername().equals(player.getName())) {
SkinProperties skinProperty = session.getSkinProperty();
if (skinProperty != null) {
applySkin(player, skinProperty.getValue(), skinProperty.getSignature());
}
session.getSkin().ifPresent(skin -> applySkin(player, skin.getValue(), skin.getSignature()));
break;
}
}

View File

@ -68,7 +68,7 @@ public class ConnectListener implements Listener {
session.setUuid(connection.getUniqueId());
PlayerProfile playerProfile = session.getProfile();
playerProfile.setUuid(connection.getUniqueId());
playerProfile.setId(connection.getUniqueId());
//bungeecord will do this automatically so override it on disabled option
if (!plugin.getCore().getConfig().get("premiumUuid", true)) {

View File

@ -44,7 +44,7 @@ public class AsyncToggleMessage implements Runnable {
}
playerProfile.setPremium(false);
playerProfile.setUuid(null);
playerProfile.setId(null);
core.getStorage().save(playerProfile);
sendMessage("remove-premium");
}

View File

@ -142,16 +142,11 @@ public class AuthStorage {
public void save(PlayerProfile playerProfile) {
try (Connection con = dataSource.getConnection()) {
UUID uuid = playerProfile.getUuid();
String uuid = playerProfile.getId().map(UUIDTypeAdapter::toMojangId).orElse(null);
if (playerProfile.isSaved()) {
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
if (uuid == null) {
saveStmt.setString(1, null);
} else {
saveStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
}
saveStmt.setString(1, uuid);
saveStmt.setString(2, playerProfile.getPlayerName());
saveStmt.setBoolean(3, playerProfile.isPremium());
saveStmt.setString(4, playerProfile.getLastIp());
@ -161,11 +156,7 @@ public class AuthStorage {
}
} else {
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
if (uuid == null) {
saveStmt.setString(1, null);
} else {
saveStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
}
saveStmt.setString(1, uuid);
saveStmt.setString(2, playerProfile.getPlayerName());
saveStmt.setBoolean(3, playerProfile.isPremium());

View File

@ -1,6 +1,7 @@
package com.github.games647.fastlogin.core;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
public class PlayerProfile {
@ -48,11 +49,11 @@ public class PlayerProfile {
}
//todo: this should be optional
public synchronized UUID getUuid() {
return uuid;
public synchronized Optional<UUID> getId() {
return Optional.ofNullable(uuid);
}
public synchronized void setUuid(UUID uuid) {
public synchronized void setId(UUID uuid) {
this.uuid = uuid;
}
@ -81,7 +82,7 @@ public class PlayerProfile {
}
@Override
public String toString() {
public synchronized String toString() {
return this.getClass().getSimpleName() + '{' +
"playerName='" + playerName + '\'' +
", rowId=" + rowId +

View File

@ -49,7 +49,7 @@ public abstract class ForceLoginManagement<P extends C, C, L extends LoginSessio
if (success) {
//update only on success to prevent corrupt data
if (playerProfile != null) {
playerProfile.setUuid(session.getUuid());
playerProfile.setId(session.getUuid());
playerProfile.setPremium(true);
storage.save(playerProfile);
}
@ -59,7 +59,7 @@ public abstract class ForceLoginManagement<P extends C, C, L extends LoginSessio
}
} else if (playerProfile != null) {
//cracked player
playerProfile.setUuid(null);
playerProfile.setId(null);
playerProfile.setPremium(false);
storage.save(playerProfile);
}