forked from TuxCoding/FastLogin
Prevent duplicate save requests
This commit is contained in:
@ -40,27 +40,27 @@ public class PremiumCommand extends ToggleCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UUID id = ((Player) sender).getUniqueId();
|
UUID id = ((Player) sender).getUniqueId();
|
||||||
if (plugin.getConfig().getBoolean("premium-warning") && !plugin.getCore().getPendingConfirms().contains(id)) {
|
if (plugin.getConfig().getBoolean("premium-warning") && !plugin.getCore().getPendingConfirms().contains(id)) {
|
||||||
sender.sendMessage(plugin.getCore().getMessage("premium-warning"));
|
sender.sendMessage(plugin.getCore().getMessage("premium-warning"));
|
||||||
plugin.getCore().getPendingConfirms().add(id);
|
plugin.getCore().getPendingConfirms().add(id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.getCore().getPendingConfirms().remove(id);
|
plugin.getCore().getPendingConfirms().remove(id);
|
||||||
//todo: load async
|
//todo: load async
|
||||||
StoredProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName());
|
StoredProfile profile = plugin.getCore().getStorage().loadProfile(sender.getName());
|
||||||
if (profile.isPremium()) {
|
if (profile.isPremium()) {
|
||||||
plugin.getCore().sendLocaleMessage("already-exists", sender);
|
plugin.getCore().sendLocaleMessage("already-exists", sender);
|
||||||
} else {
|
} else {
|
||||||
//todo: resolve uuid
|
//todo: resolve uuid
|
||||||
profile.setPremium(true);
|
profile.setPremium(true);
|
||||||
plugin.getScheduler().runAsync(() -> {
|
plugin.getScheduler().runAsync(() -> {
|
||||||
plugin.getCore().getStorage().save(profile);
|
plugin.getCore().getStorage().save(profile);
|
||||||
});
|
});
|
||||||
|
|
||||||
plugin.getCore().sendLocaleMessage("add-premium", sender);
|
plugin.getCore().sendLocaleMessage("add-premium", sender);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onPremiumOther(CommandSender sender, Command command, String[] args) {
|
private void onPremiumOther(CommandSender sender, Command command, String[] args) {
|
||||||
|
@ -184,31 +184,36 @@ public class AuthStorage {
|
|||||||
try (Connection con = dataSource.getConnection()) {
|
try (Connection con = dataSource.getConnection()) {
|
||||||
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
|
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
|
||||||
|
|
||||||
if (playerProfile.isSaved()) {
|
playerProfile.getSaveLock().lock();
|
||||||
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
|
try {
|
||||||
saveStmt.setString(1, uuid);
|
if (playerProfile.isSaved()) {
|
||||||
saveStmt.setString(2, playerProfile.getName());
|
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
|
||||||
saveStmt.setBoolean(3, playerProfile.isPremium());
|
saveStmt.setString(1, uuid);
|
||||||
saveStmt.setString(4, playerProfile.getLastIp());
|
saveStmt.setString(2, playerProfile.getName());
|
||||||
|
saveStmt.setBoolean(3, playerProfile.isPremium());
|
||||||
|
saveStmt.setString(4, playerProfile.getLastIp());
|
||||||
|
|
||||||
saveStmt.setLong(5, playerProfile.getRowId());
|
saveStmt.setLong(5, playerProfile.getRowId());
|
||||||
saveStmt.execute();
|
saveStmt.execute();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
|
try (PreparedStatement saveStmt = con.prepareStatement(INSERT_PROFILE, RETURN_GENERATED_KEYS)) {
|
||||||
saveStmt.setString(1, uuid);
|
saveStmt.setString(1, uuid);
|
||||||
|
|
||||||
saveStmt.setString(2, playerProfile.getName());
|
saveStmt.setString(2, playerProfile.getName());
|
||||||
saveStmt.setBoolean(3, playerProfile.isPremium());
|
saveStmt.setBoolean(3, playerProfile.isPremium());
|
||||||
saveStmt.setString(4, playerProfile.getLastIp());
|
saveStmt.setString(4, playerProfile.getLastIp());
|
||||||
|
|
||||||
saveStmt.execute();
|
saveStmt.execute();
|
||||||
try (ResultSet generatedKeys = saveStmt.getGeneratedKeys()) {
|
try (ResultSet generatedKeys = saveStmt.getGeneratedKeys()) {
|
||||||
if (generatedKeys != null && generatedKeys.next()) {
|
if (generatedKeys != null && generatedKeys.next()) {
|
||||||
playerProfile.setRowId(generatedKeys.getInt(1));
|
playerProfile.setRowId(generatedKeys.getInt(1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
playerProfile.getSaveLock().unlock();
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
core.getPlugin().getLog().error("Failed to save playerProfile {}", playerProfile, ex);
|
core.getPlugin().getLog().error("Failed to save playerProfile {}", playerProfile, ex);
|
||||||
|
@ -5,10 +5,12 @@ import com.github.games647.craftapi.model.Profile;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class StoredProfile extends Profile {
|
public class StoredProfile extends Profile {
|
||||||
|
|
||||||
private long rowId;
|
private long rowId;
|
||||||
|
private final ReentrantLock saveLock = new ReentrantLock();
|
||||||
|
|
||||||
private boolean premium;
|
private boolean premium;
|
||||||
private String lastIp;
|
private String lastIp;
|
||||||
@ -27,6 +29,10 @@ public class StoredProfile extends Profile {
|
|||||||
this(-1, uuid, playerName, premium, lastIp, Instant.now());
|
this(-1, uuid, playerName, premium, lastIp, Instant.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReentrantLock getSaveLock() {
|
||||||
|
return saveLock;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized boolean isSaved() {
|
public synchronized boolean isSaved() {
|
||||||
return rowId >= 0;
|
return rowId >= 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user