JackOUT

Untitled

Dec 24th, 2021 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1.     public void restoreRegion(final Arena arena) {
  2.         final Clipboard clipboard = savedClipboards.removeWeak(arena.getName());
  3.         final Region region = arena.getSettings().getRegion();
  4.         final org.bukkit.World world = region.getWorld();
  5.  
  6.         for (final Player player : world.getPlayers())
  7.             player.kickPlayer("The game has ended you should not be here.");
  8.  
  9.         processedWorlds.add(world);
  10.  
  11.         if (clipboard == null)
  12.             return;
  13.  
  14.         try (EditSession editSession = WorldEdit.getInstance().newEditSession(new BukkitWorld(region.getWorld())/*EditSession editSession = createSession(new BukkitWorld(region.getWorld()))*/)) {
  15.             final List<BlockVector3> vectors = Common.convert(region.getBlocks(), ArenaMapManager::toVector);
  16.  
  17.             new ChunkedTask(100_000) {
  18.  
  19.                 /**
  20.                  * For each block in region find block stored in the clipboard,
  21.                  * if it exists, restore it back
  22.                  */
  23.                 @Override
  24.                 protected void onProcess(final int index) {
  25.                     final BlockVector3 vector = vectors.get(index);
  26.                     final BaseBlock copy = clipboard.getFullBlock(vector);
  27.  
  28.                     if (copy != null)
  29.                         try {
  30.                             editSession.setBlock(vector, copy);
  31.  
  32.                         } catch (final MaxChangedBlocksException e) {
  33.                             e.printStackTrace();
  34.                         }
  35.                 }
  36.  
  37.                 /**
  38.                  * Flush the operation to make the blocks visible on finish
  39.                  */
  40.                 @Override
  41.                 protected void onFinish() {
  42.                     editSession.flushSession();
  43.                     processedWorlds.remove(world);
  44.                     Common.log("Finished restoring the arena (" + arena.getName() + ")");
  45.                 }
  46.  
  47.                 /**
  48.                  * Return if we can pull more blocks from our region or we are finished
  49.                  */
  50.                 @Override
  51.                 protected boolean canContinue(final int index) {
  52.                     return index < vectors.size();
  53.                 }
  54.  
  55.                 /**
  56.                  * Also show percentage how many blocks we have restored to finish
  57.                  */
  58.                 @Override
  59.                 protected String getProcessMessage(final long initialTime, final int processed) {
  60.                     final long progress = Math.round(((double) getCurrentIndex() / (double) vectors.size()) * 100);
  61.  
  62.                     return "[" + progress + "%] " + super.getProcessMessage(initialTime, processed);
  63.                 }
  64.  
  65.             }.startChain();
  66.         }
  67.  
  68.     }
Add Comment
Please, Sign In to add comment