From 00898f996ae3c8c56951859f0d36d96f1c16ce2a Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 5 Dec 2019 09:52:33 +0100 Subject: [PATCH] Rework to save schematics in database --- SpigotCore_12/pom.xml | 23 +++ .../src/de/steamwar/sql/Schematic_12.java | 42 ++++++ SpigotCore_14/pom.xml | 11 ++ .../src/de/steamwar/sql/Schematic_14.java | 80 +++++++++++ SpigotCore_8/pom.xml | 12 ++ .../src/de/steamwar/sql/Schematic_8.java | 60 ++++++++ SpigotCore_API/pom.xml | 28 ++++ .../de/steamwar/sql/NoClipboardException.java | 4 + SpigotCore_Main/pom.xml | 12 ++ SpigotCore_Main/src/de/steamwar/sql/SQL.java | 4 + .../src/de/steamwar/sql/Schematic.java | 134 +++++++++++++----- pom.xml | 1 + 12 files changed, 378 insertions(+), 33 deletions(-) create mode 100644 SpigotCore_12/src/de/steamwar/sql/Schematic_12.java create mode 100644 SpigotCore_14/src/de/steamwar/sql/Schematic_14.java create mode 100644 SpigotCore_8/src/de/steamwar/sql/Schematic_8.java create mode 100644 SpigotCore_API/pom.xml create mode 100644 SpigotCore_API/src/de/steamwar/sql/NoClipboardException.java diff --git a/SpigotCore_12/pom.xml b/SpigotCore_12/pom.xml index eb4680f..29e7adb 100644 --- a/SpigotCore_12/pom.xml +++ b/SpigotCore_12/pom.xml @@ -33,5 +33,28 @@ 1.12 provided + + steamwar + FAWE + 1.0 + provided + + + steamwar + WorldEdit + 1.0 + provided + + + steamwar + SpigotCore_API + 2.0 + compile + + + steamwar + SpigotCore_8 + 2.0 + \ No newline at end of file diff --git a/SpigotCore_12/src/de/steamwar/sql/Schematic_12.java b/SpigotCore_12/src/de/steamwar/sql/Schematic_12.java new file mode 100644 index 0000000..5a4989e --- /dev/null +++ b/SpigotCore_12/src/de/steamwar/sql/Schematic_12.java @@ -0,0 +1,42 @@ +package de.steamwar.sql; + +import com.boydti.fawe.FaweAPI; +import com.sk89q.worldedit.EmptyClipboardException; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; +import com.sk89q.worldedit.session.ClipboardHolder; +import org.bukkit.entity.Player; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +class Schematic_12 { + private Schematic_12(){} + + static byte[] getPlayerClipboard(Player player) throws IOException, NoClipboardException { + ClipboardHolder clipboardHolder; + try { + clipboardHolder = FaweAPI.wrapPlayer(player).getSession().getClipboard(); + } catch (EmptyClipboardException e) { + throw new NoClipboardException(); + } + + @SuppressWarnings("deprecation") + Clipboard clipboard = clipboardHolder.getClipboard(); + if(clipboard == null) + throw new NoClipboardException(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + return outputStream.toByteArray(); + } + + static void setPlayerClipboard(Player player, InputStream is) throws IOException, NoClipboardException { + Schematic_8.setPlayerClipboard(player, is); + } + + static Clipboard getClipboard(InputStream is) throws IOException, NoClipboardException { + return Schematic_8.getClipboard(is); + } +} diff --git a/SpigotCore_14/pom.xml b/SpigotCore_14/pom.xml index 9b030de..cf128c3 100644 --- a/SpigotCore_14/pom.xml +++ b/SpigotCore_14/pom.xml @@ -33,5 +33,16 @@ 1.14 provided + + steamwar + FAWE + 1.14 + provided + + + steamwar + SpigotCore_API + 2.0 + \ No newline at end of file diff --git a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java new file mode 100644 index 0000000..8320c36 --- /dev/null +++ b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java @@ -0,0 +1,80 @@ +package de.steamwar.sql; + +import com.boydti.fawe.FaweAPI; +import com.sk89q.worldedit.EmptyClipboardException; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.extension.platform.Actor; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats; +import com.sk89q.worldedit.session.ClipboardHolder; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +class Schematic_14 { + private Schematic_14(){} + + private static final String SCHEM = "schem"; + private static final String SCHEMATIC = "schematic"; + + static byte[] getPlayerClipboard(Player player, boolean schemFormat) throws IOException, NoClipboardException { + ClipboardHolder clipboardHolder; + try { + clipboardHolder = FaweAPI.wrapPlayer(player).getSession().getClipboard(); + } catch (EmptyClipboardException e) { + throw new NoClipboardException(); + } + + @SuppressWarnings("deprecation") + Clipboard clipboard = clipboardHolder.getClipboard(); + if(clipboard == null) + throw new NoClipboardException(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + try{ + if(schemFormat) + ClipboardFormats.findByExtension(SCHEM).getWriter(outputStream).write(clipboard); + else + ClipboardFormats.findByExtension(SCHEMATIC).getWriter(outputStream).write(clipboard); + }catch(NullPointerException e){ + throw new IOException(e); + } + return outputStream.toByteArray(); + } + + static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) throws IOException, NoClipboardException { + Clipboard clipboard; + try { + if (schemFormat) + clipboard = ClipboardFormats.findByExtension(SCHEM).getReader(is).read(); + else + clipboard = ClipboardFormats.findByExtension(SCHEMATIC).getReader(is).read(); + }catch(NullPointerException e){ + throw new IOException(e); + } + + if (clipboard == null) + throw new NoClipboardException(); + + Actor actor = getWorldEditPlugin().wrapCommandSender(player); + getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard)); + } + + static Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException, NoClipboardException { + try { + if(schemFormat) + return ClipboardFormats.findByExtension(SCHEM).getReader(is).read(); + else + return ClipboardFormats.findByExtension(SCHEMATIC).getReader(is).read(); + } catch (NullPointerException e) { + throw new NoClipboardException(); + } + } + + private static WorldEditPlugin getWorldEditPlugin() { + return (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"); + } +} diff --git a/SpigotCore_8/pom.xml b/SpigotCore_8/pom.xml index 5f39805..482fe5b 100644 --- a/SpigotCore_8/pom.xml +++ b/SpigotCore_8/pom.xml @@ -33,5 +33,17 @@ 1.8 provided + + steamwar + WorldEdit + 1.0 + provided + + + steamwar + SpigotCore_API + 2.0 + compile + \ No newline at end of file diff --git a/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java b/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java new file mode 100644 index 0000000..e6ef7f9 --- /dev/null +++ b/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java @@ -0,0 +1,60 @@ +package de.steamwar.sql; + +import com.sk89q.worldedit.EmptyClipboardException; +import com.sk89q.worldedit.bukkit.BukkitWorld; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; +import com.sk89q.worldedit.data.DataException; +import com.sk89q.worldedit.extension.platform.Actor; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat; +import com.sk89q.worldedit.schematic.MCEditSchematicFormat; +import com.sk89q.worldedit.schematic.SchematicFormat; +import com.sk89q.worldedit.session.ClipboardHolder; +import com.sk89q.worldedit.world.registry.WorldData; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +class Schematic_8 { + private Schematic_8(){} + + static byte[] getPlayerClipboard(Player player) throws IOException, NoClipboardException { + ClipboardHolder clipboardHolder; + try { + clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard(); + } catch (EmptyClipboardException e) { + throw new NoClipboardException(); + } + + Clipboard clipboard = clipboardHolder.getClipboard(); + if(clipboard == null) + throw new NoClipboardException(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + return outputStream.toByteArray(); + } + + static void setPlayerClipboard(Player player, InputStream is) throws IOException, NoClipboardException { + WorldData world = new BukkitWorld(player.getWorld()).getWorldData(); + Clipboard clipboard = getClipboard(is); + + Actor actor = getWorldEditPlugin().wrapCommandSender(player); + getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard, world)); + } + + static Clipboard getClipboard(InputStream is) throws IOException, NoClipboardException { + try { + return (Clipboard) ((MCEditSchematicFormat)SchematicFormat.getFormat("mcedit")).load(is); + } catch (DataException e) { + throw new NoClipboardException(); + } + } + + private static WorldEditPlugin getWorldEditPlugin() { + return (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"); + } +} diff --git a/SpigotCore_API/pom.xml b/SpigotCore_API/pom.xml new file mode 100644 index 0000000..26c1621 --- /dev/null +++ b/SpigotCore_API/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + + steamwar + SpigotCore + 2.0 + + + SpigotCore_API + 2.0 + + + src + + + src + + **/*.java + **/*.kt + + + + + \ No newline at end of file diff --git a/SpigotCore_API/src/de/steamwar/sql/NoClipboardException.java b/SpigotCore_API/src/de/steamwar/sql/NoClipboardException.java new file mode 100644 index 0000000..f8b6e02 --- /dev/null +++ b/SpigotCore_API/src/de/steamwar/sql/NoClipboardException.java @@ -0,0 +1,4 @@ +package de.steamwar.sql; + +public class NoClipboardException extends Exception { +} diff --git a/SpigotCore_Main/pom.xml b/SpigotCore_Main/pom.xml index c05ee63..10c1151 100644 --- a/SpigotCore_Main/pom.xml +++ b/SpigotCore_Main/pom.xml @@ -50,6 +50,18 @@ 1.12 provided + + steamwar + WorldEdit + 1.0 + provided + + + steamwar + SpigotCore_API + 2.0 + compile + steamwar SpigotCore_8 diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQL.java b/SpigotCore_Main/src/de/steamwar/sql/SQL.java index fd243d6..8375a99 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQL.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQL.java @@ -48,6 +48,10 @@ public class SQL { throw new SecurityException("Could not close connection", e); } } + + static Connection getCon(){ + return con; + } static void update(String qry) { try { diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 340252e..bf00fc2 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -1,5 +1,13 @@ package de.steamwar.sql; +import com.sk89q.worldedit.extent.clipboard.Clipboard; +import de.steamwar.core.Core; +import org.bukkit.entity.Player; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -8,38 +16,31 @@ import java.util.UUID; public class Schematic { + private final int schemID; private final String schemName; private final int schemOwner; + private boolean schemFormat; private String item; private SchematicType schemType; private Schematic(ResultSet rs) throws SQLException { - this( - rs.getString("SchemName"), - rs.getInt("SchemOwner"), - rs.getString("Item"), - SchematicType.fromDB(rs.getString("SchemType")), - false); - } - - private Schematic(String schemName, int schemOwner, String item, SchematicType schemType, boolean updateDB){ - this.schemName = schemName; - this.schemOwner = schemOwner; - this.item = item; - this.schemType = schemType; - if(updateDB) - updateDB(); - } - - public Schematic(String schemName, int schemOwner, String item, SchematicType schemType){ - this(schemName, schemOwner, item, schemType, true); - } - - public Schematic(String schemName, UUID schemOwner, String item, SchematicType schemType){ - this(schemName, SteamwarUser.get(schemOwner).getId(), item, schemType, true); + this.schemID = rs.getInt("SchemID"); + this.schemName = rs.getString("SchemName"); + this.schemOwner = rs.getInt("SchemOwner"); + this.item = rs.getString("Item"); + this.schemType = SchematicType.fromDB(rs.getString("SchemType")); + this.schemFormat = rs.getBoolean("SchemFormat"); } private void updateDB(){ + createSchem(schemName, schemOwner, item, schemType); + } + + 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){ SQL.update("INSERT INTO Schematic" + " (SchemName, SchemOwner, Item, SchemType)" + " VALUES" + @@ -65,9 +66,8 @@ public class Schematic { } return new Schematic(schematic); } catch (SQLException e) { - e.printStackTrace(); + throw new SecurityException("Failed loading schematic", e); } - return null; } public static List getSchemsAccessibleByUser(UUID schemOwner){ @@ -87,9 +87,8 @@ public class Schematic { } return schematics; }catch(SQLException e){ - e.printStackTrace(); + throw new SecurityException("Failed listing schematics", e); } - return null; } public static List getSchemsOfType(UUID schemOwner, SchematicType schemType){ @@ -99,10 +98,9 @@ public class Schematic { public static List getSchemsOfType(int schemOwner, SchematicType schemType){ //Unsauber, dafür auch geaddede Schematics dabei List schems = getSchemsAccessibleByUser(schemOwner); - if(schems != null) - for(int i = schems.size()-1; i >= 0; i--) - if(!schems.get(i).getSchemType().equals(schemType)) - schems.remove(i); + for(int i = schems.size()-1; i >= 0; i--) + if(!schems.get(i).getSchemType().equals(schemType)) + schems.remove(i); return schems; } @@ -115,9 +113,12 @@ public class Schematic { } return schematics; }catch(SQLException e){ - e.printStackTrace(); + throw new SecurityException("Failed loading all schems of type", e); } - return null; + } + + public int getSchemID() { + return schemID; } public String getSchemName() { @@ -146,8 +147,75 @@ public class Schematic { updateDB(); } + public Clipboard load() throws WrongVersionException, SQLException, IOException, NoClipboardException { + if(Core.getVersion() <= 12 && schemFormat) + throw new WrongVersionException(); + + ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID); + rs.next(); + InputStream is = rs.getBlob("SchemData").getBinaryStream(); + switch(Core.getVersion()){ + case 8: + return Schematic_8.getClipboard(is); + case 14: + return Schematic_14.getClipboard(is, schemFormat); + default: + return Schematic_12.getClipboard(is); + } + } + + public void loadToPlayer(Player player) throws SQLException, IOException, NoClipboardException, WrongVersionException { + if(Core.getVersion() <= 12 && schemFormat) + throw new WrongVersionException(); + + ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID); + rs.next(); + InputStream is = rs.getBlob("SchemData").getBinaryStream(); + switch(Core.getVersion()){ + case 8: + Schematic_8.setPlayerClipboard(player, is); + break; + case 14: + Schematic_14.setPlayerClipboard(player, is, schemFormat); + break; + default: + Schematic_12.setPlayerClipboard(player, is); + } + } + + public void saveOldFormatFromPlayer(Player player) throws SQLException, IOException, NoClipboardException { + saveFromPlayer(player, false); + } + + public void saveFromPlayer(Player player) throws SQLException, IOException, NoClipboardException { + saveFromPlayer(player, true); + } + + private void saveFromPlayer(Player player, boolean newFormat) throws SQLException, IOException, NoClipboardException { + PreparedStatement st = SQL.getCon().prepareStatement("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = " + schemID); + byte[] data; + switch(Core.getVersion()){ + case 8: + newFormat = false; + data = Schematic_8.getPlayerClipboard(player); + break; + case 14: + data = Schematic_14.getPlayerClipboard(player, newFormat); + break; + default: + newFormat = false; + data = Schematic_12.getPlayerClipboard(player); + } + st.setBlob(1, new ByteArrayInputStream(data)); + st.setBoolean(2, newFormat); + st.executeUpdate(); + schemFormat = newFormat; + } + public void remove(){ SQL.update("DELETE FROM Schematic WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); SQL.update("DELETE FROM SchemMember WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); } + + public static class WrongVersionException extends Exception{} } diff --git a/pom.xml b/pom.xml index dbc56a3..f403dc5 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,7 @@ + SpigotCore_API SpigotCore_8 SpigotCore_12 SpigotCore_14