From 18c4318df3d893f16f65fcb2004504323a72adbf Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 27 Jan 2021 12:19:56 +0100 Subject: [PATCH] WIP Translation Layer and Node Merging --- .../src/de/steamwar/sql/NodeData.java | 157 --------------- .../src/de/steamwar/sql/NodeMember.java | 5 + .../src/de/steamwar/sql/Schematic.java | 160 +++++++++++++++ .../src/de/steamwar/sql/SchematicMember.java | 129 ++++++++++++ .../src/de/steamwar/sql/SchematicNode.java | 186 ++++++++++++++++-- 5 files changed, 464 insertions(+), 173 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/sql/NodeData.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/Schematic.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeData.java b/SpigotCore_Main/src/de/steamwar/sql/NodeData.java deleted file mode 100644 index c3d6e36..0000000 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeData.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 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.VersionedCallable; -import de.steamwar.core.VersionedRunnable; -import org.bukkit.entity.Player; - -import java.io.IOException; -import java.io.InputStream; -import java.sql.Blob; -import java.sql.ResultSet; -import java.sql.SQLException; - -public class NodeData { - - public static NodeData getSchemdata(int node) { - ResultSet set = SQL.select("SELECT NodeId, SchemFormat, NodeRank FROM NodeData WHERE NodeId = ?", node); - try { - if(!set.next()) - return null; - return new NodeData(set); - } catch (SQLException e) { - throw new SecurityException("Could not load NodeData", e); - } - } - - public static NodeData createNodeDate(SchematicNode node) { - SQL.update("INSERT INTO NodeData (NodeId) VALUES(?)", node.getId()); - return getSchemdata(node.getId()); - } - - private final int node; - private boolean schemFormat; - private int rank; - - private NodeData(ResultSet set) throws SQLException { - node = set.getInt("NodeId"); - schemFormat = set.getBoolean("SchemFormat"); - rank = set.getInt("NodeRank"); - } - - public Clipboard load() throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); - try { - rs.next(); - Blob schemData = rs.getBlob("SchemData"); - if(schemData == null) - throw new IOException("SchemData is null"); - InputStream is = schemData.getBinaryStream(); - return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), - new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void loadToPlayer(Player player) throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM NodeData WHERE NodeId = ?", node); - try { - rs.next(); - Blob blob = rs.getBlob("SchemData"); - if(blob == null) - throw new NoClipboardException(); - InputStream is = blob.getBinaryStream(); - VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), - new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); - } catch (SQLException e) { - throw new IOException(e); - } - } - - public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, false); - } - - public void saveFromPlayer(Player player) throws IOException, NoClipboardException { - saveFromPlayer(player, true); - } - - public void saveFromBytes(byte[] bytes, boolean newFormat) { - Blob blob = SQL.blob(); - try { - blob.setBytes(1, bytes); - updateDatabase(blob, newFormat); - } catch (SQLException e) { - throw new SecurityException(e); - } - } - - private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { - Blob blob = SQL.blob(); - VersionedRunnable.call(new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - } catch (SQLException e) { - throw new RuntimeException(e.getMessage(), e); - } - updateDatabase(blob, false); - }, 8), new VersionedRunnable(() -> { - try { - blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); - } catch (SQLException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - updateDatabase(blob, newFormat); - }, 14)); - } - - private void updateDatabase(Blob blob, boolean newFormat) { - SQL.update("UPDATE NodeData SET SchemData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, node); - schemFormat = newFormat; - } - - public int getNode() { - return node; - } - - public boolean isSchemFormat() { - return schemFormat; - } - - public int getRank() { - return rank; - } - - public void setRank(int rank) { - this.rank = rank; - updateDB(); - } - - private void updateDB() { - SQL.update("UPDATE NodeData SET Rank = ? WHERE NodeId = ?", rank, node); - } - - public void delete() { - SQL.update("DELETE FROM NodeData WHERE NodeId = ?", node); - } -} diff --git a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java index 22c23c3..ad265e7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java +++ b/SpigotCore_Main/src/de/steamwar/sql/NodeMember.java @@ -61,6 +61,11 @@ public class NodeMember { } } + public static NodeMember createNodeMember(int node, int member) { + SQL.update("INSERT INTO NodeMember (NodeId, UserId) VALUES (?, ?)", node, member); + return getNodeMember(node, member); + } + final int node; final int member; diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java new file mode 100644 index 0000000..b4b2115 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -0,0 +1,160 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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.VersionedCallable; +import de.steamwar.core.VersionedRunnable; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class Schematic { + + private SchematicNode node; + + private Schematic(SchematicNode node) { + this.node = node; + } + + public static void createSchem(String schemName, UUID schemOwner, String item, SchematicType schemType){ + createSchem(schemName, SteamwarUser.get(schemOwner).getId(), item, schemType); + } + + public static void createSchem(String schemName, int schemOwner, String item, SchematicType schemType){ + SchematicNode.createSchematicNode(schemOwner, schemName, 0, schemType.toDB(), item); + } + + public static Schematic getSchemFromDB(String schemName, UUID schemOwner){ + return getSchemFromDB(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static Schematic getSchemFromDB(String schemName, int schemOwner){ + return new Schematic(SchematicNode.getSchematicNode(schemOwner, schemName, 0)); + } + + public static Schematic getSchemFromDB(int schemID){ + return new Schematic(SchematicNode.getSchematicNode(schemID)); + } + + public static List getSchemsAccessibleByUser(UUID schemOwner){ + return getSchemsAccessibleByUser(SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemsAccessibleByUser(int schemOwner){ + List nodes = SchematicNode.getSchematicsAccessibleByUser(schemOwner, 0); + List schematics = new ArrayList<>(); + nodes.forEach(node1 -> schematics.add(new Schematic(node1))); + return schematics; + } + + public static List getSchemsOfType(UUID schemOwner, SchematicType schemType){ + return getSchemsOfType(SteamwarUser.get(schemOwner).getId(), schemType); + } + + public static List getSchemsOfType(int schemOwner, SchematicType schemType){ + //Unsauber, dafür auch geaddede Schematics dabei + List schems = getSchemsAccessibleByUser(schemOwner); + for(int i = schems.size()-1; i >= 0; i--) + if(!schems.get(i).getSchemType().equals(schemType)) + schems.remove(i); + return schems; + } + + public static List getAllSchemsOfType(SchematicType schemType){ + List nodes = SchematicNode.getAllSchematicsOfType(schemType.toDB()); + List schematics = new ArrayList<>(); + nodes.forEach(node1 -> schematics.add(new Schematic(node1))); + return schematics; + } + + public int getSchemID() { + return node.getId(); + } + + public String getSchemName() { + return node.getName(); + } + + public int getSchemOwner() { + return node.getOwner(); + } + + public int getRank(){ + return node.getRank(); + } + + public String getItem() { + return node.getItem(); + } + + public void setItem(String item) { + node.setItem(item); + } + + public void setRank(int rank){ + node.setRank(rank); + } + + public SchematicType getSchemType() { + return node.getSchemtype(); + } + + public void setSchemType(SchematicType schemType) { + node.setType(schemType.toDB()); + } + + public boolean availible(){ + return true; + } + + public Clipboard load() throws IOException, NoClipboardException { + return node.load(); + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + node.loadToPlayer(player); + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + node.saveOldFormatFromPlayer(player); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + node.saveFromPlayer(player); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + node.saveFromBytes(bytes, newFormat); + } + + public void remove(){ + node.delete(); + } + + public static class WrongVersionException extends Exception{} +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java new file mode 100644 index 0000000..cf1f07e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicMember.java @@ -0,0 +1,129 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class SchematicMember { + + private NodeMember member; + + private SchematicMember(NodeMember member){ + this.member = member; + } + + public SchematicMember(String schemName, int schemOwner, int schemMember){ + this(schemName, schemOwner, schemMember, true); + } + + public SchematicMember(String schemName, UUID schemOwner, UUID schemMember){ + this(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId(), true); + } + + private void updateDB(){ + SQL.update("INSERT INTO SchemMember (SchemName, SchemOwner, Member) VALUES (?, ?, ?)", schemName, schemOwner, member); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, UUID schemOwner, UUID schemMember){ + return getSchemMemberFromDB(schemName, SteamwarUser.get(schemOwner).getId(), SteamwarUser.get(schemMember).getId()); + } + + public static SchematicMember getSchemMemberFromDB(String schemName, int schemOwner, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ? AND Member = ?", schemName, schemOwner, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get schemmember", e); + } + } + + public static SchematicMember getMemberBySchematic(String schemName, int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND Member = ?", schemName, schemMember); + try { + if(schematicMember == null || !schematicMember.next()){ + return null; + } + int schemOwner = schematicMember.getInt("SchemOwner"); + return new SchematicMember(schemName, schemOwner, schemMember, false); + } catch (SQLException e) { + throw new SecurityException("Could not get member", e); + } + } + + public static List getSchemMembers(String schemName, UUID schemOwner){ + return getSchemMembers(schemName, SteamwarUser.get(schemOwner).getId()); + } + + public static List getSchemMembers(String schemName, int schemOwner){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE SchemName = ? AND SchemOwner = ?", schemName, schemOwner); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + int schemMember = schematicMember.getInt("Member"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + throw new SecurityException("Could not get schemmembers", e); + } + } + + public static List getAccessibleSchems(UUID schemMember){ + return getAccessibleSchems(SteamwarUser.get(schemMember).getId()); + } + + public static List getAccessibleSchems(int schemMember){ + ResultSet schematicMember = SQL.select("SELECT * FROM SchemMember WHERE Member = ?", schemMember); + try { + List schematicMembers = new ArrayList<>(); + while(schematicMember.next()){ + String schemName = schematicMember.getString("SchemName"); + int schemOwner = schematicMember.getInt("SchemOwner"); + schematicMembers.add(new SchematicMember(schemName, schemOwner, schemMember, false)); + } + return schematicMembers; + } catch (SQLException e) { + throw new SecurityException("Could not get accessible schems", e); + } + } + + public int getSchemOwner() { + return schemOwner; + } + + public String getSchemName() { + return schemName; + } + + public int getMember() { + return member; + } + + public void remove(){ + SQL.update("DELETE FROM SchemMember WHERE SchemOwner = ? AND SchemName = ? AND Member = ?", schemOwner, schemName, member); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index d584ddb..226f3c3 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -19,6 +19,14 @@ package de.steamwar.sql; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.VersionedCallable; +import de.steamwar.core.VersionedRunnable; +import org.bukkit.entity.Player; + +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; @@ -29,17 +37,28 @@ public class SchematicNode { public static final int ROOT_DIR = 0; public static SchematicNode createSchematic(int owner, String name, int parent) { - return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB()); + return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); } public static SchematicNode createSchematicDirectory(int owner, String name, int parent) { - return createSchematicNode(owner, name, parent, DIR_TYPE); + return createSchematicNode(owner, name, parent, DIR_TYPE, ""); } - private static SchematicNode createSchematicNode(int owner, String name, int parent, String type) { - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", - name, owner, parent, type); - return getSchematicNode(owner, name, parent); + public static SchematicNode createSchematicNode(int owner, String name, int parent, String type, String item) { + SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeType, NodeItem) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUES(NodeName), ParentNode = VALUES(ParentNode), NodeItem = VALUES(NodeItem), NodeType = VALUES(NodeType), NodeItem = VALUES(NodeItem)", + name, owner, parent, type, item); + return getSchematicNode(owner, name, type, parent); + } + + public static SchematicNode getSchematicNode(int owner, String name, String type, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeOwner = ? AND NodeName = ? AND ParentNode = ? AND NodeType = ?", owner, name, parent, type); + try { + if(!set.next()) + return null; + return new SchematicNode(set); + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } } public static SchematicNode getSchematicNode(int owner, String name, int parent) { @@ -94,8 +113,22 @@ public class SchematicNode { } } + public static SchematicNode getSchematicInParent(String name, int parent) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeName = ? AND ParentNode = ?", name, parent); + try { + while (set.next()) { + SchematicNode node = new SchematicNode(set); + if(!node.isDir()) + return node; + } + return null; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + public static SchematicNode getSchematicNode(int id) { - ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId", id); + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE NodeId = ?", id); try { if(!set.next()) return null; @@ -117,6 +150,18 @@ public class SchematicNode { } } + public static List getAllSchematicsOfType(String schemType) { + ResultSet set = SQL.select("SELECT * FROM SchematicNode WHERE SchemType = ?", schemType); + try { + List nodes = new ArrayList<>(); + while (set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch (SQLException e) { + throw new SecurityException("Failed to load Schemnodes", e); + } + } + public static List getSchematicsOfType(int owner, String schemType, int parent) { List schems = getAllSchematicsOfType(owner, schemType); Map nodesInParent = new LinkedHashMap<>(); @@ -131,6 +176,21 @@ public class SchematicNode { } public static List getSchematicsAccessibleByUser(int user, int parent) { + if(parent != 0) { + SchematicNode node = SchematicNode.getSchematicNode(parent); + boolean isAdded = false; + while (node.getId() != 0) { + for (NodeMember member:node.getMembers()) { + if (member.getMember() == user) { + isAdded = true; + break; + } + } + node = SchematicNode.getSchematicNode(node.getParent()); + } + if(isAdded) + return getSchematicNodeInNode(parent); + } ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE ( s.NodeOwner = ? OR nm.UserId = ? ) AND s.ParentNode = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user, parent); try{ List nodes = new ArrayList<>(); @@ -142,12 +202,26 @@ public class SchematicNode { } } + public static List getAllSchematicsAccessibleByUser(int user) { + ResultSet set = SQL.select("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode FROM SchematicNode s LEFT JOIN NodeMember nm ON nm.NodeId = s.NodeId WHERE s.NodeOwner = ? OR nm.UserId = ? GROUP BY s.NodeId ORDER BY s.NodeName", user, user); + try{ + List nodes = new ArrayList<>(); + while(set.next()) + nodes.add(new SchematicNode(set)); + return nodes; + }catch(SQLException e){ + throw new SecurityException("Failed listing schematics", e); + } + } + private final int id; private final int owner; private String name; private int parent; private String item; private String type; + private boolean schemFormat; + private int rank; private SchematicNode(ResultSet set) throws SQLException { id = set.getInt("NodeId"); @@ -156,6 +230,8 @@ public class SchematicNode { parent = set.getInt("ParentNode"); item = set.getString("NodeItem"); type = set.getString("NodeType"); + rank = set.getInt("NodeRank"); + schemFormat = set.getBoolean("SchemFormat"); } public int getId() { @@ -206,8 +282,20 @@ public class SchematicNode { return type.equals(DIR_TYPE); } + public boolean getSchemFormat() { + return schemFormat; + } + + public int getRank() { + return rank; + } + + public void setRank(int rank) { + this.rank = rank; + } + public SchematicType getSchemtype() { - if(!isDir()) + if(isDir()) throw new RuntimeException("Is Directory"); return SchematicType.fromDB(type); } @@ -216,12 +304,6 @@ public class SchematicNode { return SchematicNode.getSchematicNode(parent); } - public NodeData getNodeData() { - if(isDir()) - throw new SecurityException("Could not get NodeData of Directory"); - return NodeData.getSchemdata(id); - } - public boolean accessibleByUser(int user) { return NodeMember.getNodeMember(id, user) != null; } @@ -231,8 +313,8 @@ public class SchematicNode { } private void updateDB() { - SQL.update("INSERT INTO SchematicNode (NodeName, NodeOwner, ParentNode, NodeItem, NodeType) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE NodeName = VALUE(NodeName), ParentNode = VALUE(ParentNode), NodeItem = VALUE(NodeItem), NodeType = VALUE(NodeType)", - name, owner, parent, item, type); + SQL.update("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ? WHERE NodeId = ?", + name, owner, parent, item, type, id); } public void delete() { @@ -241,4 +323,76 @@ public class SchematicNode { } SQL.update("DELETE FROM SchematicNode WHERE NodeId = ?", id); } + + public Clipboard load() throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); + try { + rs.next(); + Blob schemData = rs.getBlob("SchemData"); + if(schemData == null) + throw new IOException("SchemData is null"); + InputStream is = schemData.getBinaryStream(); + return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), + new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void loadToPlayer(Player player) throws IOException, NoClipboardException { + ResultSet rs = SQL.select("SELECT NodeData FROM SchematicNode WHERE NodeId = ?", id); + try { + rs.next(); + Blob blob = rs.getBlob("SchemData"); + if(blob == null) + throw new NoClipboardException(); + InputStream is = blob.getBinaryStream(); + VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), + new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); + } catch (SQLException e) { + throw new IOException(e); + } + } + + public void saveOldFormatFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, false); + } + + public void saveFromPlayer(Player player) throws IOException, NoClipboardException { + saveFromPlayer(player, true); + } + + public void saveFromBytes(byte[] bytes, boolean newFormat) { + Blob blob = SQL.blob(); + try { + blob.setBytes(1, bytes); + updateDatabase(blob, newFormat); + } catch (SQLException e) { + throw new SecurityException(e); + } + } + + private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { + Blob blob = SQL.blob(); + VersionedRunnable.call(new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); + } catch (SQLException e) { + throw new RuntimeException(e.getMessage(), e); + } + updateDatabase(blob, false); + }, 8), new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); + } catch (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + updateDatabase(blob, newFormat); + }, 14)); + } + + private void updateDatabase(Blob blob, boolean newFormat) { + SQL.update("UPDATE SchematicNode SET NodeData = ?, SchemFormat = ? WHERE NodeId = ?", blob, newFormat, id); + schemFormat = newFormat; + } }