Add isSaved helper

This commit is contained in:
games647
2018-03-02 19:56:17 +01:00
parent 86694982c7
commit e2c04f2c26
5 changed files with 39 additions and 33 deletions

View File

@ -71,7 +71,7 @@ public class CrackedCommand implements CommandExecutor {
} }
//existing player is already cracked //existing player is already cracked
if (profile.getUserId() != -1 && !profile.isPremium()) { if (profile.isSaved() && !profile.isPremium()) {
plugin.getCore().sendLocaleMessage("not-premium-other", sender); plugin.getCore().sendLocaleMessage("not-premium-other", sender);
} else { } else {
plugin.getCore().sendLocaleMessage("remove-premium", sender); plugin.getCore().sendLocaleMessage("remove-premium", sender);

View File

@ -38,7 +38,7 @@ public class AsyncToggleMessage implements Runnable {
private void turnOffPremium() { private void turnOffPremium() {
PlayerProfile playerProfile = core.getStorage().loadProfile(targetPlayer); PlayerProfile playerProfile = core.getStorage().loadProfile(targetPlayer);
//existing player is already cracked //existing player is already cracked
if (playerProfile.getUserId() != -1 && !playerProfile.isPremium()) { if (playerProfile.isSaved() && !playerProfile.isPremium()) {
sendMessage("not-premium"); sendMessage("not-premium");
return; return;
} }

View File

@ -143,8 +143,23 @@ public class AuthStorage {
public void save(PlayerProfile playerProfile) { public void save(PlayerProfile playerProfile) {
try (Connection con = dataSource.getConnection()) { try (Connection con = dataSource.getConnection()) {
UUID uuid = playerProfile.getUuid(); UUID uuid = playerProfile.getUuid();
if (playerProfile.getUserId() == -1) { 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(2, playerProfile.getPlayerName());
saveStmt.setBoolean(3, playerProfile.isPremium());
saveStmt.setString(4, playerProfile.getLastIp());
saveStmt.setLong(5, playerProfile.getRowId());
saveStmt.execute();
}
} else {
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) { try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
if (uuid == null) { if (uuid == null) {
saveStmt.setString(1, null); saveStmt.setString(1, null);
@ -160,25 +175,10 @@ public class AuthStorage {
try (ResultSet generatedKeys = saveStmt.getGeneratedKeys()) { try (ResultSet generatedKeys = saveStmt.getGeneratedKeys()) {
if (generatedKeys != null && generatedKeys.next()) { if (generatedKeys != null && generatedKeys.next()) {
playerProfile.setUserId(generatedKeys.getInt(1)); playerProfile.setRowId(generatedKeys.getInt(1));
} }
} }
} }
} else {
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
if (uuid == null) {
saveStmt.setString(1, null);
} else {
saveStmt.setString(1, UUIDTypeAdapter.toMojangId(uuid));
}
saveStmt.setString(2, playerProfile.getPlayerName());
saveStmt.setBoolean(3, playerProfile.isPremium());
saveStmt.setString(4, playerProfile.getLastIp());
saveStmt.setLong(5, playerProfile.getUserId());
saveStmt.execute();
}
} }
} catch (SQLException ex) { } catch (SQLException ex) {

View File

@ -7,15 +7,15 @@ public class PlayerProfile {
private String playerName; private String playerName;
private long userId; private long rowId;
private UUID uuid; private UUID uuid;
private boolean premium; private boolean premium;
private String lastIp; private String lastIp;
private Instant lastLogin; private Instant lastLogin;
public PlayerProfile(long userId, UUID uuid, String playerName, boolean premium, String lastIp, Instant lastLogin) { public PlayerProfile(long rowId, UUID uuid, String playerName, boolean premium, String lastIp, Instant lastLogin) {
this.userId = userId; this.rowId = rowId;
this.uuid = uuid; this.uuid = uuid;
this.playerName = playerName; this.playerName = playerName;
this.premium = premium; this.premium = premium;
@ -27,6 +27,10 @@ public class PlayerProfile {
this(-1, uuid, playerName, premium, lastIp, Instant.now()); this(-1, uuid, playerName, premium, lastIp, Instant.now());
} }
public synchronized boolean isSaved() {
return rowId >= 0;
}
public synchronized String getPlayerName() { public synchronized String getPlayerName() {
return playerName; return playerName;
} }
@ -35,12 +39,12 @@ public class PlayerProfile {
this.playerName = playerName; this.playerName = playerName;
} }
public synchronized long getUserId() { public synchronized long getRowId() {
return userId; return rowId;
} }
public synchronized void setUserId(long generatedId) { public synchronized void setRowId(long generatedId) {
this.userId = generatedId; this.rowId = generatedId;
} }
//todo: this should be optional //todo: this should be optional
@ -80,7 +84,7 @@ public class PlayerProfile {
public String toString() { public String toString() {
return this.getClass().getSimpleName() + '{' + return this.getClass().getSimpleName() + '{' +
"playerName='" + playerName + '\'' + "playerName='" + playerName + '\'' +
", userId=" + userId + ", rowId=" + rowId +
", uuid=" + uuid + ", uuid=" + uuid +
", premium=" + premium + ", premium=" + premium +
", lastIp='" + lastIp + '\'' + ", lastIp='" + lastIp + '\'' +

View File

@ -29,7 +29,13 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
String ip = source.getAddress().getAddress().getHostAddress(); String ip = source.getAddress().getAddress().getHostAddress();
profile.setLastIp(ip); profile.setLastIp(ip);
try { try {
if (profile.getUserId() == -1) { if (profile.isSaved()) {
if (profile.isPremium()) {
requestPremiumLogin(source, profile, username, true);
} else {
startCrackedSession(source, profile, username);
}
} else {
if (core.getPendingLogin().remove(ip + username) != null && config.get("secondAttemptCracked", false)) { if (core.getPendingLogin().remove(ip + username) != null && config.get("secondAttemptCracked", false)) {
core.getPlugin().getLog().info("Second attempt login -> cracked {}", username); core.getPlugin().getLog().info("Second attempt login -> cracked {}", username);
@ -54,10 +60,6 @@ public abstract class JoinManagement<P extends C, C, S extends LoginSource> {
startCrackedSession(source, profile, username); startCrackedSession(source, profile, username);
} }
} else if (profile.isPremium()) {
requestPremiumLogin(source, profile, username, true);
} else {
startCrackedSession(source, profile, username);
} }
} catch (Exception ex) { } catch (Exception ex) {
core.getPlugin().getLog().error("Failed to check premium state", ex); core.getPlugin().getLog().error("Failed to check premium state", ex);