SteamWar/SpigotCore
Archiviert
13
0

Implement Streaming
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-11-03 16:28:01 +01:00
Ursprung 1e3d387ff8
Commit de6d8bbb10
4 geänderte Dateien mit 56 neuen und 39 gelöschten Zeilen

Datei anzeigen

@ -29,10 +29,9 @@ import com.sk89q.worldedit.world.registry.LegacyMapper;
import de.steamwar.sql.NoClipboardException; import de.steamwar.sql.NoClipboardException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.*; import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -43,7 +42,7 @@ public class WorldEditWrapper14 implements WorldEditWrapper.IWorldEditWrapper {
private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC; private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC;
@Override @Override
public byte[] getPlayerClipboard(Player player, boolean schemFormat) { public InputStream getPlayerClipboard(Player player, boolean schemFormat) {
ClipboardHolder clipboardHolder; ClipboardHolder clipboardHolder;
try { try {
clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard(); clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard();
@ -55,21 +54,36 @@ public class WorldEditWrapper14 implements WorldEditWrapper.IWorldEditWrapper {
if(clipboard == null) if(clipboard == null)
throw new NoClipboardException(); throw new NoClipboardException();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PipedOutputStream outputStream = new PipedOutputStream();
try{ PipedInputStream inputStream;
if(schemFormat){ try {
ClipboardWriter writer = SCHEM.getWriter(outputStream); inputStream = new PipedInputStream(outputStream, 4096);
writer.write(clipboard);
writer.close();
}else{
SCHEMATIC.getWriter(outputStream).write(clipboard);
}
}catch(NullPointerException e){ }catch(NullPointerException e){
throw new RuntimeException(e.getMessage(), new IOException(e)); throw new RuntimeException(e.getMessage(), new IOException(e));
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.getMessage(), e); throw new SecurityException("Could not init piped input stream", e);
} }
return outputStream.toByteArray();
new Thread(() -> {
try{
if(schemFormat){
ClipboardWriter writer = SCHEM.getWriter(outputStream);
writer.write(clipboard);
writer.close();
}else{
SCHEMATIC.getWriter(outputStream).write(clipboard);
}
}catch(NullPointerException | IOException e) {
Core.getInstance().getLogger().log(Level.SEVERE, "Could not write schematic", e);
}
try {
outputStream.close();
} catch (IOException e) {
Core.getInstance().getLogger().log(Level.SEVERE, "Could not close schem writer", e);
}
}, "SchemWriter").start();
return inputStream;
} }
@Override @Override

Datei anzeigen

@ -19,19 +19,18 @@ import com.sk89q.worldedit.world.registry.WorldData;
import de.steamwar.sql.NoClipboardException; import de.steamwar.sql.NoClipboardException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper { public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
@Override @Override
public byte[] getPlayerClipboard(Player player, boolean schemFormat) { public InputStream getPlayerClipboard(Player player, boolean schemFormat) {
ClipboardHolder clipboardHolder; ClipboardHolder clipboardHolder;
try { try {
clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard(); clipboardHolder = WorldEditWrapper.getWorldEditPlugin().getSession(player).getClipboard();
@ -43,13 +42,28 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
if(clipboard == null) if(clipboard == null)
throw new NoClipboardException(); throw new NoClipboardException();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PipedOutputStream outputStream = new PipedOutputStream();
PipedInputStream inputStream;
try { try {
ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); inputStream = new PipedInputStream(outputStream, 4096);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e.getMessage(), e); throw new SecurityException("Could not init piped input stream", e);
} }
return outputStream.toByteArray();
new Thread(() -> {
try {
ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData());
} catch (IOException e) {
Core.getInstance().getLogger().log(Level.SEVERE, "Could not write schematic", e);
}
try {
outputStream.close();
} catch (IOException e) {
Core.getInstance().getLogger().log(Level.SEVERE, "Could not close schem writer", e);
}
}, "SchemWriter").start();
return inputStream;
} }
@Override @Override

Datei anzeigen

@ -14,7 +14,7 @@ public class WorldEditWrapper {
public static final IWorldEditWrapper impl = VersionDependent.getVersionImpl(Core.getInstance()); public static final IWorldEditWrapper impl = VersionDependent.getVersionImpl(Core.getInstance());
public interface IWorldEditWrapper { public interface IWorldEditWrapper {
byte[] getPlayerClipboard(Player player, boolean schemFormat); InputStream getPlayerClipboard(Player player, boolean schemFormat);
void setPlayerClipboard(Player player, InputStream is, boolean schemFormat); void setPlayerClipboard(Player player, InputStream is, boolean schemFormat);
Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException; Clipboard getClipboard(InputStream is, boolean schemFormat) throws IOException;

Datei anzeigen

@ -25,6 +25,7 @@ import de.steamwar.core.VersionedRunnable;
import de.steamwar.core.WorldEditWrapper; import de.steamwar.core.WorldEditWrapper;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.sql.Blob; import java.sql.Blob;
@ -476,28 +477,16 @@ public class SchematicNode {
public void saveFromBytes(byte[] bytes, boolean newFormat) { public void saveFromBytes(byte[] bytes, boolean newFormat) {
if(isDir) if(isDir)
throw new SecurityException("Node is Directory"); throw new SecurityException("Node is Directory");
Blob blob = SQL.blob(); updateDatabase(new ByteArrayInputStream(bytes), newFormat);
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 { private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException {
if(isDir) if(isDir)
throw new SecurityException("Node is Directory"); throw new SecurityException("Node is Directory");
Blob blob = SQL.blob(); updateDatabase(WorldEditWrapper.impl.getPlayerClipboard(player, newFormat), newFormat);
try {
blob.setBytes(1, WorldEditWrapper.impl.getPlayerClipboard(player, newFormat));
} catch (SQLException exception) {
throw new RuntimeException(exception.getMessage(), exception);
}
updateDatabase(blob, newFormat);
} }
private void updateDatabase(Blob blob, boolean newFormat) { private void updateDatabase(InputStream blob, boolean newFormat) {
SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id); SQL.update("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?", blob, newFormat, id);
schemFormat = newFormat; schemFormat = newFormat;
} }