SteamWar/SpigotCore
Archiviert
13
0

Saving schematics in database #13

Geschlossen
Lixfel möchte 5 Commits von schematic nach master mergen
12 geänderte Dateien mit 386 neuen und 33 gelöschten Zeilen

Datei anzeigen

@ -33,5 +33,27 @@
<version>1.12</version>
<scope>provided</scope>
</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>
</dependency>
<dependency>
<groupId>steamwar</groupId>
<artifactId>SpigotCore_8</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</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 {
Schematic_8.setPlayerClipboard(player, is);
}
static Clipboard getClipboard(InputStream is) throws IOException {
return Schematic_8.getClipboard(is);
}
}

Datei anzeigen

@ -33,5 +33,16 @@
<version>1.14</version>
<scope>provided</scope>
</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>
</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,16 @@
<version>1.8</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>
</dependency>
</dependencies>
</project>

Datei anzeigen

@ -0,0 +1,54 @@
package de.steamwar.sql;
import com.sk89q.worldedit.EmptyClipboardException;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitWorld;
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.ClipboardFormat;
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 {
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 {
return ClipboardFormat.findByAlias("mcedit").getReader(is).read(WorldEdit.getInstance().getServer().getWorlds().get(0).getWorldData());
}
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>
<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>

Datei anzeigen

@ -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 {

Datei anzeigen

@ -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<Schematic> 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<Schematic> getSchemsOfType(UUID schemOwner, SchematicType schemType){
@ -99,10 +98,9 @@ public class Schematic {
public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){
//Unsauber, dafür auch geaddede Schematics dabei
List<Schematic> 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,91 @@ public class Schematic {
updateDB();
}
public boolean availible(){
return Core.getVersion() > 12 || !schemFormat;
}
public Clipboard load() throws WrongVersionException, IOException, NoClipboardException {
if(Core.getVersion() <= 12 && schemFormat)
throw new WrongVersionException();
ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID);
try {
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);
}
} catch (SQLException e) {
throw new IOException(e);
}
}
public void loadToPlayer(Player player) throws IOException, NoClipboardException, WrongVersionException {
if(Core.getVersion() <= 12 && schemFormat)
throw new WrongVersionException();
ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = " + schemID);
try {
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);
}
} 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);
}
private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException {
try{
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;
}catch(SQLException e){
throw new IOException(e);
}
}
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{}
}

Datei anzeigen

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