Skip to content

Instantly share code, notes, and snippets.

@blackphreak
Last active June 26, 2017 09:39
Show Gist options
  • Select an option

  • Save blackphreak/6bb70be80e7c2a07b08ba448fc4db87d to your computer and use it in GitHub Desktop.

Select an option

Save blackphreak/6bb70be80e7c2a07b08ba448fc4db87d to your computer and use it in GitHub Desktop.
AsyncWorldEdit Schematic Load & Place
/**
* this file protected byy MIT License
* this file is a modified version for public use.
* the original file: https://github.com/blackphreak/DynamicDungeon/blob/master/src/me/blackphreak/dynamicdungeon/MapBuilding/Builder.java
* feel free to use it :)
*
* I make this because I can't find much async schematic placing code.
* I take a while to find it out how to do it.
* Hope this can help you to make a wonderful plugin :)
*
* Author: BlackPhreak@AlsaceWork
*/
import com.sk89q.worldedit.CuboidClipboard;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.MaxChangedBlocksException;
import com.sk89q.worldedit.bukkit.BukkitUtil;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldedit.schematic.SchematicFormat;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.primesoft.asyncworldedit.AsyncWorldEditBukkit;
import org.primesoft.asyncworldedit.api.blockPlacer.IBlockPlacer;
import org.primesoft.asyncworldedit.api.blockPlacer.IJobEntryListener;
import org.primesoft.asyncworldedit.api.playerManager.IPlayerEntry;
import org.primesoft.asyncworldedit.blockPlacer.entries.JobEntry;
import org.primesoft.asyncworldedit.worldedit.ClipboardAsyncTask;
public void placeSchematic(String fileWithFullPathAndExtension, Location theFirstLocation)
{
try
{
File schematic = new File(fileWithFullPathAndExtension);
//load the schematic file to CuboidClipboard
CuboidClipboard ccb = SchematicFormat.MCEDIT.load(schematic);
Location theFirstLocation = new Location(Bukkit.getWorld("worldNameHere~"), /*X*/0, /*Y*/0, /*Z*/0); //selecting the first point(location)
Location max = theFirstLocation.clone().add((double)ccb.getWidth(), 0, (double) ccb.getLength()); //selecting the second point(location)
CuboidSelection region = new CuboidSelection(theFirstLocation.getWorld(), loc, max);
EditSession session = new EditSession(new BukkitWorld(theFirstLocation.getWorld()), -1);
session.enableQueue();
IBlockPlacer placer = AsyncWorldEditBukkit.getInstance().getBlockPlacer();
IPlayerEntry iPlayer = AsyncWorldEditBukkit.getInstance().getPlayerManager().getConsolePlayer(); //using console as player
/* using player as iPlayer :
* (getPlayer by playerName) :
* IPlayerEntry iPlayer = AsyncWorldEditBukkit.getInstance().getPlayerManager().getPlayer(player.getName());
* (getPlayer by UUID) :
* IPlayerEntry iPlayer = AsyncWorldEditBukkit.getInstance().getPlayerManager().getPlayer(player.getUniqueID());
*/
JobEntry job = new JobEntry(iPlayer, placer.getJobId(iPlayer), "JobNameHere, can be any name");
//creating a BlockPlacing task.
new ClipboardAsyncTask(ccb, session, iPlayer, "JobNameHere2, can be any name (can be different with the previous one)", placer, job)
{
@Override
public void task(CuboidClipboard cuboidClipboard) throws MaxChangedBlocksException
{
ccb.place(session, BukkitUtil.toVector(loc), true);
}
}.run();
//add listener for the placing task
IJobEntryListener jobListener = iJobEntry -> {
switch (iJobEntry.getStatus().getSeqNumber())
{
case 3: //placing
// ...
break;
case 4: //done
// ...
break;
default:
break;
}
};
job.addStateChangedListener(jobListener);
session.flushQueue();
}
catch (Exception ex)
{
ex.printStackTrace();
}
// do anything after place
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment