SteamWar/SpigotCore
Archiviert
13
0

Rework to save schematics in database

Dieser Commit ist enthalten in:
Lixfel 2019-12-05 09:52:33 +01:00
Ursprung 11e9dfb6aa
Commit bd6b5f48c3
12 geänderte Dateien mit 378 neuen und 33 gelöschten Zeilen

Datei anzeigen

@ -33,5 +33,28 @@
<version>1.12</version> <version>1.12</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>FAWE</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>WorldEdit</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_API</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_8</artifactId>
<version>2.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

Datei anzeigen

@ -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);
}
}

Datei anzeigen

@ -33,5 +33,16 @@
<version>1.14</version> <version>1.14</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>FAWE</artifactId>
<version>1.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_API</artifactId>
<version>2.0</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

Datei anzeigen

@ -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");
}
}

Datei anzeigen

@ -33,5 +33,17 @@
<version>1.8</version> <version>1.8</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>WorldEdit</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_API</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

Datei anzeigen

@ -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");
}
}

28
SpigotCore_API/pom.xml Normale Datei
Datei anzeigen

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>steamwar</groupId>
<artifactId>SpigotCore</artifactId>
<version>2.0</version>
</parent>
<artifactId>SpigotCore_API</artifactId>
<version>2.0</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/*.kt</exclude>
</excludes>
</resource>
</resources>
</build>
</project>

Datei anzeigen

@ -0,0 +1,4 @@
package de.steamwar.sql;
public class NoClipboardException extends Exception {
}

Datei anzeigen

@ -50,6 +50,18 @@
<version>1.12</version> <version>1.12</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>WorldEdit</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_API</artifactId>
<version>2.0</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>steamwar</groupId> <groupId>steamwar</groupId>
<artifactId>SpigotCore_8</artifactId> <artifactId>SpigotCore_8</artifactId>

Datei anzeigen

@ -48,6 +48,10 @@ public class SQL {
throw new SecurityException("Could not close connection", e); throw new SecurityException("Could not close connection", e);
} }
} }
static Connection getCon(){
return con;
}
static void update(String qry) { static void update(String qry) {
try { try {

Datei anzeigen

@ -1,5 +1,13 @@
package de.steamwar.sql; 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.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,38 +16,31 @@ import java.util.UUID;
public class Schematic { public class Schematic {
private final int schemID;
private final String schemName; private final String schemName;
private final int schemOwner; private final int schemOwner;
private boolean schemFormat;
private String item; private String item;
private SchematicType schemType; private SchematicType schemType;
private Schematic(ResultSet rs) throws SQLException { private Schematic(ResultSet rs) throws SQLException {
this( this.schemID = rs.getInt("SchemID");
rs.getString("SchemName"), this.schemName = rs.getString("SchemName");
rs.getInt("SchemOwner"), this.schemOwner = rs.getInt("SchemOwner");
rs.getString("Item"), this.item = rs.getString("Item");
SchematicType.fromDB(rs.getString("SchemType")), this.schemType = SchematicType.fromDB(rs.getString("SchemType"));
false); this.schemFormat = rs.getBoolean("SchemFormat");
}
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);
} }
private void updateDB(){ 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" + SQL.update("INSERT INTO Schematic" +
" (SchemName, SchemOwner, Item, SchemType)" + " (SchemName, SchemOwner, Item, SchemType)" +
" VALUES" + " VALUES" +
@ -65,9 +66,8 @@ public class Schematic {
} }
return new Schematic(schematic); return new Schematic(schematic);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); throw new SecurityException("Failed loading schematic", e);
} }
return null;
} }
public static List<Schematic> getSchemsAccessibleByUser(UUID schemOwner){ public static List<Schematic> getSchemsAccessibleByUser(UUID schemOwner){
@ -87,9 +87,8 @@ public class Schematic {
} }
return schematics; return schematics;
}catch(SQLException e){ }catch(SQLException e){
e.printStackTrace(); throw new SecurityException("Failed listing schematics", e);
} }
return null;
} }
public static List<Schematic> getSchemsOfType(UUID schemOwner, SchematicType schemType){ public static List<Schematic> getSchemsOfType(UUID schemOwner, SchematicType schemType){
@ -99,10 +98,9 @@ public class Schematic {
public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){ public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){
//Unsauber, dafür auch geaddede Schematics dabei //Unsauber, dafür auch geaddede Schematics dabei
List<Schematic> schems = getSchemsAccessibleByUser(schemOwner); List<Schematic> schems = getSchemsAccessibleByUser(schemOwner);
if(schems != null) for(int i = schems.size()-1; i >= 0; i--)
for(int i = schems.size()-1; i >= 0; i--) if(!schems.get(i).getSchemType().equals(schemType))
if(!schems.get(i).getSchemType().equals(schemType)) schems.remove(i);
schems.remove(i);
return schems; return schems;
} }
@ -115,9 +113,12 @@ public class Schematic {
} }
return schematics; return schematics;
}catch(SQLException e){ }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() { public String getSchemName() {
@ -146,8 +147,75 @@ public class Schematic {
updateDB(); 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(){ public void remove(){
SQL.update("DELETE FROM Schematic WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); SQL.update("DELETE FROM Schematic WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'");
SQL.update("DELETE FROM SchemMember WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'"); SQL.update("DELETE FROM SchemMember WHERE SchemOwner = " + schemOwner + " AND SchemName = '" + schemName + "'");
} }
public static class WrongVersionException extends Exception{}
} }

Datei anzeigen

@ -37,6 +37,7 @@
</build> </build>
<modules> <modules>
<module>SpigotCore_API</module>
<module>SpigotCore_8</module> <module>SpigotCore_8</module>
<module>SpigotCore_12</module> <module>SpigotCore_12</module>
<module>SpigotCore_14</module> <module>SpigotCore_14</module>