Advertisement
JackOUT

Untitled

Dec 13th, 2021
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. package games.coob.skywars.model;
  2.  
  3. import lombok.experimental.UtilityClass;
  4. import org.bukkit.Chunk;
  5. import org.bukkit.World;
  6. import org.bukkit.block.Block;
  7. import org.bukkit.entity.Player;
  8. import org.mineacademy.fo.Common;
  9. import org.mineacademy.fo.Valid;
  10. import org.mineacademy.fo.collection.StrictSet;
  11. import org.mineacademy.fo.model.ChunkedTask;
  12. import org.mineacademy.fo.region.Region;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.HashSet;
  16. import java.util.List;
  17. import java.util.Set;
  18.  
  19. /**
  20.  * Enable us to reset arena world by
  21.  * 1) Stopping the autosave when the arena starts
  22.  * 2) Restoring back chunks to their previous state on arena end
  23.  */
  24. @UtilityClass
  25. public final class ArenaWorldManager {
  26.  
  27.     /**
  28.      * A list of worlds that are being processes
  29.      */
  30.     private final StrictSet<World> processedWorlds = new StrictSet<>();
  31.  
  32.     /**
  33.      * Disable autosave for the given world
  34.      *
  35.      * @param arena
  36.      */
  37.     public void disableAutoSave(final Arena arena) {
  38.         checkApplicable(arena);
  39.  
  40.         final World world = arena.getSettings().getRegion().getWorld();
  41.         world.setAutoSave(false);
  42.  
  43.         Common.log("Disabled world save for " + world.getName());
  44.     }
  45.  
  46.     /**
  47.      * Attempts to restore all chunks in the world before autosave was disabled
  48.      *
  49.      * @param arena
  50.      */
  51.     public void restoreWorld(final Arena arena) {
  52.         checkApplicable(arena);
  53.  
  54.         final Region region = arena.getSettings().getRegion();
  55.         final World world = region.getWorld();
  56.  
  57.         for (final Player player : world.getPlayers())
  58.             player.kickPlayer("The game has ended you should not be here.");
  59.  
  60.         processedWorlds.add(world);
  61.  
  62.         final List<Block> blocks = region.getBlocks();
  63.  
  64.         new ChunkedTask(500_000) {
  65.  
  66.             final Set<Chunk> chunks = new HashSet<>();
  67.  
  68.             @Override
  69.             protected void onProcess(final int index) {
  70.                 final Block block = blocks.get(index);
  71.  
  72.                 chunks.add(block.getChunk());
  73.             }
  74.  
  75.             @Override
  76.             protected boolean canContinue(final int index) {
  77.                 return index < blocks.size();
  78.             }
  79.  
  80.             @Override
  81.             protected String getLabel() {
  82.                 return "blocks";
  83.             }
  84.  
  85.             @Override
  86.             protected void onFinish() {
  87.                 Common.log("Arena " + arena.getName() + " finished converting blocks to chunks.");
  88.  
  89.                 new ChunkedTask(50) {
  90.  
  91.                     final List<Chunk> chunksCopy = new ArrayList<>(chunks);
  92.  
  93.                     @Override
  94.                     protected void onProcess(final int index) {
  95.                         final Chunk chunk = chunksCopy.get(index);
  96.  
  97.                         chunk.unload(false);
  98.                         chunk.load();
  99.                     }
  100.  
  101.                     @Override
  102.                     protected boolean canContinue(final int index) {
  103.                         return index < chunksCopy.size();
  104.                     }
  105.  
  106.                     @Override
  107.                     protected String getLabel() {
  108.                         return "chunks";
  109.                     }
  110.  
  111.                     @Override
  112.                     protected void onFinish() {
  113.                         Common.log("Arena " + arena.getName() + " finished resetting world " + world.getName() + ".");
  114.  
  115.                         processedWorlds.remove(world);
  116.                     }
  117.                 }.startChain();
  118.             }
  119.         }.startChain();
  120.     }
  121.  
  122.     /**
  123.      * Return if the given world is being processed right now
  124.      *
  125.      * @param world
  126.      * @return
  127.      */
  128.     public boolean isWorldBeingProcessed(final World world) {
  129.         return processedWorlds.contains(world);
  130.     }
  131.  
  132.     /*
  133.      * Check a few settings if we can proceed
  134.      */
  135.     private void checkApplicable(final Arena arena) {
  136.         final ArenaSettings settings = arena.getSettings();
  137.  
  138.         Valid.checkBoolean(!isWorldBeingProcessed(settings.getRegion().getWorld()), "Arena " + arena.getName() + " world is already being processed!");
  139.         Valid.checkBoolean(arena.getSettings().isWorldResetEnabled(), "Cannot use world restore, arena does not support it!");
  140.     }
  141. }
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement