SteamWar/SpigotCore
Archiviert
13
0

New Statement system + ErrorHandler
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
Lixfel 2021-10-22 15:58:21 +02:00
Ursprung 4682f65063
Commit 1bc25aac99
15 geänderte Dateien mit 169 neuen und 223 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.core; package de.steamwar.core;
import de.steamwar.sql.SQL;
import de.steamwar.sql.SWException; import de.steamwar.sql.SWException;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -80,6 +81,9 @@ public class ErrorHandler extends Handler {
if(stacktrace.contains("POI data mismatch")) if(stacktrace.contains("POI data mismatch"))
return; return;
if(!SQL.connectionStable())
return;
SWException.log(message, stacktrace); SWException.log(message, stacktrace);
} }

Datei anzeigen

@ -19,14 +19,15 @@
package de.steamwar.sql; package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
public class BauweltMember{ public class BauweltMember{
private static final SQL.Statement getMember = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?");
private static final SQL.Statement getMembers = new SQL.Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?");
private final int bauweltID; private final int bauweltID;
private final int memberID; private final int memberID;
private boolean worldEdit; private boolean worldEdit;
@ -56,11 +57,13 @@ public class BauweltMember{
this(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId(), worldEdit, world, true); this(SteamwarUser.get(ownerID).getId(), SteamwarUser.get(memberID).getId(), worldEdit, world, true);
} }
@Deprecated
public void remove(){ public void remove(){
SQL.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", bauweltID, memberID); SQL.update("DELETE FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", bauweltID, memberID);
members.remove(this); members.remove(this);
} }
@Deprecated
private void updateDB(){ private void updateDB(){
SQL.update("INSERT INTO BauweltMember (BauweltID, MemberID, WorldEdit, World) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE WorldEdit = VALUES(WorldEdit), World = VALUES(World)", SQL.update("INSERT INTO BauweltMember (BauweltID, MemberID, WorldEdit, World) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE WorldEdit = VALUES(WorldEdit), World = VALUES(World)",
bauweltID, memberID, worldEdit, world); bauweltID, memberID, worldEdit, world);
@ -74,17 +77,11 @@ public class BauweltMember{
for(BauweltMember member : members) for(BauweltMember member : members)
if(member.memberID == memberID) if(member.memberID == memberID)
return member; return member;
ResultSet member = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?", ownerID, memberID); return getMember.select(rs -> {
try { if(!rs.next())
if(member == null || !member.next()){
return null; return null;
} return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false);
boolean worldEdit = member.getBoolean("WorldEdit"); }, ownerID, memberID);
boolean testblock = member.getBoolean("World");
return new BauweltMember(ownerID, memberID, worldEdit, testblock, false);
} catch (SQLException e) {
throw new SecurityException("Did not get member", e);
}
} }
public static List<BauweltMember> getMembers(UUID bauweltID){ public static List<BauweltMember> getMembers(UUID bauweltID){
@ -92,19 +89,12 @@ public class BauweltMember{
} }
public static List<BauweltMember> getMembers(int bauweltID){ public static List<BauweltMember> getMembers(int bauweltID){
try{ return getMembers.select(rs -> {
ResultSet memberlist = SQL.select("SELECT * FROM BauweltMember WHERE BauweltID = ?", bauweltID); List<BauweltMember> members = new ArrayList<>();
List<BauweltMember> members = new LinkedList<>(); while(rs.next())
while(memberlist.next()){ members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"), false));
int memberID = memberlist.getInt("MemberID");
boolean worldEdit = memberlist.getBoolean("WorldEdit");
boolean testblock = memberlist.getBoolean("World");
members.add(new BauweltMember(bauweltID, memberID, worldEdit, testblock, false));
}
return members; return members;
}catch(SQLException e){ }, bauweltID);
throw new SecurityException("Did not get members", e);
}
} }
public int getBauweltID() { public int getBauweltID() {
@ -119,6 +109,7 @@ public class BauweltMember{
return worldEdit; return worldEdit;
} }
@Deprecated
public void setWorldEdit(boolean worldEdit) { public void setWorldEdit(boolean worldEdit) {
this.worldEdit = worldEdit; this.worldEdit = worldEdit;
updateDB(); updateDB();
@ -128,6 +119,7 @@ public class BauweltMember{
return world; return world;
} }
@Deprecated
public void setWorld(boolean world) { public void setWorld(boolean world) {
this.world = world; this.world = world;
updateDB(); updateDB();

Datei anzeigen

@ -19,19 +19,16 @@
package de.steamwar.sql; package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
public class CheckedSchematic { public class CheckedSchematic {
private static final SQL.Statement checkHistory = new SQL.Statement("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
private final String schemName; private final String schemName;
private final int schemOwner; private final int schemOwner;
private final int validator; private final int validator;
@ -50,14 +47,17 @@ public class CheckedSchematic {
insertDB(); insertDB();
} }
@Deprecated
public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){ public CheckedSchematic(String schemName, int schemOwner, int validator, Timestamp startTime, Timestamp endTime, String declineReason){
this(schemName, schemOwner, validator, startTime, endTime, declineReason, true); this(schemName, schemOwner, validator, startTime, endTime, declineReason, true);
} }
@Deprecated
public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){ public CheckedSchematic(String schemName, UUID schemOwner, UUID validator, Timestamp startTime, Timestamp endTime, String declineReason){
this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true); this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(validator).getId(), startTime, endTime, declineReason, true);
} }
@Deprecated
private void insertDB(){ private void insertDB(){
SQL.update("INSERT INTO CheckedSchematic" + SQL.update("INSERT INTO CheckedSchematic" +
" (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)", " (SchemName, SchemOwner, Validator, StartTime, EndTime, DeclineReason) VALUES (?, ?, ?, ?, ?, ?)",
@ -69,23 +69,15 @@ public class CheckedSchematic {
} }
public static List<CheckedSchematic> getLastDelined(int schemOwner){ public static List<CheckedSchematic> getLastDelined(int schemOwner){
List<CheckedSchematic> lastDeclined = new LinkedList<>(); return checkHistory.select(rs -> {
try{ List<CheckedSchematic> history = new ArrayList<>();
ResultSet lastRS = SQL.select("SELECT * FROM CheckedSchematic WHERE SchemOwner = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC", schemOwner); while(rs.next())
while(lastRS.next()){ history.add(new CheckedSchematic(rs.getString("SchemName"), schemOwner, rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"), false));
String schemName = lastRS.getString("SchemName"); return history;
int validator = lastRS.getInt("Validator"); }, schemOwner);
Timestamp startTime = lastRS.getTimestamp("StartTime");
Timestamp endTime = lastRS.getTimestamp("EndTime");
String declineReason = lastRS.getString("DeclineReason");
lastDeclined.add(new CheckedSchematic(schemName, schemOwner, validator, startTime, endTime, declineReason, false));
}
}catch(SQLException e){
Bukkit.getLogger().log(Level.SEVERE, "getLastDeclined failed", e);
}
return lastDeclined;
} }
@Deprecated
public void remove() { public void remove() {
SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName); SQL.update("DELETE FROM CheckedSchematic WHERE SchemOwner = ? AND SchemName = ?", schemOwner, schemName);
} }

Datei anzeigen

@ -28,6 +28,8 @@ import java.time.Instant;
public class DownloadSchematic { public class DownloadSchematic {
private DownloadSchematic(){} private DownloadSchematic(){}
private static final SQL.Statement createLink = new SQL.Statement("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)");
private static final String BASE = "https://steamwar.de/download.php?schem="; private static final String BASE = "https://steamwar.de/download.php?schem=";
public static String getLink(Schematic schem){ public static String getLink(Schematic schem){
@ -40,7 +42,7 @@ public class DownloadSchematic {
digest.reset(); digest.reset();
digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes()); digest.update((Instant.now().toString() + schem.getSchemOwner() + schem.getSchemID()).getBytes());
String hash = BaseEncoding.base16().encode(digest.digest()); String hash = BaseEncoding.base16().encode(digest.digest());
SQL.update("INSERT INTO SchemDownload (SchemID, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)", schem.getSchemID(), hash); createLink.update(schem.getSchemID(), hash);
return BASE + hash; return BASE + hash;
} }
} }

Datei anzeigen

@ -19,24 +19,21 @@
package de.steamwar.sql; package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Elo { public class Elo {
private Elo(){} private Elo(){}
private static final SQL.Statement get = new SQL.Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?");
private static final SQL.Statement set = new SQL.Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)");
public static int getElo(int userId, String gameMode){ public static int getElo(int userId, String gameMode){
ResultSet rs = SQL.select("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?", userId, gameMode); return get.select(rs -> {
try{ if(rs.next())
if(!rs.next()) return rs.getInt("id");
return 1000; return 1000;
return rs.getInt("Elo"); }, userId, gameMode);
}catch(SQLException e){
throw new SecurityException("Could not get elo", e);
}
} }
public static void setElo(int userId, String gameMode, int elo){ public static void setElo(int userId, String gameMode, int elo){
SQL.update("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)", userId, gameMode, elo); set.update(userId, gameMode, elo);
} }
} }

Datei anzeigen

@ -19,15 +19,14 @@
package de.steamwar.sql; package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.logging.Level;
public class Event { public class Event {
private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Event WHERE EventID = ?");
private final int eventID; private final int eventID;
private final String eventName; private final String eventName;
private final Timestamp start; private final Timestamp start;
@ -47,16 +46,10 @@ public class Event {
} }
public static Event get(int eventID){ public static Event get(int eventID){
ResultSet rs = SQL.select("SELECT * FROM Event WHERE EventID = ?", eventID); return get.select(rs -> {
try{ rs.next();
if(!rs.next())
throw new IllegalArgumentException();
return new Event(rs); return new Event(rs);
}catch (SQLException e){ }, eventID);
Bukkit.getLogger().log(Level.SEVERE, "Failed to load Event", e);
throw new SecurityException();
}
} }
public int getEventID() { public int getEventID() {

Datei anzeigen

@ -19,19 +19,21 @@
package de.steamwar.sql; package de.steamwar.sql;
import org.bukkit.Bukkit;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.logging.Level;
public class EventFight { public class EventFight {
private int eventID;
private int fightID; private static final SQL.Statement get = new SQL.Statement("SELECT * FROM EventFight WHERE FightID = ?");
private int teamBlue; private static final SQL.Statement setResult = new SQL.Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?");
private int teamRed; private static final SQL.Statement setFight = new SQL.Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?");
private int kampfleiter;
private int ergebnis; private final int eventID;
private final int fightID;
private final int teamBlue;
private final int teamRed;
private final int kampfleiter;
private final int ergebnis;
private EventFight(ResultSet rs) throws SQLException{ private EventFight(ResultSet rs) throws SQLException{
this.eventID = rs.getInt("EventID"); this.eventID = rs.getInt("EventID");
@ -43,23 +45,19 @@ public class EventFight {
} }
public static EventFight get(int fightID){ public static EventFight get(int fightID){
ResultSet rs = SQL.select("SELECT * FROM EventFight WHERE FightID = " + fightID); return get.select(rs -> {
try{
rs.next(); rs.next();
return new EventFight(rs); return new EventFight(rs);
}catch (SQLException e){ }, fightID);
Bukkit.getLogger().log(Level.SEVERE, "Failed to load EventFight", e);
}
return null;
} }
public void setErgebnis(int winner){ public void setErgebnis(int winner){
SQL.update("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?", winner, fightID); setResult.update(winner, fightID);
} }
public void setFight(int fight){ public void setFight(int fight){
//Fight.FightID, not EventFight.FightID //Fight.FightID, not EventFight.FightID
SQL.update("UPDATE EventFight SET Fight = ? WHERE FightID = ?", fight, fightID); setFight.update(fight, fightID);
} }
public int getTeamBlue() { public int getTeamBlue() {

Datei anzeigen

@ -21,42 +21,35 @@ package de.steamwar.sql;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Blob; import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
public class Fight { public class Fight {
private Fight(){} private Fight(){}
private static final SQL.Statement create = new SQL.Statement("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
private static final SQL.Statement lastId = new SQL.Statement("SELECT LAST_INSERT_ID() AS FightID");
private static final SQL.Statement getReplay = new SQL.Statement("SELECT Replay FROM Fight WHERE FightID = ?");
private static final SQL.Statement setReplay = new SQL.Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?");
public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ public static int create(String gamemode, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){
return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); return create(gamemode, arena, null, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition);
} }
public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){ public static int create(String gamemode, String server, String arena, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){
SQL.update("INSERT INTO Fight (GameMode, Server, Arena, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", create.update(gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition);
gamemode, server, arena, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition); return lastId.select(rs -> {
ResultSet rs = SQL.select("SELECT LAST_INSERT_ID() AS FightID"); rs.next();
try{
if(!rs.next())
throw new SecurityException("No last insert id");
return rs.getInt("FightID"); return rs.getInt("FightID");
} catch (SQLException e) { });
throw new SecurityException(e);
}
} }
public static InputStream getReplay(int fightID) { public static InputStream getReplay(int fightID) {
ResultSet rs = SQL.select("SELECT Replay FROM Fight WHERE FightID = ?", fightID); return getReplay.select(rs -> {
try {
rs.next(); rs.next();
Blob replay = rs.getBlob("Replay"); Blob replay = rs.getBlob("Replay");
if(replay == null)
throw new SecurityException("Replay null");
return replay.getBinaryStream(); return replay.getBinaryStream();
} catch (SQLException e) { }, fightID);
throw new SecurityException(e);
}
} }
public static void setReplay(int fightID, byte[] data) { public static void setReplay(int fightID, byte[] data) {
@ -66,6 +59,6 @@ public class Fight {
} catch (SQLException e) { } catch (SQLException e) {
throw new SecurityException(e); throw new SecurityException(e);
} }
SQL.update("UPDATE Fight SET Replay = ? WHERE FightID = ?", blob, fightID); setReplay.update(blob, fightID);
} }
} }

Datei anzeigen

@ -22,14 +22,9 @@ package de.steamwar.sql;
public class FightPlayer { public class FightPlayer {
private FightPlayer(){} private FightPlayer(){}
public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){ private static final SQL.Statement create = new SQL.Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)");
SQL.update("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)",
fightID, userID, blue ? 1 : 2, kit, kills, isOut);
}
@Deprecated public static void create(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut){
public static void create(int fightID, int userID, String kit, int kills, boolean isOut){ create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut);
SQL.update("INSERT INTO FightPlayer (FightID, UserID, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?)",
fightID, userID, kit, kills, isOut);
} }
} }

Datei anzeigen

@ -31,6 +31,12 @@ import java.util.Objects;
public class PersonalKit { public class PersonalKit {
private static final SQL.Statement getKits = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?");
private static final SQL.Statement getKit = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?");
private static final SQL.Statement getKitInUse = new SQL.Statement("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = 1");
private static final SQL.Statement delete = new SQL.Statement("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?");
private static final SQL.Statement update = new SQL.Statement("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor, InUse) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(Name), InUse = VALUES(InUse)");
private final int userID; private final int userID;
private String name; private String name;
private final String gamemode; private final String gamemode;
@ -48,26 +54,20 @@ public class PersonalKit {
} }
public static List<PersonalKit> get(int userID, String gamemode){ public static List<PersonalKit> get(int userID, String gamemode){
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ?", userID, gamemode); return getKits.select(rs -> {
try {
List<PersonalKit> list = new ArrayList<>(); List<PersonalKit> list = new ArrayList<>();
while (rs.next()) while (rs.next())
list.add(new PersonalKit(rs)); list.add(new PersonalKit(rs));
return list; return list;
} catch (SQLException e) { }, userID, gamemode);
throw new SecurityException("Failed loading personal kit", e);
}
} }
public static PersonalKit get(int userID, String gamemode, String name) { public static PersonalKit get(int userID, String gamemode, String name) {
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); return getKit.select(rs -> {
try { if(rs.next())
if(!rs.next()) return new PersonalKit(rs);
return null; return null;
return new PersonalKit(rs); }, userID, gamemode, name);
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
} }
public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){ public static PersonalKit create(int userID, String gamemode, String name, ItemStack[] inventory, ItemStack[] armor){
@ -80,24 +80,17 @@ public class PersonalKit {
} }
public static PersonalKit getKitInUse(int userID, String gamemode) { public static PersonalKit getKitInUse(int userID, String gamemode) {
ResultSet rs = SQL.select("SELECT * FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND InUse = ?", userID, gamemode, true); return getKitInUse.select(rs -> {
try { if(!rs.next())
List<PersonalKit> list = new ArrayList<>(); return null;
PersonalKit kit = new PersonalKit(rs);
while (rs.next()) while (rs.next())
list.add(new PersonalKit(rs)); new PersonalKit(rs).setUse(false); //TODO: Violation of integrity, should not be necessary?
if(list.size() > 1) { return kit;
list.forEach(kit -> { }, userID, gamemode);
if(list.indexOf(kit) >= 1) kit.setUse(false);
});
list.removeIf(kit -> !kit.isInUse());
}
if(list.isEmpty()) return null;
return list.get(0);
} catch (SQLException e) {
throw new SecurityException("Failed loading personal kit", e);
}
} }
@Deprecated
public static boolean nameInUse(int userID, String gamemode, String name) { public static boolean nameInUse(int userID, String gamemode, String name) {
ResultSet set = SQL.select("SELECT COUNT(*) AS Count FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); ResultSet set = SQL.select("SELECT COUNT(*) AS Count FROM PersonalKit WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name);
try { try {
@ -165,7 +158,7 @@ public class PersonalKit {
} }
public void delete() { public void delete() {
SQL.update("DELETE FROM `PersonalKit` WHERE UserID = ? AND GameMode = ? AND Name = ?", userID, gamemode, name); delete.update(userID, gamemode, name);
} }
private static String saveInvConfig(String name, ItemStack[] inv) { private static String saveInvConfig(String name, ItemStack[] inv) {
@ -176,7 +169,6 @@ public class PersonalKit {
} }
private void updateDB() { private void updateDB() {
SQL.update("INSERT INTO PersonalKit (UserID, GameMode, Name, Inventory, Armor, InUse) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE Inventory = VALUES(Inventory), Armor = VALUES(Armor), Name = VALUES(Name), InUse = VALUES(InUse)", update.update(userID, gamemode, name, inventory, armor, inUse);
userID, gamemode, name, inventory, armor, inUse);
} }
} }

Datei anzeigen

@ -53,7 +53,7 @@ public class SQL {
private static void connect() { private static void connect() {
try { try {
con = DriverManager.getConnection(url + "?autoreconnect=true", user, password); con = DriverManager.getConnection(url + "?autoReconnect=true&useServerPrepStmts=true", user, password);
} catch (SQLException e) { } catch (SQLException e) {
throw new SecurityException("Could not start SQL connection", e); throw new SecurityException("Could not start SQL connection", e);
} }
@ -71,14 +71,14 @@ public class SQL {
try { try {
con.close(); con.close();
} catch (SQLException e) { } catch (SQLException e) {
Core.getInstance().getLogger().log(Level.WARNING, "Could not close SQL-Connection", e); Core.getInstance().getLogger().log(Level.INFO, "Could not close SQL connection", e);
} }
} }
private static void reset(SQLException e) { private static void reset() {
Core.getInstance().getLogger().log(Level.WARNING, "SQL Exception thrown", e);
close(); close();
connect(); connect();
try { try {
for (Statement statement : Statement.statements) { for (Statement statement : Statement.statements) {
statement.init(); statement.init();
@ -88,16 +88,20 @@ public class SQL {
} }
} }
public static boolean connectionStable() {
try {
return !con.isClosed();
} catch (SQLException e) {
return false;
}
}
static void update(String qry, Object... objects) { static void update(String qry, Object... objects) {
try { try {
prepare(qry, objects).executeUpdate(); prepare(qry, objects).executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
reset(e); reset();
try { throw new SecurityException("Could not execute SQL statement", e);
prepare(qry, objects).executeUpdate();
} catch (SQLException ex) {
throw new SecurityException("Could not perform update", ex);
}
} }
} }
@ -105,12 +109,8 @@ public class SQL {
try { try {
return prepare(qry, objects).executeQuery(); return prepare(qry, objects).executeQuery();
} catch (SQLException e) { } catch (SQLException e) {
reset(e); reset();
try { throw new SecurityException("Could not execute SQL statement", e);
return prepare(qry, objects).executeQuery();
} catch (SQLException ex) {
throw new SecurityException("Could not perform select", ex);
}
} }
} }
@ -118,12 +118,8 @@ public class SQL {
try { try {
return con.createBlob(); return con.createBlob();
} catch (SQLException e) { } catch (SQLException e) {
reset(e); reset();
try { throw new SecurityException("Could not execute SQL statement", e);
return con.createBlob();
} catch (SQLException ex) {
throw new SecurityException("Could not create blob", ex);
}
} }
} }
@ -147,7 +143,7 @@ public class SQL {
try { try {
init(); init();
} catch (SQLException e) { } catch (SQLException e) {
reset(e); throw new SecurityException("Could not init statement", e);
} }
} }
@ -173,13 +169,8 @@ public class SQL {
setObjects(objects); setObjects(objects);
return runnable.run(); return runnable.run();
} catch (SQLException e) { } catch (SQLException e) {
reset(e); reset();
try { throw new SecurityException("Could not execute SQL statement", e);
setObjects(objects);
return runnable.run();
} catch (SQLException ex) {
throw new SecurityException("Could not execute SQL statement", ex);
}
} }
} }

Datei anzeigen

@ -25,6 +25,8 @@ import org.bukkit.entity.Player;
public class SWException { public class SWException {
private SWException(){} private SWException(){}
private static final SQL.Statement insert = new SQL.Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)");
public static void log(String message, String stacktrace){ public static void log(String message, String stacktrace){
message += "\n"; message += "\n";
for(Player player : Bukkit.getOnlinePlayers()) for(Player player : Bukkit.getOnlinePlayers())
@ -36,6 +38,6 @@ public class SWException {
else else
server = Bukkit.getWorlds().get(0).getName(); server = Bukkit.getWorlds().get(0).getName();
SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)", server, message, stacktrace); insert.update(server, message, stacktrace);
} }
} }

Datei anzeigen

@ -28,6 +28,10 @@ import java.util.*;
public class SteamwarUser { public class SteamwarUser {
private static final SQL.Statement getId = new SQL.Statement("SELECT * FROM UserData WHERE id = ?");
private static final SQL.Statement getUUID = new SQL.Statement("SELECT * FROM UserData WHERE UUID = ?");
private static final SQL.Statement getName = new SQL.Statement("SELECT * FROM UserData WHERE lower(UserName) = ?");
private static final Map<UUID, SteamwarUser> byUUID = new HashMap<>(); private static final Map<UUID, SteamwarUser> byUUID = new HashMap<>();
private static final Map<String, SteamwarUser> byName = new HashMap<>(); private static final Map<String, SteamwarUser> byName = new HashMap<>();
private static final Map<Integer, SteamwarUser> byId = new HashMap<>(); private static final Map<Integer, SteamwarUser> byId = new HashMap<>();
@ -88,35 +92,34 @@ public class SteamwarUser {
return bedrock; return bedrock;
} }
private static SteamwarUser fromDB(String statement, Object identifier){
ResultSet rs = SQL.select(statement, identifier);
try {
if(rs.next())
return new SteamwarUser(rs);
} catch (SQLException e) {
throw new SecurityException("SQL Statement failed to get User", e);
}
return null;
}
public static SteamwarUser get(String userName){ public static SteamwarUser get(String userName){
SteamwarUser user = byName.get(userName.toLowerCase()); SteamwarUser user = byName.get(userName.toLowerCase());
if(user == null) if(user == null)
user = fromDB("SELECT * FROM UserData WHERE lower(UserName) = ?", userName.toLowerCase()); user = getName.select(rs -> {
if(rs.next())
return new SteamwarUser(rs);
return null;
}, userName.toLowerCase());
return user; return user;
} }
public static SteamwarUser get(UUID uuid){ public static SteamwarUser get(UUID uuid){
SteamwarUser user = byUUID.get(uuid); SteamwarUser user = byUUID.get(uuid);
if(user == null) if(user == null)
user = fromDB("SELECT * FROM UserData WHERE UUID = ?", uuid.toString()); user = getUUID.select(rs -> {
rs.next();
return new SteamwarUser(rs);
}, uuid.toString());
return user; return user;
} }
public static SteamwarUser get(int id) { public static SteamwarUser get(int id) {
SteamwarUser user = byId.get(id); SteamwarUser user = byId.get(id);
if(user == null) if(user == null)
user = fromDB("SELECT * FROM UserData WHERE id = ?", id); user = getId.select(rs -> {
rs.next();
return new SteamwarUser(rs);
}, id);
return user; return user;
} }
} }

Datei anzeigen

@ -21,10 +21,14 @@ package de.steamwar.sql;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.LinkedList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Team { public class Team {
private static final SQL.Statement get = new SQL.Statement("SELECT * FROM Team WHERE TeamID = ?");
private static final SQL.Statement getMembers = new SQL.Statement("SELECT id FROM UserData WHERE Team = ?");
private final int teamId; private final int teamId;
private final String teamKuerzel; private final String teamKuerzel;
private final String teamName; private final String teamName;
@ -46,14 +50,10 @@ public class Team {
public static Team get(int id){ public static Team get(int id){
if(id == 0) if(id == 0)
return pub; return pub;
ResultSet rs = SQL.select("SELECT * FROM Team WHERE TeamID = ?", id); return get.select(rs -> {
try { rs.next();
if(!rs.next())
return null;
return new Team(rs); return new Team(rs);
} catch (SQLException e) { }, id);
throw new SecurityException("Could not load team", e);
}
} }
public int getTeamId() { public int getTeamId() {
@ -73,14 +73,11 @@ public class Team {
} }
public List<Integer> getMembers(){ public List<Integer> getMembers(){
try{ return getMembers.select(rs -> {
ResultSet memberlist = SQL.select("SELECT id FROM UserData WHERE Team = ?", teamId); List<Integer> members = new ArrayList<>();
List<Integer> members = new LinkedList<>(); while(rs.next())
while(memberlist.next()) members.add(rs.getInt("id"));
members.add(memberlist.getInt("id"));
return members; return members;
}catch(SQLException e){ }, teamId);
throw new SecurityException("Could not load Teammembers", e);
}
} }
} }

Datei anzeigen

@ -19,30 +19,25 @@
package de.steamwar.sql; package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID; import java.util.UUID;
public class UserConfig { public class UserConfig {
private UserConfig() {}
private UserConfig() { private static final SQL.Statement get = new SQL.Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?");
private static final SQL.Statement set = new SQL.Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)");
} private static final SQL.Statement delete = new SQL.Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?");
public static String getConfig(UUID player, String config) { public static String getConfig(UUID player, String config) {
return getConfig(SteamwarUser.get(player).getId(), config); return getConfig(SteamwarUser.get(player).getId(), config);
} }
public static String getConfig(int player, String config) { public static String getConfig(int player, String config) {
ResultSet configResult = SQL.select("SELECT * FROM UserConfig WHERE User = ? AND Config = ?", player, config); return get.select(rs -> {
try { if(rs.next())
if (!configResult.next()) { return rs.getString("Value");
return null; return null;
} }, player, config);
return configResult.getString("Value");
} catch (SQLException e) {
throw new SecurityException(e.getMessage(), e);
}
} }
public static void updatePlayerConfig(UUID uuid, String config, String value) { public static void updatePlayerConfig(UUID uuid, String config, String value) {
@ -54,7 +49,7 @@ public class UserConfig {
removePlayerConfig(id, config); removePlayerConfig(id, config);
return; return;
} }
SQL.update("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)", id, config, value); set.update(id, config, value);
} }
public static void removePlayerConfig(UUID uuid, String config) { public static void removePlayerConfig(UUID uuid, String config) {
@ -62,6 +57,6 @@ public class UserConfig {
} }
public static void removePlayerConfig(int id, String config) { public static void removePlayerConfig(int id, String config) {
SQL.update("DELETE FROM UserConfig WHERE User = ? AND Config = ?", id, config); delete.update(id, config);
} }
} }