SteamWar/SpigotCore
Archiviert
13
0

standalone #174

Zusammengeführt
Lixfel hat 18 Commits von standalone nach master 2022-02-20 15:00:33 +01:00 zusammengeführt
13 geänderte Dateien mit 411 neuen und 169 gelöschten Zeilen
Nur Änderungen aus Commit 877aaea1df werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -31,6 +31,7 @@ import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
@ -66,6 +67,8 @@ public class Core extends JavaPlugin{
}
private ErrorHandler errorHandler;
private File sqlConfig;
private boolean standalone;
@Override
public void onLoad() {
@ -74,6 +77,8 @@ public class Core extends JavaPlugin{
@Override
public void onEnable() {
sqlConfig = new File(Core.getInstance().getDataFolder(), "MySQL.yml");
standalone = !sqlConfig.exists();
errorHandler = new ErrorHandler();
Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this);
@ -84,7 +89,7 @@ public class Core extends JavaPlugin{
AuthlibInjector.inject();
TinyProtocol.init();
try {
Bukkit.getLogger().log(Level.INFO, "Running on: " + new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream())).readLine());
getLogger().log(Level.INFO, "Running on: " + new BufferedReader(new InputStreamReader(Runtime.getRuntime().exec("hostname").getInputStream())).readLine());
} catch (IOException e) {
throw new SecurityException("Could not load Hostname", e);
}
@ -97,6 +102,14 @@ public class Core extends JavaPlugin{
Statement.close();
}
public static File sqlConfig() {
return instance.sqlConfig;
}
public static boolean standalone() {
return instance.standalone;
}
public static Core getInstance() {
return instance;
}

Datei anzeigen

@ -23,28 +23,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class BauweltMember{
private static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?");
private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?");
private final int bauweltID;
private final int memberID;
private final boolean worldEdit;
private final boolean world;
private static final List<BauweltMember> members = new ArrayList<>();
public class BauweltMember {
private static final List<BauweltMember> memberCache = new ArrayList<>();
public static void clear() {
members.clear();
}
private BauweltMember(int ownerID, int memberID, boolean worldEdit, boolean world) {
bauweltID = ownerID;
this.memberID = memberID;
this.worldEdit = worldEdit;
this.world = world;
members.add(this);
memberCache.clear();
}
public static BauweltMember getBauMember(UUID ownerID, UUID memberID){
@ -52,14 +35,10 @@ public class BauweltMember{
}
public static BauweltMember getBauMember(int ownerID, int memberID){
for(BauweltMember member : members)
if(member.memberID == memberID)
for(BauweltMember member : memberCache)
if(member.getMemberID() == memberID)
return member;
return getMember.select(rs -> {
if(!rs.next())
return null;
return new BauweltMember(ownerID, memberID, rs.getBoolean("WorldEdit"), rs.getBoolean("World"));
}, ownerID, memberID);
return Provider.impl.getBauMember(ownerID, memberID);
}
public static List<BauweltMember> getMembers(UUID bauweltID){
@ -67,12 +46,20 @@ public class BauweltMember{
}
public static List<BauweltMember> getMembers(int bauweltID){
return getMembers.select(rs -> {
List<BauweltMember> members = new ArrayList<>();
while(rs.next())
members.add(new BauweltMember(bauweltID, rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World")));
return members;
}, bauweltID);
return Provider.impl.getMembers(bauweltID);
}
private final int bauweltID;
private final int memberID;
private final boolean worldEdit;
private final boolean world;
public BauweltMember(int bauweltID, int memberID, boolean worldEdit, boolean world) {
this.bauweltID = bauweltID;
this.memberID = memberID;
this.worldEdit = worldEdit;
this.world = world;
memberCache.add(this);
}
public int getBauweltID() {

Datei anzeigen

@ -20,45 +20,17 @@
package de.steamwar.sql;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class CheckedSchematic {
private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC");
private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
private final Integer node;
private final int validator;
private final Timestamp startTime;
private final Timestamp endTime;
private final String declineReason;
private CheckedSchematic(int node, int validator, Timestamp startTime, Timestamp endTime, String declineReason){
this.node = node;
this.validator = validator;
this.startTime = startTime;
this.endTime = endTime;
this.declineReason = declineReason;
}
public static List<CheckedSchematic> getLastDeclinedOfNode(SchematicNode node){
return getLastDeclinedOfNode(node.getId());
}
public static List<CheckedSchematic> getLastDeclinedOfNode(int node){
return nodeHistory.select(rs -> {
List<CheckedSchematic> lastDeclined = new ArrayList<>();
while(rs.next()){
int validator = rs.getInt("Validator");
Timestamp startTime = rs.getTimestamp("StartTime");
Timestamp endTime = rs.getTimestamp("EndTime");
String declineReason = rs.getString("DeclineReason");
lastDeclined.add(new CheckedSchematic(node, validator, startTime, endTime, declineReason));
}
return lastDeclined;
}, node);
return Provider.impl.getLastDeclinedOfNode(node);
}
public static List<CheckedSchematic> getLastDeclined(UUID uuid){
@ -66,12 +38,21 @@ public class CheckedSchematic {
}
public static List<CheckedSchematic> getLastDelined(int schemOwner){
return checkHistory.select(rs -> {
List<CheckedSchematic> history = new ArrayList<>();
while(rs.next())
history.add(new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason")));
return history;
}, schemOwner);
return Provider.impl.getLastDelined(schemOwner);
}
private final Integer node;
private final int validator;
private final Timestamp startTime;
private final Timestamp endTime;
private final String declineReason;
public CheckedSchematic(Integer node, int validator, Timestamp startTime, Timestamp endTime, String declineReason) {
this.node = node;
this.validator = validator;
this.startTime = startTime;
this.endTime = endTime;
this.declineReason = declineReason;
}
public int getValidator() {

Datei anzeigen

@ -22,18 +22,11 @@ package de.steamwar.sql;
public class Elo {
private Elo(){}
private static final Statement get = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?");
private static final Statement set = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)");
public static int getElo(int userId, String gameMode){
return get.select(rs -> {
if(rs.next())
return rs.getInt("Elo");
return 1000;
}, userId, gameMode);
return Provider.impl.getElo(userId, gameMode);
}
public static void setElo(int userId, String gameMode, int elo){
set.update(userId, gameMode, elo);
Provider.impl.setElo(userId, gameMode, elo);
}
}

Datei anzeigen

@ -19,13 +19,13 @@
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
public class Event {
private static final Statement get = new Statement("SELECT * FROM Event WHERE EventID = ?");
public static Event get(int eventID){
return Provider.impl.getEvent(eventID);
}
private final int eventID;
private final String eventName;
@ -35,21 +35,14 @@ public class Event {
private final boolean publicSchemsOnly;
private final boolean spectateSystem;
private Event(ResultSet rs) throws SQLException{
this.eventID = rs.getInt("EventID");
this.eventName = rs.getString("EventName");
this.start = rs.getTimestamp("Start");
this.end = rs.getTimestamp("End");
this.maximumTeamMembers = rs.getInt("MaximumTeamMembers");
this.publicSchemsOnly = rs.getBoolean("PublicSchemsOnly");
this.spectateSystem = rs.getBoolean("SpectateSystem");
}
public static Event get(int eventID){
return get.select(rs -> {
rs.next();
return new Event(rs);
}, eventID);
public Event(int eventID, String eventName, Timestamp start, Timestamp end, int maximumTeamMembers, boolean publicSchemsOnly, boolean spectateSystem) {
this.eventID = eventID;
this.eventName = eventName;
this.start = start;
this.end = end;
this.maximumTeamMembers = maximumTeamMembers;
this.publicSchemsOnly = publicSchemsOnly;
this.spectateSystem = spectateSystem;
}
public int getEventID() {

Datei anzeigen

@ -19,14 +19,13 @@
package de.steamwar.sql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.function.BiConsumer;
public class EventFight {
private static final Statement get = new Statement("SELECT * FROM EventFight WHERE FightID = ?");
private static final Statement setResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?");
private static final Statement setFight = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?");
public static EventFight get(int fightID) {
return Provider.impl.getEventFight(fightID);
}
private final int eventID;
private final int fightID;
@ -34,30 +33,27 @@ public class EventFight {
private final int teamRed;
private final int kampfleiter;
private final int ergebnis;
private final BiConsumer<EventFight, Integer> setErgebnis;
private final BiConsumer<EventFight, Integer> setFight;
private EventFight(ResultSet rs) throws SQLException{
this.eventID = rs.getInt("EventID");
this.fightID = rs.getInt("FightID");
this.teamBlue = rs.getInt("TeamBlue");
this.teamRed = rs.getInt("TeamRed");
this.kampfleiter = rs.getInt("Kampfleiter");
this.ergebnis = rs.getInt("Ergebnis");
}
public static EventFight get(int fightID){
return get.select(rs -> {
rs.next();
return new EventFight(rs);
}, fightID);
public EventFight(int eventID, int fightID, int teamBlue, int teamRed, int kampfleiter, int ergebnis, BiConsumer<EventFight, Integer> setErgebnis, BiConsumer<EventFight, Integer> setFight) {
this.eventID = eventID;
this.fightID = fightID;
this.teamBlue = teamBlue;
this.teamRed = teamRed;
this.kampfleiter = kampfleiter;
this.ergebnis = ergebnis;
this.setErgebnis = setErgebnis;
this.setFight = setFight;
}
public void setErgebnis(int winner){
setResult.update(winner, fightID);
setErgebnis.accept(this, winner);
}
public void setFight(int fight){
//Fight.FightID, not EventFight.FightID
setFight.update(fight, fightID);
setFight.accept(this, fight);
}
public int getTeamBlue() {

Datei anzeigen

@ -27,32 +27,19 @@ import java.util.function.Consumer;
public class Fight {
private Fight(){}
private static final Statement create = new Statement("INSERT INTO Fight (GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition, ReplayLock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
private static final Statement lastId = new Statement("SELECT LAST_INSERT_ID() AS FightID");
private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?");
private static final Statement setReplay = new Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?");
public static int create(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition){
return create(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, Timestamp.from(Instant.now()));
return create(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, Timestamp.from(Instant.now()));
}
public static int create(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock){
create.update(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock);
return lastId.select(rs -> {
rs.next();
return rs.getInt("FightID");
});
return Provider.impl.createFight(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock);
}
public static void getReplay(int fightID, Consumer<InputStream> reader) {
getReplay.select(rs -> {
rs.next();
reader.accept(rs.getBinaryStream("Replay"));
return null;
}, fightID);
Provider.impl.getReplay(fightID, reader);
}
public static void setReplay(int fightID, InputStream data) {
setReplay.update(data, fightID);
Provider.impl.setReplay(fightID, data);
}
}

Datei anzeigen

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

Datei anzeigen

@ -26,8 +26,7 @@ import java.security.NoSuchAlgorithmException;
import java.time.Instant;
public class NodeDownload {
private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)");
private NodeDownload() {}
private static final String BASE = "https://steamwar.de/download.php?schem=";
@ -43,7 +42,7 @@ public class NodeDownload {
digest.reset();
digest.update((Instant.now().toString() + schem.getOwner() + schem.getId()).getBytes());
String hash = BaseEncoding.base16().encode(digest.digest());
createLink.update(schem.getId(), hash);
Provider.impl.createDownloadLink(schem.getId(), hash);
return BASE + hash;
}
}

Datei anzeigen

@ -0,0 +1,51 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import de.steamwar.core.Core;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.List;
import java.util.function.Consumer;
public interface Provider {
Provider impl = Core.standalone() ? new StandaloneProvider() : new SQLProvider();
BauweltMember getBauMember(int ownerID, int memberID);
List<BauweltMember> getMembers(int bauweltID);
List<CheckedSchematic> getLastDeclinedOfNode(int node);
List<CheckedSchematic> getLastDelined(int schemOwner);
int getElo(int userId, String gameMode);
void setElo(int userId, String gameMode, int elo);
Event getEvent(int eventID);
EventFight getEventFight(int fightID);
int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock);
void getReplay(int fightID, Consumer<InputStream> reader);
void setReplay(int fightID, InputStream data);
void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut);
void createDownloadLink(int nodeId, String hash);
}

Datei anzeigen

@ -0,0 +1,157 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class SQLProvider implements Provider {
private static final Statement getMember = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ? AND MemberID = ?");
@Override
public BauweltMember getBauMember(int ownerID, int memberID) {
return getMember.select(rs -> rs.next() ? newBauweltMember(rs) : null, ownerID, memberID);
}
private static final Statement getMembers = new Statement("SELECT * FROM BauweltMember WHERE BauweltID = ?");
@Override
public List<BauweltMember> getMembers(int bauweltID) {
return getMembers.select(rs -> {
List<BauweltMember> members = new ArrayList<>();
while(rs.next())
members.add(newBauweltMember(rs));
return members;
}, bauweltID);
}
private BauweltMember newBauweltMember(ResultSet rs) throws SQLException {
return new BauweltMember(rs.getInt("BauweltID"), rs.getInt("MemberID"), rs.getBoolean("WorldEdit"), rs.getBoolean("World"));
}
private static final Statement nodeHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId = ? AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' ORDER BY EndTime DESC");
@Override
public List<CheckedSchematic> getLastDeclinedOfNode(int node) {
return nodeHistory.select(rs -> {
List<CheckedSchematic> lastDeclined = new ArrayList<>();
while(rs.next())
lastDeclined.add(newCheckedSchematic(rs));
return lastDeclined;
}, node);
}
private static final Statement checkHistory = new Statement("SELECT * FROM CheckedSchematic WHERE NodeId IN (SELECT NodeId FROM SchematicNode WHERE NodeOwner = ?) AND DeclineReason != '' AND DeclineReason != 'Prüfvorgang abgebrochen' AND NodeId is not NULL ORDER BY EndTime DESC");
@Override
public List<CheckedSchematic> getLastDelined(int schemOwner) {
return checkHistory.select(rs -> {
List<CheckedSchematic> history = new ArrayList<>();
while(rs.next())
history.add(newCheckedSchematic(rs));
return history;
}, schemOwner);
}
private CheckedSchematic newCheckedSchematic(ResultSet rs) throws SQLException {
return new CheckedSchematic(rs.getInt("NodeId"), rs.getInt("Validator"), rs.getTimestamp("StartTime"), rs.getTimestamp("EndTime"), rs.getString("DeclineReason"));
}
private static final Statement getElo = new Statement("SELECT Elo FROM Elo WHERE UserID = ? AND GameMode = ?");
@Override
public int getElo(int userId, String gameMode) {
return getElo.select(rs -> {
if(rs.next())
return rs.getInt("Elo");
return 1000;
}, userId, gameMode);
}
private static final Statement setElo = new Statement("INSERT INTO Elo (UserID, GameMode, Elo) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Elo = VALUES(Elo)");
@Override
public void setElo(int userId, String gameMode, int elo) {
setElo.update(userId, gameMode, elo);
}
private static final Statement getEvent = new Statement("SELECT * FROM Event WHERE EventID = ?");
@Override
public Event getEvent(int eventID) {
return getEvent.select(rs -> {
rs.next();
return new Event(rs.getInt("EventID"), rs.getString("EventName"), rs.getTimestamp("Start"), rs.getTimestamp("End"), rs.getInt("MaximumTeamMembers"), rs.getBoolean("PublicSchemsOnly"), rs.getBoolean("SpectateSystem"));
}, eventID);
}
private static final Statement getEventFight = new Statement("SELECT * FROM EventFight WHERE FightID = ?");
private static final Statement setEventFightResult = new Statement("UPDATE EventFight SET Ergebnis = ? WHERE FightID = ?");
private static final Statement setEventFightFightID = new Statement("UPDATE EventFight SET Fight = ? WHERE FightID = ?");
@Override
public EventFight getEventFight(int fightID) {
return getEventFight.select(rs -> {
rs.next();
return new EventFight(
rs.getInt("EventID"), rs.getInt("FightID"), rs.getInt("TeamBlue"), rs.getInt("TeamRed"), rs.getInt("Kampfleiter"), rs.getInt("Ergebnis"),
(eventFight, winner) -> setEventFightResult.update(winner, eventFight.getFightID()),
(eventFight, fight) -> setEventFightFightID.update(fight, eventFight.getFightID())
);
}, fightID);
}
private static final Statement createFight = new Statement("INSERT INTO Fight (GameMode, Server, StartTime, Duration, BlueLeader, RedLeader, BlueSchem, RedSchem, Win, WinCondition, ReplayLock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
private static final Statement getLastFightID = new Statement("SELECT LAST_INSERT_ID() AS FightID");
@Override
public int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock) {
createFight.update(gamemode, server, starttime, duration, blueleader, redleader, blueschem, redschem, win, wincondition, replayLock);
return getLastFightID.select(rs -> {
rs.next();
return rs.getInt("FightID");
});
}
private static final Statement getReplay = new Statement("SELECT Replay FROM Fight WHERE FightID = ?");
@Override
public void getReplay(int fightID, Consumer<InputStream> reader) {
getReplay.select(rs -> {
rs.next();
reader.accept(rs.getBinaryStream("Replay"));
return null;
}, fightID);
}
private static final Statement setReplay = new Statement("UPDATE Fight SET Replay = ? WHERE FightID = ?");
@Override
public void setReplay(int fightID, InputStream data) {
setReplay.update(data, fightID);
}
private static final Statement create = new Statement("INSERT INTO FightPlayer (FightID, UserID, Team, Kit, Kills, IsOut) VALUES (?, ?, ?, ?, ?, ?)");
@Override
public void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut) {
create.update(fightID, userID, blue ? 1 : 2, kit, kills, isOut);
}
private static final Statement createLink = new Statement("INSERT INTO NodeDownload (NodeId, Link) VALUES (?, ?) ON DUPLICATE KEY UPDATE Link = VALUES(Link)");
@Override
public void createDownloadLink(int nodeId, String hash) {
createLink.update(nodeId, hash);
}
}

Datei anzeigen

@ -0,0 +1,91 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.sql;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.io.InputStream;
import java.sql.Timestamp;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class StandaloneProvider implements Provider {
@Override
public BauweltMember getBauMember(int ownerID, int memberID) {
OfflinePlayer player = Bukkit.getOfflinePlayer(SteamwarUser.get(memberID).getUUID());
return new BauweltMember(ownerID, memberID, player.isOp(), player.isOp());
}
@Override
public List<BauweltMember> getMembers(int bauweltID) {
return Bukkit.getOnlinePlayers().stream().map(player -> getBauMember(bauweltID, SteamwarUser.get(player.getUniqueId()).getId())).collect(Collectors.toList());
}
@Override
public List<CheckedSchematic> getLastDeclinedOfNode(int node) {
return new ArrayList<>();
}
@Override
public List<CheckedSchematic> getLastDelined(int schemOwner) {
return new ArrayList<>();
}
@Override
public int getElo(int userId, String gameMode) {
return 1000;
}
@Override
public void setElo(int userId, String gameMode, int elo) {}
@Override
public Event getEvent(int eventID) {
return new Event(eventID, "DummyEvent", Timestamp.from(Instant.now()), Timestamp.from(Instant.now()), 6, false, false);
}
@Override
public EventFight getEventFight(int fightID) {
return new EventFight(0, fightID, 0, 0, 0, 0, (eventFight, winner) -> {}, (eventFight, fightId) -> {});
}
@Override
public int createFight(String gamemode, String server, Timestamp starttime, int duration, int blueleader, int redleader, Integer blueschem, Integer redschem, int win, String wincondition, Timestamp replayLock) {
return 0;
}
@Override
public void getReplay(int fightID, Consumer<InputStream> reader) {}
@Override
public void setReplay(int fightID, InputStream data) {}
@Override
public void createFightPlayer(int fightID, int userID, boolean blue, String kit, int kills, boolean isOut) {}
@Override
public void createDownloadLink(int nodeId, String hash) {}
}

Datei anzeigen

@ -40,7 +40,7 @@ public class Statement {
private static final List<Statement> statements = new ArrayList<>();
static {
File file = new File(Core.getInstance().getDataFolder(), "MySQL.yml");
File file = Core.sqlConfig();
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
if(!file.exists())
@ -63,10 +63,12 @@ public class Statement {
public static void close() {
for (Statement statement : statements) {
try {
statement.st.close();
} catch (SQLException e) {
Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e);
if (statement.st != null) {
try {
statement.st.close();
} catch (SQLException e) {
Core.getInstance().getLogger().log(Level.INFO, "Could not close statement", e);
}
}
}
@ -80,14 +82,6 @@ public class Statement {
private static void reset() {
close();
connect();
try {
for (Statement statement : statements) {
statement.init();
}
} catch (SQLException ex) {
throw new SecurityException("Could not reprepare SQL Statements", ex);
}
}
public static boolean connectionStable() {
@ -101,31 +95,22 @@ public class Statement {
private final String sql;
private PreparedStatement st;
Statement(String sql) {
public Statement(String sql) {
this.sql = sql;
try {
init();
} catch (SQLException e) {
throw new SecurityException("Could not init statement", e);
}
statements.add(this);
}
private synchronized void init() throws SQLException {
st = con.prepareStatement(sql);
}
<T> T select(ResultSetUser<T> user, Object... objects) {
public <T> T select(ResultSetUser<T> user, Object... objects) {
return prepare(() -> {
ResultSet rs = st.executeQuery();
ResultSet rs = getSt().executeQuery();
T result = user.use(rs);
rs.close();
return result;
}, objects);
}
void update(Object... objects) {
prepare(st::executeUpdate, objects);
public void update(Object... objects) {
prepare(getSt()::executeUpdate, objects);
}
private synchronized <T> T prepare(SQLRunnable<T> runnable, Object... objects) {
@ -143,13 +128,24 @@ public class Statement {
}
}
private PreparedStatement getSt() {
if(st == null) {
try {
st = con.prepareStatement(sql);
} catch (SQLException e) {
throw new SecurityException("Could not prepare statement", e);
}
}
return st;
}
private void setObjects(Object... objects) throws SQLException {
for (int i = 0; i < objects.length; i++) {
st.setObject(i + 1, objects[i]);
getSt().setObject(i + 1, objects[i]);
}
}
interface ResultSetUser<T> {
public interface ResultSetUser<T> {
T use(ResultSet rs) throws SQLException;
}