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.
|
* Legt ein neues Konto mit Startguthaben an, falls noch keines existiert.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -108,17 +108,15 @@ public class BCEconomyProvider implements Economy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
|
public EconomyResponse depositPlayer(OfflinePlayer player, double amount) {
|
||||||
if (amount < 0)
|
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
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);
|
db.createAccountIfAbsent(player.getUniqueId(), startBalance);
|
||||||
current = startBalance;
|
if (!db.addBalance(player.getUniqueId(), amount))
|
||||||
}
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
||||||
double newBal = current + amount;
|
"Datenbankfehler.");
|
||||||
db.saveBalance(player.getUniqueId(), newBal);
|
double newBal = db.loadBalance(player.getUniqueId());
|
||||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,18 +127,17 @@ public class BCEconomyProvider implements Economy {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
|
public EconomyResponse withdrawPlayer(OfflinePlayer player, double amount) {
|
||||||
if (amount < 0)
|
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE,
|
||||||
"Betrag darf nicht negativ sein.");
|
"Ungültiger Betrag.");
|
||||||
|
|
||||||
double current = db.loadBalance(player.getUniqueId());
|
// Atomarer Abzug: parallele Käufe können dasselbe Guthaben nicht doppelt ausgeben
|
||||||
if (current < 0) current = 0;
|
if (!db.withdrawBalance(player.getUniqueId(), amount)) {
|
||||||
if (current < amount)
|
double current = Math.max(0, db.loadBalance(player.getUniqueId()));
|
||||||
return new EconomyResponse(amount, current, EconomyResponse.ResponseType.FAILURE,
|
return new EconomyResponse(amount, current, EconomyResponse.ResponseType.FAILURE,
|
||||||
"Nicht genug Guthaben.");
|
"Nicht genug Guthaben.");
|
||||||
|
}
|
||||||
double newBal = current - amount;
|
double newBal = db.loadBalance(player.getUniqueId());
|
||||||
db.saveBalance(player.getUniqueId(), newBal);
|
|
||||||
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,10 +181,12 @@ public class BCEconomyProvider implements Economy {
|
|||||||
UUID uuid = db.resolveUUIDByName(playerName);
|
UUID uuid = db.resolveUUIDByName(playerName);
|
||||||
if (uuid == null)
|
if (uuid == null)
|
||||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
||||||
double current = db.loadBalance(uuid);
|
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||||
if (current < 0) current = 0;
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Ungültiger Betrag.");
|
||||||
double newBal = current + amount;
|
db.createAccountIfAbsent(uuid, startBalance);
|
||||||
db.saveBalance(uuid, newBal);
|
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);
|
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,12 +200,13 @@ public class BCEconomyProvider implements Economy {
|
|||||||
UUID uuid = db.resolveUUIDByName(playerName);
|
UUID uuid = db.resolveUUIDByName(playerName);
|
||||||
if (uuid == null)
|
if (uuid == null)
|
||||||
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Spieler nicht gefunden.");
|
||||||
double current = db.loadBalance(uuid);
|
if (!(amount >= 0) || Double.isInfinite(amount))
|
||||||
if (current < 0) current = 0;
|
return new EconomyResponse(0, 0, EconomyResponse.ResponseType.FAILURE, "Ungültiger Betrag.");
|
||||||
if (current < amount)
|
if (!db.withdrawBalance(uuid, amount)) {
|
||||||
|
double current = Math.max(0, db.loadBalance(uuid));
|
||||||
return new EconomyResponse(amount, current, EconomyResponse.ResponseType.FAILURE, "Nicht genug Guthaben.");
|
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);
|
return new EconomyResponse(amount, newBal, EconomyResponse.ResponseType.SUCCESS, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user