Search in sources :

Example 1 with Operation

use of com.sk89q.worldedit.function.operation.Operation in project Skree by Skelril.

the class WEDecorator method pasteAt.

public <T> ZoneWorldBoundingBox pasteAt(WorldResolver world, Vector3i origin, String resourceName, Function<ZoneWorldBoundingBox, T> initMapper, Consumer<T> callback) {
    EditSession transaction = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world.getWorldEditWorld(), -1);
    transaction.setFastMode(true);
    transaction.getChangeSet().setRecordChanges(false);
    hashRefMap.computeIfAbsent(resourceName, (a) -> {
        HashRef ref = new HashRef();
        try {
            ref.holder = getHolder(resourceName, world.getWorldEditWorld().getWorldData());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ref;
    });
    HashRef ref = hashRefMap.get(resourceName);
    if (ref == null) {
        callback.accept(null);
        return null;
    }
    ++ref.refCount;
    Clipboard clipboard = ref.holder.getClipboard();
    Region clipReg = clipboard.getRegion();
    clipboard.setOrigin(clipReg.getMinimumPoint());
    Operation operation = ref.holder.createPaste(transaction, transaction.getWorld().getWorldData()).to(new Vector(origin.getX(), origin.getY(), origin.getZ())).build();
    Vector dimensions = clipboard.getDimensions();
    ZoneWorldBoundingBox region = new ZoneWorldBoundingBox(world.getSpongeWorld(), origin, new Vector3i(dimensions.getX(), dimensions.getY(), dimensions.getZ()));
    T returnVal = initMapper.apply(region);
    RunManager.runOperation(operation, () -> {
        RunManager.runOperation(transaction.commit(), () -> {
            callback.accept(returnVal);
            if (--ref.refCount == 0) {
                hashRefMap.remove(resourceName);
                System.gc();
            }
        });
    });
    return region;
}
Also used : Vector3i(com.flowpowered.math.vector.Vector3i) Region(com.sk89q.worldedit.regions.Region) EditSession(com.sk89q.worldedit.EditSession) IOException(java.io.IOException) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) Operation(com.sk89q.worldedit.function.operation.Operation) Vector(com.sk89q.worldedit.Vector) ZoneWorldBoundingBox(com.skelril.skree.service.internal.zone.ZoneWorldBoundingBox)

Aggregations

Vector3i (com.flowpowered.math.vector.Vector3i)1 EditSession (com.sk89q.worldedit.EditSession)1 Vector (com.sk89q.worldedit.Vector)1 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)1 Operation (com.sk89q.worldedit.function.operation.Operation)1 Region (com.sk89q.worldedit.regions.Region)1 ZoneWorldBoundingBox (com.skelril.skree.service.internal.zone.ZoneWorldBoundingBox)1 IOException (java.io.IOException)1