Advertisement
JackOUT

Untitled

Jan 26th, 2022
813
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package games.coob.skywars.model;
  2.  
  3. import com.sk89q.worldedit.EditSession;
  4. import com.sk89q.worldedit.MaxChangedBlocksException;
  5. import com.sk89q.worldedit.WorldEdit;
  6. import com.sk89q.worldedit.WorldEditException;
  7. import com.sk89q.worldedit.bukkit.BukkitWorld;
  8. import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
  9. import com.sk89q.worldedit.extent.clipboard.Clipboard;
  10. import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
  11. import com.sk89q.worldedit.function.operation.Operation;
  12. import com.sk89q.worldedit.function.operation.RunContext;
  13. import com.sk89q.worldedit.math.BlockVector3;
  14. import com.sk89q.worldedit.regions.CuboidRegion;
  15. import com.sk89q.worldedit.world.World;
  16. import com.sk89q.worldedit.world.block.BaseBlock;
  17. import lombok.experimental.UtilityClass;
  18. import org.bukkit.Location;
  19. import org.bukkit.block.Block;
  20. import org.mineacademy.fo.Common;
  21. import org.mineacademy.fo.collection.StrictMap;
  22. import org.mineacademy.fo.collection.StrictSet;
  23. import org.mineacademy.fo.model.ChunkedTask;
  24. import org.mineacademy.fo.region.Region;
  25.  
  26. import java.util.List;
  27.  
  28. @UtilityClass
  29. public final class ArenaMapManager {
  30.  
  31.     /**
  32.      * Holds clipboards for saved regions (until we restore them or stop/reload/restart the server)
  33.      */
  34.     private final StrictMap<String, Clipboard> savedClipboards = new StrictMap<>();
  35.  
  36.     /**
  37.      * Saves the arena region if it can be saved
  38.      */
  39.     public void saveRegion(final Arena arena) {
  40.         final Region region = arena.getSettings().getRegion();
  41.  
  42.         if (region == null || !region.isWhole())
  43.             return;
  44.  
  45.         final CuboidRegion cuboidRegion = new CuboidRegion(new BukkitWorld(region.getWorld()), toVector(region.getPrimary()), toVector(region.getSecondary()));
  46.         final BlockArrayClipboard clipboard = new BlockArrayClipboard(cuboidRegion);
  47.  
  48.         try (EditSession editSession = createSession(cuboidRegion.getWorld())) {
  49.  
  50.             new ChunkedTask(1) {
  51.  
  52.                 /**
  53.                  * The copy operation we are using
  54.                  */
  55.                 private Operation operation = new ForwardExtentCopy(editSession, cuboidRegion, clipboard, cuboidRegion.getMinimumPoint());
  56.  
  57.                 /**
  58.                  * Resume the operation means the operation moves forward
  59.                  * until it can no longer be resumed
  60.                  */
  61.                 @Override
  62.                 protected void onProcess(final int index) {
  63.                     try {
  64.                         operation = operation.resume(new RunContext());
  65.  
  66.                     } catch (final WorldEditException e) {
  67.                         e.printStackTrace();
  68.                     }
  69.                 }
  70.  
  71.                 /**
  72.                  * Return true if the operation can be resumed, ie not null
  73.                  */
  74.                 @Override
  75.                 protected boolean canContinue(final int index) {
  76.                     return operation != null;
  77.                 }
  78.  
  79.             }.startChain();
  80.         }
  81.  
  82.         if (savedClipboards.contains(arena.getName()))
  83.             savedClipboards.remove(arena.getName());
  84.  
  85.         savedClipboards.put(arena.getName(), clipboard);
  86.     }
  87.  
  88.     /**
  89.      * Restore arena region if it has been saved
  90.      */
  91.     public void restoreRegion(final Arena arena) {
  92.         final Clipboard clipboard = savedClipboards.removeWeak(arena.getName());
  93.         final Region region = arena.getSettings().getRegion();
  94.  
  95.         if (clipboard == null)
  96.             return;
  97.  
  98.         try (EditSession editSession = createSession(new BukkitWorld(region.getWorld()))) {
  99.             final List<BlockVector3> vectors = Common.convert(region.getBlocks(), ArenaMapManager::toVector);
  100.  
  101.             new ChunkedTask(150_000) {
  102.  
  103.                 /**
  104.                  * For each block in region find block stored in the clipboard,
  105.                  * if it exists, restore it back
  106.                  */
  107.                 @Override
  108.                 protected void onProcess(final int index) {
  109.                     final BlockVector3 vector = vectors.get(index);
  110.                     final BaseBlock copy = clipboard.getFullBlock(vector);
  111.  
  112.                     if (copy != null)
  113.                         try {
  114.                             editSession.setBlock(vector, copy);
  115.                             editSession.close();
  116.  
  117.                         } catch (final MaxChangedBlocksException e) {
  118.                             e.printStackTrace();
  119.                         }
  120.                 }
  121.  
  122.                 /**
  123.                  * Flush the operation to make the blocks visible on finish
  124.                  */
  125.                 @Override
  126.                 protected void onFinish() {
  127.                     editSession.close();
  128.                 }
  129.  
  130.                 /**
  131.                  * Return if we can pull more blocks from our region or we are finished
  132.                  */
  133.                 @Override
  134.                 protected boolean canContinue(final int index) {
  135.                     return index < vectors.size();
  136.                 }
  137.  
  138.                 /**
  139.                  * Also show percentage how many blocks we have restored to finish
  140.                  */
  141.                 @Override
  142.                 protected String getProcessMessage(final long initialTime, final int processed) {
  143.                     final long progress = Math.round(((double) getCurrentIndex() / (double) vectors.size()) * 100);
  144.  
  145.                     return "[" + progress + "%] " + super.getProcessMessage(initialTime, processed);
  146.                 }
  147.  
  148.             }.startChain();
  149.         }
  150.  
  151.     }
  152.  
  153.     /*
  154.      * Create a new edit session
  155.      */
  156.     private EditSession createSession(final World world) {
  157.         // session.setReorderMode(EditSession.ReorderMode.FAST); CAUSES UNDESIRABLE SIDE (PREVENTS LIGHTING OR PHYSICS UPDATES) EFFECTS BUT INCREASES SPEED
  158.         return WorldEdit.getInstance().newEditSession(world);
  159.     }
  160.  
  161.     /*
  162.      * Create a WorldEdit vector from the given block
  163.      */
  164.     private BlockVector3 toVector(final Block block) {
  165.         return toVector(block.getLocation());
  166.     }
  167.  
  168.     /*
  169.      * Create a WorldEdit vector from the given location
  170.      */
  171.     private BlockVector3 toVector(final Location location) {
  172.         return BlockVector3.at(location.getX(), location.getY(), location.getZ());
  173.     }
  174. }
  175.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement