Upload via GUI (6 Dateien)
This commit is contained in:
@@ -131,6 +131,44 @@ public class BCDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomare Gutschrift (kein Lesen-Rechnen-Schreiben, daher keine verlorenen Updates).
|
||||
* Das Konto muss existieren (vorher createAccountIfAbsent).
|
||||
* @return true wenn eine Zeile geändert wurde
|
||||
*/
|
||||
public boolean addBalance(UUID uuid, double amount) {
|
||||
if (!isConnected()) return false;
|
||||
try (Connection con = dataSource.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(
|
||||
"UPDATE `" + TABLE + "` SET `balance` = `balance` + ? WHERE `player_name` = ?")) {
|
||||
ps.setDouble(1, amount);
|
||||
ps.setString(2, uuid.toString());
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
log.warning("[BCEconomy] addBalance fehlgeschlagen für " + uuid + ": " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomarer Abzug – nur wenn genug Guthaben vorhanden ist.
|
||||
* @return true wenn abgezogen wurde, false bei zu wenig Guthaben oder fehlendem Konto
|
||||
*/
|
||||
public boolean withdrawBalance(UUID uuid, double amount) {
|
||||
if (!isConnected()) return false;
|
||||
try (Connection con = dataSource.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement(
|
||||
"UPDATE `" + TABLE + "` SET `balance` = `balance` - ? WHERE `player_name` = ? AND `balance` >= ?")) {
|
||||
ps.setDouble(1, amount);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.setDouble(3, amount);
|
||||
return ps.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
log.warning("[BCEconomy] withdrawBalance fehlgeschlagen für " + uuid + ": " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legt ein neues Konto mit Startguthaben an, falls noch keines existiert.
|
||||
*/
|
||||
|
||||
@@ -108,17 +108,15 @@ public class BCEconomyProvider implements Economy {
|
||||
|
||||
@Override
|
||||
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
|
||||
if (amount < 0)
|
||||
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
||||
"Betrag darf nicht negativ sein.");
|
||||
"Ungültiger Betrag.");
|
||||
|
||||
double current = db.loadBalance(player.getUniqueId());
|
||||
if (current < 0) {
|
||||
db.createAccountIfAbsent(player.getUniqueId(), startBalance);
|
||||
current = startBalance;
|
||||
}
|
||||
double newBal = current + amount;
|
||||
db.saveBalance(player.getUniqueId(), newBal);
|
||||
db.createAccountIfAbsent(player.getUniqueId(), startBalance);
|
||||
if (!db.addBalance(player.getUniqueId(), amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
||||
"Datenbankfehler.");
|
||||
double newBal = db.loadBalance(player.getUniqueId());
|
||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
@@ -129,18 +127,17 @@ public class BCEconomyProvider implements Economy {
|
||||
|
||||
@Override
|
||||
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
|
||||
if (amount < 0)
|
||||
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
||||
"Betrag darf nicht negativ sein.");
|
||||
"Ungültiger Betrag.");
|
||||
|
||||
double current = db.loadBalance(player.getUniqueId());
|
||||
if (current < 0) current = 0;
|
||||
if (current < amount)
|
||||
// Atomarer Abzug: parallele Käufe können dasselbe Guthaben nicht doppelt ausgeben
|
||||
if (!db.withdrawBalance(player.getUniqueId(), amount)) {
|
||||
double current = Math.max(0, db.loadBalance(player.getUniqueId()));
|
||||
return new EconomyResponse(amount, current, EconomyResponse.ResponseType.FAILURE,
|
||||
"Nicht genug Guthaben.");
|
||||
|
||||
double newBal = current - amount;
|
||||
db.saveBalance(player.getUniqueId(), newBal);
|
||||
}
|
||||
double newBal = db.loadBalance(player.getUniqueId());
|
||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
@@ -184,10 +181,12 @@ public class BCEconomyProvider implements Economy {
|
||||
UUID uuid = db.resolveUUIDByName(playerName);
|
||||
if (uuid == null)
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
||||
double current = db.loadBalance(uuid);
|
||||
if (current < 0) current = 0;
|
||||
double newBal = current + amount;
|
||||
db.saveBalance(uuid, newBal);
|
||||
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Ungültiger Betrag.");
|
||||
db.createAccountIfAbsent(uuid, startBalance);
|
||||
if (!db.addBalance(uuid, amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Datenbankfehler.");
|
||||
double newBal = db.loadBalance(uuid);
|
||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
@@ -201,12 +200,13 @@ public class BCEconomyProvider implements Economy {
|
||||
UUID uuid = db.resolveUUIDByName(playerName);
|
||||
if (uuid == null)
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
||||
double current = db.loadBalance(uuid);
|
||||
if (current < 0) current = 0;
|
||||
if (current < amount)
|
||||
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Ungültiger Betrag.");
|
||||
if (!db.withdrawBalance(uuid, amount)) {
|
||||
double current = Math.max(0, db.loadBalance(uuid));
|
||||
return new EconomyResponse(amount, current, EconomyResponse.ResponseType.FAILURE, "Nicht genug Guthaben.");
|
||||
double newBal = current - amount;
|
||||
db.saveBalance(uuid, newBal);
|
||||
}
|
||||
double newBal = db.loadBalance(uuid);
|
||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user