Revert recent changes to DiskOptimizedClipboard

This commit is contained in:
MattBDev 2020-02-18 14:27:05 -05:00
parent d2bcc6dd35
commit 2edc3bc344

View File

@ -21,6 +21,7 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockStateHolder; import com.sk89q.worldedit.world.block.BlockStateHolder;
import com.sk89q.worldedit.world.block.BlockTypes; import com.sk89q.worldedit.world.block.BlockTypes;
import java.io.Closeable;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -35,17 +36,18 @@ import java.nio.channels.FileChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import javax.annotation.Nullable; import javax.annotation.Nullable;
/** /**
* A clipboard with disk backed storage. (lower memory + loads on crash) - Uses an auto closable * A clipboard with disk backed storage. (lower memory + loads on crash)
* RandomAccessFile for getting / setting id / data - I don't know how to reduce nbt / entities to * - Uses an auto closable RandomAccessFile for getting / setting id / data
* O(2) complexity, so it is stored in memory. * - I don't know how to reduce nbt / entities to O(2) complexity, so it is stored in memory.
*/ */
public class DiskOptimizedClipboard extends LinearClipboard { public class DiskOptimizedClipboard extends LinearClipboard implements Closeable {
private static int HEADER_SIZE = 14; private static int HEADER_SIZE = 14;
@ -57,27 +59,18 @@ public class DiskOptimizedClipboard extends LinearClipboard {
private FileChannel fileChannel; private FileChannel fileChannel;
private boolean hasBiomes; private boolean hasBiomes;
private int ylast;
private int ylasti;
private int zlast;
private int zlasti;
public DiskOptimizedClipboard(BlockVector3 dimensions, UUID uuid) { public DiskOptimizedClipboard(BlockVector3 dimensions, UUID uuid) {
this(dimensions, MainUtil this(dimensions, MainUtil.getFile(Fawe.get() != null ? Fawe.imp().getDirectory() : new File("."), Settings.IMP.PATHS.CLIPBOARD + File.separator + uuid + ".bd"));
.getFile(Fawe.get() != null ? Fawe.imp().getDirectory() : new File("."),
Settings.IMP.PATHS.CLIPBOARD + File.separator + uuid + ".bd"));
} }
public DiskOptimizedClipboard(BlockVector3 dimensions) { public DiskOptimizedClipboard(BlockVector3 dimensions) {
this(dimensions, MainUtil this(dimensions, MainUtil.getFile(Fawe.imp() != null ? Fawe.imp().getDirectory() : new File("."), Settings.IMP.PATHS.CLIPBOARD + File.separator + UUID.randomUUID() + ".bd"));
.getFile(Fawe.imp() != null ? Fawe.imp().getDirectory() : new File("."),
Settings.IMP.PATHS.CLIPBOARD + File.separator + UUID.randomUUID() + ".bd"));
} }
public DiskOptimizedClipboard(BlockVector3 dimensions, File file) { public DiskOptimizedClipboard(BlockVector3 dimensions, File file) {
super(dimensions); super(dimensions);
if (getWidth() > Character.MAX_VALUE || getHeight() > Character.MAX_VALUE if (getWidth() > Character.MAX_VALUE || getHeight() > Character.MAX_VALUE || getLength() > Character.MAX_VALUE) {
|| getLength() > Character.MAX_VALUE) {
throw new IllegalArgumentException("Too large"); throw new IllegalArgumentException("Too large");
} }
nbtMap = new HashMap<>(); nbtMap = new HashMap<>();
@ -108,6 +101,21 @@ public class DiskOptimizedClipboard extends LinearClipboard {
} }
} }
@Override
public URI getURI() {
return file.toURI();
}
private static BlockVector3 readSize(File file) {
try (DataInputStream is = new DataInputStream(new FileInputStream(file))) {
is.skipBytes(2);
return BlockVector3.at(is.readChar(), is.readChar(), is.readChar());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public DiskOptimizedClipboard(File file) { public DiskOptimizedClipboard(File file) {
super(readSize(file)); super(readSize(file));
nbtMap = new HashMap<>(); nbtMap = new HashMap<>();
@ -124,21 +132,6 @@ public class DiskOptimizedClipboard extends LinearClipboard {
} }
} }
private static BlockVector3 readSize(File file) {
try (DataInputStream is = new DataInputStream(new FileInputStream(file))) {
is.skipBytes(2);
return BlockVector3.at(is.readChar(), is.readChar(), is.readChar());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Override
public URI getURI() {
return file.toURI();
}
public File getFile() { public File getFile() {
return file; return file;
} }
@ -195,9 +188,7 @@ public class DiskOptimizedClipboard extends LinearClipboard {
@Override @Override
public void streamBiomes(IntValueReader task) { public void streamBiomes(IntValueReader task) {
if (!hasBiomes()) { if (!hasBiomes()) return;
return;
}
int index = 0; int index = 0;
int mbbIndex = HEADER_SIZE + (getVolume() << 1); int mbbIndex = HEADER_SIZE + (getVolume() << 1);
try { try {
@ -220,8 +211,7 @@ public class DiskOptimizedClipboard extends LinearClipboard {
public BlockArrayClipboard toClipboard() { public BlockArrayClipboard toClipboard() {
try { try {
CuboidRegion region = new CuboidRegion(BlockVector3.at(0, 0, 0), CuboidRegion region = new CuboidRegion(BlockVector3.at(0, 0, 0), BlockVector3.at(getWidth() - 1, getHeight() - 1, getLength() - 1));
BlockVector3.at(getWidth() - 1, getHeight() - 1, getLength() - 1));
int ox = byteBuffer.getShort(8); int ox = byteBuffer.getShort(8);
int oy = byteBuffer.getShort(10); int oy = byteBuffer.getShort(10);
int oz = byteBuffer.getShort(12); int oz = byteBuffer.getShort(12);
@ -252,9 +242,7 @@ public class DiskOptimizedClipboard extends LinearClipboard {
} }
private void closeDirectBuffer(ByteBuffer cb) { private void closeDirectBuffer(ByteBuffer cb) {
if (cb == null || !cb.isDirect()) { if (cb == null || !cb.isDirect()) return;
return;
}
// we could use this type cast and call functions without reflection code, // we could use this type cast and call functions without reflection code,
// but static import from sun.* package is risky for non-SUN virtual machine. // but static import from sun.* package is risky for non-SUN virtual machine.
//try { ((sun.nio.ch.DirectBuffer)cb).cleaner().clean(); } catch (Exception ex) { } //try { ((sun.nio.ch.DirectBuffer)cb).cleaner().clean(); } catch (Exception ex) { }
@ -270,8 +258,7 @@ public class DiskOptimizedClipboard extends LinearClipboard {
final Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe"); final Field theUnsafeField = unsafeClass.getDeclaredField("theUnsafe");
theUnsafeField.setAccessible(true); theUnsafeField.setAccessible(true);
final Object theUnsafe = theUnsafeField.get(null); final Object theUnsafe = theUnsafeField.get(null);
final Method invokeCleanerMethod = unsafeClass final Method invokeCleanerMethod = unsafeClass.getMethod("invokeCleaner", ByteBuffer.class);
.getMethod("invokeCleaner", ByteBuffer.class);
invokeCleanerMethod.invoke(theUnsafe, cb); invokeCleanerMethod.invoke(theUnsafe, cb);
} catch (Exception e) { } catch (Exception e) {
System.gc(); System.gc();
@ -298,6 +285,11 @@ public class DiskOptimizedClipboard extends LinearClipboard {
} }
} }
private int ylast;
private int ylasti;
private int zlast;
private int zlasti;
@Override @Override
public Collection<CompoundTag> getTileEntities() { public Collection<CompoundTag> getTileEntities() {
return nbtMap.values(); return nbtMap.values();
@ -420,8 +412,7 @@ public class DiskOptimizedClipboard extends LinearClipboard {
@Nullable @Nullable
@Override @Override
public Entity createEntity(Location location, BaseEntity entity) { public Entity createEntity(Location location, BaseEntity entity) {
BlockArrayClipboard.ClipboardEntity ret = new BlockArrayClipboard.ClipboardEntity(location, BlockArrayClipboard.ClipboardEntity ret = new BlockArrayClipboard.ClipboardEntity(location, entity);
entity);
entities.add(ret); entities.add(ret);
return ret; return ret;
} }
@ -436,4 +427,16 @@ public class DiskOptimizedClipboard extends LinearClipboard {
this.entities.remove(entity); this.entities.remove(entity);
} }
@Override
public void removeEntity(int x, int y, int z, UUID uuid) {
Iterator<BlockArrayClipboard.ClipboardEntity> iter = this.entities.iterator();
while (iter.hasNext()) {
BlockArrayClipboard.ClipboardEntity entity = iter.next();
UUID entUUID = entity.getState().getNbtData().getUUID();
if (uuid.equals(entUUID)) {
iter.remove();
return;
}
}
}
} }