diff --git a/SpigotCore_Main/src/de/steamwar/sql/Replay.java b/SpigotCore_Main/src/de/steamwar/sql/Replay.java new file mode 100644 index 0000000..c3bc96f --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Replay.java @@ -0,0 +1,65 @@ +/* + * 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 . + */ + +package de.steamwar.sql; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.nio.file.Files; +import java.sql.SQLException; + +@AllArgsConstructor +public class Replay { + + private static final Statement get = new Statement("SELECT Replay FROM Replay WHERE FightID = ?"); + private static final Statement insert = new Statement("INSERT INTO Replay (FightID, Replay) VALUES (?, ?)"); + + public static Replay get(int fightID) { + return get.select(rs -> { + rs.next(); + + File file; + try { + file = File.createTempFile("replay", "replay"); + file.deleteOnExit(); + Files.copy(rs.getBinaryStream("Replay"), file.toPath()); + } catch (IOException e) { + throw new SQLException(e); + } + return new Replay(fightID, file); + }, fightID); + } + + public static void save(int fightID, File file) { + try { + insert.update(fightID, new FileInputStream(file)); + } catch (FileNotFoundException e) { + throw new SecurityException("Could not save replay", e); + } + } + + private final int fightID; + @Getter + private final File replay; +} \ No newline at end of file diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicData.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicData.java new file mode 100644 index 0000000..5887887 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicData.java @@ -0,0 +1,98 @@ +/* + * 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 . + */ + +package de.steamwar.sql; + +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.Core; +import de.steamwar.core.WorldEditWrapper; +import org.bukkit.entity.Player; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.util.zip.GZIPInputStream; + +public class SchematicData { + + public static Clipboard clipboardFromStream(InputStream is, boolean schemFormat) { + try { + return WorldEditWrapper.impl.getClipboard(is, schemFormat); + } catch (IOException e) { + throw new SecurityException("Could not read schem", e); + } + } + + private static final Statement updateDatabase = new Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); + private static final Statement selSchemData = new Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); + + private final SchematicNode node; + + public SchematicData(SchematicNode node) { + this.node = node; + if(node.isDir()) + throw new SecurityException("Node is Directory"); + } + + public InputStream schemData() throws IOException { + try { + return selSchemData.select(rs -> { + rs.next(); + Blob schemData = rs.getBlob("NodeData"); + if(schemData == null) { + throw new SecurityException("SchemData is null"); + } + try { + return new GZIPInputStream(schemData.getBinaryStream()); + } catch (IOException e) { + throw new SecurityException("SchemData is wrong", e); + } + }, node.getId()); + } catch (Exception e) { + throw new IOException(e); + } + } + + public Clipboard load() throws IOException, NoClipboardException { + return WorldEditWrapper.impl.getClipboard(schemData(), node.getSchemFormat()); + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + WorldEditWrapper.impl.setPlayerClipboard(player, schemData(), node.getSchemFormat()); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, Core.getVersion() > 12); + } + + public void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + saveFromStream(WorldEditWrapper.impl.getPlayerClipboard(player, newFormat), newFormat); + } + + @Deprecated + public void saveFromBytes(byte[] bytes, boolean newFormat) { + saveFromStream(new ByteArrayInputStream(bytes), newFormat); + } + + public void saveFromStream(InputStream blob, boolean newFormat) { + updateDatabase.update(blob, newFormat, node.getId()); + node.setNodeFormat(newFormat); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index cadf8fd..157f9db 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -310,6 +310,10 @@ public class SchematicNode { return schemFormat; } + public void setNodeFormat(boolean format) { + schemFormat = format; + } + public int getRank() { if(isDir) throw new SecurityException("Node is Directory");