How to split a schematic into chunks in WorldEdit?

Discussion in 'Spigot Plugin Development' started by RandomUnkownGuy, Oct 12, 2019.

  1. Hi all, I am making a plugin that loads a schematic that was created using WorldEdit, then every certain time interval pastes it at a certain location.

    The problem however is that this schematic is large and causes huge lag spikes wen pasted or even causes the whole server to crash.

    I installed FastAsyncWorldEdit to solve this issue, but I was wandering if there was a way for me to split the schematic into smaller chunks in my plugin, load each chunk into the clipboard one by one and paste it (or whatever method as long as each chunk is pasted byitself).

    This is how I loaded the schematic in my plugin:

    String schemPath = fawe.getDataFolder() + File.separator + "schematics" + File.separator + args[0] + ".schem";
    File schemFile = new File(schemPath);
    ClipboardFormat format = ClipboardFormats.findByFile(schem);
    Schematic schem = format.load(file);
    Then to paste and flush the EditSession I did:
    com.sk89q.worldedit.world.World adaptedWorld = BukkitAdapter.adapt(player.getWorld());
    EditSession es = schem.paste(adaptedWorld, BlockVector3.(x, y, z), true, true, null);
    es.flushSession();
    Note that I had try/catch statements that I omitted in the snippets above because as far as I understand they are pretty much irrelevant to this issue. Also the variables x, y, and z are not defined in the snippets above. You can just assume they are valid block coordinates of where the user copied the schematic from in the world.
     
    #1 RandomUnkownGuy, Oct 12, 2019
    Last edited: Oct 12, 2019
  2. There is no natural way of tiling this by chunk, you just have to iterate over the schematic block contents and split it accordingly to your necessities. A better approach instead of tiling the individual schematics into chunks you could setup a Map<ChunkWrapper, Queue<...>> to generate the contents of a chunk where the Queue is covering a wrapper defining the block operation.

    World Edit as an API is a sloppy approach on doing things, if you have too large of an operation querying it will cause a serious performance plunge. Just setup a local system to commit block operations, if you process chunk loading properly you'll minimize performance hits.

    The magic behind FAWE follows a similar rule, by inserting the contents off thread and only remembering where which block operation has to run it will minimalize the computing overhead. And you can tune it even further for your own necessities to squeeze out more performance.
     
  3. So how can i actually read a schematics file, assuming it was created by WorldEdit. I.E: How do i iterate through it? Is it necessary to understand the structure of the file? Or is there some kind of API?

    The Map<ChunkWrapper, Queue<...>> idea is interesting. Thanks for suggesting it :). If I ever publish a full plugin I may implement that so that people arent required to install FAWE to use it.