SBPrime / AsyncWorldEdit-Premium

Async WorldEdit - Edit millions of blocks without lag! (Premium version)
Other
19 stars 4 forks source link

Ability to execute block of code when schematic paste job is finished? #143

Closed UltraGameCoder closed 6 years ago

UltraGameCoder commented 6 years ago

I can't find a way to check if my schematic paste is finished. I want to teleport the player to the schematic when it's finished placing all the blocks. Is there currently a way I can achieve this? (I also need to call some other functions beside the player teleportation )

SBPrime commented 6 years ago

You can do this by starting a job from the blocksplacer. You also need to use the blocksplacer to track if your job has finished. Please check the IBlocksPlacer for further information.

UltraGameCoder commented 6 years ago

@SBPrime I can't seem to figure it out. May you want to look at my code and tell me how you would implement it in the load function?

public void loadIsland() throws FileNotFoundException, IOException, MaxChangedBlocksException {
        String schematicName = getPlayer().getUniqueId().toString();
        AtomicBoolean isNew = new AtomicBoolean(false);
        File file = PlayerStatsManager.getManager().getPlayerIsland(schematicName, isNew);

        org.bukkit.util.Vector originVector = center.toVector();
        Vector to;
        if (isNew.get()) {
            to = new Vector(originVector.getBlockX() - 21,60,originVector.getBlockZ() -21);//to = new Vector(originVector.getBlockX(),originVector.getBlockY(),originVector.getBlockZ());
            Vector borderCenter = new Vector(to.getX(),to.getY()+2,to.getZ());
            generateBorder(borderCenter,21);
        }else {
            to = new Vector(originVector.getBlockX() - 21,60,originVector.getBlockZ() -21);
        }
        World weWorld = new BukkitWorld(center.getWorld());
        WorldData worldData = weWorld.getWorldData();   
        FileInputStream input = new FileInputStream(file);
        ClipboardReader reader = ClipboardFormat.SCHEMATIC.getReader(input);
        Clipboard clipboard = reader.read(worldData);
        input.close();
        Extent source = clipboard;
        Extent destination = WorldEdit.getInstance().getEditSessionFactory().getEditSession(weWorld, -1);
        ForwardExtentCopy copy = new ForwardExtentCopy(source, clipboard.getRegion(), clipboard.getOrigin(), destination, to);
        copy.setSourceMask(new ExistingBlockMask(clipboard));
        Operations.completeLegacy(copy);
    }

Source: https://github.com/PublicGameCoder/Survival-Islands/blob/master/Survival-Islands/src/main/java/islands/PlayerIsland.java

I've wanted to return some kind of object/id to check from if the schematic paste is finished.

SBPrime commented 6 years ago
  1. Cast the edit session factory to IAsyncEditSessionFactory
  2. Get an instance of IBlockPlacer and IPlayerManager from the AWE plugin
  3. Using the IPlayerManager get an instance of IPlayerEntry
  4. Use the getThreadSafeEditSession to create the edit session.
  5. Use the performAsAsyncJob to paste the schematic (as lambda method or sub class), please make sure that you used the same IPlayerEntry

To listen for jobs take a look at: addListener and addStateChangedListener. The first listener needs to be added once when you enable the plugin, the second one needs to be added and removed when you get an event from the IBlockPlacer.

UltraGameCoder commented 6 years ago

Thank you! first I didn't know all the things you've meant but no I've figured it out. Thanks for helping!

SBPrime commented 6 years ago

Your welcome.